Jump to content

Extending the Fuzzy Set

- - - - -

  • Please log in to reply
14 replies to this topic

#1
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Having a fuzzy set based on a string is nice, but sometimes you want it to be based on an integer, float, or something else. To accomplish this, we'll extend the string-based fuzzy set class to a template.

First, we'll modify fset.h and fset.cpp into the following fsett.h (I couldn't get the test program to compile with them seperate):
#ifndef FSETT_DEFINED

#define FSETT_DEFINED


#include "fbool.h"

#include <map>


template<class T> class Fset

{

  std::map<T,Fbool> setdata;

public:

  void insert(const T, const double);

  void insert(const T, const bool);

  int size();

  bool empty();

  void erase(T);

  void clear();

  Fset<T> operator&&(Fset<T>);

  Fset<T> operator||(Fset<T>);

  Fset<T> operator!();

  Fbool& operator[](T);

};


template<class T> void Fset<T>::insert(const T key, const double prob)

{

  Fset<T>::setdata.insert(std::pair<T,Fbool>(key,prob));

}


template<class T> void Fset<T>::insert(const T key, const bool prob)

{

  Fset<T>::setdata.insert(std::pair<T,Fbool>(key,prob));

}


template<class T> int Fset<T>::size()

{

  return Fset<T>::setdata.size();

}


template<class T> bool Fset<T>::empty()

{

  return Fset<T>::setdata.empty();

}


template<class T> void Fset<T>::erase(T key)

{

  Fset<T>::setdata.erase(key);

}


template<class T> void Fset<T>::clear()

{

  Fset<T>::setdata.clear();

}


template<class T> Fset<T> Fset<T>::operator&&(Fset<T> operand)

{

  Fset<T> temp;

  Fbool temp2;

  typename std::map<T,Fbool>::iterator pos1;

  typename std::map<T,Fbool>::iterator pos2;

  for (pos1 = this->setdata.begin(), pos2 = operand.setdata.begin(); pos1 != this->setdata.end() && pos2 != operand.setdata.end();)

  {

    if (pos1->first < pos2->first)

    {

      temp2.setval(false);

      temp.insert(pos1->first,temp2.getval());

      ++pos1;

    }

    else if (pos1->first > pos2->first)

    {

      temp2.setval(false);

      temp.insert(pos2->first,temp2.getval());

      ++pos2;

    }

    else

    {

      temp.insert(pos2->first,((pos1->second)&&(pos2->second)).getval());

      ++pos1;

      ++pos2;

    }

  }

  return temp;

}


template<class T> Fset<T> Fset<T>::operator||(Fset<T> operand)

{

  Fset<T> temp;

  typename std::map<T,Fbool>::iterator pos1;

  typename std::map<T,Fbool>::iterator pos2;

  for (pos1 = this->setdata.begin(), pos2 = operand.setdata.begin(); pos1 != this->setdata.end() && pos2 != operand.setdata.end();)

  {

    if (pos1->first < pos2->first)

    {

      temp.insert(pos1->first,(pos1->second).getval());

      ++pos1;

    }

    else if (pos1->first > pos2->first)

    {

      temp.insert(pos2->first,(pos2->second).getval());

      ++pos2;

    }

    else

    {

      temp.insert(pos2->first,((pos1->second)||(pos2->second)).getval());

      ++pos1;

      ++pos2;

    }

  }

  return temp;

}


template<class T> Fset<T> Fset<T>::operator!()

{

  Fset<T> temp;

  typename std::map<T,Fbool>::iterator pos1;

  for (pos1 = this->setdata.begin(); pos1 != this->setdata.end();++pos1)

  {

    temp.insert(pos1->first,(!(pos1->second)).getval());

  }

  return temp;

}


template<class T> Fbool& Fset<T>::operator[](T key)

{

  return Fset<T>::setdata[key];

}


#endif


Most of this is a fairly straightforward modification. It is worth noting, however, that you MUST add the typename keyword in front of the iterators. Also, you have to add the template<class T> keyword in front of each function definition.

We can test this template with the following testfsett.cpp:
#include "fsett.h"

#include <iostream>

#include <string>

using std::string;

using std::cout;


int main(int argc,char* argv[])

{

  Fset<int> myintset;

  cout<<"myintset.empty = "<<myintset.empty()<<"\n";

  cout<<"myintset.count = "<<myintset.size()<<"\n";

  myintset.insert(0,false);

  myintset.insert(1,true);

  myintset.insert(2,0.33);

  myintset.insert(3,0.5);

  cout<<"myintset.empty = "<<myintset.empty()<<"\n";

  cout<<"myintset.count = "<<myintset.size()<<"\n";


  Fset<string> mytestset;

  cout<<"mytestset.empty = "<<mytestset.empty()<<"\n";

  cout<<"mytestset.count = "<<mytestset.size()<<"\n";

  mytestset.insert("false",false);

  mytestset.insert("true",true);

  mytestset.insert("third",0.33);

  mytestset.insert("half",0.5);

  cout<<"mytestset.empty = "<<mytestset.empty()<<"\n";

  cout<<"mytestset.count = "<<mytestset.size()<<"\n";


  Fset<string> mytest2;

  mytest2.insert("false",true);

  mytest2.insert("true",false);

  mytest2.insert("quarter",0.25);

  mytest2.insert("half",0.5);

  cout<<"mytest2.empty = "<<mytest2.empty()<<"\n";

  cout<<"mytest2.count = "<<mytest2.size()<<"\n";


  Fset<string> andset = mytest2&&mytestset;

  Fset<string> orset = mytest2||mytestset;

  Fset<string> notset = !mytestset;


  cout<<"andset[\"false\"] = "<<andset["false"].getval()<<"\n";

  cout<<"orset[\"false\"] = "<<orset["false"].getval()<<"\n";

  cout<<"notset[\"false\"] = "<<notset["false"].getval()<<"\n";

  return 0;

}


Compiling this with g++ testfsett.cpp fbool.cpp -o testfsett produces the following output:
myintset.empty = 1

myintset.count = 0

myintset.empty = 0

myintset.count = 4

mytestset.empty = 1

mytestset.count = 0

mytestset.empty = 0

mytestset.count = 4

mytest2.empty = 0

mytest2.count = 4

andset["false"] = 0

orset["false"] = 1

notset["false"] = 1


Note: I was not able to split the fsett.h into an fsett.h and fsett.cpp and compile using: g++ testfsett.cpp fsett.cpp fbool.cpp -o testfsett without getting errors. If anyone wants to suggest corrections, feel free to add them.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#2
moks_107

moks_107

    Newbie

  • Members
  • PipPip
  • 25 posts
Hi!umm may i ask whats this for?sorry im kinda new to programming thats why,and what do you mean by having a fuzzy set based on a string?and what does the fsett.h do?

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
This is the final tutorial in a 4 part series. You should read http://forum.codecal...y-part-1-a.html to get the background math. Part three used map<string,double> instead of map<T,double> in a non-template version. fsett.h contains the definition of the Fset<T> template that is used in testfsett.cpp.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
ammaerz

ammaerz

    Newbie

  • Members
  • Pip
  • 6 posts
Hey everyone!

I just signed up for this forum..seems to be a good one!

Could anyone explain how do these codes (i.e the codes above) working?
I manage to compile and run each tutorial from implementing fuzzy boolean to extending fuzzy set tutorial,
but I still don't understand how do they work.

I have to use fuzzy logic in C++ for my final year project.
I have used fuzzy logic in MatLab but it is too heavy to be run on in PC104.

The source below looks quite good but it has too many header files..very confusing..
but main file is easy to understand i.e the Rules Sample.cpp

C++ Fuzzy Logic Programming Library | Download C++ Fuzzy Logic Programming Library software for free at SourceForge.net

another problem is i can't run it..if anyone could run it please let me know.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Are you asking about my tutorial, or about the library you linked to?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
ammaerz

ammaerz

    Newbie

  • Members
  • Pip
  • 6 posts
1) firstly, i was asking about your codes..which part of the codes should i change in order to implement my own membership function?

2) and also about the library i linked if anyone have tried it..

thanks heaps:thumbup:

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You would need to look at my tutorial on a Fuzzy Boolean, which represents the membership probability.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
ammaerz

ammaerz

    Newbie

  • Members
  • Pip
  • 6 posts
Okay..thanks mate!

I think i have to study the basics of c++ before i can understand your codes..

Thanks anyway..

Cheers;)

#9
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Hello, I am working on Fuzzy logic (Fuzzy controller) now I want a different between fuzzy logic and boolean logic can you give me a program or a sample for this job?

Sth like attachment image?

Attached Files



#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It's not at all clear to me from the image what you want to do.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I show an example, maybe you can use other thing.
I want to show why we should use fuzzy logic with simple program.
Clear I need one program in fuzzy logic and crips logic.

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Hamed, fuzzy logic and fuzzy sets are used primarily when you have imprecise selection of "true/false" or "is/is not". It comes in handy for some AI applications. The book I learned about it from included references to applications in motor controllers (balancing a pole on a wheeled block, maintaining safe following distance, VCR automatic tracking adjustment) where solving the differential equations involved would result in the controller not being able to respond fast enough, neural networks (hand-writing to text, checking for whether raspberry jam is made with real raspberries), genetic algorithms.

Ultimately, it's about having another tool in the tool belt, and could easily require a fairly deep understanding of statistics to fully grasp how to work with it. Amazon.com: The Importance of Being Fuzzy (9780691001449): Arturo Sangalli: Books
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users