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):
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.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
We can test this template with the following testfsett.cpp:
Compiling this with g++ testfsett.cpp fbool.cpp -o testfsett produces the following output: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; }
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.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
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?
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.
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.
Are you asking about my tutorial, or about the library you linked to?
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![]()
You would need to look at my tutorial on a Fuzzy Boolean, which represents the membership probability.
Okay..thanks mate!
I think i have to study the basics of c++ before i can understand your codes..
Thanks anyway..
Cheers![]()
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?
It's not at all clear to me from the image what you want to do.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks