forked from EngineeringLibrary/fuzzyLogic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzy.hpp.autosave.v11344
135 lines (121 loc) · 4.74 KB
/
fuzzy.hpp.autosave.v11344
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "fuzzy.h"
template <typename Type>
void ModelHandler::Fuzzy<Type>::addInputMF(std::string InputName, std::string MFName, advancedModelHandler::MembershipFunction<Type> *MF){
if(this->InputMF.count(InputName.c_str()) != 0){
if(this->InputMF[InputName].count(MFName.c_str()) != 0){
std::cout << "Variavel existente: " << MFName;
}else{
this->InputMF[InputName][MFName] = MF;
}
}else{
std::map<std::string, advancedModelHandler::MembershipFunction<Type>* > MFMap;
MFMap[MFName] = MF;
this->InputMF[InputName] = MFMap;
}
}
template <typename Type>
std::map<std::string, advancedModelHandler::MembershipFunction<Type>* > ModelHandler::Fuzzy<Type>::getInputMF(std::string InputName){
return this->InputMF[InputName];
}
template<typename Type>
unsigned ModelHandler::Fuzzy<Type>::getMaxNumberOfMembershipFunctions(std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>*>> &Input)
{
unsigned n = 0;
for(std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>*>>::iterator iter = Input.begin(); iter != Input.end(); ++iter)
{
if(iter->second.size() > n)
n = iter->second.size();
}
return n;
}
template <typename Type>
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::fuzzyfication( LinAlg::Matrix<Type> Input)
{
unsigned n = Input.getNumberOfRows();
unsigned n2 = this->getMaxNumberOfMembershipFunctions(this->InputMF);
LinAlg::Matrix<Type> ret(n,n2);// Cada linha é uma entrada e cada coluna nessa linha é uma função de pertinência.
// A matriz ret armazenará os valores fuzzificados nessa forma
//Ex and,Comida:doce,Bebida:Ruim,Saida:seMata
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > >::reverse_iterator iterInputs = this->InputMF.rbegin();
for(unsigned i = 1; i <= n; ++i)
{
std::map< std::string, advancedModelHandler::MembershipFunction<double>* >::reverse_iterator iterMF = iterInputs->second.rbegin();
unsigned n2 = iterInputs->second.size();
for(unsigned j = 1; j <= n2; ++j)
{
ret(i,j) = iterMF->second->sim(Input(i,1));
iterMF++;
}
iterInputs++;
}
return ret;
}
template <typename Type>
void ModelHandler::Fuzzy<Type>::addRules(std::string rule)
{
int n = std::count(rules.begin(),rules.end(),',') + 1;
std::map<std::string, std::string> ret;
while(!(rule.empty()))
{
int partRulePosition = rule.find(",");
std::string partRule;
if(partRulePosition != -1)
partRule = rule.substr(0, partRulePosition);
else
{
partRule = rule;
partRulePosition = rule.length();
}
int colonPosition = partRule.find(":");
if(colonPosition != -1)
{
ret[partRule] = " ";
}
else
{
std::string InOut = partRule.substr(0, colonPosition);
std::string MF = InOut.erase(0, colonPosition + 1);
ret[InOut] = MF;
}
rule.erase(0, partRulePosition + 1);
}
this->rules.push_back(rule);
this->countMaxOutputMFRepetitionInRules();
}
template <typename Type>
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output)
{
// Onde Paramos: como definir quem é saida e regra no indice de ret;
// verificar OR é min e AND é max?
// Permitir o uso de maxV e minV
unsigned sizeOfRules = this->rules.size();
unsigned numberOfOutputs = this->outputMF.size();
LinAlg::Matrix<Type> ret(numberOfOutputs, sizeOfRules);
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
if(iter->first == "and")
{
iter++;
for(unsigned i = 1; i < sizeOfRules; i += 2)
{
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
// for esse for é para iterar as entradas na regra
ret(saida,regra) = maxV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
iter++;
}
}
else if(iter->first == "or")
{
iter++;
for(unsigned i = 1; i < sizeOfRules; i += 2)
{
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
// for esse for é para iterar as entradas na regra
ret(saida,regra) = minV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
iter++;
}
}
}
template <typename Type>
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output)
{
}