-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScript.m
More file actions
113 lines (87 loc) · 3.19 KB
/
MainScript.m
File metadata and controls
113 lines (87 loc) · 3.19 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
figure;
nsteps = 200; %number of steps
nbots = 2; %number of bots
ngenerations = 1000;
nr = 10; %number of each resource
popsize = 100; %size of population, if nbots > 1, genotypes are considered together, e.g if nbots = 2, an individual in the population would be a pair.
pop1 = zeros(popsize,149,nbots); %Generates population of empty genotypes
pop2 = zeros(popsize,149,nbots); %Generates population of empty genotypes
% pop1 = pop1clone;
% pop2 = pop2clone;
bestP1 = 0; % initial fitness plot, which will be added to array for population 1
bestP2 = 0; % initial fitness plot, which will be added to array for population 2
bestP1i = []; % array fittnes plot for population 1
bestP2i = []; % array fitness plot for population 2
for i = 1 : popsize %Fill the populations
for j = 1 : nbots
pop1(i,:,j) = newbot;
pop2(i,:,j) = newbot;
end
end
for i = 1 : ngenerations %Run evolution
for j = 1 : popsize %Cycle through pop1
bestP1i = [bestP1i, bestP1];
%Circular selection within 3 spaces.
r = randi([-3, 3]);
if (j + r) > popsize
shift = (j+r) - popsize;
elseif (j + r) <1
shift = (j+r) + popsize;
else
shift = j + r;
end
ind1 = pop1(j,:,:);
ind2 = pop2(shift,:,:);
fit1 = run(nbots, nsteps, ind1, nr);
fit2 = run(nbots, nsteps, ind2, nr);
if fit1 > fit2
for k = 1 : nbots
pop2(shift,:,k) = botmutate(crossover(ind1(:,:,k),ind2(:,:,k)));
end
if fit1 > bestP1
bestP1 = fit1;
end
elseif fit1 < fit2
for k = 1: nbots
pop1(j,:,k) = botmutate(crossover(ind1(:,:,k),ind2(:,:,k)));
end
end
end
for j = 1 : popsize %Cycle through pop2
bestP2i = [bestP2i, bestP2];
%Circular selection within 3 spaces.
r = randi([-3, 3]);
if (j + r) > popsize
shift = (j+r) - popsize;
elseif (j + r) <1
shift = (j+r) + popsize;
else
shift = j + r;
end
ind1 = pop2(j,:,:);
ind2 = pop1(shift,:,:);
fit1 = run(nbots, nsteps, ind1, nr);
fit2 = run(nbots, nsteps, ind2, nr);
if fit1 > fit2
for k = 1 : nbots
pop1(shift,:,k) = botmutate(crossover(ind1(:,:,k),ind2(:,:,k)));
end
if fit1 > bestP2
bestP2 = fit1;
end
elseif fit1 < fit2
for k = 1: nbots
pop2(j,:,k) = botmutate(crossover(ind1(:,:,k),ind2(:,:,k)));
end
else
for k = 1: nbots
pop2(j,:,k) = botmutate(ind1(:,:,k));
pop1(shift,:,k) = botmutate(ind2(:,:,k));
end
end
end
end
plot(bestP1i)
hold on
plot(bestP2i)
hold off