-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA.m
More file actions
330 lines (275 loc) · 11.6 KB
/
GA.m
File metadata and controls
330 lines (275 loc) · 11.6 KB
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
% Define the problem parameters
nAppropriatePositions = 200; % number of appropriate positions
sensingRange = 50; % sensing range of sensors
communicationRange = 100; % communication range of sensors
maxIteration = 100; % maximum number of iterations
nTargetPoints = 50; % number of target points
bsLocation = [250, 250]; % base station location
mutationProbability = 0.2; % mutation probability
% Define the GA parameters
npopulationSize = 200; % size of the population
population = zeros(npopulationSize,3);
GApopulationsize = 100;
generationSize = zeros(GApopulationsize,1);
fitness = zeros(GApopulationsize,1);
targetpoints = zeros(nTargetPoints,3);
eliteCount = 5; % number of elite solutions to keep
tournamentSize = 5; % size of the tournament for selection
crossoverProbability = 0.7; % probability of crossover
mutationRate = 0.1; % mutation rate
% Initialize the population of candidate solutions
for i = 1:nTargetPoints
for k = 1:3
targetpoints(i,k) = randi(500);
end
end
generation = zeros(npopulationSize,3,GApopulationsize); % Generation of diffrent Population
for i = 1:GApopulationsize
for j = 1: npopulationSize
for k = 1:3
generation(j,k,i)= randi(500);
end
end
end
%Initialising other variables
plot_nodes(generation(:,:,1),targetpoints,generation(:,:,1),sensingRange,"initial position");
for i = 1:maxIteration
% Evaluate the fitness of each solution using the objective functions
for j = 1:GApopulationsize
fitness(j,1) = evaluateFitness(generation(:,:,j), nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints);
end
% Sort the population by fitness
[~, idx] = sort(fitness, 'descend');
generation = generation(:, :, idx);
fitness = fitness(idx,1);
% Select the elite solutions
eliteGeneration = generation(:, :, 1:eliteCount);
% Perform selection using tournament selection
selectedGeneration = tournamentSelection(generation, fitness, tournamentSize, GApopulationsize - eliteCount,npopulationSize);
% Perform crossover
offspringGeneration = crossover(selectedGeneration, crossoverProbability,npopulationSize);
% Perform mutation
mutatedGeneration = mutation(offspringGeneration, mutationRate,fitness,GApopulationsize, nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints);
% Combine the elite and mutated populations
generation = cat(3, eliteGeneration, mutatedGeneration);
if i == 10 || i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i ==70 || i == 80 || i ==90
str1 = "Position after " ;
str2 = int2str(i);
str3 = " Iteration";
str12 = strcat(str1 , str2);
str = strcat(str12,str3);
plot_nodes(generation(:,:,1),targetpoints,generation(:,:,1),sensingRange,str );
end
end
plot_nodes(generation(:,:,1),targetpoints,generation(:,:,1),sensingRange,"final position");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%calculation for the final output (can skip it and come on it later)
population = generation(:,:,1);
positions = generation(:,:,1);
number_of_targets_covered =0;
for j = 1: nTargetPoints
for i = 1:npopulationSize
x1 = targetpoints(j,1);
y1 = targetpoints(j,2);
z1 = targetpoints(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= sensingRange
number_of_targets_covered = number_of_targets_covered +1;
break;
end
end
end
number_of_connection =0;
for i = 1:size(positions,1)
for j = 1:size(positions,1)
x1 = positions(j,1);
y1 = positions(j,2);
z1 = positions(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= 2* communicationRange
number_of_connection = number_of_connection +1;
end
end
end
overlap = 0;
for i = 1:size(positions, 1)
for j = i+1:size(positions, 1)
x1 = positions(j,1);
y1 = positions(j,2);
z1 = positions(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= 2*sensingRange
x = distance;
h = sensingRange - x/2;
volofcurve = (h*h*(3*sensingRange - h)*3.14)/3;
overlap = overlap + 2*(volofcurve);
end
end
end
popvol =( 3.14 *4* sensingRange * sensingRange * sensingRange * size(positions,1))/3;
number_of_node_covering_target =0;
for i = 1: npopulationSize
for j = 1:nTargetPoints
x1 = targetpoints(j,1);
y1 = targetpoints(j,2);
z1 = targetpoints(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= sensingRange
number_of_node_covering_target = number_of_node_covering_target +1;
end
end
end
final_number_of_nodes =number_of_node_covering_target /npopulationSize
final_target_cover_ratio = number_of_targets_covered / nTargetPoints
final_connection_ratio = number_of_targets_covered/(size(positions,1)*(size(positions,1)-1));
final_overlap_ratio = overlap/popvol;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define the tournament selection function
function selectedGeneration = tournamentSelection(Generation, fitness, tournamentSize, numSelections,npopulationSize)
selectedGeneration = zeros(npopulationSize,3,numSelections);
for i = 1:numSelections
% Choose the individuals for the tournament
tournamentIndividuals = randperm(size(Generation, 3), tournamentSize);
% Select the best individual from the tournament
[~, idx] = max(fitness(tournamentIndividuals));
% Add the selected individual to the new population
selectedGeneration(:, :, i) = Generation(:, :, tournamentIndividuals(idx));
end
end
% Define the crossover function
function offspringGeneration = crossover(parentGeneration, crossoverProbability,npopulationSize)
offspringGeneration = parentGeneration;
for i = 1:2:size(parentGeneration, 3)
% Perform crossover with a given probability
if i == size(parentGeneration, 3)
offspringGeneration(:,:,i) = parentGeneration(:, :, i);
break;
end
if rand < crossoverProbability
% Choose two parents for crossover
parent1 = parentGeneration(:, :, i);
parent2 = parentGeneration(:, :, i+1);
breakpnt= randi(npopulationSize);
parent11 = parent1(1:breakpnt,:);
parent12 = parent2(breakpnt+1:npopulationSize,:);
parent21 = parent2(1:breakpnt,:);
parent22 = parent1(breakpnt+1:npopulationSize,:);
offspringGeneration(:,:,i) = cat(1,parent11,parent12);
offspringGeneration(:,:,i+1)= cat(1,parent21,parent22);
% Perform crossover by selecting a random split point and
% swapping the positions of the two parents at and after that
% point
end
end
end
function mutatedGeneration = mutation(generation, mutationRate,fitness,GApopulationSize, nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints)
mutatedGeneration = generation;
npopulationSize = size(generation,1);
for i = 1:GApopulationSize
if rand()< mutationRate
mutantvector = 500*rand(npopulationSize,3);
for k = 1:npopulationSize
if mutantvector(k,1) > 500 || mutantvector(k,1) < 0
mutantvector(k,1) = randi(500);
end
if mutantvector(k,2) > 500 || mutantvector(k,2) < 0
mutantvector(k,2) = randi(500);
end
if mutantvector(k,3) > 500 || mutantvector(k,3) < 0
mutantvector(k,3) = randi(500);
end
end
fitness(i,1) = evaluateFitness(mutantvector, nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints);
end
end
end
%fitness finction to get the accuricy of each solution
% Define the fitness function
function fitness = evaluateFitness(positions, nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints)
[f1, f2, f3, f4] = eof(positions, nTargetPoints, sensingRange, communicationRange, bsLocation,targetpoints,size(positions,1));
w1 = 0.25;
w2 = 0.25;
w3 = 0.25;
w4 = 0.25;
fitness = w1*f1+w2*f2-w3*f3-w4*f4;
end
% Define the objective functions
function [f1, f2, f3, f4] = eof(positions, nTargetPoints, sensingRange, communicationRange, ~,targetpoints,possize)
reward = 100;
% Objective function 1: maximize the coverage of target points by sensors
number_of_targets_covered =0;
npopulationSize = possize;
for j = 1: nTargetPoints
for i = 1:npopulationSize
x1 = targetpoints(j,1);
y1 = targetpoints(j,2);
z1 = targetpoints(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= sensingRange
number_of_targets_covered = number_of_targets_covered +1;
break;
end
end
end
f1 =100*reward*number_of_targets_covered/nTargetPoints;
% Objective function 2: maximize the connectivity of sensors
% Objective function 3: minimize the overlap between sensors
overlap = 0;
number_of_connection =0;
for i = 1:possize
for j = i+1:possize
x1 = positions(j,1);
y1 = positions(j,2);
z1 = positions(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= 2 * sensingRange
x = distance;
h = sensingRange-x/2;
volofcurve = h*h*(3*sensingRange - h)*3.14/3;
overlap = overlap + 2*(volofcurve);
end
if distance <= 2*communicationRange
number_of_connection = number_of_connection +1;
end
end
end
number_of_connection = number_of_connection /2;
f2 =reward*number_of_connection/(possize*(possize-1));
popvol = 4*3.14 * sensingRange * sensingRange* sensingRange * possize/3;
f3 =reward*overlap/popvol;
% Objective function 4: minimize numer of sensing node
number_of_node_covering_target =0;
for i = 1: npopulationSize
for j = 1:nTargetPoints
x1 = targetpoints(j,1);
y1 = targetpoints(j,2);
z1 = targetpoints(j,3);
x2 = positions(i,1);
y2 = positions(i,2);
z2 = positions(i,3);
distance = sqrt( ( (x1-x2)*(x1-x2) ) + ( (y2-y1)*(y2-y1) )+ ( (z1-z2)*(z1-z2) ) );
if distance <= sensingRange
number_of_node_covering_target = number_of_node_covering_target +1;
end
end
end
f4 =reward*number_of_node_covering_target /npopulationSize;
end