import numpy as np
import skfuzzy as fuzz
import math
import sys
根据两个输入变量:距离和角度,我想推断速度下降的百分比。
我从生成宇宙变量开始:
Distance = np.arange(0, 4, 0.5)
Angle = np.arange(-6, 7, 1)
Vitesse = np.arange(0, 110, 10)
然后生成隶属函数:
#Distance
v_near = fuzz.trimf(Distance, [0, 0.5,1])
near = fuzz.trimf(Distance, [0.5, 1, 1.5])
med = fuzz.trimf(Distance, [1, 1.5, 2])
far = fuzz.trimf(Distance, [1.5, 2, 2.5])
v_far = fuzz.trimf(Distance, [2, 2.5, 3])
#Angle
LN = fuzz.trimf(Angle, [-6, -6,-3])
MN = fuzz.trimf(Angle, [-6, -3, 0])
ZA = fuzz.trimf(Angle, [-3, 0, 3])
MP = fuzz.trimf(Angle, [0, 3, 6])
LP = fuzz.trimf(Angle, [3, 6, 6])
#Vitesse
V_0 = fuzz.trimf(Vitesse, [0, 0,20])
V_20 = fuzz.trimf(Vitesse, [0, 20, 40])
V_40 = fuzz.trimf(Vitesse, [20, 40, 60])
V_60 = fuzz.trimf(Vitesse, [40, 60, 80])
V_80 = fuzz.trimf(Vitesse, [60, 80, 100])
V_100 = fuzz.trimf(Vitesse, [80, 100, 100])
我在这里生成了Classification函数,将输入值分类到相应的类中:
def distance_classification(A):
Dist_vnear= fuzz.interp_membership(Distance, v_near, A)
Dist_near= fuzz.interp_membership(Distance, near, A)
Dist_med= fuzz.interp_membership(Distance, med, A)
Dist_far= fuzz.interp_membership(Distance, far, A)
Dist_vfar= fuzz.interp_membership(Distance, v_far, A)
return dict(verynear= round(Dist_vnear*100), near=round(Dist_near*100), medium=round(Dist_med*100), far=round(Dist_far*100),veryfar= round(Dist_vfar*100))`
def angle_classification(D):
ln= fuzz.interp_membership(Angle, LN, D)
mn= fuzz.interp_membership(Angle, MN, D)
za= fuzz.interp_membership(Angle, ZA, D)
mp= fuzz.interp_membership(Angle, MP, D)
lp= fuzz.interp_membership(Angle, LP, D)
return dict(LNEG= round(ln*100), MNEG=round(mn*100), ZERO=round(za*100), MPOS=round(mp*100), LPOS=round(lp*100))
我现在定义包含21条规则的规则库:
def rules (D_class, A_class ):
rule1=D_class['verynear']
rule2=np.fmin(D_class['near'], A_class['LNEG'])
rule3=np.fmin(D_class['near'], A_class['MNEG'])
rule4=np.fmin(D_class['near'], A_class['ZERO'])
rule5=np.fmin(D_class['near'], A_class['MPOS'])
rule6=np.fmin(D_class['near'], A_class['LPOS'])
rule7=np.fmin(D_class['medium'], A_class['LNEG'])
rule8=np.fmin(D_class['medium'], A_class['MNEG'])
rule9=np.fmin(D_class['medium'], A_class['ZERO'])
rule10=np.fmin(D_class['medium'], A_class['MPOS'])
rule11=np.fmin(D_class['medium'], A_class['LPOS'])
rule12=np.fmin(D_class['far'], A_class['LNEG'])
rule13=np.fmin(D_class['far'], A_class['MNEG'])
rule14=np.fmin(D_class['far'], A_class['ZERO'])
rule15=np.fmin(D_class['far'], A_class['MPOS'])
rule16=np.fmin(D_class['far'], A_class['LPOS'])
rule17=np.fmin(D_class['veryfar'], A_class['LNEG'])
rule18=np.fmin(D_class['veryfar'], A_class['MNEG'])
rule19=np.fmin(D_class['veryfar'], A_class['ZERO'])
rule20=np.fmin(D_class['veryfar'], A_class['MPOS'])
rule21=np.fmin(D_class['veryfar'], A_class['LPOS'])
#Rules activation
imp1=np.fmin(rule1,V_100)
imp2=np.fmin(rule2,V_80)
imp3=np.fmin(rule3,V_80)
imp4=np.fmin(rule4,V_100)
imp5=np.fmin(rule5,V_80)
imp6=np.fmin(rule6,V_80)
imp7=np.fmin(rule7,V_40)
imp8=np.fmin(rule8,V_60)
imp9=np.fmin(rule9,V_80)
imp10=np.fmin(rule10,V_60)
imp11=np.fmin(rule11,V_40)
imp12=np.fmin(rule12,V_20)
imp13=np.fmin(rule13,V_40)
imp14=np.fmin(rule14,V_60)
imp15=np.fmin(rule15,V_40)
imp16=np.fmin(rule16,V_20)
imp17=np.fmin(rule17,V_0)
imp18=np.fmin(rule18,V_20)
imp19=np.fmin(rule19,V_40)
imp20=np.fmin(rule20,V_20)
imp21=np.fmin(rule21,V_0)
对于规则聚合,现在我只使用了3条规则:
aggregated=np.fmax(imp1,np.fmax(imp2,imp3))
最后一步是去模糊化:
defuzz_centroid = fuzz.defuzz(Vitesse, aggregated, 'centroid')
print defuzz_centroid
我的模糊控制器包含两个输入,一个输出和21条规则,我在这个例子中只使用了3条规则。如何使用SCIKIT Toolkit聚合所有其他规则?
函数np.max()只接受2个参数进行比较,但是您可以在函数中使用相同的函数。
np.max(x, y)
要获得所有规则激活的最大值,您应该尝试这样做:
np.max(imp1, np.max(imp2, np.max(imp3, ...., np.max(imp20, imp21))
你还需要小心右括号,因为有很多,它通常是编译错误的来源