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 ) 另外本文用意是幫助大家能用中文較快理解 IT2FS,就不寫英文了.... 話說那輪得到我用英文解說 ![]() 上圖則是 Type II Fuzzy Logic flowchart;繼續往下讀之前建議先讀下面幾篇論文: 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. Wu, D. and J. M. Mendel, "Enhanced Karnik-Mendel Algorithms," IEEE Trans. on Fuzzy Systems, vol. 17, pp. 923-934, August 2009. Liang, Q. and J. M. Mendel, "Interval Type-2 Fuzzy Logic Systems: Theory and Design," IEEE Trans. on Fuzzy Systems, vol. 8, pp. 535550, 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) 為啥要這樣計算 ... 問老師也是簡單說書上這樣寫就帶過了 ... 0rz ~ 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) 整個實際的程式範例你可以在這邊下載,記得要先安裝一下套件 ... " TWMAN+:Python for Interval Type II Fuzzy System " GitHub: https://github.com/ideex2/MiT/tree/master/Python%20for%20Interval%20Type%20II%20Fuzzy%20System ![]() 上面做一堆事情就是在做下面這幾個圖啦 ![]() ![]() ![]() ![]() 程式輸出畫面如上,是不是跟下圖書上的結果一樣勒? ![]() 下篇再說明 Type Reduce、Enhanced Karnik–Mendel Algorithms,不然文章太長 |