-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecombination.py
115 lines (95 loc) · 4.15 KB
/
Recombination.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import random
import copy
import abc
class Recombination(abc.ABC) :
# Initialization
def __init__(self,recombinationRate) :
self.recombinationRate = recombinationRate
# Checks if can be recombinate according to rate of recombination
@abc.abstractmethod
def CanRecombinate(self) :
pass
# Runs recombination according to rate and type
@abc.abstractmethod
def RunRecombination(self,chromosome1,chromosome2) :
pass
class RandomPointCrossOver(Recombination) :
def CanRecombinate(self) :
randomValue = random.random()
if randomValue < self.recombinationRate :
return True
else :
return False
# 1 point crossover avoiding duplicated gens (random point)
def RunRecombination(self,chromosome1,chromosome2) :
if self.CanRecombinate() :
length = len(chromosome1.gensList)
startIndex = 0
targetIndex = random.randrange(1,length)
tempChromosome = copy.deepcopy(chromosome1)
CopyRemainUnique(chromosome1,chromosome2,startIndex,targetIndex,length)
CopyRemainUnique(chromosome2,tempChromosome,startIndex,targetIndex,length)
class HalfPointCrossOver(Recombination) :
def CanRecombinate(self) :
randomValue = random.random()
if randomValue < self.recombinationRate :
return True
else :
return False
# 1 point crossover avoiding duplicated gens (half point)
def RunRecombination(self,chromosome1,chromosome2) :
if self.CanRecombinate() :
length = len(chromosome1.gensList)
startIndex = 0
targetIndex = int(length/2)
tempChromosome = copy.deepcopy(chromosome1)
CopyRemainUnique(chromosome1,chromosome2,startIndex,targetIndex,length)
CopyRemainUnique(chromosome2,tempChromosome,startIndex,targetIndex,length)
class OrderOneCrossOver(Recombination) :
def CanRecombinate(self) :
randomValue = random.random()
if randomValue < self.recombinationRate :
return True
else :
return False
# 1 point crossover avoiding duplicated gens (half point)
def RunRecombination(self,chromosome1,chromosome2) :
if self.CanRecombinate() :
length = len(chromosome1.gensList)
firstRandomIndex = random.randrange(0,length)
secondRandomIndex = random.randrange(0,length)
while True :
if firstRandomIndex != secondRandomIndex :
break
else :
secondRandomIndex = random.randrange(0,length)
if firstRandomIndex > secondRandomIndex :
tempIndex = firstRandomIndex
firstRandomIndex = secondRandomIndex
secondRandomIndex = tempIndex
startIndex = firstRandomIndex
targetIndex = secondRandomIndex
tempChromosome = copy.deepcopy(chromosome1)
CopyRemainUnique(chromosome1,chromosome2,startIndex,targetIndex,length)
CopyRemainUnique(chromosome2,tempChromosome,startIndex,targetIndex,length)
# Functions
# Copies remain part of origin chromosome to destination chromosome avoiding duplicated gens.
def CopyRemainUnique(destination,origin,startIndex,targetIndex,length) :
destinationConstantPart = CopySection(destination,startIndex,targetIndex)
originIndex = targetIndex
destinationIndex = targetIndex
while destinationIndex != startIndex :
if origin.gensList[originIndex] not in destinationConstantPart :
destination.gensList[destinationIndex] = origin.gensList[originIndex]
destinationIndex += 1
originIndex += 1
if originIndex == length :
originIndex = 0
if destinationIndex == length :
destinationIndex = 0
# Copies a section of list from start index until limit
def CopySection(chromosome,start,limit) :
copied = []
for index in range(start,limit) :
copied.append(chromosome.gensList[index])
return copied