Open damiantestkonto opened 3 years ago
Same here, tried using this library in my uni assignment but kept getting wrong answers. Something is not right here
I believe you need to do fuzzyEngine = new FuzzyEngineFactory().Default();
for each iteration. It was pointed out in another issue the engine only works once. I haven't re-run your test with this to verify.
I have modified the code to create a new FuzzyEngine in each iteration, but the results still don't seem right. Using:
var water = new LinguisticVariable("Water");
var cold = water.MembershipFunctions.AddTrapezoid("Cold", 0, 0, 20, 40);
var warm = water.MembershipFunctions.AddTriangle("Warm", 30, 50, 70);
var hot = water.MembershipFunctions.AddTrapezoid("Hot", 50, 80, 100, 100);
var power = new LinguisticVariable("Power");
var low = power.MembershipFunctions.AddTriangle("Low", 0, 25, 50);
var high = power.MembershipFunctions.AddTriangle("High", 25, 50, 75);
FuzzyRule[] rules = new FuzzyRule[2];
rules[0] = Rule.If(water.Is(cold).Or(water.Is(warm))).Then(power.Is(high));
rules[1] = Rule.If(water.Is(hot)).Then(power.Is(low));
for (int i=0; i<=100; i+=10)
{
IFuzzyEngine fuzzyEngine = new FuzzyEngineFactory().Default();
fuzzyEngine.Rules.Add(rules[0], rules[1]);
Console.WriteLine("Temp: {0} Cold: {1} Warm: {2} Hot: {3} Result: {4}", i, cold.Fuzzify(i), warm.Fuzzify(i), hot.Fuzzify(i), fuzzyEngine.Defuzzify(new { water = i }));
}
Console.ReadLine();
gives the following results:
Temp: 0 Cold: 1 Warm: 0 Hot: 0 Result: 50 Temp: 10 Cold: 1 Warm: 0 Hot: 0 Result: 50 Temp: 20 Cold: 1 Warm: 0 Hot: 0 Result: 50 Temp: 30 Cold: 0.5 Warm: 0 Hot: 0 Result: 50 Temp: 40 Cold: 0 Warm: 0.5 Hot: 0 Result: 50 Temp: 50 Cold: 0 Warm: 1 Hot: 0 Result: 50 Temp: 60 Cold: 0 Warm: 0.5 Hot: 0.3333333333333333 Result: 43.75000000000001 Temp: 70 Cold: 0 Warm: 0 Hot: 0.6666666666666666 Result: 40 Temp: 80 Cold: 0 Warm: 0 Hot: 1 Result: 37.5 Temp: 90 Cold: 0 Warm: 0 Hot: 1 Result: 37.5 Temp: 100 Cold: 0 Warm: 0 Hot: 1 Result: 37.5
So, I understand the results at low temperatures - the membership of the "high" function is one, so it chooses the centre of the "high" triangle, which is 50. But at high water temperatures, I would expect the result to be the centre of the "low" triangle, which is 25. But instead we get 37.5 - so it seems to be computing the centre of both the high and low triangles, which is incorrect.
In submited example I added
for
loop to iterate through all water temperatures:Which give me:
Why there are no
result
between50
and75
? Also, there are noresult
between25
and50
.