Skip to content

Commit e7dc504

Browse files
committed
Adição da função addrules
próximo passo é implementar a função rules execute para depois implementar a defuzificação.
1 parent a2a19cf commit e7dc504

11 files changed

+561
-41
lines changed

FuzzyLogic.pro.user

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.2.1, 2017-04-28T20:07:09. -->
3+
<!-- Written by QtCreator 4.2.1, 2017-05-04T14:02:38. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

debug/FuzzyLogic.exe

24.4 KB
Binary file not shown.

debug/main.o

64 Bytes
Binary file not shown.

debug/mainwindow.o

34.5 KB
Binary file not shown.

debug/moc_mainwindow.o

68 Bytes
Binary file not shown.

fuzzy.h

+22-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,26 @@
88
#include <map>
99

1010
namespace ModelHandler {
11+
struct modelString
12+
{
13+
std::string str;
14+
public:
15+
modelString(){}
16+
modelString(std::string str) {this->str = str;}
17+
std::string getString(){return this->str;}
18+
void setString(std::string str){this->str = str;}
19+
};
20+
1121
template <typename Type>
1222
class Fuzzy: public Model<Type>
1323
{
24+
// LinAlg::Matrix<modelString> InputNames;
25+
// LinAlg::Matrix<advancedModelHandler::MembershipFunction<double>*> InputMF;
1426
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > > InputMF;
15-
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > > outputMF;
16-
std::vector< std::map<std::string, std::string> > rules;
27+
// LinAlg::Matrix<modelString> OutputNames;
28+
// LinAlg::Matrix<advancedModelHandler::MembershipFunction<double>*> OutputMF;
29+
std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double>* > > OutputMF;
30+
LinAlg::Matrix<modelString> rules;
1731
LinAlg::Matrix<Type> fuzzyficatedValue;
1832

1933
public:
@@ -24,9 +38,9 @@ namespace ModelHandler {
2438

2539
std::map<std::string, advancedModelHandler::MembershipFunction<Type>*> getInputMF(std::string InputName);
2640

27-
void addOutputMF(unsigned nOutput,
28-
std::string MFName,
29-
const advancedModelHandler::MembershipFunction<Type>* MF);
41+
void addOutputMF(std::string OutputName,
42+
std::string MFName,
43+
advancedModelHandler::MembershipFunction<Type>* MF);
3044

3145
void removeInputMF(unsigned nInput,
3246
std::string MFName,
@@ -50,6 +64,9 @@ namespace ModelHandler {
5064

5165
void defuzzyfication();
5266

67+
Type maxV(Type a, Type b);
68+
Type minV(Type a, Type b);
69+
5370
Type sim(Type x){}
5471
Type sim(Type x, Type y){}
5572
LinAlg::Matrix<Type> sim(LinAlg::Matrix<Type> x){}

fuzzy.hpp

+97-33
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ void ModelHandler::Fuzzy<Type>::addInputMF(std::string InputName, std::string MF
1616

1717
}
1818

19+
template <typename Type>
20+
void ModelHandler::Fuzzy<Type>::addOutputMF(std::string OutputName,
21+
std::string MFName,
22+
advancedModelHandler::MembershipFunction<Type> *MF){
23+
if(this->OutputMF.count(OutputName.c_str()) != 0){
24+
if(this->OutputMF[OutputName].count(MFName.c_str()) != 0){
25+
std::cout << "Variavel existente: " << MFName;
26+
}else{
27+
this->OutputMF[OutputName][MFName] = MF;
28+
}
29+
}else{
30+
std::map<std::string, advancedModelHandler::MembershipFunction<Type>* > MFMap;
31+
MFMap[MFName] = MF;
32+
this->OutputMF[OutputName] = MFMap;
33+
}
34+
35+
}
36+
1937
template <typename Type>
2038
std::map<std::string, advancedModelHandler::MembershipFunction<Type>* > ModelHandler::Fuzzy<Type>::getInputMF(std::string InputName){
2139
return this->InputMF[InputName];
@@ -61,8 +79,7 @@ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::fuzzyfication( LinAlg::Matrix<Ty
6179
template <typename Type>
6280
void ModelHandler::Fuzzy<Type>::addRules(std::string rule)
6381
{
64-
int n = std::count(rules.begin(),rules.end(),',') + 1;
65-
std::map<std::string, std::string> ret;
82+
LinAlg::Matrix<modelString> ret;
6683

6784
while(!(rule.empty()))
6885
{
@@ -77,53 +94,100 @@ void ModelHandler::Fuzzy<Type>::addRules(std::string rule)
7794
}
7895

7996
int colonPosition = partRule.find(":");
80-
if(colonPosition != -1)
97+
if(colonPosition == -1)
8198
{
82-
ret[partRule] = " ";
99+
LinAlg::Matrix<modelString> temp = LinAlg::Matrix<modelString>(modelString(partRule))||modelString(" ");
100+
ret = ret|temp;
83101
}
84102
else
85103
{
86104
std::string InOut = partRule.substr(0, colonPosition);
87-
std::string MF = InOut.erase(0, colonPosition + 1);
88-
ret[InOut] = MF;
105+
std::string MF = partRule.substr(colonPosition + 1);
106+
LinAlg::Matrix<modelString> temp = LinAlg::Matrix<modelString>(modelString(InOut))||modelString(MF);
107+
ret = ret|temp;
89108
}
90109
rule.erase(0, partRulePosition + 1);
91110
}
92-
this->rules.push_back(rule);
93-
this->countMaxOutputMFRepetitionInRules();
111+
rules = rules||ret;
94112
}
95113

96114
template <typename Type>
97-
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output)
115+
std::string ModelHandler::Fuzzy<Type>::viewRules()
98116
{
99-
// Onde Paramos: como definir quem é saida e regra no indice de ret;
100-
// verificar OR é min e AND é max?
101-
// Permitir o uso de maxV e minV
102-
unsigned sizeOfRules = this->rules.size();
103-
unsigned numberOfOutputs = this->outputMF.size();
104-
LinAlg::Matrix<Type> ret(numberOfOutputs, sizeOfRules);
105-
106-
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
107-
if(iter->first == "and")
117+
std::string str;
118+
for(unsigned i = 1; i <= rules.getNumberOfRows(); i+=2)
108119
{
109-
iter++;
110-
for(unsigned i = 1; i < sizeOfRules; i += 2)
120+
for(unsigned j = 1; j <= rules.getNumberOfColumns(); ++j)
111121
{
112-
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
113-
// for esse for é para iterar as entradas na regra
114-
ret(saida,regra) = maxV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
115-
iter++;
122+
str += rules(i,j).getString();
123+
str += ":";
124+
str += rules(i+1,j).getString();
125+
str += "\n";
116126
}
117127
}
118-
else if(iter->first == "or")
128+
return str;
129+
}
130+
131+
template <typename Type>
132+
LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output)
133+
{
134+
// Onde Paramos: como definir quem é saida e regra no indice de ret;
135+
// verificar OR é min e AND é max?
136+
// Permitir o uso de maxV e minV
137+
// unsigned sizeOfRules = this->rules.size();
138+
// unsigned numberOfOutputs = this->outputMF.size();
139+
LinAlg::Matrix<Type> ret(this->OutputMF.size(),this->rules.getNumberOfRows()/2);
140+
for(unsigned i = 1; i <= this->rules.getNumberOfRows()/2; ++i)
119141
{
120-
iter++;
121-
for(unsigned i = 1; i < sizeOfRules; i += 2)
122-
{
123-
std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
124-
// for esse for é para iterar as entradas na regra
125-
ret(saida,regra) = minV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
126-
iter++;
127-
}
142+
if(this->rules(1,1).getString() == "and")
143+
continue;
144+
else if(this->rules(1,1).getString() == "or")
145+
continue;
146+
// Algoritmo aqui deve ser:
147+
// ret deve receber o valor correspondente a qual entrada estiver na string e/ou a outra entrada
148+
// saber qual entrada e saida está na regra
149+
// realizar as operações e ou ou
150+
// atribuir a ret na sequência: às saídas que as regras afetam;
128151
}
152+
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
153+
// if(iter->first == "and")
154+
// {
155+
// iter++;
156+
// for(unsigned i = 1; i < sizeOfRules; i += 2)
157+
// {
158+
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
159+
// for esse for é para iterar as entradas na regra
160+
// ret(saida,regra) = maxV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
161+
// iter++;
162+
// }
163+
// }
164+
// else if(iter->first == "or")
165+
// {
166+
// iter++;
167+
// for(unsigned i = 1; i < sizeOfRules; i += 2)
168+
// {
169+
// std::map<std::string, std::string>::reverse_iterator iter = this->rules[i].rbegin();
170+
// // for esse for é para iterar as entradas na regra
171+
// ret(saida,regra) = minV(ret(saida,regra),this->InputMF[iter->first][iter->second].sim(Output));
172+
// iter++;
173+
// }
174+
// }
175+
}
176+
177+
template <typename Type>
178+
Type ModelHandler::Fuzzy<Type>::maxV(Type a, Type b)
179+
{
180+
if(a < b)
181+
return b;
182+
else
183+
return a;
184+
}
185+
186+
template <typename Type>
187+
Type ModelHandler::Fuzzy<Type>::minV(Type a, Type b)
188+
{
189+
if(a > b)
190+
return b;
191+
else
192+
return a;
129193
}

fuzzy.hpp.autosave.W11344

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

0 commit comments

Comments
 (0)