-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn_dominion.py
361 lines (289 loc) · 12.1 KB
/
learn_dominion.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""
Presents the implementaion for a harness to run Dominion experiments on!
"""
import argparse
import datetime
import os
import shutil
import time # For timing training
import pickle
import numpy as np
from util import strip_style
from policy import QLearningPolicy, RandomPolicy
from computer_player import ComputerPlayer
from game import Dominion
from utilities.filelog import FileLog
CWD = os.getcwd()
def namespace(experiment_name):
"""
Translates an experiment name into a string that will be used to generate
a directory and namespace data associated with that experiment.
"""
# now = datetime.datetime.now()
# return 'exp_{}_date{}_time{}-{}'.format(experiment_name, now.date(), now.hour, now.minute)
return 'exp_{}'.format(experiment_name)
def manual_cmd_str(name, desc, n_iters, test_every, levels):
"""
Returns a string representation of the manual command to run an experiment
with given parameters.
"""
format_str = 'python learn_dominion.py --name {}{} --niters {} --testevery {} --levels {}'
comment = ' -m "{}"'.format(desc) if desc else ''
return format_str.format(name, comment, n_iters, test_every, levels)
def write_metafile(dir, settings):
"""
Writes a metafile containing information for the experiment to its respective
directory.
"""
filename = os.path.join(dir, 'meta.txt')
with open(filename, 'w') as f:
name = settings['name']
desc = settings['desc']
niters = settings['niters']
testevery = settings['testevery']
levels = settings['levels']
f.write('Name: {}\n'.format(name))
f.write('Description: {}\n'.format(desc))
now = datetime.datetime.now()
when = '{} {}:{}'.format(now.date(), now.hour, now.minute)
f.write('Created: {}\n'.format(when))
f.write('Iters: {}\n'.format(niters))
f.write('Test/cache weights every: {}\n'.format(testevery))
f.write('Cmd string:\n\t{}\n'.format(manual_cmd_str(name, desc, niters, testevery, levels)))
f.close()
def write_game_log(dir, game_log, iter, info):
"""
Writes a game log, indexed by the maturity (in iters) of the policy and the
string info (e.g., win or lose). Useful to see deveopment of strategies.
"""
filename = os.path.join(dir, 'game_{}_iter{}.txt'.format(info, iter))
if os.path.exists(filename): return # Already wrote one!
with open(filename, 'w') as f:
f.write(strip_style(game_log))
f.close()
def parse_args():
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser(description='Run Dominon learning experiment.')
parser.add_argument('--name', help='experiment name')
parser.add_argument('-m, --message', default='', dest='message', help='comments on experiment/description')
parser.add_argument('-i', '--interactive', action='store_true', help='setup experiment in interactive mode')
parser.add_argument('--niters', type=int, default=100, help='number of iterations to train for')
parser.add_argument('--testevery', type=int, default=50, help='how often to test the policy')
parser.add_argument('--levels', type=int, default=0, help='number of CPU levels to train against, tournament-style')
# parser.add_argument('--experiment', '-e', default=0, type=int, help='Experiment number.')
# parser.add_argument('--test', '-t', action='store_true', help='Run test set (default: False).')
# parser.add_argument('--save_weights', '-s', action='store_true', help='Save weights (default: False).')
# parser.add_argument('--load_weights', '-w', help='Load weights from hdf5 file (default: None).')
# parser.add_argument('--tmp_dir', '-dir', default='tmp', help='Temp dir to dump info (default: tmp).')
return parser.parse_args()
def get_line(prompt):
prompt = '[?] {} '.format(prompt)
return input(prompt)
def get_integer(prompt):
prompt = '[?] {} '.format(prompt)
typed = input(prompt)
if typed.isdigit():
return int(typed)
while True:
print('Invalid input. Please type an integer.')
typed = input(prompt)
if typed.isdigit():
return int(typed)
def get_yes_or_no(prompt):
prompt = '[Y/N] {} '.format(prompt)
typed = input(prompt)
if typed and (typed.lower()[0] == 'y' or typed.lower()[0] == 'n'):
return typed.lower()[0] == 'y'
while True:
print('Invalid input. Type something beginning with "y" or "n".')
typed = input(prompt)
if typed and (typed.lower()[0] == 'y' or typed.lower()[0] == 'n'):
return typed.lower()[0] == 'y'
def prompt_settings():
"""
Guides the user through setting up an experiment.
"""
# Metadata setup (directory name, description)
okay = False
while not okay:
print('Metadata setup:')
name = get_line('Name of experiment?')
desc = get_line('Description of experiment?')
dir = namespace(name)
print('Name\t{}\nDesc:\t{}\nDirectory:\t{}'.format(name, desc, dir))
path = os.path.join(CWD, dir)
if os.path.exists(path) and os.path.isdir(path):
print('Warning: directory "{}" already exists and will be overwritten.'.format(dir))
else:
print('Directory "{}" will be created.'.format(dir))
okay = get_yes_or_no('OK?')
if os.path.exists(path) and os.path.isdir(path): # Remove folder if exists
shutil.rmtree(path)
# Experiment setup (niters, etc.)
okay = False
while not okay:
print('Experiment setup:')
levels = get_integer('Number of levels/brackets? 0 for normal iterative.')
iters = get_integer('Number of training iters/games?')
test_every = get_integer('Test agent every n games. n?')
print('Brackets:\t{}\nIters\t{}\nTest Every:\t{} games'.format(levels, iters, test_every))
okay = get_yes_or_no('OK?')
print('To run this experiment again directly from the command line, run:\n\n\t{}\n'.format(manual_cmd_str(name, desc, iters, test_every, levels)))
return {
'path': path,
'name': name,
'desc': desc,
'niters': iters,
'testevery': test_every,
'levels': levels
}
# TODO: verbosity, save game logs (?), discount factor ...
def run_bracket_experiment(settings):
levels = settings['levels']
path = settings['path']
best_policy = bracket(levels, settings)
dump_weights(path, best_policy.get_weights(), levels) # Dump the best policy
def bracket(n, settings):
if n == 0:
# Play against a random policy
policy = QLearningPolicy()
players = [ComputerPlayer(1, policy=policy), ComputerPlayer(2, policy=RandomPolicy())]
best_policy = compete(players, settings, n)
return best_policy
# Make two level n-1 brackets
best_policy_1 = bracket(n-1, settings)
best_policy_2 = bracket(n-1, settings)
players = [ComputerPlayer(1, policy=best_policy_1), ComputerPlayer(2, policy=best_policy_2)]
best_policy = compete(players, settings, n)
return best_policy
def compete(players, settings, n):
niters = settings['niters']
test_every = settings['testevery']
path = settings['path']
levels = settings['levels']
# Helper function for timing
elapsed = lambda tick, tock: time.strftime('%H:%M:%S', time.gmtime(tock - tick))
print('Competition for a {}-level bracket!'.format(n))
tick = time.time()
wins = [0, 0]
for i in range(niters):
# Create and simulate a game
game = Dominion(with_players=players, silence_output=True)
winner_idx, scores = game.play()
wins[winner_idx] += 1
tock = time.time()
write_game_log(path, game.get_log(), i, '{}-level'.format(n))
print('Iter: {}, winner: {}, scores: {}, {} elapsed'.format(i, winner_idx, scores, elapsed(tick, tock)))
if n == 0:
# First player always advances
return players[0].policy
winner_idx = np.argmax(wins)
print('Player {} advances!'.format(winner_idx + 1))
return players[winner_idx].policy # Return best player's policy
def dump_policy_losses(path, policy, i):
"""
Dumps policy losses.
"""
filename = os.path.join(path, '{}-iter{}.txt'.format(policy.file.filename, i))
policy.file.dump_to(filename)
def dump_weights(path, policy, i):
"""
Dumps policy losses.
"""
filename = os.path.join(path, 'weights-iter{}.hdf5'.format(i))
policy.save_weights(filename)
def run_experiment(settings):
"""
Runs the specified experiment.
"""
niters = settings['niters']
test_every = settings['testevery']
path = settings['path']
testiters = 10
# TODO: add verbose, cache every (?), log games on test (?), discount (?)
# Create QLearningPolicy
policy = QLearningPolicy(instanced=True, fileid=1)
policy_clone = QLearningPolicy(instanced=True, fileid=2)
assert(policy.model == policy_clone.model) # Should be same ref
# Helper function for timing
elapsed = lambda tick, tock: time.strftime('%H:%M:%S', time.gmtime(tock - tick))
def test_policy(train_iter):
"""
Helper to run_experiment that computes the win rate of the current policy
against another computer opponent.
"""
print('Testing...')
policy.set_train(False)
wins = 0
tick = time.time()
for i in range(testiters):
# Play against a random policy opponent
players = [ComputerPlayer(1, policy=policy), ComputerPlayer(2, policy=RandomPolicy())]
game = Dominion(with_players=players, silence_output=True)
winner_idx, scores = game.play()
print('win idx:', winner_idx, scores)
if winner_idx == 0:
wins += 1
write_game_log(path, game.get_log(), train_iter, 'win')
else:
write_game_log(path, game.get_log(), train_iter, 'lose')
tock = time.time()
print('(test_policy) {}% win rate (eval in: {})'.format(wins / testiters * 100, elapsed(tick, tock)))
policy.set_train(True)
return wins / testiters
# test_policy(0)
tick = time.time()
for i in range(niters):
# Create and simulate a game with itself
players = [ComputerPlayer(1, policy=policy), ComputerPlayer(2, policy=policy_clone)]
game = Dominion(with_players=players, silence_output=True)
winner_idx, scores = game.play()
tock = time.time()
# print(game.get_log())
format_str = '> [{:3}]: P{} won {} to {} in {} rounds ({} elapsed)'
rounds = game.game_info.rounds
print(format_str.format(i, winner_idx+1, *sorted(scores, reverse=True), rounds, elapsed(tick, tock)))
# Dump policy losses for each iter
dump_policy_losses(path, policy, i)
if ((i + 1) % test_every == 0):
test_policy(i)
dump_weights(path, policy, i)
print('Dumped weights.')
def main():
"""
Extract args and begin experiment.
"""
args = parse_args()
if args.interactive:
settings = prompt_settings()
else:
# Extract settings from args
path = namespace(args.name)
settings = {
'name': args.name,
'desc': args.message,
'path': path,
'niters': args.niters,
'testevery': args.testevery,
'levels': args.levels,
}
if os.path.exists(path) and os.path.isdir(path):
print('Warning: directory "{}" already exists and will be overwritten.'.format(path))
if(get_yes_or_no('OK?')):
shutil.rmtree(path) # Remove original directory
else:
print('Aborting.')
exit()
# Make experiment directory and metafile
path = settings['path']
os.mkdir(path)
write_metafile(path, settings)
levels = settings['levels']
if levels == 0:
run_experiment(settings) # Can now beat a random opponent in a fixed game!
else:
run_bracket_experiment(settings)
if __name__ == '__main__':
main()