赞
踩
实验要求:
已知有模糊规则:“如果污泥多,油脂多,则洗涤时间长”。
设:
污泥论域:{少, 中等, 多};“污泥多”=0/少+0.5/中等+1/多;
油脂论域:{无, 中等, 多} ; “油脂多”=0/少+0.5/中等+1/多;
洗涤时间论域: {很短, 短, 长, 很长} ;
“洗涤时间长”=0/很短+0.3/短+0.6/长+1.0/很长
内容:输入是被洗衣物的污泥和油脂的模糊量,计算洗涤时间。
例如已知事实:“污泥多”=0.3/少+0.6/中等+0.8/多;
“油脂多”=0.8/少+0.3/中等+0.2/多。
要求:
1.打印模糊关系R(污泥到洗涤时间R1,油脂到洗涤时间R2,两者
到洗涤时间关系R);
2.打印模糊向量;
3.计算洗涤时间。
话不多说先上代码
import numpy as np # 污泥:[0,0.5,1] 油脂:[0,0.5,1] 洗涤时间:[0,0.3,0.6,1] # 输入 def input_sludge(): print("污泥论域:[少,中等,多],请输入论域的模糊量:(用空格分割开)") sludge=[] sludge=input() sludge=sludge.split(' ') return sludge def input_grease(): print("油污论域:[无,中等,多],请输入论域的模糊量:(用空格分割开)") grease=[] grease=input() grease=grease.split(' ') return grease # 模糊合成 def Fuzzy_synthesis1(sDomain,gDomain): data=np.zeros(shape=(len(sDomain),len(gDomain)),dtype=float) for i in range(len(sDomain)): for j in range(len(gDomain)): data[i][j]=max(sDomain[i],gDomain[j]) return data def Fuzzy_synthesis2(R1,R2T): data = np.zeros(shape=(len(R1), len(R2T[0])), dtype=float) data1=0.0 for i in range(len(R1)): for j in range(len(R2T[0])): for k in range (len(R2T)): data2=min(R1[i][k],R2T[k][i]) if(data1<data2): data1=data2 data[i][j]=data1 return data # 模糊关系 def Fuzzy_relation(blendDomain,washTimeDomain): blendDomain = [y for x in blendDomain for y in x] data=np.zeros(shape=(len(blendDomain),len(washTimeDomain)),dtype=float) for i in range(len(blendDomain)): for j in range(len(washTimeDomain)): data[i][j]=min(blendDomain[i],washTimeDomain[j]) return data # 打印模糊向量 def Fuzzy_vector(vector): print(vector) # 模糊推理 def Fuzzy_inference(blend,relation): blend= [y for x in blend for y in x] data = [-1 for _ in range(len(relation[0]))] for j in range(len(relation[0])): for i in range(len(blend)): data[j] = max(data[j], min(blend[i], relation[i][j])) return data # 最大隶属度 def Fuzzy_decision(inter_result): max = 0 for i in range(len(result)): if (result[max] < result[i]): max = i return max + 1 def Fuzzy_result(grade): if(grade==1): print("很短") elif(grade==2): print("短") elif(grade==3): print("长") else: print("很长") # 论域 sludgeDomain=[0,0.5,1] greaseDomain=[0,0.5,1] washTimeDomain=[0,0.3,0.6,1] # 模糊合成 blendDomain=Fuzzy_synthesis1(sludgeDomain,greaseDomain) print("污泥和油脂混合后的模糊结果:") print(blendDomain) # 打印模糊向量 print("污泥的模糊向量") Fuzzy_vector(sludgeDomain) print("油脂的模糊向量") Fuzzy_vector(sludgeDomain) print("污泥油脂混合的模糊向量") Fuzzy_vector(sludgeDomain) print("--------------------------") # 输出模糊关系 print("模糊关系如下:") fuzzyrelation=Fuzzy_relation(blendDomain,washTimeDomain) print(fuzzyrelation) # print(len(fuzzyrelation[0])) print("\n--------------------------") # 测试输出数据 # sludge=[0.3,0.6,0.8] # grease=[0.8,0.3,0.2] sludge=input_sludge() grease=input_grease() # print(sludge) # print(grease) blendsg=Fuzzy_synthesis1(sludge,grease); result=Fuzzy_inference(blendsg,fuzzyrelation) print(result) grade=Fuzzy_decision(result) # 最大隶属度 print("最大隶属度为:") print(grade) print("在该污泥程度以及油脂程度下,洗衣时间:") Fuzzy_result(grade) # 第二中,按题目要求,感觉不太现实 print("methon 2") print("泥污与时间的混合关系一R1") R1=Fuzzy_synthesis1(sludgeDomain,washTimeDomain) print(R1) print("\n") print("油脂与时间的混合关系二R2") R2=Fuzzy_synthesis1(greaseDomain,washTimeDomain) print(R2) print("\n") print("R1与R2模糊关系合成") R2T = [[R2[j][i] for j in range(len(R2))] for i in range(len(R2[0]))] print(R2T) # print(len(R1)) # print(len(R2T[0])) print("模糊合成结果") R=Fuzzy_synthesis2(R1,R2T) print(R) print("\n") print("模糊关系如下:") fuzzyrelation2=Fuzzy_relation(R,washTimeDomain) print(fuzzyrelation2) print("input:") sludge1=input_sludge() grease1=input_grease() blendsg1=Fuzzy_synthesis1(sludge1,grease1); result1=Fuzzy_inference(blendsg1,fuzzyrelation) print(result) grade1=Fuzzy_decision(result) # 最大隶属度 print("最大隶属度为:") print(grade1) print("在该污泥程度以及油脂程度下,洗衣时间:") Fuzzy_result(grade1)
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。