我的 Python 代码执行模糊逻辑时出错
Error in my Python code doing fuzzy logic
以下是解决模糊逻辑的代码,我试图从模糊控制系统(scikit fuzzy liberary)制作但我遇到错误ValueError: Unexpected input: temprature
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
temprature = ctrl.Antecedent(np.arange(0, 15, 1), 'temperature')
moisture = ctrl.Antecedent(np.arange(0, 11, 1), 'moisture')
humidity = ctrl.Antecedent(np.arange(0, 11, 1), 'humidity')
minutes = ctrl.Consequent(np.arange(0,26, 1), 'minutes')
temprature.automf(3)
moisture.automf(3)
humidity.automf(3)
minutes['low'] = fuzz.trimf(minutes.universe, [0, 0, 13])
minutes['medium'] = fuzz.trimf(minutes.universe, [0, 13, 25])
minutes['high'] = fuzz.trimf(minutes.universe, [13, 25, 25])
rule1 = ctrl.Rule(temprature['poor'] | moisture['poor'] | humidity['poor'], minutes['low'])
rule2 = ctrl.Rule(temprature['average'] | moisture['average'] | humidity['average'], minutes['medium'])
rule3 = ctrl.Rule(temprature['good'] | moisture['good'] | humidity['good'], minutes['high'])
tap_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tap_control_system = ctrl.ControlSystemSimulation(tap_ctrl)
tap_control_system.input['temprature'] = 1
tap_control_system.input['moisture'] = 1
tap_control_system.input['humidity'] = 1
tap_control_system.compute()
print(tap_control_system.output['minutes'])
错误:
Traceback (most recent call last):
File "engine.py", line 30, in <module>
tap_control_system.input['temprature'] = 1
File "C:\Users\Toshiba\anaconda3\envs\Fuzzy-System-Project\lib\site-packages\skfuzzy\control\controlsystem.py", line 168, in __setitem__
raise ValueError("Unexpected input: " + key)
ValueError: Unexpected input: temprature
打错了。你这里有错字:
tap_control_system.input['temprature'] = 1
应该是:
tap_control_system.input['temperature'] = 1
笔记本在 Jupyter website 上试用:
# In [1]:
!pip install scikit-fuzzy
Collecting scikit-fuzzy
Downloading scikit-fuzzy-0.4.2.tar.gz (993 kB)
|████████████████████████████████| 993 kB 4.2 MB/s eta 0:00:01
Requirement already satisfied: numpy>=1.6.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (1.19.5)
Requirement already satisfied: scipy>=0.9.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (1.5.3)
Requirement already satisfied: networkx>=1.9.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (2.5)
Requirement already satisfied: decorator>=4.3.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from networkx>=1.9.0->scikit-fuzzy) (4.4.2)
Building wheels for collected packages: scikit-fuzzy
Building wheel for scikit-fuzzy (setup.py) ... done
Created wheel for scikit-fuzzy: filename=scikit_fuzzy-0.4.2-py3-none-any.whl size=894068 sha256=ac90a92c7d392cfe40f066083f856561db6152a2a6bab3bfa86bf7293fb0568a
Stored in directory: /home/jovyan/.cache/pip/wheels/31/1e/58/db8cfe08f81c72d8c31bc58690ce63d9e3d93a6e97dca5ddb4
Successfully built scikit-fuzzy
Installing collected packages: scikit-fuzzy
Successfully installed scikit-fuzzy-0.4.2
# In [2]:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
temprature = ctrl.Antecedent(np.arange(0, 15, 1), 'temperature')
moisture = ctrl.Antecedent(np.arange(0, 11, 1), 'moisture')
humidity = ctrl.Antecedent(np.arange(0, 11, 1), 'humidity')
minutes = ctrl.Consequent(np.arange(0,26, 1), 'minutes')
temprature.automf(3)
moisture.automf(3)
humidity.automf(3)
minutes['low'] = fuzz.trimf(minutes.universe, [0, 0, 13])
minutes['medium'] = fuzz.trimf(minutes.universe, [0, 13, 25])
minutes['high'] = fuzz.trimf(minutes.universe, [13, 25, 25])
rule1 = ctrl.Rule(temprature['poor'] | moisture['poor'] | humidity['poor'], minutes['low'])
rule2 = ctrl.Rule(temprature['average'] | moisture['average'] | humidity['average'], minutes['medium'])
rule3 = ctrl.Rule(temprature['good'] | moisture['good'] | humidity['good'], minutes['high'])
tap_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tap_control_system = ctrl.ControlSystemSimulation(tap_ctrl)
tap_control_system.input['temperature'] = 1
tap_control_system.input['moisture'] = 1
tap_control_system.input['humidity'] = 1
tap_control_system.compute()
print(tap_control_system.output['minutes'])
8.085852534617038
以下是解决模糊逻辑的代码,我试图从模糊控制系统(scikit fuzzy liberary)制作但我遇到错误ValueError: Unexpected input: temprature
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
temprature = ctrl.Antecedent(np.arange(0, 15, 1), 'temperature')
moisture = ctrl.Antecedent(np.arange(0, 11, 1), 'moisture')
humidity = ctrl.Antecedent(np.arange(0, 11, 1), 'humidity')
minutes = ctrl.Consequent(np.arange(0,26, 1), 'minutes')
temprature.automf(3)
moisture.automf(3)
humidity.automf(3)
minutes['low'] = fuzz.trimf(minutes.universe, [0, 0, 13])
minutes['medium'] = fuzz.trimf(minutes.universe, [0, 13, 25])
minutes['high'] = fuzz.trimf(minutes.universe, [13, 25, 25])
rule1 = ctrl.Rule(temprature['poor'] | moisture['poor'] | humidity['poor'], minutes['low'])
rule2 = ctrl.Rule(temprature['average'] | moisture['average'] | humidity['average'], minutes['medium'])
rule3 = ctrl.Rule(temprature['good'] | moisture['good'] | humidity['good'], minutes['high'])
tap_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tap_control_system = ctrl.ControlSystemSimulation(tap_ctrl)
tap_control_system.input['temprature'] = 1
tap_control_system.input['moisture'] = 1
tap_control_system.input['humidity'] = 1
tap_control_system.compute()
print(tap_control_system.output['minutes'])
错误:
Traceback (most recent call last):
File "engine.py", line 30, in <module>
tap_control_system.input['temprature'] = 1
File "C:\Users\Toshiba\anaconda3\envs\Fuzzy-System-Project\lib\site-packages\skfuzzy\control\controlsystem.py", line 168, in __setitem__
raise ValueError("Unexpected input: " + key)
ValueError: Unexpected input: temprature
打错了。你这里有错字:
tap_control_system.input['temprature'] = 1
应该是:
tap_control_system.input['temperature'] = 1
笔记本在 Jupyter website 上试用:
# In [1]:
!pip install scikit-fuzzy
Collecting scikit-fuzzy
Downloading scikit-fuzzy-0.4.2.tar.gz (993 kB)
|████████████████████████████████| 993 kB 4.2 MB/s eta 0:00:01
Requirement already satisfied: numpy>=1.6.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (1.19.5)
Requirement already satisfied: scipy>=0.9.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (1.5.3)
Requirement already satisfied: networkx>=1.9.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from scikit-fuzzy) (2.5)
Requirement already satisfied: decorator>=4.3.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from networkx>=1.9.0->scikit-fuzzy) (4.4.2)
Building wheels for collected packages: scikit-fuzzy
Building wheel for scikit-fuzzy (setup.py) ... done
Created wheel for scikit-fuzzy: filename=scikit_fuzzy-0.4.2-py3-none-any.whl size=894068 sha256=ac90a92c7d392cfe40f066083f856561db6152a2a6bab3bfa86bf7293fb0568a
Stored in directory: /home/jovyan/.cache/pip/wheels/31/1e/58/db8cfe08f81c72d8c31bc58690ce63d9e3d93a6e97dca5ddb4
Successfully built scikit-fuzzy
Installing collected packages: scikit-fuzzy
Successfully installed scikit-fuzzy-0.4.2
# In [2]:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
temprature = ctrl.Antecedent(np.arange(0, 15, 1), 'temperature')
moisture = ctrl.Antecedent(np.arange(0, 11, 1), 'moisture')
humidity = ctrl.Antecedent(np.arange(0, 11, 1), 'humidity')
minutes = ctrl.Consequent(np.arange(0,26, 1), 'minutes')
temprature.automf(3)
moisture.automf(3)
humidity.automf(3)
minutes['low'] = fuzz.trimf(minutes.universe, [0, 0, 13])
minutes['medium'] = fuzz.trimf(minutes.universe, [0, 13, 25])
minutes['high'] = fuzz.trimf(minutes.universe, [13, 25, 25])
rule1 = ctrl.Rule(temprature['poor'] | moisture['poor'] | humidity['poor'], minutes['low'])
rule2 = ctrl.Rule(temprature['average'] | moisture['average'] | humidity['average'], minutes['medium'])
rule3 = ctrl.Rule(temprature['good'] | moisture['good'] | humidity['good'], minutes['high'])
tap_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tap_control_system = ctrl.ControlSystemSimulation(tap_ctrl)
tap_control_system.input['temperature'] = 1
tap_control_system.input['moisture'] = 1
tap_control_system.input['humidity'] = 1
tap_control_system.compute()
print(tap_control_system.output['minutes'])
8.085852534617038