-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.py
227 lines (214 loc) · 10.1 KB
/
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
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
# -*- coding: utf-8 -*-
import chess
import matplotlib.pyplot as plt
from model import load_model
from multiprocessing.dummy import Pool as ThreadPool
import numpy as np
import os
from parse import featurize_board
#import random
from stockfish import Stockfish
import sys
import tensorflow as tf
import time
#CONFIDENCE_BIAS = 5
def init_stats():
stats = {}
stats['legal_moves'] = 0
stats['illegal_moves'] = 0
stats['turns'] = 0
stats['games'] = 0
stats['results'] = {}
stats['results']['1-0'] = 0
stats['results']['0-1'] = 0
stats['results']['1/2-1/2'] = 0
#stats['end_states'] = {} # Skip tracking unique end states (TODO: add this back as an option)
stats['minutes_elapsed'] = 0
stats['model_minutes_elapsed'] = 0
stats['engine_minutes_elapsed'] = 0
#stats['won_games'] = [] # Skip tracking won/draw games (TODO: add this back as an option)
#stats['draw_games'] = []
stats['en_passant_captures'] = 0
stats['castles'] = 0
return stats
def merge_stats(stats_list):
merged_stats = init_stats()
for stats in stats_list:
merged_stats['legal_moves'] += stats['legal_moves']
merged_stats['illegal_moves'] += stats['illegal_moves']
merged_stats['turns'] += stats['turns']
merged_stats['games'] += stats['games']
merged_stats['results']['1-0'] += stats['results']['1-0']
merged_stats['results']['0-1'] += stats['results']['0-1']
merged_stats['results']['1/2-1/2'] += stats['results']['1/2-1/2']
# merged_stats['end_states']
if (stats['minutes_elapsed'] > merged_stats['minutes_elapsed']):
merged_stats['minutes_elapsed'] = stats['minutes_elapsed']
# merged_stats['won_games']
# merged_stats['draw_games']
merged_stats['en_passant_captures'] += stats['en_passant_captures']
merged_stats['castles'] += stats['castles']
return merged_stats
def print_intragame_stats(stats, prefix=''):
moves = stats['legal_moves'] + stats['illegal_moves']
print(prefix + ("%d move attempts (%.2f%% illegal)" % (moves, 100 * stats['illegal_moves']/moves)))
print(prefix + ("%d en passant captures (%.2f per game)" % (stats['en_passant_captures'], stats['en_passant_captures']/stats['games'])))
print(prefix + ("%d castles (%.2f per game)" % (stats['castles'], stats['castles']/stats['games'])))
def print_stats(stats, prefix=''):
games = stats['games']
print(prefix + ("%.1f minutes (%.2f seconds per game, %.2f%% model turn time, %.2f%% engine turn time, %.2f%% overhead)" %
(stats['minutes_elapsed'],
60 * stats['minutes_elapsed']/games,
100 * stats['model_minutes_elapsed']/stats['minutes_elapsed'],
100 * stats['engine_minutes_elapsed']/stats['minutes_elapsed'],
100 * (stats['minutes_elapsed'] - stats['model_minutes_elapsed'] - stats['engine_minutes_elapsed'])/stats['engine_minutes_elapsed'])))
print_intragame_stats(stats, prefix)
print(prefix + ("%d turns (%.1f per game)" % (stats['turns'], stats['turns']/games)))
print(prefix + ("%d games (%.2f%% won, %.2f%% draw, %.2f%% lost)" %
(games,
100 * stats['results']['1-0']/games,
100 * stats['results']['1/2-1/2']/games,
100 * stats['results']['0-1']/games)))
#100 * len(stats['end_states'])/games)))
def init_stats_logfile(filename):
with open(filename, 'w') as logfile:
logfile.write("model\tgames\tillegal_move_pct\twon_pct\tdraw_pct\tlost_pct\n")
def print_stats_to_logfile(model_filename, stats, filename):
moves = stats['legal_moves'] + stats['illegal_moves']
games = stats['games']
with open(filename, 'a') as logfile:
logfile.write("%s\t%d\t%.2f%%\t%.2f%%\t%.2f%%\t%.2f%%\n" %
(model_filename,
games,
100 * stats['illegal_moves']/moves,
100 * stats['results']['1-0']/games,
100 * stats['results']['1/2-1/2']/games,
100 * stats['results']['0-1']/games))
def generate_model_move(model, board, stats, show_output=False):
assert board.turn # Must be White's turn
y = model.predict(featurize_board(board.fen(), rotate=False)).reshape((64, 64))
if show_output:
plt.imshow(y, cmap='Greys')
#remaining_confidence = 1
iterations = 0
max_iterations = 64 * 64 # Prevent infinite loops (possible due to randomization)
#first_legal_move = None
while iterations < max_iterations:
from_square, to_square = np.unravel_index(y.argmax(), y.shape) # Get best remaining move
move = chess.Move(from_square, to_square)
if board.piece_type_at(from_square) == chess.PAWN and chess.square_rank(to_square) == 7:
move.promotion = chess.QUEEN # Always promote to queen for now
if board.is_legal(move):
stats['legal_moves'] += 1
if board.is_en_passant(move):
stats['en_passant_captures'] += 1
if board.is_castling(move):
stats['castles'] += 1
break
#if first_legal_move == None:
# first_legal_move = move
#relative_move_confidence = (y[from_square, to_square] + CONFIDENCE_BIAS)/(remaining_confidence + CONFIDENCE_BIAS) # The model's prediction that this move is the best move, given the remaining choices (with a bias to ensure it's never too small)
#print(str(iterations) + ": " + str(y[from_square, to_square]) + " (relative: " + str(relative_move_confidence) + ")")
#if random.uniform(0, 1) < relative_move_confidence: # Randomization filter
# break
else:
stats['illegal_moves'] += 1
#remaining_confidence -= y[from_square, to_square]
y[from_square, to_square] = 0
iterations += 1
#if not board.is_legal(move):
# move = first_legal_move
return move
def generate_engine_move(engine, board):
engine.setfenposition(board.fen())
return chess.Move.from_uci(engine.bestmove()['move']) # TODO deal with pawn promotion
def play_interactive(model, board, move_uci=None):
if not move_uci == None:
assert board.turn == False # Must be Black's turn
board.push_uci(move_uci)
board.push(generate_model_move(model, board, init_stats(), show_output=True))
return board
def play_engine(model, engine, limit=10000):
stats = init_stats()
start_time = time.time()
model_turn_time = 0
engine_turn_time = 0
while limit > 0:
board = chess.Board()
engine.newgame()
game_node = chess.pgn.Game()
game_node.headers['White'] = 'Neural-Chess'
game_node.headers['Black'] = 'Stockfish'
while True:
if board.is_game_over():
stats['games'] += 1
stats['results'][board.result()] += 1
#end_state = board.fen().split()[0] # Only consider the board itself, not castling rights, turn count etc.
#if end_state in stats['end_states']:
# stats['end_states'][end_state] += 1
#else:
# stats['end_states'][end_state] = 1
if board.result() == '1-0':
#stats['won_games'].append(str(game_node.root()))
sys.stdout.write('X')
elif board.result() == '1/2-1/2':
#stats['draw_games'].append(str(game_node.root()))
sys.stdout.write('-')
else:
sys.stdout.write('.')
break
turn_start_time = time.time()
if board.turn:
move = generate_model_move(model, board, stats)
model_turn_time += time.time() - turn_start_time
else:
move = generate_engine_move(engine, board)
engine_turn_time += time.time() - turn_start_time
board.push(move)
game_node = game_node.add_main_variation(move)
stats['turns'] += 1
limit -= 1
stats['minutes_elapsed'] += (time.time() - start_time)/60
stats['model_minutes_elapsed'] += model_turn_time/60
stats['engine_minutes_elapsed'] += engine_turn_time/60
print()
return stats
def play_engine_forever(model_filename_root, last_model_filename=None):
with Stockfish(depth=0, param={'Skill Level':0}) as engine:
while os.path.isfile('.stopplay') == False:
model_filename = None
play_next_model = (last_model_filename == None)
for filename in os.listdir('model'):
if not filename.startswith(model_filename_root) or not filename.endswith('.json'):
continue
if play_next_model:
model_filename = filename # This is the first file after the last one we played
break
if filename == last_model_filename:
play_next_model = True
if model_filename == None:
time.sleep(5)
continue
model = load_model('model/' + model_filename)
print()
print("Playing %s..." % model_filename)
stats = play_engine(model, engine, 1000)
print()
print_stats(stats)
print_stats_to_logfile(model_filename, stats, '.playstats.txt')
last_model_filename = model_filename
os.remove('.stopplay')
def multithreaded_play_engine_core_that_does_not_work_yet(model):
# TODO: for some reason this doesn't actually work, it seems the predict
# function is regenerated during thread execution anyway...
model._make_predict_function() # Have to initialize before threading
graph = tf.get_default_graph() # See https://github.com/fchollet/keras/issues/2397
thread_pool = ThreadPool(4)
all_stats = thread_pool.starmap(play_engine, [(model, graph)])
# Note, inside of play_engine, do the following to set the thread
# context to the correct graph
#with graph.as_default():
thread_pool.close()
thread_pool.join()
stats = merge_stats(all_stats)
return stats