forked from blaa/PyGene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_prog.py
executable file
·202 lines (167 loc) · 4 KB
/
demo_prog.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
#! /usr/bin/env python
"""
Demo of genetic programming
This gp setup seeks to breed an organism which
implements func x^2 + y
Takes an average of about 40 generations
to breed a matching program
"""
import math
from random import random, uniform
from pygene.prog import ProgOrganism
from pygene.population import Population
# a tiny batch of functions
def add(x,y):
#print "add: x=%s y=%s" % (repr(x), repr(y))
try:
return x+y
except:
#raise
return x
def sub(x,y):
#print "sub: x=%s y=%s" % (repr(x), repr(y))
try:
return x-y
except:
#raise
return x
def mul(x,y):
#print "mul: x=%s y=%s" % (repr(x), repr(y))
try:
return x*y
except:
#raise
return x
def div(x,y):
#print "div: x=%s y=%s" % (repr(x), repr(y))
try:
return x / y
except:
#raise
return x
def sqrt(x):
#print "sqrt: x=%s" % repr(x)
try:
return math.sqrt(x)
except:
#raise
return x
def pow(x,y):
#print "pow: x=%s y=%s" % (repr(x), repr(y))
try:
return x ** y
except:
#raise
return x
def log(x):
#print "log: x=%s" % repr(x)
try:
return math.log(float(x))
except:
#raise
return x
def sin(x):
#print "sin: x=%s" % repr(x)
try:
return math.sin(float(x))
except:
#raise
return x
def cos(x):
#print "cos: x=%s" % repr(x)
try:
return math.cos(float(x))
except:
#raise
return x
def tan(x):
#print "tan: x=%s" % repr(x)
try:
return math.tan(float(x))
except:
#raise
return x
# define the class comprising the program organism
class MyProg(ProgOrganism):
"""
"""
funcs = {
'+': add,
# '-':sub,
'*': mul,
# '/':div,
# '**': pow,
# 'sqrt': sqrt,
# 'log' : log,
# 'sin' : sin,
# 'cos' : cos,
# 'tan' : tan,
}
vars = ['x', 'y']
consts = [0.0, 1.0, 2.0, 10.0]
testVals = [{'x':uniform(-10.0, 10.0),
'y':uniform(-10.0, 10.0),
} for i in range(20)
]
mutProb = 0.4
def testFunc(self, **vars):
"""
Just wanting to model x^2 + y
"""
return vars['x'] ** 2 + vars['y']
def fitness(self):
# choose 10 random values
badness = 0.0
try:
for vars in self.testVals:
badness += (self.calc(**vars) - self.testFunc(**vars)) ** 2
return badness
except OverflowError:
return 1.0e+255 # infinitely bad
# maximum tree depth when generating randomly
initDepth = 6
class ProgPop(Population):
"Population class for the experiment"
species = MyProg
initPopulation = 10
# cull to this many children after each generation
childCull = 20
# number of children to create after each generation
childCount = 20
mutants = 0.3
def graph(orig, best):
"Graph on -10, 10 ranges"
print("ORIG BEST:")
for y in range(10, -11, -2):
for x in range(-10, 11, 3):
z = orig(x=float(x), y=float(y))
print("%03.0f " % z, end=' ')
print(" ", end=' ')
for x in range(-10, 11, 3):
z = best(x=float(x), y=float(y))
print("%03.0f " % z, end=' ')
print()
def main(nfittest=10, nkids=100):
pop = ProgPop()
ngens = 0
i = 0
while True:
b = pop.best()
print("Generation %s: %s best=%s average=%s)" % (
i, str(b), b.fitness(), pop.fitness()))
b.dump()
graph(b.testFunc, b.calc)
if b.fitness() <= 0:
print("cracked!")
break
i += 1
ngens += 1
if ngens < 100:
pop.gen()
else:
print("failed after 100 generations, restarting")
pop = ProgPop()
ngens = 0
if __name__ == '__main__':
main()
pass