-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordercrossover.m
63 lines (53 loc) · 1.07 KB
/
ordercrossover.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
function [offspring1,offspring2] = ordercrossover(parent1,parent2)
cities = length(parent1);
offspring1 = zeros(1,cities);
offspring2 = zeros(1,cities);
p = randperm(cities,2);
if p(1)< p(2)
i = p(1);
j = p(2);
else
i = p(2);
j = p(1);
end
for k = i:j
offspring1(k)=parent1(k);
offspring2(k)=parent2(k);
end
k =1;
l =1;
w = ismember(0,offspring1);
while w == 1
w = ismember(0,offspring1);
z = ismember(parent2(k),offspring1);
if z ==0
if l< i && l > j
offspring1(l) = parent2(k);
k = k + 1;
l = l + 1;
else
l = l+1;
end
else
k = k + 1;
end
end
k =1;
l =1;
w = ismember(0,offspring2);
while w == 1
w = ismember(0,offspring2);
z = ismember(parent1(k),offspring2);
if z ==0
if l< i && l > j
offspring2(l) = parent1(k);
k = k + 1;
l = l + 1;
else
l = l+1;
end
else
k = k + 1;
end
end
end