输入过程中的模糊系统python断言错误

时间:2017-05-29 14:16:47

标签: python fuzzy-logic

我建立了一个模糊控制系统,它接受三个输入(x,y,z)接受(差,平均或好)值并返回一个输出(w)的值(低,平均或高)。不幸的是,当我测试系统时,我得到一个断言错误而没有任何附加信息。用于构建系统的代码如下所示。

import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import matplotlib.pyplot as plt
from matplotlib import rc
x=ctrl.Antecedent(np.arange(0,1,0.001), 'x')
y=ctrl.Antecedent(np.arange(0,1,0.001), 'y')
z=ctrl.Antecedent(np.arange(0,1,0.001), 'y')
w=ctrl.Consequent(np.arange(0,1,0.1),'w')
x.automf(3)
y.automf(5)
z.automf(7)
w['low'] = fuzz.trimf(w.universe, [0, 0, 0.3])
w['average'] = fuzz.trimf(w.universe, [0.25, 0.5, 0.75])
w['high'] = fuzz.trimf(w.universe, [0.7, 1, 1])
rule1 = ctrl.Rule(x['poor'] & y['poor'] & z['poor'], w['low'])
rule2 = ctrl.Rule(x['poor'] & y['poor'] & z['good'], w['low'])
rule3 = ctrl.Rule(x['poor'] & y['good'] & z['poor'], w['high'])
rule4 = ctrl.Rule(x['poor'] & y['good'] & z['good'], w['average'])
rule5 = ctrl.Rule(x['good'] & y['poor'] & z['poor'], w['low'])
rule6 = ctrl.Rule(x['good'] & y['poor'] & z['good'], w['low'])
rule7 = ctrl.Rule(x['good'] & y['good'] & z['poor'], w['average'])
rule8 = ctrl.Rule(x['good'] & y['good'] & z['good'], w['low'])
w_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8])
wResults= ctrl.ControlSystemSimulation(w_ctrl)
wResults.input['x'] = 0.03
wResults.input['y'] = 0.7
wResults.input['z'] = 0.01
wResults.compute() 

我得到的错误是:

AssertionError                            Traceback (most recent call last)
<ipython-input-162-4519156d7582> in <module>()
     25 wResults= ctrl.ControlSystemSimulation(w_ctrl)
     26 wResults.input['x'] = 0.03
---> 27 wResults.input['y'] = 0.7
     28 wResults.input['z'] = 0.01
     29 wResults.compute()

C:\Users\user01\Anaconda3\lib\site-packages\skfuzzy\control\controlsystem.py in __setitem__(self, key, value)
    129         if len(matches) == 0:
    130             raise ValueError("Unexpected input: " + key)
--> 131         assert len(matches) == 1
    132         var = matches[0]
    133 

AssertionError: 

我想知道是否有人知道这段代码出错了。

1 个答案:

答案 0 :(得分:1)

第一个问题是拼写错误,

z = ctrl.Antecedent(np.arange(0,1,0.001), 'y')    # should be 'z'!

现在代码运行时返回

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.

...因为

wResults.input['y'] = 0.7     # classifies as "decent",
                              # and you haven't given any relevant rules