-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevolve_sage_create.py
More file actions
346 lines (301 loc) · 11.3 KB
/
evolve_sage_create.py
File metadata and controls
346 lines (301 loc) · 11.3 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from evolve_sage_optimize import *
from scipy.stats import entropy
tm = SageVirtualMachine([
Function('add', [
Dereference([
Restore(),
Move('R'),
Add(),
PutInt()
]),
]),
SetTape(1),
Move('R'),
SetRegister(2),
Save(),
Move('R'),
SetTape(1),
SetTape(100),
Dereference([
SetTape(10),
Move('R'),
SetTape(9),
]),
Call('add'),
Save(),
])
# Genome looks like a list of numbers like so:
# [1, 2, 3, [4, 5, [6, 7], [8, 9], 10], 11, 12, [[13, 14, 15], 16, [17]]]
# This is a list of operations, where the numbers are the indices of the operations.
# The lists of operations are the blocks of operations.
class Genome:
def __init__(self, operations, genome=None):
self.operations = operations
if genome is None:
self.genome = []
else:
self.genome = genome
def get_size(self):
total = 0
for gene in self.genome:
if type(gene) == list:
total += Genome(self.operations, gene).get_size()
else:
total += 1
return total
def random(operations, length=100):
genome, _ = gen_random_genome(length)
return Genome(operations, genome)
def get_operation(self, index):
return self.operations[index]
def mutate(self, mutation_rate):
self._fitness = None
if random.random() < 0.5:
for i in range(len(self.genome)):
if random.random() < mutation_rate:
if type(self.genome[i]) == list:
self.genome[i] = list(Genome(self.operations, deepcopy(self.genome[i])).mutate(mutation_rate).genome)
else:
self.genome[i] = random.randint(0, len(self.operations) - 1)
else:
for i in range(random.randint(1, 5)):
if random.random() < 0.5:
if random.random() < 0.5:
self.insert_random_gene()
else:
self.remove_random_gene()
else:
if random.random() < 0.5:
self.swap_random_gene()
else:
self.modify_random_gene()
return self
def crossover(self, other):
# Randomly select a crossover point.
index = random.randint(0, min(len(self.genome) - 1, len(other.genome) - 1))
# Create two new genomes by swapping the genes after the crossover point.
return Genome(self.operations, self.genome[:index] + other.genome[index:]), Genome(self.operations, other.genome[:index] + self.genome[index:])
def crossover_splits(self, other):
result = []
for a, b in zip(self.genome, other.genome):
if type(a) == list and type(b) == list:
result.append(Genome(self.operations, a).crossover_splits(Genome(self.operations, b)).genome)
else:
if random.random() < 0.5:
result.append(a)
else:
result.append(b)
return Genome(self.operations, result)
def remove_random_gene(self):
for i in range(len(self.genome)):
if random.random() < 2 / len(self.genome):
if type(self.genome[i]) == list:
if random.random() < 0.5:
self.genome[i] = list(Genome(self.operations, self.genome[i]).remove_random_gene().genome)
else:
del self.genome[i]
else:
del self.genome[i]
return self
return self
def insert_random_gene(self):
for i in range(len(self.genome)):
if random.random() < 2 / len(self.genome):
if type(self.genome[i]) == list:
self.genome[i] = list(Genome(self.operations, self.genome[i]).insert_random_gene().genome)
else:
self.genome.insert(i, random.randint(0, len(self.operations) - 1))
return self
self.genome.append(random.randint(0, len(self.operations) - 1))
return self
def swap_random_gene(self):
for i in range(len(self.genome)):
if random.random() < 2 / len(self.genome):
if type(self.genome[i]) == list:
self.genome[i] = list(Genome(self.operations, self.genome[i]).swap_random_gene().genome)
else:
self.genome[i] = random.randint(0, len(self.operations) - 1)
return self
return self
def modify_random_gene(self):
for i in range(len(self.genome)):
if random.random() < 2 / len(self.genome):
if type(self.genome[i]) == list:
self.genome[i] = list(Genome(self.operations, self.genome[i]).modify_random_gene().genome)
else:
self.genome[i] = min(self.genome[i] + random.randint(-1, 1), len(self.operations) - 1)
return self
return self
def into_operations(self):
func_count = 0
operations = []
for operation in self.genome:
if type(operation) == list:
# Get whether this is supposed to be a loop, deref, or function
if len(operation) == 0:
continue
if type(operation[0]) == list:
operations.extend(Genome(self.operations, operation[0]).into_operations())
operations.extend(Genome(self.operations, operation[1:]).into_operations())
continue
operation_type = self.get_operation(operation[0])
if type(operation_type) == WhileLoop:
operations.append(WhileLoop(Genome(self.operations, operation[1:]).into_operations()))
if type(operation_type) == ForLoop:
operations.append(ForLoop(Genome(self.operations, operation[1:]).into_operations()))
elif type(operation_type) == Dereference:
operations.append(Dereference(Genome(self.operations, operation[1:]).into_operations()))
elif type(operation_type) == Function:
operations.append(Function(func_count, Genome(self.operations, operation[1:]).into_operations()))
func_count += 1
else:
operations.append(ForLoop(Genome(self.operations, operation).into_operations()))
else:
if operation >= len(self.operations):
raise IndexError(f"Operation index {operation} is out of range")
operations.append(self.get_operation(operation))
return operations
def fitness(self):
# Evaluate the program.
try:
result = self.evaluate()
tape = result.tape
steps = result.steps
# The fitness is the number of 1s in the tape.
# return result.tape.count(1) * 1000 - self.get_size() * 10
try:
result = entropy(np.array(list(map(lambda x: min(x, 2.0 ** 16), result.tape)))).sum()
if np.isnan(result):
return 0.0
except:
print(result)
raise Exception("Entropy failed")
return result / self.get_size()
except Exception as e:
return -1.0
def evaluate(self):
tape = Tape(100)
tm = SageVirtualMachine(self.into_operations())
tm.run(tape, 3000)
return tape
def __lt__(self, other):
return self.fitness() < other.fitness()
def __str__(self):
return str(self.genome)
def __repr__(self):
return str(self)
operations = [
SetRegister(-1),
SetTape(-1),
SetRegister(0),
SetTape(0),
SetRegister(1),
SetTape(1),
Move('R'),
Move('L'),
Move('R'),
Move('L'),
ForLoop(),
ForLoop(),
ForLoop(),
Dereference(),
Save(),
Restore(),
# Print(),
# Input(),
IncrementRegister(),
DecrementRegister(),
IncrementTape(),
DecrementTape(),
IncrementRegister(),
DecrementRegister(),
IncrementTape(),
DecrementTape(),
Add(),
Subtract(),
Multiply(),
Divide(),
Remainder(),
]
def gen_random_genome(length=100, depth=0, max_depth=5):
if depth > 7:
return [], 0
total = 0
genome = []
for i in range(1000):
if total >= length:
return genome, total
genome.append(random.randint(0, len(operations) - 1))
total += 1
if total >= length:
return genome, total
if random.random() < 0.5:
gene, partial = gen_random_genome(length, depth + 1)
genome.append(gene)
total += partial
return genome, total
# genome = Genome.random(operations, 25)
# print(genome)
# print(genome.into_operations())
# t1 = Tape()
# tm = TuringMachine(genome.into_operations())
# tm.run(t1)
# genome.mutate(0.05)
# t2 = Tape()
# tm = TuringMachine(genome.into_operations())
# tm.run(t2)
# print(genome)
# print(genome.into_operations())
# print(t1)
# print(t2)
GENOME_SIZE = 100
POPULATION_SIZE = 300
# combinator_set = [S(), K(), I(), Data(Point(0, 0)), Lambda(lambda point: Data(point.value.shift_by(1, 0))), Lambda(lambda point: Data(point.value.shift_by(0, 1)))]
genomes = [Genome.random(operations, random.randint(GENOME_SIZE//3, GENOME_SIZE)) for _ in range(POPULATION_SIZE)]
genomes.sort()
genomes = genomes[::-1]
print(list(map(lambda g: g.fitness(), genomes)))
try:
for epoch in range(200):
print(f"Epoch {epoch}")
best_genomes = genomes[:POPULATION_SIZE//10]
print(list(map(lambda g: g.fitness(), best_genomes)))
# Create the next generation.
# The first 10 genomes are the best genomes from the previous generation.
genomes = deepcopy(best_genomes) + [Genome.random(operations, random.randint(GENOME_SIZE//3, GENOME_SIZE)) for _ in range(POPULATION_SIZE - len(best_genomes))]
# Cross over the best genomes.
for i in range(0, len(best_genomes), 2):
child1, child2 = best_genomes[i].crossover(best_genomes[i + 1])
genomes.append(child1)
genomes.append(child2)
# Cross over the best genomes.
for i in range(0, len(best_genomes), 2):
child = best_genomes[i].crossover_splits(best_genomes[i + 1])
genomes.append(child)
# Mutate the genomes.
for genome in genomes:
if random.random() < 0.5:
new_genome = deepcopy(genome)
new_genome.mutate(random.random() * 0.5)
genomes.append(new_genome)
genomes.extend(best_genomes)
# Sort the genomes by fitness.
genomes.sort()
genomes = genomes[::-1]
# print(list(map(lambda g: g.fitness(), genomes)))
print(genomes[0].into_operations())
print(genomes[0].evaluate())
except KeyboardInterrupt:
genomes = best_genomes
ops = genomes[0].into_operations()
with open('entropy.py', 'w') as f:
f.write(f'''from sage import *
import random
program = {ops}
number = input('Enter a number: ')
tm = SageVirtualMachine(program, [])
tape = Tape(1000)
tm.run(tape, 100000)
print(tape.tape)
''')
exit(0)