Interval Type-2 Fuzzy Logic System (IT2FLS) 區間二型模糊邏輯系統

Interval Type-2 Fuzzy Sets (IT2FS)
區間二型模糊集合 / 區間二型模糊邏輯系統
Interval Type-2 Fuzzy Logic System (IT2FLS)

嗯 ~ 主要說明 Karnik-Mendel Algorithm 跟 Enhanced Karnik-Mendel Algorithms

由於已經很多大師級人物用英文發表了很多論文,所以在這邊我以中文為主 !!!
首先請一定要有心裡準備把 Prof. Jerry Mendel 的論文都讀過一遍

讓我們從這篇開始吧 ! Mendel, J. M., "Type-2 Fuzzy Sets and Systems: an Overview," IEEE Computational Intelligence Magazine, Vol. 2, pp. 20-29, February 2007.

Interval Type II Fuzzy Logic:區間第二型模糊邏輯
Karnik-Mendel Algorithm、Type Reduce
Enhanced Karnik–Mendel Algorithms

分別有以上三個比較重要的演算法,KM和E-KM是求中心值,另一個則是降階
(There are three important Algorithms of IT2FS, KM & E-KM are use for computed centroid )
另外本文用意是幫助大家能用中文較快理解,就不寫英文了.... 
(話說那輪得到我用英文解說) 

首先是 Karnik-Mendel Algorithm


上圖則是 Type II Fuzzy Logic flowchart;繼續往下讀之前建議先讀下面幾篇論文:

  1. H. Hagras, “A Hierarchical Type-2 Fuzzy Logic Control Architecture for Autonomous Mobile Robots”, IEEE Transactions on Fuzzy Systems, Vol. 12 No. 4, pp. 524-539, August 2004.
  2. Wu, D. and J. M. Mendel, "Enhanced Karnik-Mendel Algorithms," IEEE Trans. on Fuzzy Systems, vol. 17, pp. 923-934, August 2009.
  3. Liang, Q. and J. M. Mendel, "Interval Type-2 Fuzzy Logic Systems: Theory and Design," IEEE Trans. on Fuzzy Systems, vol. 8, pp. 535­550, 2000.
當然如果你懂 MATLAB 你也可以直接看 Prof. Jerry Mendel 的說明


這是典型的 IT2FS的架構圖
Tu是上界函數(Upper bond,UMF),Tl是下界函數(Lower bond,LMF)
Wi 就分別表示Zi的上下值 ... 一般範例題都會給


這是 A Hierarchical Type-2 Fuzzy Logic Control Architecture for Autonomous Mobile Robots 這篇論文寫的演算法,老實說 ... 我第一次看看了三天我還是不懂它要表達什麼 ... 原來是漏掉很多步驟 ...


直接看另一篇論文中演算法的說明吧 ....
計算 Yl 和 Yr 兩個大同小異 ... 但我一直搞不懂 (19) 和 (24) 為啥要這樣計算 ...
問老師也是簡單說書上這樣寫就帶過了 ... Anyway 直接用下方程式碼解釋比較快 ..


每個不同的函式區塊我已經盡量用顏色區隔了,Python的寫法我就不多說,網路很多教學,我也是看網路學的

#下面是 Python 函式庫導入
# -*- coding:utf8  -*-   
import sys
import math
import numpy as np
#網路上很多教學,我自己也是看網路慢慢學的
#===============GaussianMF_UncertainDeviation===============
#--------------下面是要使用不確定標準差透過高斯函數來計算中心值--------
def GaussianMF_UncertainDeviation(X, mean, sigma1, sigma2, UpperBond, LowerBond):
    d1 = min(sigma1, sigma2)
    d2 = max(sigma1, sigma2)
    t1, t2 = 0, 0
    for i in range (100):
        t1 = Gaussian(X[i], d1, mean)
        t2 = Gaussian(X[i], d2, mean)
        UpperBond[i] = max(t1, t2)
        LowerBond[i] = min(t1, t2)
#只是簡單呼叫幾個已經寫好的函式
#--------------上面是要使用不確定標準差透過高斯函數來計算中心值--------
#-----------------------下面開始計算中心值,分為兩部份--------------------
#接著下面是上下兩張演算法中2)的(15)(16)(20)(21),也就是要把上下界函數相加除2,為什麼? #值得注意的是這邊會算出一個值,在整個程式中定義它為Y'或Y1再回傳(也就是 t1/t2)
#=================Compute Centroid - PART 2=================
def Sum(Y, Theta):
    t1, t2 =0.0, 0.0
    for i in range (100):        
        t1 += (Y[i] * Theta[i])
        t2 += Theta[i]
    if t2 == 0:
        return -1
    return t1/t2
#=================Compute Centroid - PART 1=================
def ComputeCentroid(Y, UpperBond, LowerBond, MaxMin):
    Y1, Y2 = 0.0, 100.0
    Theta, Delta, h = np.zeros([100],dtype=float), np.zeros([100],dtype=float), np.zeros([100],dtype=float)
    for i in range (100):
        Theta[i] = h[i] = (UpperBond[i] + LowerBond[i])/2
        Delta[i] = (UpperBond[i] - LowerBond[i])/2    
    Y1 = Sum(Y, h)
    cn = 0
    while (abs(Y2-Y1) > 0.000000001):
        cn = cn + 1
        if cn > 1:
            Y1 = Y2
        e = 0
        for i in range (100):
            if Y[i] <= Y1 and Y1 <= Y[i+1]:
                e =i
                break
        for i in range (e):
            if MaxMin > 0:
                Theta[i] = h[i] - Delta[i]
            else:
                Theta[i] = h[i] + Delta[i]
        for i in range(e,100):
            if MaxMin > 0:
                Theta[i] = h[i] + Delta[i]
            else:
                Theta[i] = h[i] - Delta[i]
        Y2 = Sum(Y, Theta)
    return Y2
#認真看的話可以發現這邊就是4)的19
#-----------------------上面是計算中心值,分為兩部份--------------------
#--------------------下面是高斯函數的定義----------------------
#=============Gaussian Function Definition=============
def Gaussian(x, sigma, mean):
    if sigma == 0:
        return -1
    Gaussian = 2.71828183 ** (-0.5 * (pow(((x-mean)/sigma),2)))
    return Gaussian
#--------------------上面是高斯函數的定義----------------------
#===============GaussianMF_UncertainMean===============
#這邊開始則是要用高斯函數計算,直接把值傳上去就好 
#可分別求出t1和t2,也就是3)和4)的(17)(18)(22)(23)
def GaussianMF_UncertainMean(X, sigma, mean1, mean2, UpperBond, LowerBond):
    m1 = min(mean1, mean2)
    m2 = max(mean1, mean2)
    t1, t2 =0, 0
    for i in range (100):
        if X[i] < m1:
            t1 = Gaussian(X[i], sigma, m1)
            t2 = Gaussian(X[i], sigma, m2)
        elif X[i] >= m1 and X[i] <= (m1+m2)/2:
            t1 = 1
            t2 = Gaussian(X[i], sigma, m2)
        elif X[i] > (m1+m2)/2 and X[i] <= m2:
            t1 = 1
            t2 = Gaussian(X[i], sigma, m1)
        elif X[i] > m2:
            t1 = Gaussian(X[i], sigma, m2)
            t2 = Gaussian(X[i], sigma, m1)
        UpperBond[i] = max(t1, t2)
        LowerBond[i] = min(t1, t2)
#----------這邊是要使用不確定中心值透過高斯函數來計算中心值---------
#------------------------------這邊開始是主程式----------------------------------
print '=====================Karnik-Mendel Algorithm Example===================='
print 'Uncertain Rule-Based Fuzzy Logic Systems: Introduction andNew Directions.'
print'==================================================================='
Y, Yl, Yr = np.zeros([100],dtype=float), np.zeros([100],dtype=float), np.zeros([100],dtype=float)
UpperBond, LowerBond = np.zeros([100],dtype=float), np.zeros([100],dtype=float)
for i in range (1, 100):
    Y[i] = Y[i-1] + 0.1
#===================Uncertain Mean Example=====================
m1 = np.array([5, 4.875, 4.75, 4.625, 4.5, 4.25, 4, 3.75, 3.5], dtype=float)
m2 = np.array([5, 5.125, 5.25, 5.375, 5.5, 5.75, 6, 6.25, 6.5], dtype=float)
for i in range (9):
    GaussianMF_UncertainMean(Y, 1, m1[i], m2[i], UpperBond, LowerBond)
    Yl = ComputeCentroid(Y, UpperBond, LowerBond, 0)
    Yr = ComputeCentroid(Y, UpperBond, LowerBond, 1)
    print 'm1=%1.4f' %(m1[i]), 'm2=%1.4f' %(m2[i]), 'm2-m1=%1.4f' %(m2[i]-m1[i]), 'Yr-Yl=%1.6f' %abs((Yr-Yl)), 'Yr=%1.6f' %(Yr), 'Yl=%1.6f' %(Yl)
#=============Uncertain standard Deviation Exampl==============
mean = 5.0
d1 = np.array([1, 0.875, 0.75, 0.625, 0.5, 0.375, 0.25], dtype=float)
d2 = np.array([1, 1.125, 1.25, 1.375, 1.5, 1.625, 1.75], dtype=float)
for i in range (7):
    GaussianMF_UncertainDeviation(Y, mean, d1[i], d2[i], UpperBond, LowerBond)
    Yl = ComputeCentroid(Y, UpperBond, LowerBond, 0)
    Yr = ComputeCentroid(Y, UpperBond, LowerBond, 1)
    print 'd1=%1.6f' %(d1[i]), 'd2=%1.6f' %(d2[i]), 'd2-d1=%1.6f' %(d2[i]-d1[i]), 'Yr-Yl=%1.6f' %abs((Yr-Yl)), 'Yr=%1.6f' %(Yr), 'Yl=%1.6f' %(Yl)

整個實際的程式範例你可以在這邊下載,記得要先安裝一下套件 ... 


上面做一堆事情就是在做下面這幾個圖啦





程式輸出畫面如上,是不是跟下圖書上的結果一樣勒?


接著說明 Type Reduce、Enhanced Karnik–Mendel Algorithms

如果你對整系列的 Interval Type II Fuzzy System 有興趣,當然不能忘記 K 一下 Prof. Jerry Mendel 的相關論文:
另外,就是要再 K 一下 Prof. Hani Hagras 的論文囉
整個實際的程式範例你可以在這邊下載,記得要先安裝一下套件 ...


那麼接著 ... 就是一樣要再來略為解釋一下這個演算法啦 !
由於很多地方是跟 KM 一模一樣,所以只解釋其中不同的地方哦 !
PS : Prof. Hagras 說 KM 演算法就很夠用了 ... (事實上 EKM 真的感覺複雜多了)

import sys
import math
import numpy as np
#===============GaussianMF_UncertainDeviation===============
def GaussianMF_UncertainDeviation(X, mean, sigma1, sigma2, UpperBond, LowerBond):
    d1 = min(sigma1, sigma2)
    d2 = max(sigma1, sigma2)
    t1, t2 = 0, 0
    for i in range (100):
        t1 = Gaussian(X[i], d1, mean)
        t2 = Gaussian(X[i], d2, mean)
        UpperBond[i] = max(t1, t2)
        LowerBond[i] = min(t1, t2)
#=======================Compute Centroid Yr======================
def ComputeCentroidYr(Y, UpperBond, LowerBond):
    Y1, Y2, k2, s = 0.0, 100.0, 0, 0.0
    k1 = int(100/1.7)
    Y1 = ComputeY2(Y, k1, UpperBond, LowerBond)
    for i in range (100):
        if Y[i] <= Y1 and Y1 <= Y[i+1]:
            k2 = i
    if k2 == k1:
        Y2 = Y1
    else:
        Y2 = ComputeS2(Y, k1, k2, UpperBond, LowerBond)
    return Y2



#===============================================================
def ComputeS2(Y, k1, k2, Up, Low):
    a, b, a1, b1, maxk, mink =0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    s = math.sin(k2 - k1)   <---- 多了這個正玹計算,也就是上圖演算法中的 5)
    maxk = max(k1,k2)
    mink = min(k1,k2)
    for i in range (100):      <---- 接著是上圖演算法的 (34)、(35)
        for i in range (0, k1+1):
            a = a + (Y[i] * Low[i])
            b = b + Low[i]
        for i in range (k1+1, 100):
            a = a + (Y[i] * Up[i])
            b = b + Up[i]
        for i in range (mink, maxk+1):
            a1 = Y[i] * (Up[i] - Low[i])
            b1 = (Up[i] - Low[i])
            a = a - (s * a1)     <------ 注意看就能發現上下兩個演算法不同的地方就是這
            b = b - (s * b1)
    if b == 0:
        return -1
    return a/b
#=======================基本上上下兩個架構是一樣的==============
def ComputeS1(Y, k1, k2, Up, Low):
    a, b, a1, b1, maxk, mink =0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    s = math.sin(k2 - k1)
    maxk = max(k1,k2)
    mink = min(k1,k2)
    for i in range (100):
        for i in range (0, k1+1):
            a = a + (Y[i] * Up[i])
            b = b + Up[i]
        for i in range (k1+1, 100):
            a = a + (Y[i] * Low[i])
            b = b + Low[i]
        for i in range (mink, maxk+1):
            a1 = Y[i] * (Up[i] - Low[i])
            b1 = (Up[i] - Low[i])
            a = a + (s * a1)
            b = b + (s * b1)
    if b == 0:
        return -1
    return a/b



#===============================================================
def ComputeY2(Y, k, Up, Low):
    a, b, a1, b1 =0.0, 0.0, 0.0, 0.0
    for i in range (100):
        for i in range (0, k+1):
            a = a + (Y[i] * Low[i])
            b = b + Low[i]
        for i in range (k+1, 100):
            a = a + (Y[i] * Up[i])
            b = b + Up[i]
    if b == 0:
        return -1
    return a/b
#===============================================================
def ComputeY1(Y, k, Up, Low):
    a, b, a1, b1 =0.0, 0.0, 0.0, 0.0
    for i in range (100):
        for i in range (0, k+1):
            a = a + (Y[i] * Up[i])
            b = b + Up[i]
        for i in range (k+1, 100):
            a = a + (Y[i] * Low[i])
            b = b + Low[i]
    if b == 0:
        return -1
    return a/b
#=======================Compute Centroid Yl======================
def ComputeCentroidYl(Y, UpperBond, LowerBond):
    Y1, Y2, k2, s = 0.0, 100.0, 0, 0.0
    k1 = int(100/2.4)
    Y1 = ComputeY1(Y, k1, UpperBond, LowerBond)
    for i in range (100):
        if Y[i] <= Y1 and Y1 <= Y[i+1]:
            k2 = i
    if k2 == k1:
        Y2 = Y1
    else:
        Y2 = ComputeS1(Y, k1, k2, UpperBond, LowerBond)
    return Y2
#=============Gaussian Function Definition=============
def Gaussian(x, sigma, mean):
    if sigma == 0:
        return -1
    Gaussian = 2.71828183 ** (-0.5 * (pow(((x-mean)/sigma),2)))
    return Gaussian
#===============GaussianMF_UncertainMean===============
def GaussianMF_UncertainMean(X, sigma, mean1, mean2, UpperBond, LowerBond):
    m1 = min(mean1, mean2)
    m2 = max(mean1, mean2)
    t1, t2 =0, 0
    for i in range (100):
        if X[i] < m1:
            t1 = Gaussian(X[i], sigma, m1)
            t2 = Gaussian(X[i], sigma, m2)
        elif X[i] >= m1 and X[i] <= (m1+m2)/2:
            t1 = 1
            t2 = Gaussian(X[i], sigma, m2)
        elif X[i] > (m1+m2)/2 and X[i] <= m2:
            t1 = 1
            t2 = Gaussian(X[i], sigma, m1)
        elif X[i] > m2:
            t1 = Gaussian(X[i], sigma, m2)
            t2 = Gaussian(X[i], sigma, m1)
        UpperBond[i] = max(t1, t2)
        LowerBond[i] = min(t1, t2)
print '=====================Enhanced Karnik–Mendel Algorithms Example=========='
print 'Uncertain Rule-Based Fuzzy Logic Systems: Introduction andNew Directions.'
print '========================================================================'
Y, Yl, Yr = np.zeros([100],dtype=float), np.zeros([100],dtype=float), np.zeros([100],dtype=float)
UpperBond, LowerBond = np.zeros([100],dtype=float), np.zeros([100],dtype=float)
for i in range (1, 100):
    Y[i] = Y[i-1] + 0.1
#===================Uncertain Mean Example=====================
m1 = np.array([5, 4.875, 4.75, 4.625, 4.5, 4.25, 4, 3.75, 3.5], dtype=float)
m2 = np.array([5, 5.125, 5.25, 5.375, 5.5, 5.75, 6, 6.25, 6.5], dtype=float)
for i in range (9):
    GaussianMF_UncertainMean(Y, 1, m1[i], m2[i], UpperBond, LowerBond)
    Yl = ComputeCentroidYl(Y, UpperBond, LowerBond)
    Yr = ComputeCentroidYr(Y, UpperBond, LowerBond)
    print 'm1=%1.4f' %(m1[i]), 'm2=%1.4f' %(m2[i]), 'm2-m1=%1.4f' %(m2[i]-m1[i]), 'Yr-Yl=%1.6f' %abs((Yr-Yl)), 'Yr=%1.6f' %(Yr), 'Yl=%1.6f' %(Yl)
#=============Uncertain standard Deviation Exampl==============
mean = 5.0
d1 = np.array([1, 0.875, 0.75, 0.625, 0.5, 0.375, 0.25], dtype=float)
d2 = np.array([1, 1.125, 1.25, 1.375, 1.5, 1.625, 1.75], dtype=float)
for i in range (7):
    GaussianMF_UncertainDeviation(Y, mean, d1[i], d2[i], UpperBond, LowerBond)
    Yl = ComputeCentroidYl(Y, UpperBond, LowerBond)
    Yr = ComputeCentroidYr(Y, UpperBond, LowerBond)
    print 'd1=%1.6f' %(d1[i]), 'd2=%1.6f' %(d2[i]), 'd2-d1=%1.6f' %(d2[i]-d1[i]), 'Yr-Yl=%1.6f' %abs((Yr-Yl)), 'Yr=%1.6f' %(Yr), 'Yl=%1.6f' %(Yl)
#raw_input("Press Enter to quit")

剩的就不多加解釋囉 ! 因為答案要一樣哦 !!!!