-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuckooMain.m
419 lines (331 loc) · 14.8 KB
/
cuckooMain.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
clc, clear, close all
%% Set problem parameters
tic
% select your cost function:
%costFunction = 'rastrigin'; % F6 +/-5.12
global ts1 ts2 ts3 ts4 ts5 ts6 y;
y = [];
npar = 6; % number of optimization variables
varLo = 0; % Lower band of parameter
varHi = 20; % Higher band of parameter
%% Set COA parameters
numCuckooS = 6; % number of initial population
minNumberOfEggs = 7; % minimum number of eggs for each cuckoo
maxNumberOfEggs = 7; % maximum number of eggs for each cuckoo
maxIter = 25; % maximum iterations of the Cuckoo Algorithm
knnClusterNum = 1; % number of clusters that we want to make
motionCoeff = 9; % Lambda variable in COA paper, default=2
accuracy = -inf; % How much accuracy in answer is needed
maxNumOfCuckoos = 10; % maximum number of cuckoos that can live at the same time
radiusCoeff = 1; % Control parameter of egg laying
cuckooPopVariance = 1e-13; % population variance that cuts the optimization
%% initialize population:
cuckooPop = cell(numCuckooS,1);
% initialize egg laying center for each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.center = ( varHi-varLo )*rand(1,npar) + varLo;
end
%% initialize COA cost minimization plot
figure(1)
hold on
xlabel 'Cuckoo iteration'
ylabel 'Cost Value'
%% Start Cuckoo Optimization Algorithm
iteration = 0;
maxProfit = -1e20; % Let initial value be negative number
goalPoint = (varHi - varLo)*rand(1,npar) + varLo; % a random goalpoint to start COA
globalBestCuckoo = goalPoint;
globalMaxProfit = maxProfit;
profitVector = [];
while ( (iteration <= maxIter) && (-maxProfit > accuracy) )
iteration = iteration + 1;
% initialize number of eggs for each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.numberOfEggs = floor( (maxNumberOfEggs - minNumberOfEggs) * rand + minNumberOfEggs );
end
% get total number of available eggs between all cuckooS
summ = 0;
for cuckooNumber = 1:numCuckooS
summ = summ + cuckooPop{cuckooNumber}.numberOfEggs;
end
% calculate egg laying radius for each Cuckoo, considering problem
% limitations and ratio of each cuckoo's eggs
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.eggLayingRadius = cuckooPop{cuckooNumber}.numberOfEggs/summ * ( radiusCoeff * (varHi-varLo) );
end
% To lay eggs, we produced some radius values less than egg laying
% radius of each cuckoo
for cuckooNumber = 1:numCuckooS
cuckooPop{cuckooNumber}.eggLayingRadiuses = cuckooPop{cuckooNumber}.eggLayingRadius * rand(cuckooPop{cuckooNumber}.numberOfEggs,1);
end
for cuckooNumber = 1:numCuckooS
params = cuckooPop{cuckooNumber}.center; % get center values
tmpRadiuses = cuckooPop{cuckooNumber}.eggLayingRadiuses;
numRadiuses = numel(tmpRadiuses);
% divide a (hyper)circle to 'numRadiuses' segments
% This is to search all over the current habitat
angles = linspace(0,2*pi,numRadiuses); % in Radians
newParams = [];
for cnt = 1:numRadiuses
addingValue = zeros(1,npar);
for iii = 1:npar
randNum = floor(2*rand)+1;
addingValue(iii) = (-1)^randNum * tmpRadiuses(cnt)*cos(angles(cnt)) + tmpRadiuses(cnt)*sin(angles(cnt));
end
newParams = [newParams; params + addingValue ];
end
% check for variable limits
newParams(find(newParams>varHi)) = varHi;
newParams(find(newParams<varLo)) = varLo;
cuckooPop{cuckooNumber}.newPosition4Egg = newParams;
end
% Now egg laying is done
% % Now that egg positions are found, they are laid, and so its time to
% % remove the eggs on the same positions (because each egg only can go to one nest)
% for cuckooNumber = 1:numCuckooS
% tmpPopulation = cuckooPop{cuckooNumber}.newPosition4Egg;
% tmpPopulation = floor(tmpPopulation * 1e20)/1e20;
% ii = 2;
% cntt = 1;
% while ii <= size(tmpPopulation,1) || cntt <= size(tmpPopulation,1)
% if all((tmpPopulation(cntt,:) == tmpPopulation(ii,:)))
% tmpPopulation(ii,:) = [];
% end
% ii = ii + 1;
% if ii > size(tmpPopulation,1) && cntt <= size(tmpPopulation,1)
% cntt = cntt + 1;
% ii = cntt + 1;
% if ii > size(tmpPopulation,1)
% break
% end
% end
% end
% cuckooPop{cuckooNumber}.newPosition4Egg = tmpPopulation;
% end
% Now we evalute egg positions
for cuckooNumber = 1:numCuckooS
%cuckooPop{cuckooNumber}.profitValues = -feval(costFunction,[cuckooPop{cuckooNumber}.center ; cuckooPop{cuckooNumber}.newPosition4Egg]);
x = [cuckooPop{cuckooNumber}.center ; cuckooPop{cuckooNumber}.newPosition4Egg];
for iz = 1:size(x,1)
ts1 = timeseries(x(iz,1) , 0 );
ts2 = timeseries(x(iz,2) , 0 );
ts3 = timeseries(x(iz,3) , 0 );
ts4 = timeseries(x(iz,4) , 0 );
ts5 = timeseries(x(iz,5) , 0 );
ts6 = timeseries(x(iz,6) , 0 );
sim('model.mdl');
y = [y , norm(e , 2)];
end
cuckooPop{cuckooNumber}.profitValues = -y;
y = [];
end
% Now we check to see if cuckoo population is more than maxNumOfCuckoos
% this case we should keep 1st maxNumOfCuckoos cuckoos and kill the others
allPositions = [];
whichCuckooPopTheEggBelongs = [];
tmpProfits = [];
if numCuckooS > maxNumOfCuckoos
for cuckooNumber = 1:numCuckooS
tmpProfits = [tmpProfits; cuckooPop{cuckooNumber}.profitValues];
allPositions = [allPositions; [cuckooPop{cuckooNumber}.center; cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar)]];
whichCuckooPopTheEggBelongs = [whichCuckooPopTheEggBelongs; cuckooNumber*ones(size(cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar),1),1) ];
end
% now we sort cuckoo profits
[sortedProfits, sortingIndex] = sort(tmpProfits,'descend');
% Get best cuckoo to be copied to next generation
bestCuckooCenter = allPositions(sortingIndex(1),1:npar);
sortedProfits = sortedProfits(1:maxNumOfCuckoos);
allPositions = allPositions(sortingIndex(1:maxNumOfCuckoos),:);
clear cuckooPop
for ii = 1:maxNumOfCuckoos
cuckooPop{ii}.newPosition4Egg = allPositions(ii,:);
cuckooPop{ii}.center = allPositions(ii,:);
cuckooPop{ii}.profitValues = sortedProfits(ii);
end
numCuckooS = maxNumOfCuckoos;
else
for cuckooNumber = 1:numCuckooS
tmpProfits = [tmpProfits; cuckooPop{cuckooNumber}.profitValues];
allPositions = [allPositions; [cuckooPop{cuckooNumber}.center; cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar)] ];
whichCuckooPopTheEggBelongs = [whichCuckooPopTheEggBelongs; cuckooNumber*ones(size(cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar),1),1) ];
end
[sortedProfits, sortingIndex] = sort(tmpProfits,'descend');
% Get best cuckoo to be copied to next generation
bestCuckooCenter = allPositions(sortingIndex(1),1:npar);
end
maxProfit = sortedProfits(1);
currentBestCuckoo = bestCuckooCenter;
%currentMaxProfit = -feval(costFunction,currentBestCuckoo);
x = currentBestCuckoo;
for iz = 1:size(x,1)
ts1 = timeseries(x(iz,1) , 0 );
ts2 = timeseries(x(iz,2) , 0 );
ts3 = timeseries(x(iz,3) , 0 );
ts4 = timeseries(x(iz,4) , 0 );
ts5 = timeseries(x(iz,5) , 0 );
ts6 = timeseries(x(iz,6) , 0 );
sim('model.mdl');
y = [y , norm(e,2)];
end
currentMaxProfit = -y;
y = [];
if currentMaxProfit > globalMaxProfit
globalBestCuckoo = currentBestCuckoo;
globalMaxProfit = currentMaxProfit;
end
% Update cost minimization plot
plot(iteration, -globalMaxProfit,'ks','linewidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
title([ 'Curent Cost = ' num2str(-globalMaxProfit) ' , at Iteration = ' num2str(iteration) ])
pause(0.01)
profitVector = [profitVector globalMaxProfit];
% ======== now we have some eggs that are safe and will grow up ==========
%========= mating: =============
% first we put all egg positions under each other
allPositions = [];
whichCuckooPopTheEggBelongs = [];
for cuckooNumber = 1:numCuckooS
allPositions = [allPositions; cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar)];
whichCuckooPopTheEggBelongs = [whichCuckooPopTheEggBelongs; cuckooNumber*ones(size(cuckooPop{cuckooNumber}.newPosition4Egg(:,1:npar),1),1) ];
end
if sum(var(allPositions)) < cuckooPopVariance
break
else
[clusterNumbers, clusterCenters] = kmeans(allPositions,knnClusterNum);
end
% make newly made clusters
cluster = cell(knnClusterNum,1);
for ii = 1:knnClusterNum
cluster{ii}.positions = [];
cluster{ii}.profits = [];
end
pointer = zeros(numel(cuckooPop),1);
for tmpCounter = 1:numel(cuckooPop)
nEggs = size(cuckooPop{tmpCounter}.newPosition4Egg,1);
pointer(tmpCounter) = nEggs;
end
for cnt = 1:length(clusterNumbers)
cluster{clusterNumbers(cnt)}.positions = [cluster{clusterNumbers(cnt)}.positions; cuckooPop{whichCuckooPopTheEggBelongs(cnt)}.newPosition4Egg(end-pointer(whichCuckooPopTheEggBelongs(cnt))+1,1:npar)];
cluster{clusterNumbers(cnt)}.profits = [cluster{clusterNumbers(cnt)}.profits; cuckooPop{whichCuckooPopTheEggBelongs(cnt)}.profitValues(end-pointer(whichCuckooPopTheEggBelongs(cnt))+1)];
pointer(whichCuckooPopTheEggBelongs(cnt)) = pointer(whichCuckooPopTheEggBelongs(cnt)) - 1;
end
% Determine the best cluster
f_mean = zeros(knnClusterNum,1);
for cnt = 1:knnClusterNum
f_mean(cnt) = mean(cluster{cnt}.profits);
end
[sorted_f_mean, sortingIndex_f_mean] = sort(f_mean,'descend');
maxFmean = sorted_f_mean(1); indexOfBestCluster = sortingIndex_f_mean(1);
% now that we know group with number 'indexOfBestCluster' is the best we
% should select their best point az Goal Point of other groups
[maxProfitInBestCluster, indexOfBestEggPosition] = max(cluster{indexOfBestCluster}.profits);
goalPoint = cluster{indexOfBestCluster}.positions(indexOfBestEggPosition,1:npar);
% now all other mature Cuckoos must go toward this goal point for laying
% their eggs
numNewCuckooS = 0;
for cntClstr = 1:size(cluster,1)
tmpCluster = cluster{cntClstr};
tmpPositions = tmpCluster.positions;
for cntPosition = 1:size(tmpPositions,1)
tmpPositions(cntPosition,:) = tmpPositions(cntPosition,:) + ...
motionCoeff * rand(1,npar) .* (goalPoint - tmpPositions(cntPosition,:));
end
% check if variables are in range
tmpPositions(find( tmpPositions>varHi )) = varHi;
tmpPositions(find( tmpPositions<varLo )) = varLo;
% update cluster positions
cluster{cntClstr}.positions = tmpPositions;
cluster{cntClstr}.center = mean(tmpPositions);
% update number of cuckoos: numCuckooS
numNewCuckooS = numNewCuckooS + size(cluster{cntClstr}.positions,1);
end
numCuckooS = numNewCuckooS;
% update cuckooPop
clear cuckooPop
cuckooPop = cell(numCuckooS,1);
cntNumCuckooS = 1;
for cnt = 1:size(cluster,1)
tmpCluster = cluster{cnt};
tmpPositions = tmpCluster.positions;
for cntPosition = 1:size(tmpPositions,1)
cuckooPop{cntNumCuckooS}.center = cluster{cnt}.positions(cntPosition,1:npar);
cntNumCuckooS = cntNumCuckooS + 1;
end
end
% Copy the Best cuckoo and its randomized form of this population to go
% to the next generation
currentBestCuckoo = bestCuckooCenter;
%currentMaxProfit = -feval(costFunction,currentBestCuckoo);
x = currentBestCuckoo;
for iz = 1:size(x,1)
ts1 = timeseries(x(iz,1) , 0 );
ts2 = timeseries(x(iz,2) , 0 );
ts3 = timeseries(x(iz,3) , 0 );
ts4 = timeseries(x(iz,4) , 0 );
ts5 = timeseries(x(iz,5) , 0 );
ts6 = timeseries(x(iz,6) , 0 );
sim('model.mdl');
y = [y , norm(e,2)];
end
currentMaxProfit = -y;
y = [];
if currentMaxProfit > globalMaxProfit
globalBestCuckoo = currentBestCuckoo;
globalMaxProfit = currentMaxProfit;
end
cuckooPop{end}.center = globalBestCuckoo; % This is because the best cuckoo will live longer and won't die right after egg laying
%cuckooPop{end}.profitValues = -feval(costFunction,cuckooPop{end}.center);
x = cuckooPop{end}.center;
for iz = 1:size(x,1)
ts1 = timeseries(x(iz,1) , 0 );
ts2 = timeseries(x(iz,2) , 0 );
ts3 = timeseries(x(iz,3) , 0 );
ts4 = timeseries(x(iz,4) , 0 );
ts5 = timeseries(x(iz,5) , 0 );
ts6 = timeseries(x(iz,6) , 0 );
sim('model.mdl');
y = [y , norm(e,2)];
end
cuckooPop{end}.profitValues = -y;
y = [];
tmp = rand(1,npar).*globalBestCuckoo;
tmp(find( tmp>varHi )) = varHi;
tmp(find( tmp<varLo )) = varLo;
cuckooPop{end-1}.center = tmp;
%cuckooPop{end-1}.profitValues = -feval(costFunction,cuckooPop{end-1}.center);
x = cuckooPop{end-1}.center;
for iz = 1:size(x,1)
ts1 = timeseries(x(iz,1) , 0 );
ts2 = timeseries(x(iz,2) , 0 );
ts3 = timeseries(x(iz,3) , 0 );
ts4 = timeseries(x(iz,4) , 0 );
ts5 = timeseries(x(iz,5) , 0 );
ts6 = timeseries(x(iz,6) , 0 );
sim('model.mdl');
y = [y , norm(e,2)];
end
cuckooPop{end-1}.profitValues = -y;
y = [];
end % end of while
%% Now Algorithm has stopped
disp('Best Params = ')
disp(globalBestCuckoo')
ts1.data = globalBestCuckoo(1);
ts2.data = globalBestCuckoo(2);
ts3.data = globalBestCuckoo(3);
ts4.data = globalBestCuckoo(4);
ts5.data = globalBestCuckoo(5);
ts6.data = globalBestCuckoo(6);
fprintf('\n')
disp('Cost = ')
disp(-globalMaxProfit)
% profit history is in: profitVector
costVector = - profitVector;
open_system('model.mdl');
sim('model.mdl');
figure
plot(costVector,'-ks','linewidth',3,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',15)
xlabel 'Cuckoo Iteration'
ylabel 'Cost Value'
title(['Current Cost = ' num2str(costVector(end)) ', at iteration = ' num2str(iteration) ])
A=ones(100,100);
toc