-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutators.py
More file actions
118 lines (105 loc) · 3.78 KB
/
Mutators.py
File metadata and controls
118 lines (105 loc) · 3.78 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 as random
import string
"""
Mutators: Class that contains the different mutating operators
as subclasses. Thie first subclass will use a coin flip to determine
which elements will be switched. The sucond subclass will simply
perfom a random flip of elements in the solution.
"""
class Mutators:
"""
__init__
Constructor of the parent Mutators class
parameters:
self - Pointer to the object
dataset - A copy of the dataset object
returns:
-None-
"""
def __init__(self, dataset):
self.dataset = dataset
"""
ExpMutator: Mutator subclass that implements the Exponential
mutation this based on the suggestions of the supervisor
"""
class ExpMutator(Mutators):
"""
__init__
Constructor of the Exponential mutator subclass
parameters:
self - Pointer to the object
dataset - A copy of the dataset object
prob - Probability of mutation
returns:
-None-
"""
def __init__(self, dataset, prob):
self.dataset = dataset
self.prob = prob
"""
mutate
Method that implements the exponential mutation on the
current solution to the problem
parameters:
self - Pointer to the object
solution - A list with the current solution to the problem
returns:
solution - An updated list with the solution after undergoing mutation
"""
def mutate(self, solution):
i = random.randint(0, len(solution)-1)
curr = i
coin = random.uniform(0, 1)
alphabet_per_column = self.dataset.get_alphabet_per_column()
while (curr < len(solution) and coin < self.prob):
solution[curr] = random.choice(alphabet_per_column[curr])
coin = random.uniform(0, 1)
#To avoid always considering the final element
#if the method reaches the third from last element
#consider the following options equally:
#go to second from last, and then either to the end or finish
#go to the last element and then finish
#finish completely
if (curr < len(solution)-2):
curr = random.randint(curr+1, len(solution))
if (curr == len(solution)) :break
else:
break
return solution
"""
RandomFlip: Mutator subclass that implements the Random Flip
mutation
"""
class RandomFlip(Mutators):
"""
__init__
Constructor of the Random Flip mutator subclass
parameters:
self - Pointer to the object
dataset - A copy of the dataset object
prob - Probability of mutation
returns:
-None-
"""
def __init__(self, dataset, prob):
self.dataset = dataset
self.prob = prob
"""
mutate
Method that implements the random flip mutation on the
current solution to the problem
parameters:
self - Pointer to the object
solution - A list with the current solution to the problem
returns:
solution - An updated list with the solution after undergoing mutation
"""
def mutate(self, solution):
i = 0
alphabet_per_column = self.dataset.get_alphabet_per_column()
while (i < len(solution)):
p = random.uniform(0, 1)
if (p < self.prob):
solution[i] = random.choice(alphabet_per_column[i])
i=i+1
return solution