-
Notifications
You must be signed in to change notification settings - Fork 94
/
FuzzyRuleConsequent.cpp
executable file
·93 lines (86 loc) · 2.48 KB
/
FuzzyRuleConsequent.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
/*
* Robotic Research Group (RRG)
* State University of Piauí (UESPI), Brazil - Piauí - Teresina
*
* FuzzyRuleConsequent.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 "FuzzyRuleConsequent.h"
// CONTRUCTORS
FuzzyRuleConsequent::FuzzyRuleConsequent()
{
this->fuzzySetOutputs = NULL;
}
// DESTRUCTOR
FuzzyRuleConsequent::~FuzzyRuleConsequent()
{
this->cleanFuzzySets(this->fuzzySetOutputs);
}
// PUBLIC METHODS
// Method to include a new FuzzySet (for Output) into FuzzyRuleConsequent
bool FuzzyRuleConsequent::addOutput(FuzzySet *fuzzySet)
{
// auxiliary variable to handle the operation
fuzzySetOutputArray *newOne;
// allocating in memory
if ((newOne = (fuzzySetOutputArray *)malloc(sizeof(fuzzySetOutputArray))) == NULL)
{
// return false if in out of memory
return false;
}
// building the object
newOne->fuzzySet = fuzzySet;
newOne->next = NULL;
// if it is the first FuzzySet (for Output), set it as the head
if (this->fuzzySetOutputs == NULL)
{
this->fuzzySetOutputs = newOne;
}
else
{
// auxiliary variable to handle the operation
fuzzySetOutputArray *aux = this->fuzzySetOutputs;
// find the last element of the array
while (aux != NULL)
{
if (aux->next == NULL)
{
// make the relations between them
aux->next = newOne;
return true;
}
aux = aux->next;
}
}
return true;
}
// Method to evaluate this FuzzyRuleConsequent
bool FuzzyRuleConsequent::evaluate(float power)
{
// auxiliary variable to handle the operation
fuzzySetOutputArray *aux = this->fuzzySetOutputs;
// while not in the end of the array, iterate
while (aux != NULL)
{
// set the pertinence of each FuzzySet with the power
aux->fuzzySet->setPertinence(power);
aux = aux->next;
}
return true;
}
// PRIVATE METHODS
// Method to recursively clean all fuzzySetOutputArray from memory
void FuzzyRuleConsequent::cleanFuzzySets(fuzzySetOutputArray *aux)
{
if (aux != NULL)
{
this->cleanFuzzySets(aux->next);
// emptying allocated memory
free(aux);
}
}