-
Notifications
You must be signed in to change notification settings - Fork 0
/
gene.py
164 lines (117 loc) · 3.59 KB
/
gene.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import random
import sys
class Gene(object):
def __init__(self, current):
self.current = current
def get_current(self):
return self._current
class FloatGene(Gene):
def __init__(self, current, std, min, max):
self.std = std
self.min = min
self.max = max
Gene.__init__(self, current)
def mutate(self):
while True:
try:
rand_val = random.gauss(self.get_current(), self.std)
self.set_current(rand_val)
except:
e = sys.exc_info()[0]
continue
break
def randomize(self):
self.set_current(random.uniform(self.min, self.max))
def set_current(self, value):
if value < self.min or value > self.max:
raise ValueError()
self._current = value
current = property(Gene.get_current, set_current)
class IntGene(Gene):
def __init__(self, current, min, max):
self.min = min
self.max = max
self.std = (max - min + 1) / 2.0
Gene.__init__(self, current)
def mutate(self):
# taking into consideration if min == max
while True and (self.min != self.max):
try:
rand_val = int(round(random.gauss(self.get_current(), self.std)))
self.set_current(rand_val)
except:
e = sys.exc_info()[0]
continue
break
def randomize(self):
if self.min != self.max:
self.set_current(random.randint(self.min, self.max))
def set_current(self, value):
if value < self.min or value > self.max:
raise ValueError()
self._current = value
current = property(Gene.get_current, set_current)
class ListGene(Gene):
def __init__(self, current, values):
self.values = values
Gene.__init__(self, current)
def mutate(self):
new_value = random.choice(self.values)
# Do not set the new value to be the same as the current value
while new_value == self.get_current():
new_value = random.choice(self.values)
self.set_current(new_value)
def randomize(self):
self.set_current(random.choice(self.values))
def set_current(self, value):
if value not in self.values:
raise ValueError()
self._current = value
current = property(Gene.get_current, set_current)
class BoolGene(Gene):
def __init__(self, current):
Gene.__init__(self, current)
def mutate(self):
# Toggle the current value
self.set_current(not self.get_current())
def randomize(self):
self.set_current(bool(random.getrandbits(1)))
def set_current(self, value):
if not isinstance(value, (bool,)):
raise ValueError()
self._current = value
current = property(Gene.get_current, set_current)
class TupleGene(Gene):
def __init__(self, current, size, gene_list):
"""
gene_list: {list} A TupleGene is made up of other gene types.
The variable gene_list is a list of the genes that make
up the values in the tuple. Must be the length of the maximum
size of the tuple
size: {IntGene} The number of values in the tuple
current: {tuple} the default tuple
"""
self.size = size
self.gene_list = gene_list
Gene.__init__(self, current)
def mutate(self):
tuple_values = []
self.size.mutate()
for i in range(self.size.current):
self.gene_list[i].mutate()
while self.gene_list[i].current == 0:
self.gene_list[i].mutate()
tuple_values.append(self.gene_list[i].current)
self.set_current(tuple(tuple_values))
def randomize(self):
tuple_values = []
self.size.randomize()
for i in range(self.size.current):
self.gene_list[i].randomize()
while self.gene_list[i].current == 0:
self.gene_list[i].randomize()
tuple_values.append(self.gene_list[i].current)
self.set_current(tuple(tuple_values))
def set_current(self, value):
self._current = value
current = property(Gene.get_current, set_current)