-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgp.py
More file actions
112 lines (95 loc) · 3.24 KB
/
gp.py
File metadata and controls
112 lines (95 loc) · 3.24 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
#Module to use genetic programming to learn functions
import agents
import math
import lisp
import os
import popen2
initial_file = "composition.lisp"
def bell_payment(x, y):
difference = x-y
return 2 ** (- (difference ** 2))
max_payment = 1
class GP():
project_name = None
source_folder = None
fifo = 'pyfifo'
generation = 0
population = []
parallelizer = None
evaluator = None
grader = None
breeder = None
coder = None
source = None
bytecode = None
population_size = 100
generation_cap = 1000 #the number of generations we will run for
#datasets should be a tuple of answers and data
def __init__(self, project_name, initial, dataset):
self.project_name = project_name
self.source_folder = project_name + '_dir'
os.system("mkdir " + self.source_folder)
os.system("fifo " + self.fifo)
self.population = initial
self.required = "composition.lisp"
data = dataset[0]
answers = dataset[1]
self.parallelizer = agents.Parallelizer(['pyscreen0', 'pyscreen1', 'pyscreen2', 'pyscreen3'], self.population, self.fifo)
self.evaluator = agents.Evaluator(self.population, data, self.required, self.source_folder, self.parallelizer)
self.grader = agents.Grader(self.population, answers, bell_payment, max_payment)
self.breeder = agents.Breeder(self.population, self.population_size)
self.coder = agents.Coder(self.population, self.required, self.source_folder)
for agent in self.population:
self.grader.pay(agent,1)
self.breeder.refresh()
self.update_population( self.breeder.breed() )
def increment_generation(self):
self.generation += 1
self.evaluator.generation += 1
self.grader.generation += 1
self.breeder.generation += 1
self.coder.generation += 1
def update_population(self, new_population):
self.population += new_population
self.evaluator.population = new_population
self.grader.population = new_population
self.breeder.population += new_population
self.coder.population = new_population
def update_source(self, new_source):
sourcecode = new_source[0]
bytecode = new_source[1]
self.source = sourcecode
self.bytecode = bytecode
self.evaluator.required = sourcecode
self.coder.required = sourcecode
def cycle(self):
self.update_source( the_big_one.coder.code() )
self.evaluator.evaluate()
self.grader.grade()
self.increment_generation()
self.update_population( the_big_one.breeder.breed() )
def run(self):
while self.grader.final_answers == [] and self.generation < self.generation_cap:
print "Generation: ", self.generation, "..."
self.cycle()
print "MVP: ", self.grader.mvp
print "Final answer: ", self.grader.final_answers
data = []
answers = []
for i in range(-10, 10):
for j in range(-10, 10):
data.append([i,j])
answers.append( math.sqrt( i*i + j*j) )
plus = lisp.Function('plus', ['x','y'], 'built-in')
minus = lisp.Function('minus', ['x','y'], 'built-in')
mult = lisp.Function('mult', ['x','y'], 'built-in')
div = lisp.Function('div' , ['x','y'], 'built-in')
one = lisp.Function('one', [], 'built-in')
Plus = agents.Agent(plus)
Minus = agents.Agent(minus)
Times = agents.Agent(mult)
Div = agents.Agent(div)
One = agents.Agent(one)
initial_population = [Plus, Minus, Times, Div, One]
the_big_one = GP('the_big_one', initial_population, [data,answers])
the_big_one.run()