人工智能作业——python实现洗衣机模糊推理系统实验

人工智能作业——python实现洗衣机模糊推理系统实验

    • 实验环境
    • 实验要求
    • 代码
    • 实验结果

书中实验要求用Matlab实现,但是Matlab包太大了,且还需要重新学习Matlab的使用。发现python也可以实现,故使用python完成。

实验环境

python3.6
开发环境Pycharm
需要安装的包:scikit-fuzzy,numpy
cmd命令:pip install scikit-fuzzy
	     pip install numpy 

实验要求

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

输入是污泥和油脂,论域:[0, 100]
输出是洗衣机的洗涤时间,论域:[0, 120] 

在这里插入图片描述

代码

import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
from skfuzzy import control as ctrl


#  污泥和油脂范围为[0,100]
#  洗涤时间范围为[0,120]
x_stain = np.arange(0, 110, 10)
x_oil = np.arange(0, 110, 10)
x_time = np.arange(0, 130, 10)
# 定义模糊控制变量
stain = ctrl.Antecedent(x_stain, 'stain')
oil = ctrl.Antecedent(x_oil, 'oil')
time = ctrl.Consequent(x_time, 'time')
# 生成模糊隶属函数
#函数中的三元变量,第一个代表折线的起点,第二是最大值,第三是终点
stain['SD'] = fuzz.trimf(x_stain, [0, 0, 50])  #定义污渍的三角隶属度函数横坐标
stain['MD'] = fuzz.trimf(x_stain, [0, 50, 100])
stain['LD'] = fuzz.trimf(x_stain, [50, 100, 100])
oil['NG'] = fuzz.trimf(x_oil, [0, 0, 50]) #定义油污的三角隶属度函数横坐标
oil['MG'] = fuzz.trimf(x_oil, [0, 50, 100])
oil['LG'] = fuzz.trimf(x_oil, [50, 100, 100])
time['VS'] = fuzz.trimf(x_time, [0, 0, 20]) #定义洗涤时间的三角隶属度函数横坐标
time['S'] = fuzz.trimf(x_time, [0, 20, 50])
time['M'] = fuzz.trimf(x_time, [20, 50, 80])
time['L'] = fuzz.trimf(x_time, [50, 80, 120])
time['VL'] = fuzz.trimf(x_time, [80, 120, 120])

#采用解模糊方法——质心解模糊方式
time.defuzzify_method='centroid'

#规则
rule1=ctrl.Rule(antecedent=((stain['SD'] & oil['NG'])),consequent=time['VS'],label='time=VS')
rule2=ctrl.Rule(antecedent=((stain['SD'] & oil['MG'])|(stain['MD'] & oil['MG'])|(stain['LD'] & oil['NG'])),consequent=time['M'],label='time=M')
rule3=ctrl.Rule(antecedent=((stain['SD'] & oil['LG'])|(stain['MD'] & oil['LG'])|(stain['LD'] & oil['MG'])),consequent=time['L'],label='time=L')
rule4=ctrl.Rule(antecedent=((stain['MD'] & oil['NG'])),consequent=time['S'],label='time=S')
rule5=ctrl.Rule(antecedent=((stain['LD'] & oil['LG'])),consequent=time['VL'],label='time=VL')

# 系统和运行环境初始化
rule=[rule1, rule2, rule3,rule4,rule5]
time_ctrl = ctrl.ControlSystem(rule)
wash_time = ctrl.ControlSystemSimulation(time_ctrl)
#规则中带一些奇怪的规则,处理后输出
for i in range(len(rule)):
    print("rule",i,end=":")
    for item in str(rule[i]):
        if(item!='
'):
            print(item,end="")
        else:
            break
    print('	')
#输入输出
print("
请输入污渍和油污的值")
p=input("污渍的值:")
q=input("油污的值:")
wash_time.input['stain'] = int(p)
wash_time.input['oil'] = int(q)
wash_time.compute()
print ("洗涤时间为:",wash_time.output['time'])

#画图
stain.view()
oil.view()
time.view()
#time.view()
plt.show()

实验结果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
参考:
链接: https://blog.csdn.net/dcyywin8/article/details/103460871.
官方库的介绍: https://pythonhosted.org/scikit-fuzzy/auto_examples/index.html.

你可能感兴趣的:(面试,学习路线,阿里巴巴,python,人工智能,numpy,java,运维)