-
Notifications
You must be signed in to change notification settings - Fork 93
/
FuzzyRuleAntecedent.cpp
executable file
·295 lines (283 loc) · 9.74 KB
/
FuzzyRuleAntecedent.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
/*
* Robotic Research Group (RRG)
* State University of Piauí (UESPI), Brazil - Piauí - Teresina
*
* FuzzyRuleAntecedent.cpp
*
* Author: AJ Alves <[email protected]>
* Co authors: Dr. Ricardo Lira <[email protected]>
* Msc. Marvin Lemos <[email protected]>
* Douglas S. Kridi <[email protected]>
* Kannya Leal <[email protected]>
*/
#include "FuzzyRuleAntecedent.h"
// CONTRUCTORS
FuzzyRuleAntecedent::FuzzyRuleAntecedent()
{
// set the initial values
this->op = 0;
this->mode = 0;
this->fuzzySet1 = NULL;
this->fuzzySet2 = NULL;
this->fuzzyRuleAntecedent1 = NULL;
this->fuzzyRuleAntecedent2 = NULL;
}
// PUBLIC METHODS
// Method to create a FuzzyRuleAntecedent with just one single FuzzySet
bool FuzzyRuleAntecedent::joinSingle(FuzzySet *fuzzySet)
{
// check if FuzzySet is not null
if (fuzzySet != NULL)
{
// set the mode and reference
this->mode = MODE_FS;
this->fuzzySet1 = fuzzySet;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with two FuzzySet, with AND
bool FuzzyRuleAntecedent::joinWithAND(FuzzySet *fuzzySet1, FuzzySet *fuzzySet2)
{
// check if two FuzzySet are valid
if (fuzzySet1 != NULL && fuzzySet2 != NULL)
{
// set the mode and references
this->op = OP_AND;
this->mode = MODE_FS_FS;
this->fuzzySet1 = fuzzySet1;
this->fuzzySet2 = fuzzySet2;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with two FuzzySet, with OR
bool FuzzyRuleAntecedent::joinWithOR(FuzzySet *fuzzySet1, FuzzySet *fuzzySet2)
{
// check if two FuzzySet are valid
if (fuzzySet1 != NULL && fuzzySet2 != NULL)
{
// set the mode and references
this->op = OP_OR;
this->mode = MODE_FS_FS;
this->fuzzySet1 = fuzzySet1;
this->fuzzySet2 = fuzzySet2;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with one FuzzySet and one FuzzyRuleAntecedent, with AND
bool FuzzyRuleAntecedent::joinWithAND(FuzzySet *fuzzySet, FuzzyRuleAntecedent *fuzzyRuleAntecedent)
{
// check if the FuzzySet and FuzzyRuleAntecedent are valid
if (fuzzySet != NULL && fuzzyRuleAntecedent != NULL)
{
// set the mode and references
this->op = OP_AND;
this->mode = MODE_FS_FRA;
this->fuzzySet1 = fuzzySet;
this->fuzzyRuleAntecedent1 = fuzzyRuleAntecedent;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with one FuzzySet and one FuzzyRuleAntecedent, with AND (Inverse Params)
bool FuzzyRuleAntecedent::joinWithAND(FuzzyRuleAntecedent *fuzzyRuleAntecedent, FuzzySet *fuzzySet)
{
return this->joinWithAND(fuzzySet, fuzzyRuleAntecedent);
}
// Method to create a FuzzyRuleAntecedent with one FuzzySet and one FuzzyRuleAntecedent, with OR
bool FuzzyRuleAntecedent::joinWithOR(FuzzySet *fuzzySet, FuzzyRuleAntecedent *fuzzyRuleAntecedent)
{
// check if the FuzzySet and FuzzyRuleAntecedent are valid
if (fuzzySet != NULL && fuzzyRuleAntecedent != NULL)
{
// set the mode and references
this->op = OP_OR;
this->mode = MODE_FS_FRA;
this->fuzzySet1 = fuzzySet;
this->fuzzyRuleAntecedent1 = fuzzyRuleAntecedent;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with one FuzzySet and one FuzzyRuleAntecedent, with OR (Inverse Params)
bool FuzzyRuleAntecedent::joinWithOR(FuzzyRuleAntecedent *fuzzyRuleAntecedent, FuzzySet *fuzzySet)
{
return this->joinWithOR(fuzzySet, fuzzyRuleAntecedent);
}
// Method to create a FuzzyRuleAntecedent with two FuzzyRuleAntecedent, with AND
bool FuzzyRuleAntecedent::joinWithAND(FuzzyRuleAntecedent *fuzzyRuleAntecedent1, FuzzyRuleAntecedent *fuzzyRuleAntecedent2)
{
// check if two FuzzyRuleAntecedent are valid
if (fuzzyRuleAntecedent1 != NULL && fuzzyRuleAntecedent2 != NULL)
{
// set the mode and references
this->op = OP_AND;
this->mode = MODE_FRA_FRA;
this->fuzzyRuleAntecedent1 = fuzzyRuleAntecedent1;
this->fuzzyRuleAntecedent2 = fuzzyRuleAntecedent2;
return true;
}
return false;
}
// Method to create a FuzzyRuleAntecedent with two FuzzyRuleAntecedent, with OR
bool FuzzyRuleAntecedent::joinWithOR(FuzzyRuleAntecedent *fuzzyRuleAntecedent1, FuzzyRuleAntecedent *fuzzyRuleAntecedent2)
{
// check if two FuzzyRuleAntecedent are valid
if (fuzzyRuleAntecedent1 != NULL && fuzzyRuleAntecedent2 != NULL)
{
// set the mode and references
this->op = OP_OR;
this->mode = MODE_FRA_FRA;
this->fuzzyRuleAntecedent1 = fuzzyRuleAntecedent1;
this->fuzzyRuleAntecedent2 = fuzzyRuleAntecedent2;
return true;
}
return false;
}
// Method to evaluate this FuzzyRuleAntecedent
float FuzzyRuleAntecedent::evaluate()
{
// switch by the mode value
switch (this->mode)
{
case MODE_FS:
// case it is a single FuzzySet join, just return its pertinence
return this->fuzzySet1->getPertinence();
break;
case MODE_FS_FS:
// case it is a join of two FuzzySet, switch by the operator
switch (this->op)
{
case OP_AND:
// case the operator is AND, check if both has pertinence bigger then 0.0
if (this->fuzzySet1->getPertinence() > 0.0 && this->fuzzySet2->getPertinence() > 0.0)
{
// in this case, return the small pertinence between two FuzzySet
if (this->fuzzySet1->getPertinence() < this->fuzzySet2->getPertinence())
{
return this->fuzzySet1->getPertinence();
}
else
{
return this->fuzzySet2->getPertinence();
}
}
else
{
return 0.0;
}
break;
case OP_OR:
// case the operator is OR, check if one has pertinence bigger then 0.0
if (this->fuzzySet1->getPertinence() > 0.0 || this->fuzzySet2->getPertinence() > 0.0)
{
// in this case, return the one pertinence is bigger
if (this->fuzzySet1->getPertinence() > this->fuzzySet2->getPertinence())
{
return this->fuzzySet1->getPertinence();
}
else
{
return this->fuzzySet2->getPertinence();
}
}
else
{
return 0.0;
}
break;
}
break;
case MODE_FS_FRA:
// case it is a join of one FuzzySet and one FuzzyRuleAntecedent, switch by the operator
switch (this->op)
{
case OP_AND:
// case the operator is AND, check if both has pertinence bigger then 0.0
if (this->fuzzySet1->getPertinence() > 0.0 && fuzzyRuleAntecedent1->evaluate() > 0.0)
{
// in this case, return the small pertinence between two FuzzySet
if (this->fuzzySet1->getPertinence() < fuzzyRuleAntecedent1->evaluate())
{
return this->fuzzySet1->getPertinence();
}
else
{
return fuzzyRuleAntecedent1->evaluate();
}
}
else
{
return 0.0;
}
break;
case OP_OR:
// case the operator is OR, check if one has pertinence bigger then 0.0
if (this->fuzzySet1->getPertinence() > 0.0 || fuzzyRuleAntecedent1->evaluate() > 0.0)
{
// in this case, return the one pertinence is bigger
if (this->fuzzySet1->getPertinence() > fuzzyRuleAntecedent1->evaluate())
{
return this->fuzzySet1->getPertinence();
}
else
{
return fuzzyRuleAntecedent1->evaluate();
}
}
else
{
return 0.0;
}
break;
}
break;
case MODE_FRA_FRA:
// case it is a join of two FuzzyRuleAntecedent, switch by the operator
switch (this->op)
{
case OP_AND:
// case the operator is AND, check if both has pertinence bigger then 0.0
if (fuzzyRuleAntecedent1->evaluate() > 0.0 && fuzzyRuleAntecedent2->evaluate() > 0.0)
{
// in this case, return the small pertinence between two FuzzySet
if (fuzzyRuleAntecedent1->evaluate() < fuzzyRuleAntecedent2->evaluate())
{
return fuzzyRuleAntecedent1->evaluate();
}
else
{
return fuzzyRuleAntecedent2->evaluate();
}
}
else
{
return 0.0;
}
break;
case OP_OR:
// case the operator is OR, check if one has pertinence bigger then 0.0
if (fuzzyRuleAntecedent1->evaluate() > 0.0 || fuzzyRuleAntecedent2->evaluate() > 0.0)
{
// in this case, return the one pertinence is bigger
if (fuzzyRuleAntecedent1->evaluate() > fuzzyRuleAntecedent2->evaluate())
{
return fuzzyRuleAntecedent1->evaluate();
}
else
{
return fuzzyRuleAntecedent2->evaluate();
}
}
else
{
return 0.0;
}
break;
}
break;
}
return 0.0;
}