-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableLookUpAlgorithem.m
242 lines (199 loc) · 8.96 KB
/
TableLookUpAlgorithem.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
clc
clear all
close all
%% TABLE LOOK UP TRAINING ALGORITHEM.
% This programm developed as a course project for Fuzzu systems course by
% Habibollah Naeimi.
disp(' TABLE LOOK UP TRAINING ALGORITHEM.');
disp(' This programm developed as a course project for Fuzzu systems course')
disp(' by Habibollah Naeimi.')
disp('*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*------------------------------------------------------')
%% 1st Part: Parameter Setting.
%% Parameters Initiating.
disp(' Parameters Initiating...');
InpuNumb = 6;%input(' How many input do you want? Inputs =');% Input Numbers.
disp(' ');
disp(' How many membership functions do you want?');
MemFuNu = [18];%input(' Please Enter a vector. MFN ='); % Membership Function Numbers.
e1 = numel(MemFuNu); % Number of Matrix Elements.
disp(' ');
disp(' Which membership functions type do you want?');
disp(' Triangular = 1 , Trapezoidal = 2 , Gaussian = 3');
MemFTy = [3];%input(' Please Enter a vector. MF ='); % Membership Function Types.
e4 = numel(MemFTy); % Number of Matrix Elements.
disp(' ');
disp(' Enter the Lower Bound.');
LowBnd = [0.25];%input(' Please Enter a vector. Low Bnd ='); % Lower Bound.
e2 = numel(LowBnd); % Number of Matrix Elements.
disp(' ');
disp(' Enter the Upper Bound.'); % Upper Bound.
UpBnd = [1.6];%input(' Please Enter a vector. Upper Bnd =');
e3 = numel(UpBnd); % Number of Matrix Elements.
q1 = e1~=InpuNumb;
q2 = e2~=InpuNumb;
q3 = e3~=InpuNumb;
q4 = e4~=InpuNumb;
Training_Ratio = 0.5;
%% Fixing Parameters dimentions.
switch q1
case 1 % Fixing Membership Function dimentions.
if e1==1
MemFuNu = repmat(MemFuNu,1,InpuNumb+1);
else
disp(' Invalid Membership Function Number!');
disp(' Please Start again.');
end
end
switch q2
case 1 % Fixing Lower Bound dimentions.
if e2==1
LowBnd = repmat(LowBnd,1,InpuNumb+1);
else
disp(' Invalid Lower Boundary!');
disp(' Please Start again.');
end
end
switch q3
case 1 % Fixing Upper Bound dimentions.
if e3==1
UpBnd = repmat(UpBnd,1,InpuNumb+1);
else
disp(' Invalid Upper Boundary!');
disp(' Please Start again.');
end
end
switch q4
case 1 % Fixing Membership Type dimentions.
if e4==1
MemFTy = repmat(MemFTy,1,InpuNumb+1);
else
disp(' Invalid Membership Function Handle!');
disp(' Please Start again.');
end
end
disp(' ');
disp('*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*------------------------------------------------------')
disp(' ');
%% 2nd Part: Data.
%% Sampling.
SAMPLES = rand(1,32); % Primary sampling.
%SAMPLES = 0.2:0.01:0.51;
for v=33:1033 % Secoundary Sampling.
SAMPLES(v) = 0.2*SAMPLES(v-31)/(1+(SAMPLES(v-31)^10))+0.9*SAMPLES(v-1);
end
SAMPLES = SAMPLES(34:end); % Samples.
plot(SAMPLES)
%% Data Paires.
e5 = numel(SAMPLES);
w1 = round(Training_Ratio*e5);
Training_Datas = zeros(w1,InpuNumb+1);
for v=1:w1
Training_Datas(v,:) = SAMPLES(v:v+InpuNumb); % Creating data paires.
end
disp(' Data sampling check!');
disp('*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-----------------------------------------------------')
disp(' ');
%% 3rd Part: Rule Base.
%% Finding Rules.
% Implementing a function to
% find rules.
[Rules , RulesMVlu] = RuleFinder(Training_Datas,MemFuNu,LowBnd,UpBnd,MemFTy);
PRB = Rules(:,:); % Primary Rule Base.
disp(' The Rule Base is READY!');
disp(' Here is the detailes: ');
Rules_Number_all = numel(PRB(:,1)); % Calculating Number of Rules.
disp(' Number of All Rules:');
disp(Rules_Number_all);
% Implimenting a function to
Rules = ConflictChecking(Rules,RulesMVlu); % eliminate conflictions.
RuleBase = [Rules ones(size(Rules,1),2)]; % Creating final Rule Base.
nonconflict_rules = numel(Rules(:,1));
disp(' And Number of No Conflicting Rules:'); % Nonconflicting Rules.
disp(nonconflict_rules);
conflict_Rules = Rules_Number_all-nonconflict_rules; % Calculating Number
disp(' And Number of Conflicted Rules:'); % of Conflicted
disp(conflict_Rules); % Rules.
disp(' ');
disp('*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*------------------------------------------------------')
disp(' ');
%% 4th Part: Fuzzy System.
%% Defining Fuzzy System.
TSP = newfis('Time Series Prediction'); % Creating the fuzzy system.
e1 = numel(MemFuNu);
for i=1:e1-1 % Defining the Inputs.
Name = ['X' num2str(i)]; % Rule Name's Creation.
TSP = addvar(TSP,'input',Name,[LowBnd(i),UpBnd(i)]);
switch MemFTy(i)
case 1 % For Triangular Membership Function.
Step = (UpBnd(i)-LowBnd(i))/(MemFuNu(i)-1);
for j = 1:MemFuNu(i)
Center = LowBnd(i)+(j-1)*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'input',i,MemFName,'trimf',[Center-Step,Center,Center+Step]);
end
case 2 % For Trapezoidal Membership Function.
Step = (UpBnd(i)-LowBnd(i))/(MemFuNu(i)-1)/3;
for j = 1:MemFuNu(i)
Center = LowBnd(i)+(j-1)*3*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'input',i,MemFName,'trapmf',[Center-2*Step,Center-Step,Center+Step,Center+2*Step]);
end
case 3 % For Gaussian Membership Function.
Step = (UpBnd(i)-LowBnd(i))/(MemFuNu(i)-1)/2;
for j = 1:MemFuNu(i)
Center = LowBnd(i)+(j-1)*2*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'input',i,MemFName,'gaussmf',[Step,Center]);
end
end
end
% Deffining the Output.
TSP = addvar(TSP,'output','Y',[LowBnd(end),UpBnd(end)]);
Name = ['Y'];
switch MemFTy(end)
case 1 % For Triangular Membership Function.
Step = (UpBnd(end)-LowBnd(end))/(MemFuNu(end)-1);
for j=1:MemFuNu(end)
Center = LowBnd(end)+(j-1)*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'output',1,MemFName,'trimf',[Center-Step,Center,Center+Step]);
end
case 2 % For Trapezoidal Membership Function.
Step = (UpBnd(end)-LowBnd(end))/(MemFuNu(end)-1)/3;
for j=1:MemFuNu(end)
Center = LowBnd(end)+(j-1)*3*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'output',1,MemFName,'trapmf',[Center-2*Step,Center-Step,Center+Step,Center+2*Step]);
end
case 3 % For Gaussian Membership Function.
Step = (UpBnd(end)-LowBnd(end))/(MemFuNu(end)-1)/2;
for j=1:MemFuNu(end)
Center = LowBnd(end)+(j-1)*2*Step;
MemFName = [Name '_MF' num2str(j)]; % Rule Name's Creation.
TSP = addmf(TSP,'output',1,MemFName,'gaussmf',[Step,Center]);
end
end
TSP = addrule(TSP,RuleBase); % Deploying Rule Base to our Fuzzy System.
disp(' Fuzzy System is Ready!');
disp(' Fuzzy System detailes:');
showfis(TSP) % Showing Fuzzy System detailes.
disp(' ');
disp('*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*-.-*------------------------------------------------------')
disp(' ');
%% 5th Part: Conclusion.
%% Predicting.
Y = SAMPLES(1:InpuNumb);
for i=InpuNumb+1:numel(SAMPLES) % Calculating Pridictions.
Y(i)=evalfis(SAMPLES(i-InpuNumb:i-1),TSP);
end
Error = sum(abs(SAMPLES-Y))/100; % Calculating Error.
ED = [' Error of Prediction = ' num2str(Error) '%' ];
%% Plotting.
AH = axes;
sam_plt = plot(AH,SAMPLES);
hold on
output_plt = plot(AH,Y,'r');
xlim([0 e5]);
ylim([LowBnd(end)-0.2 UpBnd(end)+0.2]);
legend(AH,'Real Value','Prediction');
disp(ED);