-
Notifications
You must be signed in to change notification settings - Fork 0
/
CXCrossOver.py
30 lines (24 loc) · 1010 Bytes
/
CXCrossOver.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
from CrossOver import CrossOver
from random import randrange
class CXCrossOver(CrossOver):
def __init__(self, firstParent, secondParent):
super().__init__(firstParent, secondParent)
def cross(self):
beginingPointIndex = randrange(len(self.firstParent))
beginingPoint = self.firstParent[beginingPointIndex]
firstParentIndexes = [beginingPointIndex]
secondPoint = -1
firstPointIndex = beginingPointIndex
while secondPoint != beginingPoint:
secondPoint = self.secondParent[firstPointIndex]
for i in range(len(self.firstParent)):
if self.firstParent[i] == secondPoint:
firstPointIndex = i
firstParentIndexes.append(i)
child = []
for i in range(len(self.firstParent)):
if i in firstParentIndexes:
child.append(self.firstParent[i])
else:
child.append(self.secondParent[i])
return child