-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainFuzzyUsingGA.m
211 lines (135 loc) · 4.49 KB
/
TrainFuzzyUsingGA.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
function bestfis=TrainFuzzyUsingGA(fis,data)
%% Problem Definition
p0=GetFISParams(fis);
Problem.CostFunction=@(x) TrainFISCost(x,fis,data);
Problem.nVar=numel(p0);
Problem.VarMin=-25;
Problem.VarMax=25;
%% GA Params
Params.MaxIt=1000;
Params.nPop=25;
%% Run GA
results=RunGA(Problem,Params);
%% Get Results
p=results.BestSol.Position.*p0;
bestfis=SetFISParams(fis,p);
end
function results=RunGA(Problem,Params)
disp('Starting GA ...');
%% Problem Definition
CostFunction=Problem.CostFunction; % Cost Function
nVar=Problem.nVar; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=Problem.VarMin; % Lower Bound of Variables
VarMax=Problem.VarMax; % Upper Bound of Variables
%% GA Parameters
MaxIt=Params.MaxIt; % Maximum Number of Iterations
nPop=Params.nPop; % Population Size
pc=0.9; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (Parnets)
pm=0.2; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
gamma=0.7;
mu=0.15; % Mutation Rate
beta=8; % Selection Pressure
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
% Initialize Position
if i>1
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
else
pop(i).Position=ones(VarSize);
end
% Evaluation
pop(i).Cost=CostFunction(pop(i).Position);
end
% Sort Population
Costs=[pop.Cost];
[Costs, SortOrder]=sort(Costs);
pop=pop(SortOrder);
% Store Best Solution
BestSol=pop(1);
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);
% Store Cost
WorstCost=pop(end).Cost;
%% Main Loop
for it=1:MaxIt
P=exp(-beta*Costs/WorstCost);
P=P/sum(P);
% Crossover
popc=repmat(empty_individual,nc/2,2);
for k=1:nc/2
% Select Parents Indices
i1=RouletteWheelSelection(P);
i2=RouletteWheelSelection(P);
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Apply Crossover
[popc(k,1).Position, popc(k,2).Position]=...
Crossover(p1.Position,p2.Position,gamma,VarMin,VarMax);
% Evaluate Offsprings
popc(k,1).Cost=CostFunction(popc(k,1).Position);
popc(k,2).Cost=CostFunction(popc(k,2).Position);
end
popc=popc(:);
% Mutation
popm=repmat(empty_individual,nm,1);
for k=1:nm
% Select Parent
i=randi([1 nPop]);
p=pop(i);
% Apply Mutation
popm(k).Position=Mutate(p.Position,mu,VarMin,VarMax);
% Evaluate Mutant
popm(k).Cost=CostFunction(popm(k).Position);
end
% Create Merged Population
pop=[pop
popc
popm]; %#ok
% Sort Population
Costs=[pop.Cost];
[Costs, SortOrder]=sort(Costs);
pop=pop(SortOrder);
% Update Worst Cost
WorstCost=max(WorstCost,pop(end).Cost);
% Truncation
pop=pop(1:nPop);
Costs=Costs(1:nPop);
% Store Best Solution Ever Found
BestSol=pop(1);
% Store Best Cost Ever Found
BestCost(it)=BestSol.Cost;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
end
disp('End of GA.');
disp(' ');
%% Results
results.BestSol=BestSol;
results.BestCost=BestCost;
end
function [y1, y2]=Crossover(x1,x2,gamma,VarMin,VarMax)
alpha=unifrnd(-gamma,1+gamma,size(x1));
y1=alpha.*x1+(1-alpha).*x2;
y2=alpha.*x2+(1-alpha).*x1;
y1=max(y1,VarMin);
y1=min(y1,VarMax);
y2=max(y2,VarMin);
y2=min(y2,VarMax);
end
function y=Mutate(x,mu,VarMin,VarMax)
nVar=numel(x);
nmu=ceil(mu*nVar);
j=randsample(nVar,nmu)';
sigma=0.1*(VarMax-VarMin);
y=x;
y(j)=x(j)+sigma*randn(size(j));
y=max(y,VarMin);
y=min(y,VarMax);
end