-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
179 lines (156 loc) · 5.69 KB
/
core.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
"""
Proxies between command-line utilities and statically compiled implementations.
"""
from collections import OrderedDict as odict
from datetime import datetime
from heapq import heappop, heappush
from time import clock
import cython_init # Executes pyximport install on module import # noqa.
from bots import available_bots
from collectors import available_collectors
from iop import (common_printoptions, first_free, load_data, load_level,
load_params, save_data, save_params)
from prngs import seed_prngs
from processors import available_processors
from trainers import available_trainers
from interface import finish
def do_play(bot, params, levels, runs, prngs_seed, verbosity, **kwargs):
"""
Evaluates the bot with params on some levels.
"""
common_printoptions()
prngs_seed = seed_prngs(prngs_seed)
start = clock()
bot_class = available_bots[bot]
params_key, params = load_params(bot, params, verbosity)[:2]
scores = odict()
for level in levels:
level = load_level(level, verbosity)
scores[level['key']] = bot_class(level, params).evaluate(runs)
finish(verbose=verbosity > 3)
end = clock()
return {'date': datetime.utcnow(),
'bot': bot,
'params_key': params_key,
'levels': levels,
'runs': runs,
'scores': scores,
'time': end - start,
'prngs_seed': prngs_seed}
def do_train(bot, trainer, config, dists, emphases, phases,
random_seeds, random_seeds_pool,
stored_seeds, param_map, param_freeze, param_scale,
level, eval_levels,
runs, prngs_seed, output, verbosity, **kwargs):
"""
Chooses the right bot and trainer, loads the level, generates or loads
some seeds and lets the trainer work.
"""
if output is None:
output = first_free('params/{}'.format(bot))
common_printoptions()
prngs_seed = seed_prngs(prngs_seed)
start = clock()
bot_class = available_bots[bot]
trainer_class = available_trainers[trainer]
# Load the level we'll be training on.
level = load_level(level, verbosity)
# Convert the (index, weight) emphases to a flat tuple of weights.
weights = [1.] * level['features']
for index, weight in emphases:
weights[int(index)] = float(weight)
emphases = tuple(weights)
# Load and/or draw some starting parameters.
seeds = []
for index, seed in enumerate(stored_seeds):
params_key, params, history = load_params(bot, seed, verbosity)
bot_ = bot_class(level, params, param_map, param_freeze, param_scale,
dists, emphases, phases)
heappush(seeds, (float('inf'), index, bot_, history))
for index in range(random_seeds_pool):
bot_ = bot_class(level, dists=dists, emphases=emphases, phases=phases)
score = bot_.evaluate(runs)
heappush(seeds, (score, index + len(stored_seeds), bot_, []))
if len(seeds) > len(stored_seeds) + random_seeds:
heappop(seeds)
seeds = tuple((s[2], s[3]) for s in seeds)
trainer_ = trainer_class(level, config, dists, emphases, seeds, runs)
params, history = trainer_.train()
# Evaluate the params on more than just the training level.
scores = odict()
for key in eval_levels:
level_ = load_level(key, verbosity)
scores[key] = bot_class(level_, params).evaluate(runs)
end = clock()
# Save the final parameters with their history.
meta = {
'date': datetime.utcnow(),
'bot': bot,
'trainer': trainer,
'config': config,
'dists': dists,
'emphases': emphases,
'phases': phases,
'seeds': (stored_seeds, random_seeds, random_seeds_pool),
'param_map': param_map,
'param_freeze': param_freeze,
'param_scale': param_scale,
'level': level,
'runs': runs,
'output': output,
'scores': scores,
'time': end - start,
'prngs_seed': prngs_seed
}
history.append(meta)
save_params(bot, output, params, history, verbosity)
return params, meta
def do_collect(collector, level, bot, prngs_seed, output, verbosity, **kwargs):
"""
Collects data from the level using the given collector.
"""
if output is None:
output = first_free('data/{}'.format(collector))
common_printoptions()
prngs_seed = seed_prngs(prngs_seed)
start = clock()
level = load_level(level, verbosity)
bot_class = available_bots[bot[0]]
if bot[1] is not None:
params_key, params = load_params(*bot, verbosity=verbosity)[:2]
else:
params_key, params = None, {}
bot_ = bot_class(level, params)
collector_ = available_collectors[collector](level, bot_)
data = collector_.collect()
end = clock()
meta = {
'date': datetime.utcnow(),
'collector': collector,
'level': level,
'bot': (bot[0], params_key),
'output': '{}_{}'.format(collector, output),
'time': end - start,
'prngs_seed': prngs_seed
}
save_data(collector, output, data, meta, verbosity)
return data, meta
def do_process(processor, input_, prngs_seed, verbosity, **kwargs):
"""
Analyses data collected by a collector.
"""
common_printoptions()
prngs_seed = seed_prngs(prngs_seed)
start = clock()
data = tuple(load_data(*key, verbosity=verbosity) for key in input_)
processor_ = available_processors[processor](data)
results = processor_.process()
end = clock()
meta = {
'date': datetime.utcnow(),
'processor': processor,
'input': input_,
'time': end - start,
'prngs_seed': prngs_seed
}
return results, meta