-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgp.py
208 lines (169 loc) · 6.34 KB
/
cgp.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import numpy as np
from PIL import Image
import math
import random
import copy
from settings import VERBOSE, N_COLS, LEVEL_BACK
'''
remove weight
'''
def zero(x, y):
return x
def one(x, y):
return y
def five(x, y):
value = 0
value += math.cos(2 * math.pi * x / 255.0)
value += math.sin(2 * math.pi * y / 255.0)
value = 255 * abs(value) / 2.0
return int(value)
def six(x, y):
value = 0
value += math.cos(3 * math.pi * x / 255.0)
value += math.sin(2 * math.pi * y / 255.0)
value = 255 * abs(value) / 2.0
return int(value)
def nine(x, y):
value = 0
value += math.cosh(x + y) % 256
return int(value)
def thirteen(x, y):
value = 0
value += 255 * abs(math.tan((x + y) * math.pi / (8.0 * 255)))
return int(value)
class Function:
def __init__(self, f, arity):
self.f = f
self.arity = arity
def __call__(self, *args, **kwargs):
return self.f(*args, **kwargs)
"""
Node in CGP Graph
"""
class Node:
def __init__(self, max_arity):
self.i_func = None
self.i_inputs = [None] * max_arity
'''
weight
'''
# self.weights = [None] * max_arity
self.i_output = None
self.output = None
self.active = False
"""
(chromosome, genotype, etc) in evolution
"""
class Individual:
function_set = None
# weight_range = [-1, 1]
max_arity = 2
n_inputs = 2
n_outputs = 3
n_cols = N_COLS
level_back = LEVEL_BACK
def __init__(self):
self.nodes = []
for pos in range(0, self.n_cols):
self.nodes.append(self._create_random_node(pos))
for i in range(1, self.n_outputs + 1):
self.nodes[-i].active = True
self.fitness = None
self._active_determined = False
def _create_random_node(self, pos):
node = Node(self.max_arity)
temp_func = random.randint(0, len(self.function_set) - 1)
node.i_func = random.uniform(temp_func / len(self.function_set), (temp_func + 1)/ len(self.function_set))
for i in range(self.function_set[math.floor(node.i_func * len(self.function_set))].arity):
'''
rethink
'''
temp_in = random.randint(max(pos - self.level_back, -self.n_inputs), pos - 1)
node.i_inputs[i] = random.uniform(temp_in / (pos + self.n_inputs), (temp_in + 1) / (pos + self.n_inputs))
'''
weight
'''
# node.weights[i] = random.uniform(self.weight_range[0], self.weight_range[1])
node.i_output = pos
return node
'''
determine which nodes are active
'''
def _dermine_activate_nodes(self):
n_active = 0
for pos in range(len(self.nodes)-1, -1, -1):
node = self.nodes[pos]
if node.active:
n_active += 1
for i in range(self.function_set[math.floor(node.i_func * len(self.function_set))].arity):
i_input = math.floor(node.i_inputs[i] * (pos + self.n_inputs))
if i_input >= 0:
self.nodes[i_input].active = True
if VERBOSE:
print("# active genes:", n_active)
'''
compute
'''
def eval(self, *args):
if not self._active_determined:
self._dermine_activate_nodes()
self._active_determined = True
# forward pass: compute
for pos in range(len(self.nodes)):
node = self.nodes[pos]
if node.active:
inputs = []
for i in range(self.function_set[math.floor(node.i_func * len(self.function_set))].arity):
i_function = math.floor(node.i_func * len(self.function_set))
i_input = math.floor(node.i_inputs[i] * (pos + self.n_inputs))
# w = node.weights[i]
if i_input < 0:
inputs.append(args[-i_input - 1])
else:
inputs.append(self.nodes[i_input].output)
node.output = self.function_set[math.floor(node.i_func * len(self.function_set))](*inputs)
return [node.output for node in self.nodes[-3:]]
'''
mutate
'''
def mutate(self, mut_rate = 0.01):
child = copy.deepcopy(self)
for pos, node in enumerate(child.nodes):
if random.random() < mut_rate:
temp_func = random.choice(range(len(self.function_set)))
node.i_func = random.uniform(temp_func / len(self.function_set), (temp_func + 1)/ len(self.function_set))
i_function = math.floor(node.i_func * len(self.function_set))
arity = self.function_set[i_function].arity
for i in range(arity):
if node.i_inputs[i] is None or random.random() < mut_rate:
temp_in = random.randint(max(pos - self.level_back, -self.n_inputs), pos - 1)
node.i_inputs[i] = random.uniform(temp_in / (pos + self.n_inputs), (temp_in + 1) / (pos + self.n_inputs))
# if node.weights[i] is None or random.uniform(self.weight_range[0], self.weight_range[1])
node.active = False
for i in range(1, self.n_outputs + 1):
child.nodes[-i].active = True
child.fitness = None
child._active_determined = False
return child
'''
crossover
'''
def cross_over(self, parentb, cross_rate = 0.5):
child = Individual()
for pos, node in enumerate(child.nodes):
node.i_func = cross_rate * self.nodes[pos].i_func + (1-cross_rate) * parentb.nodes[pos].i_func
i_function = math.floor(node.i_func * len(self.function_set))
arity = self.function_set[i_function].arity
for i in range(arity):
node.i_inputs[i] = cross_rate * self.nodes[pos].i_inputs[i] + (1-cross_rate) * parentb.nodes[pos].i_inputs[i]
node.active = False
for i in range(1, self.n_outputs + 1):
child.nodes[-i].active = True
child.fitness = None
child._active_determined = False
return child
fs = [Function(zero, 2), Function(one, 2), Function(five, 2), Function(six, 2), Function(nine, 2), Function(thirteen, 2)]
Individual.function_set = fs
Individual.max_arity = max(f.arity for f in fs)
def create_population(n):
return[Individual() for _ in range(n)]