解决断言错误和值错误的解决方案?

时间:2020-03-12 18:23:09

标签: python numpy skfuzzy

这是我的代码,存在一些问题。

AssertionError:解模糊时总面积为零!

ValueError:无法计算出清晰的输出,可能是因为系统太稀疏。检查以确保这组输入值将通过当前的一组规则激活每个前项中的至少一个连接的术语。 运行我的代码后,显示这两个错误的顶部。

下面的代码是我运行后的完整错误消息。 我可以知道问题出在哪里吗?

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.

1 个答案:

答案 0 :(得分:1)

当它说“太稀疏”时,表示存在未激活规则的区域,因此无法计算面积(hiper区域),因此无法计算重心(基本上除以零)。 就您而言:

规则1:C0和C02的成员资格为0

规则2:湿度和C02的成员资格为

规则3:温度,湿度和一氧化碳的成员资格为0

规则4:温度和二氧化碳的成员资格为0。

因此不会生成任何区域