-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateChildrens.m
72 lines (59 loc) · 2.3 KB
/
GenerateChildrens.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
function childrens = GenerateChildrens(parents, parents_cost,approach)
%GENERATECHILDRENS this function gets parents and generate childrens
%
children_number = 10;
[r, c] = size(parents);
childrens = zeros(r,children_number);
% parents_cost = 1 ./ parents_cost; % this is another way
if (approach == 1)
parents_cost = max(parents_cost) - parents_cost + 1 ; % +1 is for the situation that every costs are equal to avoid /0
accumulate_cost = sum(parents_cost);
parents_probability = parents_cost / accumulate_cost;
[max_p,index] = max(parents_probability);
for i = 1:children_number
selected_sequence = zeros(1,r);
p = rand(1,r);
selected = 0;
probs = 0;
for j = parents_probability
selected = selected + 1;
selected_sequence = selected_sequence + ( (p > probs) & (p <= probs + j) ) * selected;
probs = probs + j;
end
new_child = (selected_sequence == index) .* transpose(parents(:,index)) ;
counter = 1;
for j = selected_sequence
if (j ~= index)
for k = transpose(parents(:,j))
if (~any(new_child == k))
new_child(counter) = k;
break;
end
end
end
counter = counter + 1;
end
% Mutation
random_number = randi([1,r],1,2);
temp = new_child(random_number(1));
new_child(random_number(1)) = new_child(random_number(2));
new_child(random_number(2)) = temp;
childrens(:,i) = new_child';
end
elseif (approach == 2)
for i = 1:children_number
[max_p,index] = min(parents_cost);
break_points = randi([1,r],1,2);
new_child = parents(:,index);
new_child(min(break_points):max(break_points)) = flip (new_child(min(break_points):max(break_points)));
parents(:,index) = [];
parents_cost(index) = [];
% Mutation
random_number = randi([1,r],1,2);
temp = new_child(random_number(1));
new_child(random_number(1)) = new_child(random_number(2));
new_child(random_number(2)) = temp;
childrens(:,i) = new_child';
end
end
end