660 likes | 2.22k Views
FUZZY & ANFIS. Use MATLAB. OUTLINE. Fuzzy logic toolbox Fuzzy: use command line Fuzzy: use GUI ANFIS: use command line ANFIS: use GUI. OUTLINE. Fuzzy logic toolbox Fuzzy: use command line Fuzzy: use GUI ANFIS: use command line ANFIS: use GUI. FUZZY LOGIC TOOLBOX. 附檔名: XXX.fis
E N D
FUZZY & ANFIS Use MATLAB
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
FUZZY LOGIC TOOLBOX • 附檔名:XXX.fis • 包含System、Input、Output及Rule四個部分 • 歸屬函數種類:常用包含三角形(trimf)、梯形(trapmf)、通用鐘形(gbellmf)、高斯(gaussmf)、高斯2(gauss2mf),不常用包含sigmf、dsigmf、psigmf、pimf、smf、zmf等。 • 模糊規則(Rule):由 if……then…… 組成。
歸屬函數種類 • Sigmf • Dsigmf • Psigmf • Pimf • Smf • Zmf • Trimf • Trapmf • Gbellmf • Gaussmf • Gauss2mf
模糊規則(RULE) • 語意表示法: • if (x is A) and (y is B) then (z is α)(1) • if (x is A) or (y is C) then (z is β)(1) • 符號表示法: • (x == A) & (y == B) => (z == α)(1) • (x ==A) | (y == C) => (z == β)(1) • 指標表示法: • 1 2, 1(1):1 假設有輸入有ABC三個歸屬函數 • 1 3, 2(1):2 輸出有α β兩個歸屬函數
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
SYSTEM [System] %用中括弧[]包住四大區域,即System,Inputs,Outputs及Rules。 Name='tipper1'; %取名為tipper1.fis Type='mamdani'; %Mamdani style NumInputs=2; %有兩個輸入變數 NumOutputs=1; %有一個輸出變數 NumRules=3; %有三條規則 AndMethod='min'; %定義推論過程中所用之運算子 OrMethod='max'; ImpMethod='min'; AggMethod='max'; DefuzzMethod='centroid';
INPUT1 [Input1] %定義第一輸入變數 Name='service'; %變數名稱 Range=[0 10]; %變數範圍 NumMFs=3; %分三個程度或歸屬函數 MF1='poor':'gaussmf',[1.5 0]; %第一歸屬函數定義 MF2='good':'gaussmf',[1.5 5]; %第二歸屬函數定義 MF3='excellent':'gaussmf',[1.5 10]; %第三歸屬函數定義
INPUT2 [Input2] %定義第二輸入變數 Name='food'; Range=[0 10]; NumMFs=2; MF1='rancid':'trapmf',[0 0 1 3]; MF2='delicious':'trapmf',[7 9 10 10];
OUTPUT1 [Output1]; %定義輸出變數 Name='tip'; Range=[0 30]; NumMFs=3; MF1='cheap':'trimf',[0 5 10]; MF2='average':'trimf',[10 15 20]; MF3='generous':'trimf',[20 25 30];
RULES • If (service is poor) or (food is rancid) then (tip is cheap) (1) • If (service is good) then (tip is average) (1) • If (service is excellent) or (food is delicious) then (tip is generous) (1)
RULES [Rules] %定義三條規則,用指標方式定義 1 1, 1 (1) : 2; 2 0, 2 (1) : 2; 3 2, 3 (1) : 2;
儲存成tipper1.fis • 將tipper1.fis讀入MATLAB中: tipper=readfis('tipper1'); • 測試tipper: evalfis([5 5],tipper) %當服務與食物尚可時 ans= 15.0000 evalfis([5 5;10 3;2 9],tipper) %同時多組輸入 ans = 15.0000 24.9220 16.8196
其他指令 • 畫出歸屬函數: plotmf(tipper,'input',1) • 顯示模糊規則: showrule(tipper) %語意表示法 showrule(tipper,[1 3],'symbolic') %符號表示法 • 畫出輸入輸出對照圖: surfview(tipper)
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
開啟FUZZYLOGICTOOLBOXGUI • 在MATLAB command window輸入fuzzy
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
讀入DEMO資料並處理 load mgdata.dat time = mgdata(:, 1); x = mgdata(:, 2); for t=118:1117, Data(t-117,:)=[x(t-18) x(t-12) x(t-6) x(t) x(t+6)]; end trnData=Data(1:500, :); chkData=Data(501:end, :);
初始化並產生FIS fismat = genfis1(trnData); %fismat=genfis1(inputData,2,'gauss2mf','constant');
訓練ANFIS [fismat1,error1,ss,fismat2,error2] = anfis(trnData,fismat,[],[],chkData);
測試ANFIS anfis_output = evalfis([trnData(:,1:4); chkData(:,1:4)],fismat2); index = 125:1124; subplot(211), plot(time(index), [x(index) anfis_output]); xlabel('Time (sec)'); title('MG Time Series and ANFIS Prediction'); subplot(212), plot(time(index), x(index) - anfis_output); xlabel('Time (sec)'); title('Prediction Errors');
OUTLINE • Fuzzy logic toolbox • Fuzzy: use command line • Fuzzy: use GUI • ANFIS: use command line • ANFIS: use GUI
開啟ANFISEDIT GUI • 在MATLAB command window輸入anfisedit
讀入DEMO資料 • 在MATLAB command window輸入下列指令: load fuzex1trnData.dat load fuzex2trnData.dat load fuzex1chkData.dat load fuzex2chkData.dat