一、实验目的

理解模糊逻辑推理的原理及特点,熟练应用模糊推理

二、实验内容

用python设计洗衣机洗涤时间的模糊控制

三、实验要求

已知人的操作经验是

污泥越多,油脂越多,洗涤时间越长

污泥适中,油脂适中,洗涤时间适中

污泥越少,油脂越少,洗涤时间越短

洗衣机的模糊控制规则表
x y z
SD NG VS
SD MG M
SD LG L
MD NG S
MD MG M
MD LG L
LD NG M
LD MG L
LD LG VL

其中SD(污泥少)、MD(污泥中)、LD(污泥多)、NG油脂少、MG油脂中、LG油脂多、VS洗涤时间很短、S洗涤时间短、M洗涤时间中等、L洗涤时间长、VL洗涤时间很长

(1)假设污泥、油脂、洗涤时间的论域分别为[0,100] [0,100] [0,120],设计相应的模糊推理系统,给出输入、输出语言变量的隶属函数图,模糊控制规则表和推论结果立体图。

(2)假定当前传感器测得的信息为x0(污泥)=60,y0(油脂)=70,采用模糊决策,给出模糊推理结果,并观察模糊推理的动态仿真环境,给出其动态仿真环境图。

第一小题,代码如下


#需要先安装pip install scikit-fuzzy
#released in 2021.2
"""
==========================================
Fuzzy Control Systems: The washtimeping Problem
==========================================
The 'washtimeping problem' is commonly used to illustrate the power of fuzzy logic
principles to generate complex behavior from a compact, intuitive set of
expert rules.
If you're new to the world of fuzzy control systems, you might want
to check out the `Fuzzy Control Primer
<../userguide/fuzzy_control_primer.html>`_
before reading through this worked example.
The washtimeping Problem
-------------------
Let's create a fuzzy control system which models how you might choose to washtime
at a restaurant.  When washtimeping, you consider the oil and stain,
rated between 0 and 10.  You use this to leave a washtime of between 0 and 25%.
We would formulate this problem as:
* Antecedents (Inputs)
   - `oil`
      * How  was the oil on a scale of 0 to 100?
      * Fuzzy set (ie, fuzzy value range): poor (SD), acceptable(MD), amazing (LD)
   - `stain`
      * Universe: stain on a scale of 0 to 100?
      * Fuzzy set: bad, decent, great
* Consequents (Outputs)
   - `washtime`
      * Universe: How much should we washtime, on a scale of 0 to 120
      * Fuzzy set: low, medium, high
* Rules
   - refer to P302
* Usage
   - If I tell this controller that I rated:
      * the oil as 10, and
      * the stain as 10,
   - it would recommend :
      * a 29 washtime.
Creating the washtimeping Controller Using the skfuzzy control API
-------------------------------------------------------------
We can use the `skfuzzy` control system API to model this.  First, let's
define fuzzy variables
"""
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import matplotlib.pyplot as plt
# New Antecedent/Consequent objects hold universe variables and membership
# functions
stain = ctrl.Antecedent(np.arange(0, 101, 1), 'stain')
oil = ctrl.Antecedent(np.arange(0, 101, 1), 'oil')
washtime = ctrl.Consequent(np.arange(0, 120, 1), 'washtime')
# Auto-membership function population is possible with .automf(3, 5, or 7)
stain.automf(3, variable_type='quant')
oil.automf(3, variable_type='quant')
# Custom membership functions can be built interactively with a familiar,
# Pythonic API
washtime['VS'] = fuzz.trimf(washtime.universe, [0, 0, 20])
washtime['S'] = fuzz.trimf(washtime.universe, [0, 20, 50])
washtime['M'] = fuzz.trimf(washtime.universe, [20, 50, 70])
washtime['L'] = fuzz.trimf(washtime.universe, [50, 70, 100])
washtime['VL'] = fuzz.trimf(washtime.universe, [70, 100, 120])
"""
To help understand what the membership looks like, use the ``view`` methods.
These return the matplotlib `Figure` and `Axis` objects. They are persistent
as written in Jupyter notebooks; other environments may require a `plt.show()`
command after each `.view()`.
"""
# You can see how these look with .view()
stain['average'].view()
plt.show()
oil.view()
plt.show()
washtime.view()
plt.show()
"""
.. image:: PLOT2RST.current_figure
Fuzzy rules
-----------
Now, to make these triangles useful, we define the *fuzzy relationship*
between input and output variables.
"""
# low = SD or NG;average = MD or MG;high=LD or LG
rule1 = ctrl.Rule(stain['low'] & oil['low'], washtime['VS'])
rule2 = ctrl.Rule(stain['low'] & oil['average'], washtime['M'])
rule3 = ctrl.Rule(stain['low'] & oil['high'], washtime['L'])
rule4 = ctrl.Rule(stain['average'] & oil['low'], washtime['S'])
rule5 = ctrl.Rule(stain['average'] & oil['average'], washtime['M'])
rule6 = ctrl.Rule(stain['average'] & oil['high'], washtime['L'])
rule7 = ctrl.Rule(stain['high'] & oil['low'], washtime['M'])
rule8 = ctrl.Rule(stain['high'] & oil['average'], washtime['L'])
rule9 = ctrl.Rule(stain['high'] & oil['high'], washtime['VL'])
"""
.. image:: PLOT2RST.current_figure
Control System Creation and Simulation
---------------------------------------
Now that we have our rules defined, we can simply create a control system
via:
"""
washtimeping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9])
"""
In order to simulate this control system, we will create a
``ControlSystemSimulation``.  Think of this object representing our controller
applied to a specific set of circumstances.  For washtimeping, this might be washtimeping
Sharon at the local brew-pub.  We would create another
``ControlSystemSimulation`` when we're trying to apply our ``washtimeping_ctrl``
for Travis at the cafe because the inputs would be different.
"""
washtimeping = ctrl.ControlSystemSimulation(washtimeping_ctrl)
"""
We can now simulate our control system by simply specifying the inputs
and calling the ``compute`` method.  
"""
# Pass inputs to the ControlSystem using Antecedent labels with Pythonic API
# Note: if you like passing many inputs all at once, use .inputs(dict_of_data)
washtimeping.input['stain'] = 2
washtimeping.input['oil'] = 2
# Crunch the numbers
washtimeping.compute()
"""
Once computed, we can view the result as well as visualize it.
"""
print(washtimeping.output['washtime'])
washtime.view(sim=washtimeping)
plt.show()

第二小题代码在第一小题基础上进行稍微改变

人工智能洗衣机模糊推理系统实验(课本实验)

改为       washtimeping.input['stain'] = 60      就可以了

              washtimeping.input['oil'] = 70

运行结果如下:

人工智能洗衣机模糊推理系统实验(课本实验)

人工智能洗衣机模糊推理系统实验(课本实验)

好了,就这些,大家如果觉得有帮助的话就太好了,我做实验的时候就没有 找到这些。