-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparralel_self_play.py
184 lines (150 loc) · 5.33 KB
/
parralel_self_play.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
import argparse
# import datetime
import multiprocessing
import os
import random
import time
import tempfile
from collections import namedtuple
import h5py
import numpy as np
import kerasutils
from dlgo import scoring
from dlgo.agent.ac_agent import load_ac_agent
from dlgo.goboard_fast import GameState, Player, Point
from dlgo.rl.experience import load_experience, combine_experience, ExperienceCollector
COLS = 'ABCDEFGHJKLMNOPQRST'
STONE_TO_CHAR = {
None: '.',
Player.black: 'x',
Player.white: 'o',
}
def avg(items):
if not items:
return 0.0
return sum(items) / float(len(items))
def print_board(board):
for row in range(board.num_rows, 0, -1):
line = []
for col in range(1, board.num_cols + 1):
stone = board.get(Point(row=row, col=col))
line.append(STONE_TO_CHAR[stone])
print('%2d %s' % (row, ''.join(line)))
print(' ' + COLS[:board.num_cols])
class GameRecord(namedtuple('GameRecord', 'moves winner margin')):
pass
def name(player):
if player == Player.black:
return 'B'
return 'W'
def simulate_game(black_player, white_player, board_size):
moves = []
game = GameState.new_game(board_size)
agents = {
Player.black: black_player,
Player.white: white_player,
}
while not game.is_over():
next_move = agents[game.next_player].select_move(game)
moves.append(next_move)
game = game.apply_move(next_move)
print_board(game.board)
game_result = scoring.compute_game_result(game)
print(game_result)
return GameRecord(
moves=moves,
winner=game_result.winner,
margin=game_result.winning_margin,
)
def get_temp_file():
fd, fname = tempfile.mkstemp(prefix='dlgo-train')
os.close(fd)
return fname
def do_self_play(board_size, agent_filename,
num_games, temperature,
experience_filename,
gpu_frac):
kerasutils.set_gpu_memory_target(gpu_frac)
random.seed(int(time.time()) + os.getpid())
np.random.seed(int(time.time()) + os.getpid())
agent1 = load_ac_agent(h5py.File(agent_filename))
agent1.set_temperature(temperature)
agent2 = load_ac_agent(h5py.File(agent_filename))
agent2.set_temperature(temperature)
collector1 = ExperienceCollector()
collector2 = ExperienceCollector()
color1 = Player.black
for i in range(num_games):
print('Simulating game %d/%d...' % (i + 1, num_games))
collector1.begin_episode()
agent1.set_collector(collector1)
collector2.begin_episode()
agent2.set_collector(collector2)
if color1 == Player.black:
black_player, white_player = agent1, agent2
else:
white_player, black_player = agent1, agent2
game_record = simulate_game(black_player, white_player, board_size)
if game_record.winner == color1:
print('Agent 1 wins.')
collector1.complete_episode(reward=1)
collector2.complete_episode(reward=-1)
else:
print('Agent 2 wins.')
collector2.complete_episode(reward=1)
collector1.complete_episode(reward=-1)
color1 = color1.other
experience = combine_experience([collector1, collector2])
print('Saving experience buffer to %s\n' % experience_filename)
with h5py.File(experience_filename, 'w') as experience_outf:
experience.serialize(experience_outf)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--learning-agent', required=True)
parser.add_argument('--num-games', '-n', type=int, default=10)
parser.add_argument('--experience-out', '-o', required=True)
parser.add_argument('--num-workers', '-w', type=int, default=1)
parser.add_argument('--temperature', '-t', type=float, default=1.0)
parser.add_argument('--board-size', '-b', type=int, default=19)
args = parser.parse_args()
experience_files = []
workers = []
gpu_frac = 0.95 / float(args.num_workers)
games_per_worker = args.num_games // args.num_workers
print('Starting workers...')
for i in range(args.num_workers):
filename = get_temp_file()
experience_files.append(filename)
worker = multiprocessing.Process(
target=do_self_play,
args=(
args.board_size,
args.learning_agent,
games_per_worker,
args.temperature,
filename,
gpu_frac,
)
)
worker.start()
workers.append(worker)
# Wait for all workers to finish.
print('Waiting for workers...')
for worker in workers:
worker.join()
# Merge experience buffers.
print('Merging experience buffers...')
# first_filename = experience_files[0]
other_filenames = experience_files[1:]
combined_buffer = load_experience(h5py.File(filename))
for filename in other_filenames:
next_buffer = load_experience(h5py.File(filename))
combined_buffer = combine_experience([combined_buffer, next_buffer])
print('Saving into %s...' % args.experience_out)
with h5py.File(args.experience_out, 'w') as experience_outf:
combined_buffer.serialize(experience_outf)
# Clean up.
for fname in experience_files:
os.unlink(fname)
if __name__ == '__main__':
main()