赞
踩
理解模糊逻辑推理的原理及特点,熟练应用模糊推理。
设计洗衣机洗涤时间的模糊控制。
已知人的操作经验为:
模糊控制规则如表1所示:
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 |
其中:
# 污泥隶属函数
sludge['SD'] = fuzz.trimf(sludge.universe, [0, 0, 50])
sludge['MD'] = fuzz.trimf(sludge.universe, [0, 50, 100])
sludge['LD'] = fuzz.trimf(sludge.universe, [50, 100, 100])
# 油脂隶属函数
grease['NG'] = fuzz.trimf(grease.universe, [0, 0, 50])
grease['MG'] = fuzz.trimf(grease.universe, [0, 50, 100])
grease['LG'] = fuzz.trimf(grease.universe, [50, 100, 100])
# 洗涤时间隶属度函数
washing_time['VS'] = fuzz.trimf(washing_time.universe, [0, 0, 30])
washing_time['S'] = fuzz.trimf(washing_time.universe, [0, 30, 60])
washing_time['M'] = fuzz.trimf(washing_time.universe, [30, 60, 90])
washing_time['L'] = fuzz.trimf(washing_time.universe, [60, 90, 120])
washing_time['VL'] = fuzz.trimf(washing_time.universe, [90, 120, 120])
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import matplotlib.pyplot as plt
# 创建输入变量和输出变量
sludge = ctrl.Antecedent(np.arange(0, 101, 1), 'sludge')
grease = ctrl.Antecedent(np.arange(0, 101, 1), 'grease')
washing_time = ctrl.Consequent(np.arange(0, 121, 1), 'washing_time')
# 定义隶属函数
sludge['SD'] = fuzz.trimf(sludge.universe, [0, 0, 50])
sludge['MD'] = fuzz.trimf(sludge.universe, [0, 50, 100])
sludge['LD'] = fuzz.trimf(sludge.universe, [50, 100, 100])
grease['NG'] = fuzz.trimf(grease.universe, [0, 0, 50])
grease['MG'] = fuzz.trimf(grease.universe, [0, 50, 100])
grease['LG'] = fuzz.trimf(grease.universe, [50, 100, 100])
washing_time['VS'] = fuzz.trimf(washing_time.universe, [0, 0, 30])
washing_time['S'] = fuzz.trimf(washing_time.universe, [0, 30, 60])
washing_time['M'] = fuzz.trimf(washing_time.universe, [30, 60, 90])
washing_time['L'] = fuzz.trimf(washing_time.universe, [60, 90, 120])
washing_time['VL'] = fuzz.trimf(washing_time.universe, [90, 120, 120])
# 定义模糊控制规则
rule1 = ctrl.Rule(sludge['SD'] & grease['NG'], washing_time['VS'])
rule2 = ctrl.Rule(sludge['SD'] & grease['MG'], washing_time['M'])
rule3 = ctrl.Rule(sludge['SD'] & grease['LG'], washing_time['L'])
rule4 = ctrl.Rule(sludge['MD'] & grease['NG'], washing_time['S'])
rule5 = ctrl.Rule(sludge['MD'] & grease['MG'], washing_time['M'])
rule6 = ctrl.Rule(sludge['MD'] & grease['LG'], washing_time['L'])
rule7 = ctrl.Rule(sludge['LD'] & grease['MG'], washing_time['M'])
rule8 = ctrl.Rule(sludge['LD'] & grease['LG'], washing_time['L'])
rule9 = ctrl.Rule(sludge['LD'] & grease['LG'], washing_time['VL'])
# 创建控制系统
washing_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9])
washing = ctrl.ControlSystemSimulation(washing_ctrl)
# 输入污泥和油脂值
washing.input['sludge'] = 70
washing.input['grease'] = 60
# 进行模糊推理
washing.compute()
# 获取洗涤时间的输出
washing_time.view(sim=washing)
# 打印输出洗涤时间
print("洗涤时间:", washing.output['washing_time'])
# 绘制隶属度函数
sludge.view()
grease.view()
washing_time.view()
plt.show()
注:SD(污泥少)、MD(污泥中)、LD(污泥多)、NG(油脂少)、MG(油脂中)、LG(油脂多)、VS(洗涤时间很短)、S(洗涤时间短)、M(洗涤时间中等)、L(洗涤时间长)、VL(洗涤时间很长)。
推论结果立体图
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]
# 洗涤时间范围为[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')
# 生成模糊隶属函数
#函数中的三元变量,第一个代表折线的起点,第二是最大值,第三是终点
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!='\n'):
print(item,end="")
else:
break
print('\t')
#画图
stain.view()
oil.view()
time.view()
#time.view()
plt.show()
#绘制3D图
upsampled=np.linspace(0,101,21)#步距参数
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))
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()
(2)假定当前传感器测得的信息为x0(污泥)=60,y0(油脂)=70,采用模糊决策,给出模糊推理结果,并观察模糊推理的动态仿真环境,给出其动态仿真环境图。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 导入NumPy库用于数学运算
import numpy as np
# 在Sludge函数和Grease函数中添加输入值检查
def Sludge(a):
sludge = [0, 0, 0] # 默认隶属度为0,依次对应SD, MD, LD
if a < 0:
a = 0
elif a > 100:
a = 100
if 0 <= a <= 50:
sludge[0] = (50 - a) / 50
sludge[1] = a / 50
elif 50 < a <= 100:
sludge[1] = (100 - a) / 50
sludge[2] = (a - 50) / 50
return sludge
def Grease(a):
grease = [0, 0, 0] # 默认隶属度为0,依次对应NG, MG, LG
if a < 0:
a = 0
elif a > 100:
a = 100
if 0 <= a <= 50:
grease[0] = (50 - a) / 50
grease[1] = a / 50
elif 50 < a <= 100:
grease[1] = (100 - a) / 50
grease[2] = (a - 50) / 50
return grease
# 3. 规则前提推理,并运算取最小值
def Rules(a, b): # a为污泥隶属度,b为油脂隶属度
rules_value = [0, 0, 0, 0, 0, 0, 0, 0, 0] # 依次对应9条规则结果VS, M, L, S, M, L, M, L, VL
if a[0] != 0 and b[0] != 0:
rules_value[0] = min(a[0], b[0])
if a[0] != 0 and b[1] != 0:
rules_value[1] = min(a[0], b[1])
if a[0] != 0 and b[2] != 0:
rules_value[2] = min(a[0], b[2])
if a[1] != 0 and b[0] != 0:
rules_value[3] = min(a[1], b[0])
if a[1] != 0 and b[1] != 0:
rules_value[4] = min(a[1], b[1])
if a[1] != 0 and b[2] != 0:
rules_value[5] = min(a[1], b[2])
if a[2] != 0 and b[0] != 0:
rules_value[6] = min(a[2], b[0])
if a[2] != 0 and b[1] != 0:
rules_value[7] = min(a[2], b[1])
if a[2] != 0 and b[2] != 0:
rules_value[8] = min(a[2], b[2])
return rules_value
# 创建一个时间步骤列表
time_steps = np.arange(0, 100, 1)
# 存储模糊输出值的列表
output_values = []
# 创建一个初始传感器输入
x0 = 60 # 初始污泥值
y0 = 70 # 初始油脂值
# 模拟模糊推理过程
for t in time_steps:
# 模拟传感器测得的信息,可以根据需要在此处引入噪声
x = x0
y = y0
# 计算污泥和油脂的隶属度
sludge_membership = Sludge(x)
grease_membership = Grease(y)
# 规则前提推理
rule_results = Rules(sludge_membership, grease_membership)
# 模糊系统总的输出,取各条规则推理结果的最小值
final_output = max(rule_results)
# 存储输出值
output_values.append(final_output)
# 更新传感器输入(这里示例中的输入简单地增加)
x0 += 1
y0 += 1
# 创建图形窗口
fig, ax = plt.subplots()
# 设置初始图形
line, = ax.plot([], [])
ax.set_xlim(0, len(time_steps))
ax.set_ylim(0, 1)
ax.set_xlabel("Time Step")
ax.set_ylabel("Fuzzy Output")
ax.set_title("Fuzzy Logic Control Simulation")
# 更新函数
def update(frame):
line.set_data(time_steps[:frame + 1], output_values[:frame + 1])
return line,
# 创建动画
ani = FuncAnimation(fig, update, frames=len(time_steps), blit=True, repeat=False)
plt.show()
(1) 模糊匹配
将x=60,y=70带入的隶属度函数中:
(2)规则触发。被触发的规则有4条:
Rule1:IF x is MD AND y is MG THEN z is M;
Rule2:IF x is MD AND y is LG THEN z is L;
Rule3:IF x is LD AND y is MG THEN z is L;
Rule4:IF x is LD AND y is LG THEN z is VL。
(3)规则前提推理。在同一条规则内,前提之间通过交运算得到规则结论。
(4)模糊系统总的输出(聚合),取各条规则推理结果的并。
(5)去模糊化。采用加权平均判决发的方法。根据洗涤时间隶属函数的反函数用相应的隶属度求出时间论域中对应的值。
# 1. 污泥隶属度计算
def Sludge(a):
sludge = [0, 0, 0] # 默认隶属度为0,依次对应SD,MD,LD
if a < 0 or a > 100:
return (print("输入值有误"))
elif 0 <= a <= 50:
sludge[0] = (50 - a) / 50
sludge[1] = a / 50
elif 50 < a <= 100:
sludge[1] = (100 - a) / 50 # 40/50
sludge[2] = (a - 50) / 50 # 10/50
return sludge
# 2. 油脂隶属度计算
def Grease(a):
grease = [0, 0, 0] # 默认隶属度为0,依次对应NG,MG,LG
if a < 0 or a > 100:
return (print("输入值有误"))
elif 0 <= a <= 50:
grease[0] = (50 - a) / 50
grease[1] = a / 50
elif 50 < a <= 100:
grease[1] = (100 - a) / 50 # 30/50
grease[2] = (a - 50) / 50 # 10/50
return grease
# 3. 规则前提推理,并运算取最小值
def Rules(a, b): # a为污泥隶属度,b为油脂隶属度
rules_value = [0, 0, 0, 0, 0, 0, 0, 0, 0] # 依次对应9条规则结果VS,M,L,S,M,L,M,L,VL
if a[0] != 0 and b[0] != 0:
rules_value[0] = min(a[0], b[0]) # 返回规则下最小值
if a[0] != 0 and b[1] != 0:
rules_value[1] = min(a[0], b[1])
if a[0] != 0 and b[2] != 0:
rules_value[2] = min(a[0], b[2])
if a[1] != 0 and b[0] != 0:
rules_value[3] = min(a[1], b[0])
if a[1] != 0 and b[1] != 0:
rules_value[4] = min(a[1], b[1])
if a[1] != 0 and b[2] != 0:
rules_value[5] = min(a[1], b[2])
if a[2] != 0 and b[0] != 0:
rules_value[6] = min(a[2], b[0])
if a[2] != 0 and b[1] != 0:
rules_value[7] = min(a[2], b[1])
if a[2] != 0 and b[2] != 0:
rules_value[8] = min(a[2], b[2])
return rules_value
# 4. 每条规则推理输出
def Inference(a): # a为9条规则下的结果隶属度
time_level = [0, 0, 0, 0, 0] # 默认时间隶属值为0,依次对应VS,S,M,L,VL
time_level[0] = a[0] # 0
time_level[1] = a[3] # 0
if (a[1] != 0 or a[4] != 0 or a[6] != 0): # 去零值然后取剩下的最小值
list_1 = [a[1], a[4], a[6]]
for i in range(len(list_1) - 1, -1, -1):
if list_1[i] == 0:
list_1.remove(0)
time_level[2] = min(list_1)
if (a[2] != 0 or a[5] != 0 or a[7] != 0):
list_2 = [a[2], a[5], a[7]]
for i in range(len(list_2) - 1, -1, -1):
if list_2[i] == 0:
list_2.remove(0)
time_level[3] = min(list_2)
time_level[4] = a[8]
return time_level
# 5. 由加权平均判决法可得,根据洗涤时间隶属函数的反函数用相应的隶属度求出时间论域中对应的值
def Max_membership(a): # a为时间隶属度
time = [0, 0, 0, 0, 0, 0, 0, 0] # 时间隶属函数八个区间分别对应的时间值
time[0] = 30 - 30 * a[0]
time[1] = 30 * a[1]
time[2] = 60 - 30 * a[1]
time[3] = 30 * a[2] + 30
time[4] = 90 - 30 * a[2]
time[5] = 30 * a[3] + 60
time[6] = 120 - 30 * a[3]
time[7] = 30 * a[4] + 90
sum_1 = time[0] * a[0] + time[1] * a[1] + time[2] * a[1] + time[3] * a[2] + time[4] * a[2] + time[5] * a[3] + time[
6] * a[3] + time[7] * a[4]
sum_2 = a[0] + 2 * a[1] + 2 * a[2] + 2 * a[3] + a[4]
result = sum_1 / sum_2
return result
# 洗涤时间隶属度函数
def WashTime(a): # a为时间值
if 0 <= a <= 30:
u1 = (30 - a) / 30
u2 = a / 30
if (u1 > u2):
time_level = 'VS'
else:
time_level = 'S'
if 30 < a <= 60:
u3 = (60 - a) / 30
u4 = (a - 30) / 30
if (u3 > u4):
time_level = 'S'
else:
time_level = 'M'
if 60 < a <= 90:
u5 = (90 - a) / 30
u6 = (a - 60) / 30
if (u5 > u6):
time_level = 'M'
else:
time_level = 'L'
if 90 < a <= 120:
u7 = (120 - a) / 30
u8 = (a - 90) / 30
if (u7 > u8):
time_level = 'L'
else:
time_level = 'VL'
return time_level
if __name__ == '__main__':
sludge = int(input("输入污泥指数:"))
grease = int(input("输入油脂指数:"))
rules_value = Rules(Sludge(sludge), Grease(grease))
time_level = Inference(rules_value) # 时间隶属度
time_pre = Max_membership(time_level) # 加权平均判决发求预测时间
time_sign = WashTime(time_pre) # 根据预测时间来计算洗涤时间符号
flag = {'VS': '很短', 'S': '短', 'M': '中等', 'L': '长', 'VL': '很长'}
print("模糊推理系统预测洗涤时间:{}{},预计洗涤时间:{}".format(time_sign,flag[time_sign], time_pre))
理解模糊逻辑原理:模糊逻辑是一种处理模糊信息和不确定性的有效方法。在这个实验中,你需要理解模糊逻辑的原理,包括模糊隶属度、模糊规则以及模糊推理的基本概念。
模糊隶属度函数的设计:在实验中,你需要设计污泥、油脂和洗涤时间的隶属度函数。这需要一定的创造力和理解,以确保隶属度函数可以正确地映射输入值到模糊集合。
模糊规则的构建:根据已知的人类操作经验,你需要构建模糊规则。这包括定义哪些条件下应该触发哪些规则,以及规则的前提和结论是如何关联的。
模糊推理系统的设计:你需要设计一个模糊推理系统,以根据输入的污泥和油脂指数来推断洗衣机的洗涤时间。这包括模糊匹配、规则触发、规则前提推理、模糊系统输出和去模糊化等步骤。
模糊推理结果的解释:在实验中,你将获得模糊推理的结果,包括洗涤时间的隶属度和具体时间值。了解如何解释和应用这些结果对于模糊控制非常重要。
动态仿真环境的观察:根据传感器测得的污泥和油脂指数,你需要观察模糊推理的动态仿真环境。这有助于理解模糊推理系统在不同输入条件下的表现。
模糊推理系统是一种基于模糊逻辑的计算机程序,用于处理模糊信息和进行模糊推理。与传统的布尔逻辑系统相比,模糊推理系统能够处理模糊的、不确定的或部分真实的信息。
模糊推理系统基于模糊逻辑,它引入了模糊集合和模糊关系的概念,以便更好地处理不确定性和模糊性。在模糊推理系统中,命题的真假不再是严格的二元取值,而是一个连续的范围,通常用一个介于0和1之间的实数表示。
传统的布尔逻辑只有两个取值,即真和假,而模糊逻辑可以处理更为复杂的情况。模糊逻辑允许命题的真假是一个连续的范围,这更符合真实世界中的不确定性和模糊性。
模糊逻辑与布尔逻辑的不同之处在于,模糊逻辑使用模糊集合来表示模糊概念和模糊关系,而布尔逻辑使用精确的集合和关系。
模糊推理系统由以下几个主要组成部分构成:
模糊规则库是模糊推理系统的核心组件。它包含一系列模糊规则,每个规则描述了输入变量和输出变量之间的关系。模糊规则通常采用"IF-THEN"的形式,其中IF部分是输入变量的条件,THEN部分是输出变量的结论。
模糊推理引擎是模糊推理系统的计算核心。它根据模糊规则库和输入数据进行推理,并生成模糊结果。推理引擎通常包括模糊化、规则匹配、推理和去模糊化等步骤。
在模糊推理系统中,模糊化将现实世界的模糊输入转换为模糊集合。模糊化过程使用隶属函数来描述变量的隶属度,表示变量对某个模糊集合的归属程度。
去模糊化是将模糊结果转换为精确的输出。去模糊化通过使用去模糊函数来确定最终结果的精确值。
模糊推理系统在许多领域中有广泛的应用,特别是在以下领域:
模糊控制系统使用模糊推理系统来处理模糊输入和模糊输出,实现对模糊系统的控制。它在工程领域中广泛应用于控制系统设计,特别是在模糊控制器中。
模糊决策系统使用模糊推理系统来处理模糊输入和模糊输出,帮助进行管理和决策支持。它可以不确定性和模糊性为基础,帮助决策者进行决策分析和决策制定。
模糊识别系统使用模糊推理系统来处理模糊输入和模糊输出,用于模式识别和数据挖掘。它可以应用于图像识别、语音识别、模式分类等领域。
模糊推理系统具有以下优点:
然而,模糊推理系统也存在一些局限性和挑战:
模糊推理系统的研究和应用正不断发展和演进。未来的发展趋势包括但不限于:
模糊推理系统作为一种处理模糊信息的有效工具,在各个领域中发挥着重要作用,持续不断地推动着科学研究和技术进步。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。