-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparison_NEW.m
417 lines (368 loc) · 11.9 KB
/
comparison_NEW.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
%{
saTime = an array to store the execution times for simulated
annealing
gaTime = an array to store the execution times for genetic algorithm
randTime = an array to store the execution times for random algorithm
maxNoc = the maximum value to which the loop will run
ySa = costs for simulated annealing
YGa = costs for genetic algorithm
yRand = costs for random algorithm
populationSize = size of population
maxIterations = maximum number of iterations in genetic algorithm
%}
clc;
clear all;
close all;
fileName = 'Janetbackbone.graphml'; %network
% fileName = 'GERMANY50.graphml'; %network
% fileName = 'Iris.graphml'; %network
% fileName = 'TATANET.graphml'; %network
inputfile = fopen(fileName);
[topology,latlong,nodenames,mat,P] = importGraphML(fileName); %--Read GML file to find nodes and adjacency matrix-
s = size (mat);
n = s (1,2);
xx = zeros (1,2); %x values
saTime = zeros (1,2);
index = 1; %to keep track of the arrays
maxNoc = 4; %maximum number of controllers
maxIterations = 500; %maximum number of iterations
index = 1; %to keep track of the arrays
temps = zeros (2,2); %to store the temperatures
saCosts = zeros (2,2); %to store the respective costs
saIter = zeros (2,2); %to store the total number of temperature changes
randomControllers = zeros (maxNoc,maxNoc); %to store the first random combination of controllers taken by sa and ga
% yGerm = [80 69 63 54];
% Simulated Annealing by changing the positions of the controllers
ySa = zeros (1,2); %to store the costs
latSa = zeros (1,2); %to store the latencies
for cont = 1 : maxNoc %for each number of controllers
tic; %timer starts
% [controllers, cost] = simulatedAnnealing2 (mat, n, 10, 0.001, maxIterations, 0.95, cont); %perform simuated annealing
[controllers, cost, lat, loads, xTemps, yCosts, xxIts, counter, randCont, optConnSa] = simulatedAnnealing (mat, n, 10, 0.001, maxIterations, 0.95, cont); %perform simuated annealing
cont
cost
lat
controllers
loads
for j = 1 : cont %for each controller
optConnSa (1:cont,controllers(1,j)) = zeros(cont,1);
end
optConnSa
networkPlotFunction(optConnSa, fileName, controllers);
randomControllers (cont, 1:cont) = randCont; %this is needed for random algorithm
xx (1, index) = cont; %update the x values
ySa (1, index) = cost; %update the costs
latSa (1,index) = lat; %update the latency
temps (index, 1:counter) = xTemps;
saCosts (index, 1:counter) = yCosts;
saIter (index, 1:counter) = xxIts;
saTime (1, index) = toc; %update the execution time
index = index + 1; %increment the index
fprintf('\n\n');
end
index = 1; %reset the index
% genetic algorithm
yGa = zeros (1,2); %to store the costs
yCustom = zeros (1,2);
gaTime = zeros (1,2); %to store the execution times
populationSize = 20;
global mutProb; %counts till 100
global randomIterations; %generates the iterations in which mutation is to be done
mutProb = 0;
if (mutProb == 0) %if the count is 0
randomIterations = randi ([1 100],1,5); %then generate 5 iterations
randomIterations
end
for cont = 1 : maxNoc %for each no of controllers
tic; %timer starts
[optVal, optLoc, optLoad, returnPop, optConnGa, latVal] = modifiedGeneticAlgorithmImpl052 (mat, n, populationSize, maxIterations , cont);
cont
optVal
optLoc
xx(1,index) = cont;
yCustom (1,index) = latVal;
for j = 1 : cont %for each controller
optConnGa (1:cont,optLoc(1,j)) = zeros(cont,1);
end
optConnGa
networkPlotFunction(optConnGa, fileName, optLoc);
yGa (1, index) = optVal; %storing the best fitness value
gaTime (1, index) = toc; %update the execution times
index = index + 1; %increment the index
end
index = 1; %reset the index
%pso
psoTime = zeros (1,2); %to store the execution time
yPso = zeros (1,2); %to store the costs
latPso = zeros (1,2); %to store the latencies
c1 = 2;
c2 = 2;
r1 = 0.5;
r2 = 0.5;
w = 1;
wDamp = 0.98;
populationSize = 20;
for cont = 1 : maxNoc %for each number of controllers
tic;
[ optCost, optLoc, optLoad, optLat ] = psoFunc(maxIterations, c1, c2, r1, r2, w, wDamp, populationSize, mat, n, latlong, cont, returnPop);
yPso(1,index) = optCost;
latPso(1,index) = optLat;
cont
optLoc
optLoad
psoTime(1,index) = toc;
index = index+1;
fprintf('\n\n');
end
index = 1;
%random
randTime = zeros (1,2); %to store the execution times
yRand = zeros (1,2); %to store the costs
latRand = zeros (1,2); %to store the latencies;
for cont = 1 : maxNoc %for each number of controllers
tic; %timer starts
rc = randomControllers (cont, 1:cont); %generating random controllers
[l, c, y] = capacitedCostLatency (rc, mat, n); %finding out the cost
yRand (1, index) = c; %storing the costs
latRand (1, index) = y; %storing the latencies
cont
yRand (1, index)
latRand (1, index)
rc
l
randTime (1, index) = toc; %updating the execution times
index = index + 1; %incrementing the index
fprintf('\n\n');
end
index = 1; %reset the index
y = [yRand; ySa; yGa; yPso]; %merge the costs to a single matrix for bar plot
% y = [yRand; ySa; yGa]; %merge the costs to a single matrix for bar plot
figure; %bar plot for no of controllers vs costs
% bar (y',1);
% ylabel ('Fitness Function');
% xlabel ('Number of Controllers');
% legend ('Random','SA','GA');
h = bar(y',1);
% set(gca,'xticklabel',x);
% title ('Scenario- 1');
ylabel ('Cost');
xlabel ('No. Of Controllers');
legend ('Random','SA','GA','PSO');
% legend ('Random','SA','GA');
fH = gcf;
colormap(jet(4));
% Apply Brandon's function
% tH = title('Scenario- 1');
% applyhatch_pluscolor(fH, '\-x.', 0, [1 0 1 0], jet(4));
% set(tH, 'String', 'Scenario- 1');
t = [randTime; saTime; gaTime; psoTime]; %merge the times to a single matrix for bar plot
% t = [randTime; saTime; gaTime]; %merge the times to a single matrix for bar plot
figure; %bar plot for no of controllers vs execution times
% bar (t',1);
% ylabel ('Execution Time');
% xlabel ('Number of Controllers');
% legend ('Random','SA','GA');
bar(t',1);
% set(gca,'xticklabel',x);
% title ('Scenario- 1');
ylabel ('Execution Time');
xlabel ('No. of Controller');
legend ('Random','SA','GA','PSO');
% legend ('Random','SA','GA');
% Apply Brandon's function
% tH = title('Scenario- 1');
% applyhatch_pluscolor(fH2, '\-x.', 0, [1 0 1 0], jet(4));
figure; %line plot for no of controllers vs costs
plot (xx, yRand, 'm-x');
hold on;
plot (xx, ySa, 'b-s');
hold on;
plot (xx, yGa, 'g-o');
hold on;
plot (xx, yPso, 'c-*');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Cost');
legend ('Random','SA','GA','PSO');
% legend ('Random','SA','GA');
figure; %lne plot for no of controllers vs execution times
plot (xx, randTime, 'm-x');
hold on;
plot (xx, saTime, 'b-s');
hold on;
plot (xx, gaTime, 'g-o');
hold on;
plot (xx, psoTime, 'c-*');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Execution Time');
legend ('Random','SA','GA','PSO');
% legend ('Random','SA','GA');
%{
y2 = [yRand; ySa]; %merge the costs to a single matrix for bar plot
figure (1); %bar plot for no of controllers vs costs
h = bar (y2',1);
ylabel ('Cost');
xlabel ('Number of Controllers');
legend ('Random','SA');
fH = gcf;
colormap(jet(4));
y3 = [latRand; latSa]; %merge the latencies to a single matrix for bar plot
figure (2); %bar plot for no of controllers vs costs
bar (y3',1);
ylabel ('Latency');
xlabel ('Number of Controllers');
legend ('Random','SA');
t2 = [randTime; saTime]; %merge the times to a single matrix for bar plot
figure (3); %bar plot for no of controllers vs execution times
bar (t2',1);
ylabel ('Execution Time');
xlabel ('Number of Controllers');
legend ('Random','SA');
figure (4); %line plot for no of controllers vs costs
plot (xx, yRand, 'm-x');
hold on;
plot (xx, ySa, 'b-s');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Cost');
legend ('Random','SA');
figure (5); %line plot for no of controllers vs latencies
plot (xx, latRand, 'g-x');
hold on;
plot (xx, latSa, 'r-s');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Latency');
legend ('Random','SA');
figure (6); %line plot for no of controllers vs execution times
plot (xx, randTime, 'm-x');
hold on;
plot (xx, saTime, 'b-s');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Execution Time');
legend ('Random','SA');
%}
%{
yCustom
figure; %line plot for no of controllers vs costs
plot (xx, yGerm, 'g-o');
hold on;
plot (xx, yCustom, 'c-*');
set (gca, 'XTick',1:1:maxNoc);
xlabel ('Number of Controllers');
ylabel ('Average Propagation Delay in micro seconds');
% legend ('Random','SA','GA','PSO');
legend ('GAPA','Proposed GA');
%}
noOfControllers = 2;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 3;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
%{
noOfControllers = 4;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 5;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 6;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 7;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 8;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 9;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
noOfControllers = 10;
figure;
plot (temps (noOfControllers, 1:counter),saCosts (noOfControllers, 1:counter));
set (gca,'xDir','reverse');
xlabel ('Temperature');
ylabel ('Cost');
legend ('SA');
figure; %cost plot for SA
plot (saIter (noOfControllers, 1:counter) ,saCosts (noOfControllers, 1:counter));
xlabel ('No. of Iterations');
ylabel ('Cost');
legend ('SA');
%}