Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31d57f4

Browse files
committed
working on TSP crossover
1 parent 99705fc commit 31d57f4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crossover.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
3+
A = [1, 2, 3, 4, 5, 6, 7, 8]
4+
B = [2, 4, 6, 8, 7, 5, 3, 1]
5+
6+
def cycle_crossover(A, B):
7+
n = len(A)
8+
child = [None] * n
9+
case = random.choice([0,1])
10+
case = 0
11+
if(case == 0):
12+
# the first cycle
13+
child[0] = A[0]
14+
cycle = [A[0]]
15+
n = B.index(A[0])
16+
child[n] = A[n]
17+
cycle.append(A[n])
18+
while(cycle[-1] != cycle[0]):
19+
n = B.index(cycle[-1])
20+
child[n] = A[n]
21+
cycle.append(A[n])
22+
print(cycle_1)
23+
24+
print("the end")
25+
print(child)
26+
27+
28+
cycle_crossover(A, B)
29+
30+

0 commit comments

Comments
 (0)