Fuzzy system ValueError

835 Views Asked by At

I have built a fuzzy control system that takes 9 inputs (normal, fire, water, electric, grass, ice, fighting, poison) that accept (low, medium, or high) values and returns one output (weakness) with (weak or strong) 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.

import skfuzzy as fuzz
from skfuzzy import control as ctrl

normal = ctrl.Antecedent(np.arange(0, 100, 1), 'normal')
fire = ctrl.Antecedent(np.arange(0, 100, 1), 'fire')
water = ctrl.Antecedent(np.arange(0, 100, 1), 'water')
electric = ctrl.Antecedent(np.arange(0, 100, 1), 'electric')
grass = ctrl.Antecedent(np.arange(0, 100, 1), 'grass')
ice = ctrl.Antecedent(np.arange(0, 100, 1), 'ice')
fighting = ctrl.Antecedent(np.arange(0, 100, 1), 'fighting')
poison = ctrl.Antecedent(np.arange(0, 100, 1), 'poison')
weakness = ctrl.Consequent(np.arange(0, 100, 1), 'weakness')

#normal.automf(3)
#fire.automf(3)
#water.automf(3)
#electric.automf(3)
#grass.automf(3)
#ice.automf(3)
#fighting.automf(3)
#poison.automf(3)
#weakness.autof(3)

#create membership function for normal
normal['low'] = fuzz.trapmf (normal.universe, [0, 0, 10, 20])
normal['medium'] = fuzz.trimf (normal.universe, [0, 25, 50])
normal['high'] = fuzz.trapmf (normal.universe, [25, 75, 100, 101])

#create membership function for fire
fire['low'] = fuzz.trapmf (fire.universe, [0, 0, 20, 50])
fire['medium'] = fuzz.trapmf (fire.universe, [0, 0, 20, 50])
fire['high'] = fuzz.trapmf (fire.universe, [25, 75, 100, 101])

#create membership function for water
water['low'] = fuzz.trapmf (water.universe, [0, 0, 20, 55])
water['medium'] = fuzz.trapmf (water.universe, [0, 0, 20, 55])
water['high'] = fuzz.trapmf (water.universe, [25, 75, 100, 101])

#create membership function for electric
electric['low'] = fuzz.trapmf (electric.universe, [0, 0, 25, 50])
electric['medium'] = fuzz.trapmf (electric.universe, [0, 0, 25, 50])
electric['high'] = fuzz.trapmf (electric.universe, [25, 75, 100, 101])

#create membership function for grass
grass['low'] = fuzz.trapmf (grass.universe, [0, 0,50, 65])
grass['medium'] = fuzz.trapmf (grass.universe, [0, 0,50, 65])
grass['high'] = fuzz.trapmf (grass.universe, [25, 75, 100, 101])

#create membership function for ice
ice['low'] = fuzz.trapmf (ice.universe, [0, 0, 10, 20])
ice['medium'] = fuzz.trapmf (ice.universe, [0, 25, 50, 60])
ice['high'] = fuzz.trapmf (ice.universe, [25, 75, 100, 101])

#create membership function for fighting
fighting['low'] = fuzz.trapmf (fighting.universe, [0, 0, 10, 20])
fighting['medium'] = fuzz.trapmf (fighting.universe, [00, 25, 50, 60])
fighting['high'] = fuzz.trapmf (fighting.universe, [25, 75, 100, 101])

#create membership function for poison
poison['low'] = fuzz.trapmf (poison.universe, [0, 0, 10, 20])
poison['medium'] = fuzz.trapmf (poison.universe, [0, 25, 50, 60])
poison['high'] = fuzz.trapmf (poison.universe, [25, 75, 100, 101])

#create membership function for weakness
weakness['weak'] = fuzz.trapmf (weakness.universe, [0, 0,50, 65])
weakness['strong'] = fuzz.trapmf (weakness.universe, [5, 50, 100, 100])

normal.view()
fire.view()
water.view()
electric.view()
grass.view()
ice.view()
fighting.view()
poison.view()

rule1 = ctrl.Rule(normal['low'] & fire['low'] & water['low'] & electric['low'] & grass['low'] & ice['low'] & fighting['low'] & poison['low'], weakness['strong'] )
rule2 = ctrl.Rule(normal['medium'] & fire['medium'] & water['medium'] & electric['medium'] & grass['medium'] & ice['medium'] & fighting['medium'] & poison['medium'], weakness['strong'] )
rule3 = ctrl.Rule(normal['high'] & fire['high'] & water['high'] & electric['high'] & grass['high'] & ice['high'] & fighting['high'] & poison['high'], weakness['weak'] )
rule4 = ctrl.Rule(normal['medium'] & fire['medium'] & water['high'] & electric['low'] & grass['low'] & ice['low'] & fighting['medium'] & poison['low'], weakness['strong'] )
rule5 = ctrl.Rule(normal['high'] & fire['high'] & water['medium'] & electric['medium'] & grass['low'] & ice['high'] & fighting['high'] & poison['high'], weakness['weak'] )

rule1.view()
rule2.view()
rule3.view()
rule4.view()
rule5.view()

weaklevel_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5])
weaklevel = ctrl.ControlSystemSimulation(weaklevel_ctrl)

# Pass inputs to the ControlSystem using Antecedent labels with Pythonic API
weaklevel.input['normal'] = 25
weaklevel.input['fire'] = 57
weaklevel.input['water'] = 35
weaklevel.input['electric'] = 27
weaklevel.input['grass'] = 50
weaklevel.input['ice'] = 48
weaklevel.input['fighting'] = 35
weaklevel.input['poison'] = 22

#Crunch the number
weaklevel.compute()

print (weaklevel.output['weakness'])
weakness.view(sim=weaklevel)

and the error I got is

/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
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.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.

0

There are 0 best solutions below