This is my code with some problem.
AssertionError: Total area is zero in defuzzification!
ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules. Top of this two error is show after i run my code.
Below the code is the full error message after i run. may i know what and where is the problem ?
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
Temp = ctrl.Antecedent(np.arange(0,101,1),'temperature')
Temp['Low'] = fuzz.trapmf(Temp.universe, [0,0,20,30])
Temp['Medium'] = fuzz.trimf(Temp.universe, [25,50,70])
Temp['High'] = fuzz.trapmf(Temp.universe, [60,80,100,100])
Humidity = ctrl.Antecedent(np.arange(0,101,1),'Humidity')
Humidity['Poor']=fuzz.trapmf(Humidity.universe, [0,0,10,20])
Humidity['Fair']=fuzz.trimf(Humidity.universe, [15,30,50])
Humidity['Good'] = fuzz.trapmf(Humidity.universe, [45,60,100,100])
CO = ctrl.Antecedent(np.arange(0,101,1),'CO')
CO['Low'] = fuzz.trapmf(CO.universe, [0,0,25,35])
CO['Medium'] = fuzz.trimf(CO.universe, [30,50,70])
CO['High'] = fuzz.trapmf(CO.universe, [65,80,100,100])
CO2 = ctrl.Antecedent(np.arange(0,101,1),'CO2')
CO2['Low'] = fuzz.trapmf(CO2.universe, [0,0,10,20])
CO2['Medium'] = fuzz.trimf(CO2.universe, [15,30,50])
CO2['High'] = fuzz.trapmf(CO2.universe, [45,60,100,100])
Condition = ctrl.Consequent(np.arange(0,101,1),'Health Condition')
Condition['Very Good']=fuzz.trapmf(Condition.universe, [0,0,10,30])
Condition['Good']=fuzz.trimf(Condition.universe, [20,30,40])
Condition['Medium']=fuzz.trimf(Condition.universe, [35,50,65])
Condition['Bad']=fuzz.trimf(Condition.universe, [60,70,80])
Condition['Dangerous']=fuzz.trapmf(Condition.universe, [75,90,100,100])
Temp.view()
Humidity.view()
CO.view()
CO2.view()
Condition.view()
rule1 = ctrl.Rule(Temp['Medium'] & Humidity['Fair'] & CO['Low'] & CO2['Low'], Condition['Good'])
rule2 = ctrl.Rule(Temp['Medium'] & Humidity['Good'] & CO['Medium'] & CO2['Low'], Condition['Very Good'])
rule3 = ctrl.Rule(Temp['Low'] & Humidity['Poor'] & CO['High'] & CO2['High'], Condition['Dangerous'])
rule4 = ctrl.Rule(Temp['High'] & Humidity['Poor'] & CO['Medium'] & CO2['Low'], Condition['Bad'])
rule5 = ctrl.Rule(Temp['Low'] & Humidity['Fair'] & CO['Medium'] & CO2['Medium'], Condition['Medium'])
rule1.view()
rule2.view()
rule3.view()
rule4.view()
rule5.view()
Health_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5])
HealthResult = ctrl.ControlSystemSimulation(Health_ctrl)
HealthResult.input['temperature'] =45
HealthResult.input['Humidity'] =40
HealthResult.input['CO'] =50
HealthResult.input['CO2'] =75
HealthResult.compute()
print(HealthResult.output['Health Condition'])
Condition.view(sim=HealthResult)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/skfuzzy/control/controlsystem.py in defuzz(self)
586 return defuzz(ups_universe, output_mf,
--> 587 self.var.defuzzify_method)
588 except AssertionError:
3 frames
/usr/local/lib/python3.6/dist-packages/skfuzzy/defuzzify/defuzz.py in defuzz(x, mfx, mode)
247 zero_truth_degree = mfx.sum() == 0 # Approximation of total area
--> 248 assert not zero_truth_degree, 'Total area is zero in defuzzification!'
249
AssertionError: Total area is zero in defuzzification!
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-4-f358e5f1a15d> in <module>()
56 HealthResult.input['CarbonDioxide'] =75
57
---> 58 HealthResult.compute()
59
60 print(HealthResult.output['Health Condition'])
/usr/local/lib/python3.6/dist-packages/skfuzzy/control/controlsystem.py in compute(self)
371 for consequent in self.ctrl.consequents:
372 consequent.output[self] = \
--> 373 CrispValueCalculator(consequent, self).defuzz()
374 self.output[consequent.label] = consequent.output[self]
375
/usr/local/lib/python3.6/dist-packages/skfuzzy/control/controlsystem.py in defuzz(self)
587 self.var.defuzzify_method)
588 except AssertionError:
--> 589 raise ValueError("Crisp output cannot be calculated, likely "
590 "because the system is too sparse. Check to "
591 "make sure this set of input values will "
ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules.
When it says "too sparse" is saying that there are zones where no rule is activated, therefore no area can be calculated (hiper-area), so no center of gravity can be calculated (division by zero basically). In your case:
Rule 1: C0 and C02 have 0 membership
Rule 2: Humidity and C02 have 0 membership
Rule 3: Temperature, Humidity and CO have 0 membership
Rule 4: Tempreature and CO2 have 0 membership.
Therefore no area is generated