@@ -16,6 +16,48 @@ void ModelHandler::Fuzzy<Type>::addInputMF(std::string InputName, std::string MF
16
16
17
17
}
18
18
19
+ template <typename Type>
20
+ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::membershipFunctionQuantities(std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double > *> > &MF)
21
+ {
22
+ std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double >* > >::reverse_iterator iter = MF.rbegin ();
23
+ unsigned n = MF.size ();
24
+ LinAlg::Matrix<Type> ret (n,1 );
25
+ for (unsigned i = 1 ; i <= n; ++i)
26
+ {
27
+ ret (i,1 ) = iter->second .size ();
28
+ iter++;
29
+ }
30
+ return ret;
31
+ }
32
+
33
+ template <typename Type>
34
+ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::membershipFunctionPosition(std::map<std::string, std::map<std::string, advancedModelHandler::MembershipFunction<double > *> > &MF, const std::string &firstKey, const std::string &secondKey)
35
+ {
36
+ std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double >* > >::reverse_iterator iterInputs = MF.rbegin ();
37
+ unsigned n = MF.size ();
38
+ LinAlg::Matrix<Type> ret (1 ,2 );
39
+ for (unsigned i = 1 ; i <= n; ++i)
40
+ {
41
+ if (iterInputs->first == firstKey)
42
+ {
43
+ ret (1 ,1 ) = i;
44
+ std::map< std::string, advancedModelHandler::MembershipFunction<double >* >::reverse_iterator iterMF = iterInputs->second .rbegin ();
45
+ unsigned n2 = iterInputs->second .size ();
46
+ for (unsigned j = 1 ; j <= n2; ++j)
47
+ {
48
+ if (iterMF->first == secondKey)
49
+ {
50
+ ret (1 ,2 ) = j;
51
+ return ret;
52
+ }
53
+ iterMF++;
54
+ }
55
+ }
56
+ iterInputs++;
57
+ }
58
+ return ret;
59
+ }
60
+
19
61
template <typename Type>
20
62
void ModelHandler::Fuzzy<Type>::addOutputMF(std::string OutputName,
21
63
std::string MFName,
@@ -129,49 +171,80 @@ std::string ModelHandler::Fuzzy<Type>::viewRules()
129
171
}
130
172
131
173
template <typename Type>
132
- LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> Output )
174
+ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::rulesExecute(LinAlg::Matrix<Type> fuzzyficatedValue )
133
175
{
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)
176
+ unsigned sumOfMFOutputs = 0 ;
177
+ LinAlg::Matrix<Type> outputQuantities = membershipFunctionQuantities (this ->OutputMF );
178
+ for (unsigned i = 1 ; i <= outputQuantities.getNumberOfRows (); ++i)
179
+ sumOfMFOutputs += outputQuantities (i,1 );
180
+
181
+ LinAlg::Matrix<Type> ret (sumOfMFOutputs, this ->rules .getNumberOfRows ()/2 );
182
+
183
+ for (unsigned i = 1 ; i <= this ->rules .getNumberOfRows (); i += 2 )
141
184
{
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;
185
+ Type tempMFValue;
186
+ for (unsigned j = 2 ; j <= rules.getNumberOfColumns (); ++j)
187
+ {
188
+ LinAlg::Matrix<Type> inputPosition = this ->membershipFunctionPosition (this ->InputMF ,this ->rules (i,j).getString (),this ->rules (i+1 ,j).getString ());
189
+ if (!LinAlg::isEqual (inputPosition,LinAlg::Matrix<Type>(" 0,0" )))
190
+ {
191
+ if (j == 2 )
192
+ tempMFValue = fuzzyficatedValue (inputPosition (1 ,1 ),inputPosition (1 ,2 ));
193
+ else if (this ->rules (i,1 ).getString () == " and" )
194
+ tempMFValue = this ->minV (tempMFValue,fuzzyficatedValue (inputPosition (1 ,1 ),inputPosition (1 ,2 )));
195
+ else if (this ->rules (i,1 ).getString () == " or" )
196
+ tempMFValue = this ->maxV (tempMFValue,fuzzyficatedValue (inputPosition (1 ,1 ),inputPosition (1 ,2 )));
197
+
198
+ continue ;
199
+ }
200
+
201
+ LinAlg::Matrix<Type> outputPosition = this ->membershipFunctionPosition (this ->OutputMF ,this ->rules (i,j).getString (),this ->rules (i+1 ,j).getString ());
202
+ if (!LinAlg::isEqual (outputPosition,LinAlg::Matrix<Type>(" 0,0" )))
203
+ {
204
+ sumOfMFOutputs = 0 ;
205
+ for (unsigned k = 2 ; k <= outputPosition (1 ,1 ); ++k)
206
+ sumOfMFOutputs += outputQuantities (k-1 ,1 );
207
+ ret (outputPosition (1 ,2 ) + sumOfMFOutputs,(i+1 )/2 ) = tempMFValue;
208
+ }
209
+ }
210
+ }
211
+ return ret;
212
+ }
213
+
214
+ template <typename Type>
215
+ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::defuzzyfication(LinAlg::Matrix<Type> rulesMatrix)
216
+ {
217
+ LinAlg::Matrix<Type> outputQuantities = membershipFunctionQuantities (this ->OutputMF );
218
+ LinAlg::Matrix<double > ret (outputQuantities.getNumberOfRows (),1 );
219
+ std::map< std::string, std::map< std::string, advancedModelHandler::MembershipFunction<double >* > >::reverse_iterator iterOutputs = this ->OutputMF .rbegin ();
220
+ unsigned counter = 1 ;
221
+ for (unsigned i = 1 ; i <= outputQuantities.getNumberOfRows (); ++i)
222
+ {
223
+ std::map< std::string, advancedModelHandler::MembershipFunction<double >* >::reverse_iterator iter = iterOutputs->second .rbegin ();
224
+ unsigned n2 = iterOutputs->second .size ();
225
+ Type den = 0 , num = 0 ;
226
+ for (unsigned j = 1 ; j <= n2; ++j)
227
+ {
228
+ for (unsigned k = 1 ; k <= rulesMatrix.getNumberOfColumns (); ++k)
229
+ {
230
+ num += rulesMatrix (counter,k)*iter->second ->getAverage ();
231
+ den += rulesMatrix (counter,k);
232
+ }
233
+ counter++;
234
+ iter++;
235
+ }
236
+ if (den != 0 )
237
+ ret (i,1 ) = num/den;
238
+ iterOutputs++;
151
239
}
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
- // }
240
+ return ret;
241
+ }
242
+
243
+ template <typename Type>
244
+ LinAlg::Matrix<Type> ModelHandler::Fuzzy<Type>::sim(LinAlg::Matrix<Type> x)
245
+ {
246
+ this ->fuzzyficatedValue = this ->fuzzyfication (x);
247
+ return this ->defuzzyfication (this ->rulesExecute (this ->fuzzyficatedValue ));
175
248
}
176
249
177
250
template <typename Type>
@@ -191,3 +264,4 @@ Type ModelHandler::Fuzzy<Type>::minV(Type a, Type b)
191
264
else
192
265
return a;
193
266
}
267
+
0 commit comments