diff --git a/EFLLConstants.h b/EFLLConstants.h new file mode 100644 index 0000000..626d248 --- /dev/null +++ b/EFLLConstants.h @@ -0,0 +1,12 @@ +#ifndef EFLLCONSTANTS_H +#define EFLLCONSTANTS_H + +namespace EFLLConstants +{ + const int DEFUZZIFICATION_CETROID = 1; + const int DEFUZZIFICATION_MEAN_MAX = 2; + + const int METHOD_MIN = 1; + const int METHOD_MAX = 2; +} +#endif \ No newline at end of file diff --git a/Fuzzy.cpp b/Fuzzy.cpp index 102db4a..f74bc03 100755 --- a/Fuzzy.cpp +++ b/Fuzzy.cpp @@ -13,9 +13,19 @@ #include "Fuzzy.h" // CONTRUCTORS -Fuzzy::Fuzzy() +Fuzzy::Fuzzy(int andMethod, int orMethod, int implicationMethod, int aggregationMethod, int defuzzifyMethod) { // Initializing pointers with NULL + // andMethod + this->andMethod = andMethod; + // orMethod + this->orMethod = orMethod; + // implicationMethod + this->implicationMethod = implicationMethod; + // aggregationMethod + this->aggregationMethod = aggregationMethod; + // defuzzifyMethod + this->defuzzifyMethod = defuzzifyMethod; // FuzzyInput this->fuzzyInputs = NULL; // FuzzyOutput @@ -268,7 +278,7 @@ float Fuzzy::defuzzify(int fuzzyOutputIndex) if (aux->fuzzyOutput->getIndex() == fuzzyOutputIndex) { // return the calculated result - return aux->fuzzyOutput->getCrispOutput(); + return aux->fuzzyOutput->getCrispOutput(this->defuzzifyMethod); } aux = aux->next; } diff --git a/Fuzzy.h b/Fuzzy.h index 8cfbed5..b5f57af 100755 --- a/Fuzzy.h +++ b/Fuzzy.h @@ -18,6 +18,7 @@ #include "FuzzyInput.h" #include "FuzzyOutput.h" #include "FuzzyRule.h" +#include "EFLLConstants.h" // Array struct for FuzzyInput objects struct fuzzyInputArray @@ -45,7 +46,7 @@ class Fuzzy { public: // CONTRUCTORS - Fuzzy(); + Fuzzy(int andMethod = EFLLConstants::METHOD_MIN, int orMethod = EFLLConstants::METHOD_MAX, int implicationMethod = EFLLConstants::METHOD_MIN, int aggregationMethod = EFLLConstants::METHOD_MAX, int defuzzifyMethod = EFLLConstants::DEFUZZIFICATION_CETROID); // DESTRUCTOR ~Fuzzy(); @@ -61,6 +62,16 @@ class Fuzzy private: // PRIVATE VARIABLES + // holds the and method + int andMethod; + // holds the or method + int orMethod; + // holds the implication method + int implicationMethod; + // holds the aggregation method + int aggregationMethod; + // holds the final defuzzification method + int defuzzifyMethod; // pointers to manage the array of FuzzyInput fuzzyInputArray *fuzzyInputs; // pointers to manage the array of FuzzyOutput diff --git a/FuzzyComposition.cpp b/FuzzyComposition.cpp index da6f9c2..17f60b6 100755 --- a/FuzzyComposition.cpp +++ b/FuzzyComposition.cpp @@ -122,8 +122,46 @@ bool FuzzyComposition::build() return true; } +// Method to return the calculated value of this FuzzyComposition +float FuzzyComposition::calculate(int method) +{ + if (method == EFLLConstants::DEFUZZIFICATION_MEAN_MAX) + { + return this->calculateMeanMax(); + } + return this->calculateCentroid(); +} + +// Method to reset the Object +bool FuzzyComposition::empty() +{ + // clean all pointsArray from memory + this->cleanPoints(this->points); + // reset the pointer + this->points = NULL; + return true; +} + +// Method to count the amount of points used in this FuzzyComposition +int FuzzyComposition::countPoints() +{ + // variable to hold the count + int count = 0; + // auxiliary variable to handle the operation + pointsArray *aux = this->points; + // while not in the end of the array, iterate + while (aux != NULL) + { + count = count + 1; + aux = aux->next; + } + return count; +} + +// PRIVATE METHODS + // Method to calculate the center of the area of this FuzzyComposition -float FuzzyComposition::calculate() +float FuzzyComposition::calculateCentroid() { // auxiliary variable to handle the operation, instantiate with the first element from array pointsArray *aux = this->points; @@ -187,40 +225,48 @@ float FuzzyComposition::calculate() { return 0.0; } - else - { - return numerator / denominator; - } -} - -// Method to reset the Object -bool FuzzyComposition::empty() -{ - // clean all pointsArray from memory - this->cleanPoints(this->points); - // reset the pointer - this->points = NULL; - return true; + return numerator / denominator; } -// Method to count the amount of points used in this FuzzyComposition -int FuzzyComposition::countPoints() +// Method to calculate the middle of the maxima of this FuzzyComposition +float FuzzyComposition::calculateMeanMax() { - // variable to hold the count - int count = 0; - // auxiliary variable to handle the operation + // it holds the max pertinence value for reference + float maxPertinence = 0.0; + // it holds the count of max occurrences + float count = 0; + // it holds the sum of all max + float sumOfMaxPoints = 0.0; + // auxiliary variable to handle the operation, instantiate with the first element from array pointsArray *aux = this->points; // while not in the end of the array, iterate while (aux != NULL) { - count = count + 1; + // check if there is a new max + if (aux->pertinence > maxPertinence) + { + // if so, use it + maxPertinence = aux->pertinence; + // reset all auxiliary values + count = 1; + sumOfMaxPoints = aux->point; + } + else if (aux->pertinence == maxPertinence) + { + // if equal pertinence, increment values + count += 1; + sumOfMaxPoints += aux->point; + } aux = aux->next; } - return count; + // avoiding zero division + if (count == 0) + { + return 0.0; + } + return sumOfMaxPoints / count; } -// PRIVATE METHODS - // Method to recursively clean all pointsArray structs from memory void FuzzyComposition::cleanPoints(pointsArray *aux) { diff --git a/FuzzyComposition.h b/FuzzyComposition.h index b3b3f89..93bac11 100755 --- a/FuzzyComposition.h +++ b/FuzzyComposition.h @@ -15,6 +15,7 @@ // IMPORTING NECESSARY LIBRARIES #include +#include "EFLLConstants.h" // CONSTANTS #define EPSILON_VALUE 1.0E-3 @@ -39,7 +40,7 @@ class FuzzyComposition bool addPoint(float point, float pertinence); bool checkPoint(float point, float pertinence); bool build(); - float calculate(); + float calculate(int method); bool empty(); int countPoints(); @@ -48,6 +49,8 @@ class FuzzyComposition pointsArray *points; // PRIVATE METHODS + float calculateCentroid(); + float calculateMeanMax(); void cleanPoints(pointsArray *aux); bool rebuild(pointsArray *aSegmentBegin, pointsArray *aSegmentEnd, pointsArray *bSegmentBegin, pointsArray *bSegmentEnd); bool rmvPoint(pointsArray *point); diff --git a/FuzzyOutput.cpp b/FuzzyOutput.cpp index b305efe..742a8cd 100755 --- a/FuzzyOutput.cpp +++ b/FuzzyOutput.cpp @@ -140,9 +140,9 @@ bool FuzzyOutput::truncate() } // Method to run the calculate of FuzzyComposition and return the result -float FuzzyOutput::getCrispOutput() +float FuzzyOutput::getCrispOutput(int method) { - return this->fuzzyComposition->calculate(); + return this->fuzzyComposition->calculate(method); } // Method to sort the FuzzySet by the reference of the point A in an ascending order diff --git a/FuzzyOutput.h b/FuzzyOutput.h index c058810..f9fa7e8 100755 --- a/FuzzyOutput.h +++ b/FuzzyOutput.h @@ -27,7 +27,7 @@ class FuzzyOutput : public FuzzyIO ~FuzzyOutput(); // PUBLIC METHODS bool truncate(); - float getCrispOutput(); + float getCrispOutput(int method); bool order(); FuzzyComposition *getFuzzyComposition();