-
Notifications
You must be signed in to change notification settings - Fork 93
/
Fuzzy.cpp
executable file
·311 lines (297 loc) · 8.8 KB
/
Fuzzy.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Robotic Research Group (RRG)
* State University of Piauí (UESPI), Brazil - Piauí - Teresina
*
* Fuzzy.cpp
*
* Author: AJ Alves <aj.alves@zerokol.com>
* Co authors: Dr. Ricardo Lira <ricardor_usp@yahoo.com.br>
* Msc. Marvin Lemos <marvinlemos@gmail.com>
* Douglas S. Kridi <douglaskridi@gmail.com>
* Kannya Leal <kannyal@hotmail.com>
*/
#include "Fuzzy.h"
// CONTRUCTORS
Fuzzy::Fuzzy()
{
// Initializing pointers with NULL
// FuzzyInput
this->fuzzyInputs = NULL;
// FuzzyOutput
this->fuzzyOutputs = NULL;
// FuzzyRule
this->fuzzyRules = NULL;
}
// DESTRUCTOR
Fuzzy::~Fuzzy()
{
this->cleanFuzzyInputs(this->fuzzyInputs);
this->cleanFuzzyOutputs(this->fuzzyOutputs);
this->cleanFuzzyRules(this->fuzzyRules);
}
// PUBLIC METHODS
// Method to include a new FuzzyInput into Fuzzy
bool Fuzzy::addFuzzyInput(FuzzyInput *fuzzyInput)
{
// auxiliary variable to handle the operation
fuzzyInputArray *newOne;
// allocating in memory
if ((newOne = (fuzzyInputArray *)malloc(sizeof(fuzzyInputArray))) == NULL)
{
// return false if in out of memory
return false;
}
// building the object
newOne->fuzzyInput = fuzzyInput;
newOne->next = NULL;
// if it is the first FuzzyInput, set it as the head
if (this->fuzzyInputs == NULL)
{
this->fuzzyInputs = newOne;
}
else
{
// auxiliary variable to handle the operation
fuzzyInputArray *aux = this->fuzzyInputs;
// find the last element of the array
while (aux != NULL)
{
if (aux->next == NULL)
{
// make the ralations between them
aux->next = newOne;
return true;
}
aux = aux->next;
}
}
return true;
}
// Method to include a new FuzzyOutput into Fuzzy
bool Fuzzy::addFuzzyOutput(FuzzyOutput *fuzzyOutput)
{
// auxiliary variable to handle the operation
fuzzyOutputArray *newOne;
// allocating in memory
if ((newOne = (fuzzyOutputArray *)malloc(sizeof(fuzzyOutputArray))) == NULL)
{
// return false if in out of memory
return false;
}
// building the object
newOne->fuzzyOutput = fuzzyOutput;
newOne->next = NULL;
// sorting the fuzzyOutput
fuzzyOutput->order();
// if it is the first FuzzyOutput, set it as the head
if (this->fuzzyOutputs == NULL)
{
this->fuzzyOutputs = newOne;
}
else
{
// auxiliary variable to handle the operation
fuzzyOutputArray *aux = this->fuzzyOutputs;
// find the last element of the array
while (aux != NULL)
{
if (aux->next == NULL)
{
// make the ralations between them
aux->next = newOne;
return true;
}
aux = aux->next;
}
}
return true;
}
// Method to include a new FuzzyRule into Fuzzy
bool Fuzzy::addFuzzyRule(FuzzyRule *fuzzyRule)
{
// auxiliary variable to handle the operation
fuzzyRuleArray *newOne;
// allocating in memory
if ((newOne = (fuzzyRuleArray *)malloc(sizeof(fuzzyRuleArray))) == NULL)
{
// return false if in out of memory
return false;
}
// building the object
newOne->fuzzyRule = fuzzyRule;
newOne->next = NULL;
// if it is the first FuzzyOutput, set it as the head
if (this->fuzzyRules == NULL)
{
this->fuzzyRules = newOne;
}
else
{
// auxiliary variable to handle the operation
fuzzyRuleArray *aux = this->fuzzyRules;
// find the last element of the array
while (aux != NULL)
{
if (aux->next == NULL)
{
// make the ralations between them
aux->next = newOne;
return true;
}
aux = aux->next;
}
}
return true;
}
// Method to set a crisp value to one FuzzyInput
bool Fuzzy::setInput(int fuzzyInputIndex, float crispValue)
{
// auxiliary variable to handle the operation
fuzzyInputArray *aux;
// instantiate with the first element from array
aux = this->fuzzyInputs;
// while not in the end of the array, iterate
while (aux != NULL)
{
// if the FuzzyInput index match with the desired
if (aux->fuzzyInput->getIndex() == fuzzyInputIndex)
{
// set crisp value for this FuzzyInput and return true
aux->fuzzyInput->setCrispInput(crispValue);
return true;
}
aux = aux->next;
}
// if no FuzzyInput was found, return false
return false;
}
// Method to start the calculate of the result
bool Fuzzy::fuzzify()
{
// auxiliary variable to handle the operation
fuzzyInputArray *fuzzyInputAux;
fuzzyOutputArray *fuzzyOutputAux;
fuzzyRuleArray *fuzzyRuleAux;
// to reset the data of all FuzzyInput and FuzzyOutput objects
// instantiate with first element of the array
fuzzyInputAux = this->fuzzyInputs;
// while not in the end of the array, iterate
while (fuzzyInputAux != NULL)
{
// for each FuzzyInput, reset its data
fuzzyInputAux->fuzzyInput->resetFuzzySets();
fuzzyInputAux = fuzzyInputAux->next;
}
// instantiate with first element of the array
fuzzyOutputAux = this->fuzzyOutputs;
// while not in the end of the array, iterate
while (fuzzyOutputAux != NULL)
{
// for each FuzzyOutput, reset its data
fuzzyOutputAux->fuzzyOutput->resetFuzzySets();
fuzzyOutputAux = fuzzyOutputAux->next;
}
// to calculate the pertinence of all FuzzyInput objects
// instantiate with first element of the array
fuzzyInputAux = this->fuzzyInputs;
// while not in the end of the array, iterate
while (fuzzyInputAux != NULL)
{
// for each FuzzyInput, calculate its pertinence
fuzzyInputAux->fuzzyInput->calculateFuzzySetPertinences();
fuzzyInputAux = fuzzyInputAux->next;
}
// to evaluate which rules were triggered
// instantiate with first element of the array
fuzzyRuleAux = this->fuzzyRules;
// while not in the end of the array, iterate
while (fuzzyRuleAux != NULL)
{
// for each FuzzyRule, evaluate its expressions
fuzzyRuleAux->fuzzyRule->evaluateExpression();
fuzzyRuleAux = fuzzyRuleAux->next;
}
// to truncate the output sets
// instantiate with first element of the array
fuzzyOutputAux = this->fuzzyOutputs;
// while not in the end of the array, iterate
while (fuzzyOutputAux != NULL)
{
// for each FuzzyOutput, truncate the result
fuzzyOutputAux->fuzzyOutput->truncate();
fuzzyOutputAux = fuzzyOutputAux->next;
}
return true;
}
// Method to verify if one specific FuzzyRule was triggered
bool Fuzzy::isFiredRule(int fuzzyRuleIndex)
{
// auxiliary variable to handle the operation
fuzzyRuleArray *aux;
// instantiate with first element of the array
aux = this->fuzzyRules;
// while not in the end of the array, iterate
while (aux != NULL)
{
// if the FuzzyRule index match with the desired
if (aux->fuzzyRule->getIndex() == fuzzyRuleIndex)
{
// return the calculated result
return aux->fuzzyRule->isFired();
}
aux = aux->next;
}
// if no FuzzyRule was found, return false
return false;
}
// Method to retrieve the result of the process for one specific FuzzyOutput
float Fuzzy::defuzzify(int fuzzyOutputIndex)
{
// auxiliary variable to handle the operation
fuzzyOutputArray *aux;
// instantiate with first element of the array
aux = this->fuzzyOutputs;
// while not in the end of the array, iterate
while (aux != NULL)
{
// if the FuzzyOutput index match with the desired
if (aux->fuzzyOutput->getIndex() == fuzzyOutputIndex)
{
// return the calculated result
return aux->fuzzyOutput->getCrispOutput();
}
aux = aux->next;
}
return 0;
}
// PRIVATE METHODS
// Method to recursively clean all FuzzyInput from memory
void Fuzzy::cleanFuzzyInputs(fuzzyInputArray *aux)
{
if (aux != NULL)
{
this->cleanFuzzyInputs(aux->next);
// emptying allocated memory
free(aux);
}
}
// Method to recursively clean all FuzzyOutput from memory
void Fuzzy::cleanFuzzyOutputs(fuzzyOutputArray *aux)
{
if (aux != NULL)
{
this->cleanFuzzyOutputs(aux->next);
// emptying allocated memory
free(aux);
}
}
// Method to recursively clean all FuzzyRule from memory
void Fuzzy::cleanFuzzyRules(fuzzyRuleArray *aux)
{
if (aux != NULL)
{
this->cleanFuzzyRules(aux->next);
// emptying allocated memory
free(aux);
}
}