-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustom_Genetic_Algorithm.m
More file actions
173 lines (140 loc) · 4.46 KB
/
Custom_Genetic_Algorithm.m
File metadata and controls
173 lines (140 loc) · 4.46 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
clear all;
clc;
% INITIALIZE
POPULATION_SIZE = 2000;
MAX_GEN = 1000;
pm = 0.1; % probability of mutation
pc = 0.9; % probability of crossover
alpha = 2; % blend crossover hyperparameter
candidates = 100*rand(POPULATION_SIZE,2);
%candidates = [3 3; 2 2; 9 9; 5 5];
%% BEGIN EVOLUTION
tic;
best = zeros(MAX_GEN,2);
worst = zeros(MAX_GEN,2);
for g = 1:1:MAX_GEN
fitnesses = fitness(candidates);
%% Rank candidates according to fitness score
[out,idx] = sort(fitnesses, 'ascend');
fitnesses = fitnesses(idx);
candidates = candidates(idx,:);
% track best and worst candidate in each gen
best(g,:) = candidates(1,:);
worst(g,:) = candidates(POPULATION_SIZE,:);
%% SELECTION - Rank Based Selection
weights = 1./(fitnesses+1);
weights = weights./(sum(weights));
selected_candidates = zeros(POPULATION_SIZE,2);
ids = 1:1:POPULATION_SIZE;
for i = 1:1:POPULATION_SIZE
ix = randsample(ids,1,true,weights);
selected_candidates(i,:) = candidates(ix,:);
end
%% CROSSOVER - Blend Crossover
offsprings = zeros(POPULATION_SIZE,2);
for i = 1:2:POPULATION_SIZE
parent_1 = selected_candidates(i,:);
parent_2 = selected_candidates(i+1,:);
if rand() < pc
% blend crossover
[child_1, child_2] = blendCrossover(alpha, parent_1, parent_2);
else
child_1 = parent_1;
child_2 = parent_2;
end
offsprings(i,:) = child_1;
offsprings(i+1,:) = child_2;
end
%% MUTATION
for i = 1:1:POPULATION_SIZE
if rand() < pm
% mutate
gi = randi(2);
u = offsprings(i,gi);
sd = 0.5;
gene = sd*randn() + u;
offsprings(i,gi) = gene;
%offsprings(i,:) = mutate(offsprings(i,:));
end
end
%% CYCLE
candidates = offsprings;
end
toc;
%% ANALYSIS
figure();
hold on;
plot(worst(:,1));
plot(ones(MAX_GEN));
title('gene 1 of worst candidate in each generation');
xlabel('generation');
ylabel('gene value');
%ylabel(['average ' num2str(mean(worst(:,1)))]);
legend('gene 1 of worst candidate','global optimum gene 1');
hold off;
%disp(mean(worst(:,1)));
figure();
hold on;
plot(worst(:,2));
plot(ones(MAX_GEN));
title('gene 2 of worst candidate in each generation');
xlabel('generation');
ylabel('gene value');
%ylabel(['average ' num2str(mean(worst(:,2)))]);
legend('gene 2 of worst candidate','global optimum gene 2');
hold off;
%disp(mean(worst(:,2)));
figure();
hold on;
plot(best(:,1));
plot(ones(MAX_GEN));
title('gene 1 of best candidate in each generation');
xlabel('generation');
ylabel('gene value');
%ylabel(['average ' num2str(mean(best(:,1)))]);
legend('gene 1 of best candidate','global optimum gene 1');
hold off;
%disp(mean(best(:,1)));
figure();
hold on;
plot(best(:,2));
plot(ones(MAX_GEN));
title('gene 2 of best candidate in each generation');
xlabel('generation');
ylabel('gene value');
%ylabel(['average ' num2str(mean(best(:,2)))]);
legend('gene 2 of best candidate','global optimum gene 2');
%disp(mean(best(:,2)));
disp(['last best candidate solution: (' num2str(best(end,1)) ', ' num2str(best(end,2)) ')']);
disp(['average worst candidate: (' num2str(mean(worst(:,1))) ', ' num2str(mean(worst(:,2))) ')']);
disp(['average best candidate: (' num2str(mean(best(:,1))) ', ' num2str(mean(best(:,2))) ')']);
%% Function Defs
function y = fitness(x)
y = arrayfun(@rosenbrock,x(:,1),x(:,2));
end
function y = rosenbrock(x1,x2)
y = 100*((x2-(x1^2))^2) + (1-x1)^2;
end
% Blend Crossover
function [child_1, child_2] = blendCrossover(alpha, parent_1, parent_2)
child_1 = zeros(1,2);
child_2 = zeros(1,2);
% blending first gene only
edge_1 = parent_1(1) - alpha*( parent_2(1) - parent_1(1) );
edge_2 = parent_2(1) + alpha*( parent_2(1) - parent_1(1) );
a = min( edge_1, edge_2 );
b = max( edge_1, edge_2 );
g1 = (b-a)*rand() + a;
g2 = (b-a)*rand() + a;
child_1(1) = g1;
child_2(1) = g2;
% blending second gene only
edge_1 = parent_1(2) - alpha*( parent_2(2) - parent_1(2) );
edge_2 = parent_2(2) + alpha*( parent_2(2) - parent_1(2) );
a = min( edge_1, edge_2 );
b = max( edge_1, edge_2 );
g1 = (b-a)*rand() + a;
g2 = (b-a)*rand() + a;
child_1(2) = g1;
child_2(2) = g2;
end