In: Computer Science
Write a C++ program
Given fuzzy sets A and B, find complement A, A ∪ B, and A ∩ B
A = {0.2 : a, 0.7 : b, 0.5 : c, 0.01 : k, 0.98 : z} and
B = {0.42 : a, 0.6 : b, 0.5 : h, 0.1 : k, 0.44 : m, 0.8 : z}
the above problem can be broken down in following steps :
more details can be seen in the code .:
text code :
#include <iostream>
#include <map>
#include <iterator>
#include <vector>
using namespace std;
map<string , double>::iterator itr;
map<string , double >
gen_fuzzy_set(vector<string> members , vector<double>
elements){
//fucntion to generate a fuzzy set given its keys and values
map<string , double> fuzzy_set;
for(int i=0;i<members.size();i++){
fuzzy_set[members[i]] = elements[i];
}
return fuzzy_set;
}
map<string , double> get_union(map<string ,
double> &fs1 , map<string , double> &fs2){
//fucntion to calculate the union betweeen two fs by taking the max
value of two same keys in both the fs.
vector<string> mem;
vector<double> ele;
map<string , double> smaller_fs = (fs1.size() <=
fs2.size())?fs1:fs2;
map<string , double> bigger_fs = (fs1.size() >
fs2.size())?fs1:fs2;
for(itr = smaller_fs.begin(); itr != smaller_fs.end() ;
itr++){
string key = itr->first;
if(bigger_fs.count(key) != 0){
mem.push_back(key);
ele.push_back(max(smaller_fs[key] , bigger_fs[key]));
}
}
return gen_fuzzy_set(mem , ele);
}
map<string , double>
get_intersection(map<string , double> &fs1 ,
map<string , double> &fs2){
//fucntion to calculate the union betweeen two fs by taking the min
value of two same keys in both the fs.
vector<string> mem;
vector<double> ele;
map<string , double> smaller_fs = (fs1.size() <=
fs2.size())?fs1:fs2;
map<string , double> bigger_fs = (fs1.size() >
fs2.size())?fs1:fs2;
for(itr = smaller_fs.begin(); itr != smaller_fs.end() ;
itr++){
string key = itr->first;
if(bigger_fs.count(key) != 0){
mem.push_back(key);
ele.push_back(min(smaller_fs[key] , bigger_fs[key]));
}
}
return gen_fuzzy_set(mem , ele);
}
map<string , double> complement(map<string ,
double> &fs){
//given a fuzzy set , calculating its complement , by subtracting 1
from value of each key in the fs.
for(itr = fs.begin(); itr != fs.end() ; itr++){
fs[itr->first] = 1 - itr->second;
}
return fs;
}
void print_fs(map<string , double> &fs){
for(itr = fs.begin(); itr != fs.end();itr++){
cout<<itr->first<< "
"<<itr->second<<endl;
}
}
int main(){
vector<string> mem1;
mem1.push_back("a");
mem1.push_back("b");
mem1.push_back("c");
mem1.push_back("k");
mem1.push_back("z");
vector<double> ele1;
ele1.push_back(0.2);
ele1.push_back(0.7);
ele1.push_back(0.5);
ele1.push_back(0.01);
ele1.push_back(0.98);
vector<string> mem2;
mem2.push_back("a");
mem2.push_back("b");
mem2.push_back("h");
mem2.push_back("k");
mem2.push_back("m");
mem2.push_back("z");
vector<double> ele2;
ele2.push_back(0.42);
ele2.push_back(0.6);
ele2.push_back(0.5);
ele2.push_back(0.1);
ele2.push_back(0.44);
ele2.push_back(0.8);
map<string , double> fs1 = gen_fuzzy_set(mem1 ,
ele1);
cout<<"fuzzy_set 1"<<endl;
print_fs(fs1);
map<string , double> fs2 = gen_fuzzy_set(mem2 , ele2);
cout<<"fuzzy_set 2"<<endl;
print_fs(fs2);
map<string , double> uni = get_union(fs1 , fs2);
map<string , double> inter = get_intersection(fs1 ,
fs2);
map<string , double> comp = complement(fs1);
cout<<"union"<<endl;
print_fs(uni);
cout<<"intersection"<<endl;
print_fs(inter);
cout<<"complement of fs1"<<endl;
print_fs(comp);
}