/
crossover.py
68 lines (52 loc) · 2.36 KB
/
crossover.py
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
from mutation import apply_rotation
from commons import inverted_dir_dict
from fitness_function import *
def crossover(protein,parents):
possible_rotations = [0,90,180,270]
is_invalid = True
while is_invalid:
is_invalid = False
p1 = choice(parents)
p2 = choice(parents)
parent1 = p1[0]
parent2 = p2[0]
offspring = []
cut_point = choice(range(len(protein)))
rotation_to_apply = choice(possible_rotations)
offspring += parent1[:cut_point+1]
pivot = offspring[cut_point]
offspring_tail = remap_parent2_coords(pivot,parent2[cut_point:],rotation_to_apply)
if offspring_tail == None:
is_invalid = True
else:
for c in offspring_tail:
if c in offspring:
is_invalid = True
break
else:
offspring += offspring_tail
new_ind = (offspring,fitness_function(protein,offspring))
return new_ind
def remap_parent2_coords(reference_am,pos_cut_point_parent2,angle_to_rotate):
remaped_parent = []
#gets the directons which the amino-acids are arranged
for i in range(len(pos_cut_point_parent2)-1):
remaped_parent.append((pos_cut_point_parent2[i+1][0] - pos_cut_point_parent2[i][0],pos_cut_point_parent2[i+1][1] - pos_cut_point_parent2[i][1]))
#applys random_rotation
for i in range(len(remaped_parent)):
remaped_parent[i] = apply_rotation(remaped_parent[i],angle_to_rotate)
#puts the new amino-acids configuraton according to the cut_point amino-acid
temp = [reference_am]
for i in range(len(remaped_parent)):
new_pos = (remaped_parent[i][0] + temp[i][0],remaped_parent[i][1] + temp[i][1])
if(new_pos in temp):
print "exists"
return None
else:
temp.append(new_pos)
remaped_parent = temp[1:]
return remaped_parent
#parent1 = ([(0,0),(1,0),(1,1),(1,2),(0,2),(0,1),(-1,1),(-1,2),(-2,2),(-2,3),(-3,3),(-3,2),(-3,1),(-2,1),(-2,0),(-2,-1),(-3,-1),(-3,-2),(-3,-3),(-2,-3)],0)
#parent2 = ([(0,0),(1,0),(1,-1),(1,-2),(1,-3),(1,-4),(0,-4),(-1,-4),(-1,-3),(-1,-2),(-2,-2),(-3,-2),(-3,-3),(-4,-3),(-5,-3),(-5,-2),(-6,-2),(-6,-3),(-6,-4),(-5,-4)],0)
#p = "BWBWWBBWBWWBWBBWWBWB"
#print crossover(p,[parent1,parent2])