- 使用python設(shè)計(jì)并實(shí)現(xiàn)一個(gè)洗衣機(jī)模糊推理系統(tǒng)實(shí)驗(yàn)。
已知人的操作經(jīng)驗(yàn)是:
污泥越多,油脂越多,洗滌時(shí)間越長
污泥適中,油脂適中,洗滌時(shí)間適中
污泥越少,油脂越少,洗滌時(shí)間越短
洗衣機(jī)的模糊控制規(guī)則如表1所示:
表1 洗衣機(jī)的模糊控制規(guī)則表
污泥油脂 | NG(無油脂) | MG(中等油脂) | LG(油脂多) |
---|---|---|---|
SD(污泥少) | VS | M | L |
MD(中等污泥) | S | M | L |
LD(污泥多) | M | L | VL |
其中SD(污泥少)、MD(污泥中)、LD(污泥多)、NG油脂少、MG油脂中、LG油脂多、VS洗滌時(shí)間很短、S洗滌時(shí)間短、M洗滌時(shí)間中等、L洗滌時(shí)間長、VL洗滌時(shí)間很長。
(1)假設(shè)污泥、油脂、洗滌時(shí)間的論域分別為[0,100] [0,100] [0,120],設(shè)計(jì)相應(yīng)的模糊推理系統(tǒng),給出輸入、輸出語言變量的隸屬函數(shù)圖,模糊控制規(guī)則表和推論結(jié)果立體圖。
(2)假定當(dāng)前傳感器測得的信息為污泥=60,油脂=70,采用模糊決策,給出模糊推理結(jié)果,并觀察模糊推理的動(dòng)態(tài)仿真環(huán)境,給出其動(dòng)態(tài)仿真環(huán)境圖。
import numpy as np
import skfuzzy as fuzz
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from skfuzzy import control as ctrl
#定義輸入輸出的取值范圍
# 污泥和油脂范圍為[0,100]
# 洗滌時(shí)間范圍為[0,120]
x_stain = np.arange(0, 101, 1)
x_oil = np.arange(0, 101, 1)
x_time = np.arange(0, 121, 1)
# 定義模糊控制變量
stain = ctrl.Antecedent(x_stain, 'stain')
oil = ctrl.Antecedent(x_oil, 'oil')
time = ctrl.Consequent(x_time, 'time')
# 生成模糊隸屬函數(shù)
#函數(shù)中的三元變量,第一個(gè)代表折線的起點(diǎn),第二是最大值,第三是終點(diǎn)
stain['SD'] = fuzz.trimf(x_stain, [0, 0, 50]) #定義污漬的三角隸屬度函數(shù)橫坐標(biāo)
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]) #定義油污的三角隸屬度函數(shù)橫坐標(biāo)
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]) #定義洗滌時(shí)間的三角隸屬度函數(shù)橫坐標(biāo)
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])
#采用解模糊方法——質(zhì)心解模糊方式
time.defuzzify_method='centroid'
#規(guī)則
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')
# 系統(tǒng)和運(yùn)行環(huán)境初始化
rule=[rule1, rule2, rule3,rule4,rule5]
time_ctrl = ctrl.ControlSystem(rule)
wash_time = ctrl.ControlSystemSimulation(time_ctrl)
#規(guī)則中帶一些奇怪的規(guī)則,處理后輸出
for i in range(len(rule)):
print("rule",i,end=":")
for item in str(rule[i]):
if(item!='\n'):
print(item,end="")
else:
break
print('\t')
#畫圖
stain.view()
oil.view()
time.view()
#time.view()
plt.show()
rule 0:IF stain[SD] AND oil[NG] THEN time[VS]
rule 1:IF ((stain[SD] AND oil[MG]) OR (stain[MD] AND oil[MG])) OR (stain[LD] AND oil[NG]) THEN time[M]
rule 2:IF ((stain[SD] AND oil[LG]) OR (stain[MD] AND oil[LG])) OR (stain[LD] AND oil[MG]) THEN time[L]
rule 3:IF stain[MD] AND oil[NG] THEN time[S]
rule 4:IF stain[LD] AND oil[LG] THEN time[VL]
文章來源:http://www.zghlxwxcb.cn/news/detail-734149.html
#繪制3D圖
upsampled=np.linspace(0,101,21)#步距參數(shù)
x,y=np.meshgrid(upsampled,upsampled)
z=np.zeros_like(x)
pp=[]
for i in range(0,21):
for j in range(0,21):
wash_time.input['stain']=x[i,j]
wash_time.input['oil']=y[i,j]
wash_time.compute()
z[i,j]=wash_time.output['time']
pp.append(z[i,j])
print('max:',max(pp))
print('min:',min(pp))
max: 106.66666666666666
min: 6.666666666666667
文章來源地址http://www.zghlxwxcb.cn/news/detail-734149.html
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure(figsize=(8,8))#畫布大小
ax=fig.add_subplot(111,projection='3d')
surf=ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap='viridis',linewidth=0.1,antialiased=True)
ax.view_init(30,250)#觀察角度
plt.title('3D results')
ax.set_xlabel('stain')
ax.set_ylabel('oil')
ax.set_zlabel('time')
plt.show()
#輸入輸出
p=60#污漬的值
q=70#油污的值
wash_time.input['stain'] = int(p)
wash_time.input['oil'] = int(q)
wash_time.compute()
print ("洗滌時(shí)間為:",wash_time.output['time'])
洗滌時(shí)間為: 67.21682847896444
到了這里,關(guān)于python實(shí)現(xiàn)模糊洗衣機(jī)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!