Python模糊控制库使用
程序员文章站
2022-07-03 17:59:11
Python模糊控制库使用模糊控制库安装示例定义模糊控制变量模糊隶属函数模糊规则激活模糊控制器完整代码模糊控制库安装模糊控制库github:链接: Github.python安装:pip install scikit-fuzzy示例例子:输入为服务(service)和质量(quality)两个参数,输出为得到的小费(tip)定义模糊控制变量1.定义模糊控制变量,质量和服务的取值范围为[0,10],小费范围为[0,25]x_qual = np.arange(0, 11, 1)x_serv =...
模糊控制库安装
模糊控制库github:链接: Github.
python安装:pip install scikit-fuzzy
示例
1.选择输入输出模糊集
2.定义输入输出隶属度函数(不同的隶属度函数,会导致不同的控制特性)
3.建立模糊控制表
4.建立模糊控制规则
5.模糊推理
6.反模糊化
例子:输入为服务(service)和质量(quality)两个参数,输出为得到的小费(tip)
定义模糊控制变量
1.定义模糊控制变量,质量和服务的取值范围为[0,10],小费范围为[0,25]
x_qual = np.arange(0, 11, 1)
x_serv = np.arange(0, 11, 1)
x_tip = np.arange(0, 26, 1)
quality = ctrl.Antecedent(x_qual, 'quality')
service = ctrl.Antecedent(x_serv, 'service')
tip = ctrl.Consequent(x_tip, 'tip')
模糊隶属函数
2.生成模糊隶属函数,这里是三角形隶属度函数
#定义质量差时的三角隶属度函数横坐标
quality['L'] = fuzz.trimf(x_qual, [0, 0, 5])
quality['M'] = fuzz.trimf(x_qual, [0, 5, 10])
quality['H'] = fuzz.trimf(x_qual, [5, 10, 10])
#定义服务差时的三角隶属度函数横坐标
service['L'] = fuzz.trimf(x_serv, [0, 0, 5])
service['M'] = fuzz.trimf(x_serv, [0, 5, 10])
service['H'] = fuzz.trimf(x_serv, [5, 10, 10])
#定义小费的三角隶属度函数横坐标
tip['L'] = fuzz.trimf(x_tip, [0, 0, 13])
tip['M'] = fuzz.trimf(x_tip, [0, 13, 25])
tip['H'] = fuzz.trimf(x_tip, [13, 25, 25])
#解模糊方法采用质心法
tip.defuzzify_method='centroid'
#可视化这些输入输出和隶属函数
# quality.automf(3) #自动分配几种程度NS ZO PS
#service.automf(3)
#quality.view()
# service.view()
# plt.show()
模糊规则
3.制定模糊规则,即服务,质量和小费的关系
rule1=ctrl.Rule(antecedent=((quality['L'] & service['L'])|(quality['L'] & service['M'])|(quality['M'] & service['L'])),consequent=tip['L'],label='Low')
rule2=ctrl.Rule(antecedent=((quality['M']&service['M'])|(quality['L']&service['H'])|(quality['H']&service['L'])),consequent=tip['M'],label='Medium')
rule3=ctrl.Rule(antecedent=((quality['M']&service['H'])|(quality['H']&service['M'])|(quality['H']&service['H'])),consequent=tip['H'],label='High')
#rule2.view() #这个图我没看懂,有哪位大哥看懂了可以告知一下
激活模糊控制器
4.激活模糊控制器,输入质量值6.5,服务值为9.8时,输出小费的值,并绘制结果
tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
tipping.input['quality'] = 6.5
tipping.input['service'] = 9.8
tipping.compute()
print (tipping.output['tip'])
tip.view(sim=tipping)
plt.show()
完整代码
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
from skfuzzy import control as ctrl
import math
# 质量和服务范围为[0,10]
# 小费范围为[0,25]
x_qual = np.arange(0, 11, 1)
x_serv = np.arange(0, 11, 1)
x_tip = np.arange(0, 26, 1)
# 定义模糊控制变量
quality = ctrl.Antecedent(x_qual, 'quality')
service = ctrl.Antecedent(x_serv, 'service')
tip = ctrl.Consequent(x_tip, 'tip')
# 生成模糊隶属函数
quality['L'] = fuzz.trimf(x_qual, [0, 0, 5]) #定义质量差时的三角隶属度函数横坐标
quality['M'] = fuzz.trimf(x_qual, [0, 5, 10])
quality['H'] = fuzz.trimf(x_qual, [5, 10, 10])
service['L'] = fuzz.trimf(x_serv, [0, 0, 5]) #定义服务差时的三角隶属度函数横坐标
service['M'] = fuzz.trimf(x_serv, [0, 5, 10])
service['H'] = fuzz.trimf(x_serv, [5, 10, 10])
tip['L'] = fuzz.trimf(x_tip, [0, 0, 13]) #定义小费的三角隶属度函数横坐标
tip['M'] = fuzz.trimf(x_tip, [0, 13, 25])
tip['H'] = fuzz.trimf(x_tip, [13, 25, 25])
tip.defuzzify_method='centroid'
#可视化这些输入输出和隶属函数
# quality.automf(3)
#service.automf(3)#三种程度
#quality.view()
# service.view()
# plt.show()
#规则
rule1=ctrl.Rule(antecedent=((quality['L'] & service['L'])|(quality['L'] & service['M'])|(quality['M'] & service['L'])),consequent=tip['L'],label='Low')
rule2=ctrl.Rule(antecedent=((quality['M']&service['M'])|(quality['L']&service['H'])|(quality['H']&service['L'])),consequent=tip['M'],label='Medium')
rule3=ctrl.Rule(antecedent=((quality['M']&service['H'])|(quality['H']&service['M'])|(quality['H']&service['H'])),consequent=tip['H'],label='High')
rule2.view()
tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
tipping.input['quality'] = 6.5
tipping.input['service'] = 9.8
tipping.compute()
print (tipping.output['tip'])
tip.view(sim=tipping)
plt.show()
附上模糊控制库网站,有更详细说明和例子:
链接: 模糊控制网站.
本文地址:https://blog.csdn.net/qq_39330520/article/details/107298819
上一篇: 梯度下降预测房价