/
hgcpp.py
117 lines (97 loc) · 3.06 KB
/
hgcpp.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
import matnn
import numpy as np
import operator
import math
def evo_step(breed_pop, eval_hof):
chosen_one = None
eval_pop = []
while not chosen_one:
(breed_pop, eval_pop) = refresh_pop(breed_pop, eval_pop)
chosen_one = None
for e in eval_pop:
if e.evaluate_against(eval_hof):
chosen_one = e
break
return ([chosen_one] + breed_pop, chosen_one)
def refresh_pop(breed_pop, eval_pop):
breed_pop += eval_pop
breed_pop.sort(key=operator.attrgetter('perf'), reverse=True)
breed_pop = breed_pop[:cf['pop_size']]
make_random = breed_pop[0].__class__
eval_pop = [b.makeChild() for b in breed_pop] + [make_random() for i in xrange(cf['rand_probes'])]
return (breed_pop, eval_pop)
def gen_process_params(output_size = None, weights = None, biases = None, gen_input = None):
if gen_input is None:
gen_input = matnn.randomInput
if output_size is None:
output_size = len(cf['alphabet'])
hidden_size = cf['init_hidden']
size = hidden_size + output_size
if weights is None:
weights = np.mat(matnn.genentry((size,size+cf['gen_insize'])))
if biases is None:
biases = np.mat(matnn.genbias((size, 1)))
return (output_size, weights, biases, gen_input)
class Generator(matnn.GenMatRNN):
def __init__(self, *params):
matnn.GenMatRNN.__init__(self,*gen_process_params(*params))
self.perf = 0.0
def evaluate_against(self,c_hof):
successful = True
self.perf = 0.0
for c in c_hof:
(score, ratio) = self.run_eval(c.run(self.run_clean()))
self.perf += ratio
if ratio < cf['perf_thresh']:
successful = False
break
return successful
def run_eval(self, c):
totalweights = 0
score = 0.0
count = 0.0
while totalweights < 1:
for (i,out) in enumerate(c):
w = min(i, cf['fitweight_thresh'])
totalweights += w
score += w * out[0,0]
count += math.copysign(1.0, out[0,0])
return (score / totalweights, count/(i+1))
def class_process_params(output_size = 1, weights = None, biases = None):
input_size = len(cf['alphabet'])
hidden_size = cf['init_hidden']
size = hidden_size + output_size
if weights is None:
weights = np.mat(matnn.genentry((size,size+input_size)))
if biases is None:
biases = np.mat(matnn.genbias((size, 1)))
return (output_size, weights, biases)
class Classifier(matnn.MatRNN):
def __init__(self, *params):
matnn.MatRNN.__init__(self, *class_process_params(*params))
self.perf = 0.0
def evaluate_against(self,g_hof):
successful = True
(score, self.perf) = self.run_eval(self.run(ref_data.next()))
self.perf *= -1
if self.perf < cf['perf_thresh']:
successful = False
else:
for g in g_hof:
(score, ratio) = self.run_eval(self.run(g.run_clean()))
self.perf += ratio
if ratio < cf['perf_thresh']:
successful = False
break
return successful
def run_eval(self, g):
totalweights = 0
score = 0.0
count = 0.0
while totalweights < 1:
for (i,out) in enumerate(g):
w = min(i, cf['fitweight_thresh'])
totalweights += w
score -= w * out[0,0]
count -= math.copysign(1.0, out[0,0])
return (score / totalweights, count/(i+1))