-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoach.py
248 lines (176 loc) · 6.25 KB
/
coach.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
#%%
import numpy as np
import chess
from game import *
from play_game import *
from gameapi import GameAPI
from mcts import MCTS
from rbmcnet import NNetWrapper
from pickle import Pickler, Unpickler
from stockfish import Stockfish
import sys
import os
import math
from multiprocessing import Pool
#%%
def loadTrainExamples(filename):
examplesFile = filename + ".examples"
if not os.path.isfile(examplesFile):
return []
else:
with open(examplesFile, "rb") as f:
training_examples = Unpickler(f).load()
return training_examples
def save_training_examples(filename, train):
filename = filename + ".examples"
with open(filename, "w") as fp:
for example in train:
fp.write('%s,%s,%d\n' % (example[0], example[1], example[2]))
def saveTrainExamples(filename, train):
#folder = "training_examples"
#if not os.path.exists(folder):
# os.makedirs(folder)
filename = filename + ".examples"
with open(filename, "wb+") as f:
Pickler(f).dump(train)
f.closed
def play_one_game(nnet, use_stockfish, play_random_agent = False):
game = Game()
board = game.truth_board
move_number = 0
game.start()
training_examples = []
player = random.choice([0, 1])
if play_random_agent:
use_end_game_move = False
else:
use_end_game_move = True
#print(player)
while not game.is_over():
move_number += 1
if move_number > 200:
# Just end the game in a draw
break
if play_random_agent == True and game.turn != player:
#print(game.turn)
moves = game.get_moves()
move = random.choice(moves)
requested_move, taken_move, captured_square, reason = game.handle_move(move)
#format_print_board(game.truth_board)
game.end_turn()
continue
current_board = game.truth_board
gameapi = GameAPI(current_board)
# End game move to capture opponent king if possible
#if use_end_game_move:
# move = gameapi.end_game_move(game.turn)
# if move is not None:
# pi = gameapi.getValidMoves(moves=[move])
# pi[-1] = 0
# training_examples.append([gameapi.fen, game.turn, pi])
# game.handle_move(move)
# game.end_turn()
# continue
if use_stockfish:
# Use this to train our agent to play like stockfish
stockfish = Stockfish("/usr/games/stockfish")
stockfish.set_fen_position(gameapi.fen)
move_uci = stockfish.get_best_move()
move = chess.Move.from_uci(move_uci)
pi = gameapi.getValidMoves(moves=[move])
pi[-1] = 0
training_examples.append([gameapi.fen, game.turn, move_uci])
else:
# Use this when testing our neural network
mcts = MCTS(gameapi, nnet, num_mcts_sims=777, cpuct=0.5)
pi = mcts.getActionProb(move_number < 30)
move = np.random.choice(len(pi), p=pi)
# Collect training examples
training_examples.append([gameapi.fen, game.turn, pi])
move = gameapi.make_move(move, apply_move = False)
requested_move, taken_move, captured_square, reason = game.handle_move(move)
#format_print_board(game.truth_board)
game.end_turn()
#format_print_board(game.truth_board)
if move_number > 200:
win_color = None
win_reason = "Draw, exceeded move count"
else:
win_color, win_reason = game.get_winner()
if player == win_color:
result = 1
print("You win!", move_number)
else:
result = -1
print("You lost, what did you expect? ", move_number, win_reason)
if win_color is None:
return [(x[0], x[2], 0) for x in training_examples], 0
else:
return [(x[0], x[2], 1 if x[1] == win_color else -1) for x in training_examples], result
def play_games_with_stockfish(filename):
nnet = NNetWrapper()
nnet.load_checkpoint()
training_examples = []
num_games = 4000
for i in range(num_games):
print("game", i, end=" ")
examples, result = play_one_game(nnet, use_stockfish=True)
training_examples.extend(examples)
save_training_examples(filename, training_examples)
return True
def gen_games_with_mcts(filename):
np.random.seed(int(filename) * 5)
if int(filename) < 6:
nnet = NNetWrapper()
else:
nnet = NNetWrapper(id=1)
nnet.load_checkpoint()
training_examples = []
num_games = 30
for i in range(num_games):
print("game", i, end=" ")
examples, result = play_one_game(nnet, use_stockfish=False)
training_examples.extend(examples)
#save_training_examples(filename, training_examples)
return training_examples
def play_games_with_mcts(num_games):
nnet = NNetWrapper()
nnet.load_checkpoint(folder="models/", filename="mcts.pth.tar")
wins = 0
for i in range(num_games):
print("game", i, end=" ")
_, result = play_one_game(nnet, use_stockfish=False, play_random_agent=True)
if result == 1:
wins += 1
return wins
#nnet.save_checkpoint()
#training_examples = []
#for i in range(1000):
# training_examples.extend(play_one_game(nnet))
# saveTrainExamples(filename, training_examples)
files = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
# Run this with a pool of 5 agents having a chunksize of 3 until finished
agents = 9
chunksize = 1
print(sys.argv[1])
if sys.argv[1] == "train":
with Pool(processes=agents) as pool:
result = pool.map(gen_games_with_mcts, files, chunksize)
training_examples = []
for examples in result:
training_examples.extend(examples)
print(len(training_examples))
print(len(training_examples[0]))
nnet = NNetWrapper()
nnet.load_checkpoint()
loss = nnet.train_mcts(training_examples)
if math.isnan(loss):
print("Is nan, not saving net")
pass
else:
nnet.save_checkpoint()
else:
num_games= [5]
with Pool(processes=1) as pool:
result = pool.map(play_games_with_mcts, num_games, chunksize)
print(np.sum(result)/np.sum(num_games))