找到你要的答案

Q:How to aggregate more than three rules with scikit?

Q:如何scikit合计超过三的规则吗?

import numpy as np
import skfuzzy as fuzz
import math
import sys

According to the two input variables: Distance and Angle, I want to infer a percentage for the velocity decreasing.

I begin with generating my universe variables:

Distance = np.arange(0, 4, 0.5)
Angle = np.arange(-6, 7, 1)
Vitesse  = np.arange(0, 110, 10)

And then the generation of membership functions:

#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])

I generate here the Classification functions that classify the input values in their appropriate class:

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))

I define now the rules base which contains 21 rules :

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)

For the Rules aggregation now I only used 3 rules:

    aggregated=np.fmax(imp1,np.fmax(imp2,imp3))

The final step is the Defuzzification:

    defuzz_centroid = fuzz.defuzz(Vitesse, aggregated, 'centroid')
    print defuzz_centroid

My fuzzy controller contains two inputs, one output and 21 rules, I only use 3 rules in this example. How can I aggregate all the other rules with the SCIKIT Toolkit?

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])

我在这里生成分类功能,分类输入值在其适当的类:

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工具包?

python  controller  scikit-learn  aggregation