-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossover.m
24 lines (22 loc) · 939 Bytes
/
crossover.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
% Crossover Function
function child = crossover(parents, popu_size, crossover_rate, min_a_start_index)
[num_parents, N] = size(parents);
child = zeros(popu_size, N);
for i = 1:2:popu_size
if rand < crossover_rate
parent1 = parents(randi(num_parents), :);
parent2 = parents(randi(num_parents), :);
crossover_location = randi([2 N]); % Start from index 2 to ensure min index a start
% Ordered crossover
child(i, :) = ORDERED_CROSSING(parent1, parent2, crossover_location, N, min_a_start_index);
if i+1 <= popu_size
child(i+1, :) = ORDERED_CROSSING(parent2, parent1, crossover_location, N, min_a_start_index);
end
else
child(i, :) = parents(randi(num_parents), :);
if i+1 <= popu_size
child(i+1, :) = parents(randi(num_parents), :);
end
end
end
end