I have built a fuzzy control system that takes three inputs (fever, headache, nausea, vomiting, jaundice, enlarge_liver, joint_pain, body_weakness, dizziness, loss_of_appetite, mp) that accept (mild, moderate, severe, very severe) values and returns one output (conclusion) with (mild, moderate, severe, very severe) value. unfortunately, when I test the system I get an assertion error without any additional information. the code used to build the system is shown below.
# install required libraries; only need to run once per session
!pip install -U numpy scikit-fuzzy matplotlib
# Import necessary libraries
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import matplotlib.pyplot as plt
# Create the universe
# For this example: we'll consider the factors that determine the tip for a restaurant bill based on food quality and service.
fever = ctrl.Antecedent(np.arange(0, 11, 1), 'fever')
headache = ctrl.Antecedent(np.arange(0, 11, 1), 'headache')
nausea = ctrl.Antecedent(np.arange(0, 11, 1), 'nausea')
vomiting = ctrl.Antecedent(np.arange(0, 11, 1), 'vomiting')
jaundice = ctrl.Antecedent(np.arange(0, 11, 1), 'jaundice')
enlarge_liver = ctrl.Antecedent(np.arange(0, 11, 1), 'enlarge_liver')
joint_pain = ctrl.Antecedent(np.arange(0, 11, 1), 'joint_pain')
body_weakness = ctrl.Antecedent(np.arange(0, 11, 1), 'body_weakness')
dizziness = ctrl.Antecedent(np.arange(0, 11, 1), 'dizziness')
loss_of_appetite = ctrl.Antecedent(np.arange(0, 11, 1), 'loss_of_appetite')
mp = ctrl.Antecedent(np.arange(0, 11, 1), 'mp')
conclusion = ctrl.Consequent(np.arange(0, 11, 1), 'malaria_severity')
# Membership functions for fever
# Convert a scale of 0-10 to three fuzzy categories
fever['mild'] = fuzz.trimf(fever.universe, [0, 2, 4])
fever['moderate'] = fuzz.trimf(fever.universe, [3, 5, 7])
fever['severe'] = fuzz.trimf(fever.universe, [6, 8, 10])
fever['very_severe'] = fuzz.trimf(fever.universe, [9, 10, 10])
# Membership functions for headache
# Convert a scale of 0-10 to three fuzzy categories
headache['mild'] = fuzz.trimf(headache.universe, [0, 2, 4])
headache['moderate'] = fuzz.trimf(headache.universe, [3, 5, 7])
headache['severe'] = fuzz.trimf(headache.universe, [6, 8, 10])
headache['very_severe'] = fuzz.trimf(headache.universe, [9, 10, 10])
# Membership function for nausea
# Convert a scale of 0-10 to three fuzzy categories
nausea['mild'] = fuzz.trimf(nausea.universe, [0, 2, 4])
nausea['moderate'] = fuzz.trimf(nausea.universe, [3, 5, 7])
nausea['severe'] = fuzz.trimf(nausea.universe, [6, 8, 10])
nausea['very_severe'] = fuzz.trimf(nausea.universe, [9, 10, 10])
# Membership function for vomiting
# Convert a scale of 0-10 to three fuzzy categories
vomiting['mild'] = fuzz.trimf(vomiting.universe, [0, 2, 4])
vomiting['moderate'] = fuzz.trimf(vomiting.universe, [3, 5, 7])
vomiting['severe'] = fuzz.trimf(vomiting.universe, [6, 8, 10])
vomiting['very_severe'] = fuzz.trimf(vomiting.universe, [9, 10, 10])
# Membership function for jaundice
# Convert a scale of 0-10 to three fuzzy categories
jaundice['mild'] = fuzz.trimf(jaundice.universe, [0, 2, 4])
jaundice['moderate'] = fuzz.trimf(jaundice.universe, [3, 5, 7])
jaundice['severe'] = fuzz.trimf(jaundice.universe, [6, 8, 10])
jaundice['very_severe'] = fuzz.trimf(jaundice.universe, [9, 10, 10])
# Membership function for enlarge_liver
# Convert a scale of 0-10 to three fuzzy categories
enlarge_liver['mild'] = fuzz.trimf(enlarge_liver.universe, [0, 2, 4])
enlarge_liver['moderate'] = fuzz.trimf(enlarge_liver.universe, [3, 5, 7])
enlarge_liver['severe'] = fuzz.trimf(enlarge_liver.universe, [6, 8, 10])
enlarge_liver['very_severe'] = fuzz.trimf(enlarge_liver.universe, [9, 10, 10])
# Membership function for joint_pain
# Convert a scale of 0-10 to three fuzzy categories
joint_pain['mild'] = fuzz.trimf(joint_pain.universe, [0, 2, 4])
joint_pain['moderate'] = fuzz.trimf(joint_pain.universe, [3, 5, 7])
joint_pain['severe'] = fuzz.trimf(joint_pain.universe, [6, 8, 10])
joint_pain['very_severe'] = fuzz.trimf(joint_pain.universe, [9, 10, 10])
# Membership function for body_weakness
# Convert a scale of 0-10 to three fuzzy categories
body_weakness['mild'] = fuzz.trimf(body_weakness.universe, [0, 2, 4])
body_weakness['moderate'] = fuzz.trimf(body_weakness.universe, [3, 5, 7])
body_weakness['severe'] = fuzz.trimf(body_weakness.universe, [6, 8, 10])
body_weakness['very_severe'] = fuzz.trimf(body_weakness.universe, [9, 10, 10])
# Membership function for dizziness
# Convert a scale of 0-10 to three fuzzy categories
dizziness['mild'] = fuzz.trimf(dizziness.universe, [0, 2, 4])
dizziness['moderate'] = fuzz.trimf(dizziness.universe, [3, 5, 7])
dizziness['severe'] = fuzz.trimf(dizziness.universe, [6, 8, 10])
dizziness['very_severe'] = fuzz.trimf(dizziness.universe, [9, 10, 10])
# Membership function for loss_of_appetite
# Convert a scale of 0-10 to three fuzzy categories
loss_of_appetite['mild'] = fuzz.trimf(loss_of_appetite.universe, [0, 2, 4])
loss_of_appetite['moderate'] = fuzz.trimf(loss_of_appetite.universe, [3, 5, 7])
loss_of_appetite['severe'] = fuzz.trimf(loss_of_appetite.universe, [6, 8, 10])
loss_of_appetite['very_severe'] = fuzz.trimf(loss_of_appetite.universe, [9, 10, 10])
# Membership function for mp
# Convert a scale of 0-10 to three fuzzy categories
mp['mild'] = fuzz.trimf(mp.universe, [0, 2, 4])
mp['moderate'] = fuzz.trimf(mp.universe, [3, 5, 7])
mp['severe'] = fuzz.trimf(mp.universe, [6, 8, 10])
mp['very_severe'] = fuzz.trimf(mp.universe, [9, 10, 10])
# Membership function for conclusion
# Convert a scale of 0-10 to three fuzzy categories
conclusion['mild'] = fuzz.trimf(conclusion.universe, [0, 2, 4])
conclusion['moderate'] = fuzz.trimf(conclusion.universe, [3, 5, 7])
conclusion['severe'] = fuzz.trimf(conclusion.universe, [6, 8, 10])
conclusion['very_severe'] = fuzz.trimf(conclusion.universe, [9, 10, 10])
# Display the membership functions
fever.view()
headache.view()
nausea.view()
vomiting.view()
jaundice.view()
enlarge_liver.view()
joint_pain.view()
body_weakness.view()
dizziness.view()
loss_of_appetite.view()
mp.view()
conclusion.view()
# Fuzzy rules
rule1 = ctrl.Rule(fever['mild'] & headache['mild'] &
nausea['mild'] & vomiting['mild'] &
jaundice['mild'] & enlarge_liver['mild'] &
joint_pain['mild'] & body_weakness['mild'] &
dizziness['severe'] & loss_of_appetite['mild'] &
mp['mild'] , conclusion['mild'])
rule2 = ctrl.Rule(fever['moderate'] & headache['mild'] &
nausea['mild'] & vomiting['mild'] &
jaundice['mild'] & enlarge_liver['mild'] &
joint_pain['moderate'] & body_weakness['moderate'] &
dizziness['severe'] & loss_of_appetite['severe'] &
mp['moderate'] , conclusion['moderate'])
rule3 = ctrl.Rule(fever['severe'] & headache['moderate'] &
nausea['mild'] & vomiting['mild'] &
jaundice['mild'] & enlarge_liver['mild'] &
joint_pain['mild'] & body_weakness['severe'] &
dizziness['severe'] & loss_of_appetite['severe'] &
mp['moderate'] , conclusion['severe'])
rule4 = ctrl.Rule(fever['very_severe'] & headache['severe'] &
nausea['severe'] & vomiting['severe'] &
jaundice['severe'] & enlarge_liver['severe'] &
joint_pain['severe'] & body_weakness['severe'] &
dizziness['severe'] & loss_of_appetite['severe'] &
mp['moderate'] , conclusion['very_severe'])
# Rule evaluation
tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4])
tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
# Input values
tipping.input['fever'] = 9
tipping.input['headache'] = 8
tipping.input['nausea'] = 10
tipping.input['vomiting'] = 8
tipping.input['jaundice'] = 9
tipping.input['enlarge_liver'] = 8
tipping.input['joint_pain'] = 10
tipping.input['body_weakness'] = 9
tipping.input['dizziness'] = 9
tipping.input['loss_of_appetite'] = 10
tipping.input['mp'] = 10
# Compute and view the result
tipping.compute()
print("Conclusions:", tipping.output['conclusion'])
conclusion.view(sim=tipping, method='centroid')
and the error I get is:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/skfuzzy/control/controlsystem.py in defuzz(self)
585 try:
--> 586 return defuzz(ups_universe, output_mf,
587 self.var.defuzzify_method)
3 frames
AssertionError: Total area is zero in defuzzification!
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.10/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.