Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - add mean of maximum #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions EFLLConstants.h
Original file line number Diff line number Diff line change
@@ -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
14 changes: 12 additions & 2 deletions Fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion Fuzzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "FuzzyInput.h"
#include "FuzzyOutput.h"
#include "FuzzyRule.h"
#include "EFLLConstants.h"

// Array struct for FuzzyInput objects
struct fuzzyInputArray
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
94 changes: 70 additions & 24 deletions FuzzyComposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 4 additions & 1 deletion FuzzyComposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// IMPORTING NECESSARY LIBRARIES
#include <stdlib.h>
#include "EFLLConstants.h"

// CONSTANTS
#define EPSILON_VALUE 1.0E-3
Expand All @@ -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();

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions FuzzyOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion FuzzyOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FuzzyOutput : public FuzzyIO
~FuzzyOutput();
// PUBLIC METHODS
bool truncate();
float getCrispOutput();
float getCrispOutput(int method);
bool order();
FuzzyComposition *getFuzzyComposition();

Expand Down