+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Extending the Fuzzy Set

  1. #1
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Extending the Fuzzy Set

    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):
    Code:
    #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:
    Code:
    #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:
    Code:
    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    moks_107 is offline Newbie
    Join Date
    Nov 2008
    Posts
    25
    Rep Power
    0

    Re: Extending the Fuzzy Set

    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?

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Extending the Fuzzy Set

    This is the final tutorial in a 4 part series. You should read Getting Fuzzy: Part 1 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

  5. #4
    ammaerz is offline Newbie
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Re: Extending the Fuzzy Set

    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.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Extending the Fuzzy Set

    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

  7. #6
    ammaerz is offline Newbie
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Re: Extending the Fuzzy Set

    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

  8. #7
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Extending the Fuzzy Set

    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

  9. #8
    ammaerz is offline Newbie
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Re: Extending the Fuzzy Set

    Okay..thanks mate!

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

    Thanks anyway..

    Cheers

  10. #9
    Hamed's Avatar
    Hamed is offline Programming Professional
    Join Date
    May 2009
    Location
    Iran-Shiraz
    Posts
    223
    Rep Power
    0

    Re: Extending the Fuzzy Set

    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 Thumbnails Attached Thumbnails Extending the Fuzzy Set-.jpg  

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Extending the Fuzzy Set

    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

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Implementing a Fuzzy Set
    By WingedPanther in forum C Tutorials
    Replies: 11
    Last Post: 01-25-2011, 02:42 AM
  2. Getting Fuzzy: Part 1
    By WingedPanther in forum Tutorials
    Replies: 12
    Last Post: 01-25-2011, 02:23 AM
  3. Help to run Fuzzy Logic Library
    By ammaerz in forum C and C++
    Replies: 4
    Last Post: 09-03-2010, 12:42 AM
  4. My Fuzzy Boolean Implementation
    By MeTh0Dz in forum C and C++
    Replies: 6
    Last Post: 11-22-2008, 12:47 PM
  5. Extending Logical Volumes
    By Hektor in forum Linux Hardware
    Replies: 4
    Last Post: 06-19-2008, 02:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts