alvesoaj / eFLL

eFLL (Embedded Fuzzy Logic Library) is a standard library for Embedded Systems
MIT License
211 stars 91 forks source link

Defuzzify output is unexpected #28

Closed docwelch closed 2 years ago

docwelch commented 3 years ago

Using the library for temperature control and ran into this issue. The rule fires and the pertinence for the rule is 1.0 yet the value returned from defuzzify is 0.0 - I would expect it to be 1 given the output set is a singleton of 1. I've simplified the input and output sets and rules down to the following and still get the same results:

FuzzyInput *e = new FuzzyInput(1);
FuzzySet *PL = new FuzzySet(6.5, 10, 12, 12);
e->addFuzzySet(PL);
fuzzy->addFuzzyInput(e);

FuzzyOutput *adjust = new FuzzyOutput(1);
FuzzySet *plus100 = new FuzzySet(1, 1, 1, 1);
adjust->addFuzzySet(plus100);
fuzzy->addFuzzyOutput(adjust);

FuzzyRuleAntecedent *PLg = new FuzzyRuleAntecedent();
PLg->joinSingle(PL);
FuzzyRuleConsequent *p100 = new FuzzyRuleConsequent();
p100->addOutput(plus100);

FuzzyRule *FR40 = new FuzzyRule(40, PLg, p100);
fuzzy->addFuzzyRule(FR40);

tError = 11; 
fuzzy->setInput(1, tError);
fuzzy->fuzzify();
float adjustmentFactor = fuzzy->defuzzify(1);

bool wasRuleFired = fuzzy->isFiredRule(40); 
float pert = plus100 -> getPertinence(); 

As noted above, wasRuleFired returns TRUE, and pert=1.0. However, adjustmentFactor returns 0.0. I've modeled this in Octave with fuzzy-logic-toolkit and it works as expected giving an output of 1 with the above conditions.

Of note, changing only the plus100 FuzzySet to the following:

FuzzySet *plus100 = new FuzzySet(.99, 1, 1.01, 1.01);

I get the expected value for adjustmentFactor (1 or 1.01). It appears there is something about using a singleton in the output set and/or consequent of a rule that is problematic.

Thanks for your library and your time to evaluate this.

alvesoaj commented 3 years ago

Please, check the last tag, version 1.4.1