-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutation.py
More file actions
116 lines (99 loc) · 4.06 KB
/
Copy pathMutation.py
File metadata and controls
116 lines (99 loc) · 4.06 KB
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
116
import random
import abc
class Mutation(abc.ABC) :
# Initialization
def __init__(self,mutationRate) :
self.mutationRate = mutationRate
# Checks if can be mutate according to rate of mutation
@abc.abstractmethod
def CanMutate(self) :
pass
@abc.abstractmethod
# Runs mutation
def RunMutation(self,chromosome) :
pass
class Swap(Mutation) :
def CanMutate(self) :
randomValue = random.random()
if randomValue < self.mutationRate :
return True
else :
return False
def RunMutation(self,chromosome) :
if self.CanMutate() :
length = len(chromosome.gensList)
firstRandomIndex = random.randrange(0,length)
secondRandomIndex = random.randrange(0,length)
while True :
if firstRandomIndex != secondRandomIndex :
break
else :
secondRandomIndex = random.randrange(0,length)
temp = chromosome.gensList[firstRandomIndex]
chromosome.gensList[firstRandomIndex] = chromosome.gensList[secondRandomIndex]
chromosome.gensList[secondRandomIndex] = temp
class Insert(Mutation) :
def CanMutate(self) :
randomValue = random.random()
if randomValue < self.mutationRate :
return True
else :
return False
def RunMutation(self,chromosome) :
if self.CanMutate() :
length = len(chromosome.gensList)
temp = []
tempIndex = 0
firstRandomIndex = random.randrange(0,length-1)
secondRandomIndex = random.randrange(0,length)
while True :
if firstRandomIndex != secondRandomIndex :
break
else :
secondRandomIndex = random.randrange(0,length)
secondGen = chromosome.gensList[secondRandomIndex]
for index in range(0,secondRandomIndex) :
temp.append(chromosome.gensList[index])
for index in range(secondRandomIndex + 1 , length) :
temp.append(chromosome.gensList[index])
for index in range(length) :
if index == firstRandomIndex + 1 :
chromosome.gensList[index] = secondGen
else :
chromosome.gensList[index] = temp[tempIndex]
tempIndex += 1
class Inversion(Mutation) :
def CanMutate(self) :
randomValue = random.random()
if randomValue < self.mutationRate :
return True
else :
return False
def RunMutation(self,chromosome) :
if self.CanMutate() :
length = len(chromosome.gensList)
temp = []
tempIndex = None
tempGen = None
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
if firstRandomIndex + 1 == secondRandomIndex :
tempGen = chromosome.gensList[firstRandomIndex]
chromosome.gensList[firstRandomIndex] = chromosome.gensList[secondRandomIndex]
chromosome.gensList[secondRandomIndex] = tempGen
else :
for index in range(firstRandomIndex,secondRandomIndex + 1) :
temp.append(chromosome.gensList[index])
tempIndex = len(temp) - 1
for index in range(firstRandomIndex,secondRandomIndex + 1) :
chromosome.gensList[index] = temp[tempIndex]
tempIndex -= 1