-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.py
More file actions
292 lines (239 loc) · 7.36 KB
/
play.py
File metadata and controls
292 lines (239 loc) · 7.36 KB
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
import os
import chess
import time
import traceback
import signal
import sys
from state import State
import numpy as np
import tensorflow as tf
import base64
from flask import Flask, Response, request
MAXVAL = 10000
model = tf.keras.models.load_model('trained_model.h5')
moves = []
move_times = []
class ClassicValuator(object):
values = {chess.PAWN: 1,
chess.KNIGHT: 3.2,
chess.BISHOP: 3.3,
chess.ROOK: 5,
chess.QUEEN: 9,
chess.KING: 0}
def __init__(self):
self.reset()
self.memo = {}
def reset(self):
self.count = 0
def __call__(self, s):
self.count += 1
key = s.key()
if key not in self.memo:
self.memo[key] = self.value(s)
return self.memo[key]
def value(self, s):
b = s.board
# game over values
if b.is_game_over():
if b.result() == "1-0":
return MAXVAL
elif b.result() == "0-1":
return -MAXVAL
else:
return 0
val = 0.0
# piece values
pm = s.board.piece_map()
for x in pm:
tval = self.values[pm[x].piece_type]
if pm[x].color == chess.WHITE:
val += tval
else:
val -= tval
# add a number of legal moves term
bak = b.turn
b.turn = chess.WHITE
val += 0.1 * b.legal_moves.count()
b.turn = chess.BLACK
val -= 0.1 * b.legal_moves.count()
b.turn = bak
return val
class Valuator(object):
def __init__(self):
self.memo = {}
self.count = 0
self.values = [0, 1, 3, 3, 5, 9, 0] # From pawn to king
def reset(self):
self.count = 0
def __call__(self, s):
input_data = s.serialize()
output = model.predict(input_data.reshape(1, 5, 8, 8))
return output
def value(self, s):
b = s.board
# game over values
if b.is_game_over():
if b.result() == "1-0":
return MAXVAL
elif b.result() == "0-1":
return -MAXVAL
else:
return 0
val = 0.0
# piece values
pm = s.board.piece_map()
for x in pm:
tval = self.values[pm[x].piece_type]
if pm[x].color == chess.WHITE:
val += tval
else:
val -= tval
# add a number of legal moves term
bak = b.turn
b.turn = chess.WHITE
val += 0.1 * b.legal_moves.count()
b.turn = chess.BLACK
val -= 0.1 * b.legal_moves.count()
b.turn = bak
return val
def computer_minimax(s, v, depth, a, b, big=False):
#! Change the depth, plays good with 5 depth
if depth >= 5 or s.board.is_game_over():
return v(s)
# white is maximizing player
turn = s.board.turn
if turn == chess.WHITE:
ret = -MAXVAL
else:
ret = MAXVAL
if big:
bret = []
checks, captures, others = order_moves(s.board)
ordered_moves = checks + captures + others
# can prune here with beam search
isort = []
for e in ordered_moves:
s.board.push(e)
isort.append((v(s), e))
s.board.pop()
move = sorted(isort, key=lambda x: x[0], reverse=s.board.turn)
# beam search beyond depth 3
if depth >= 3:
move = move[:10]
for e in [x[1] for x in move]:
s.board.push(e)
tval = computer_minimax(s, v, depth+1, a, b)
s.board.pop()
if big:
bret.append((tval, e))
if turn == chess.WHITE:
ret = max(ret, tval)
a = max(a, ret)
if a >= b:
break # b cut-off
else:
ret = min(ret, tval)
b = min(b, ret)
if a >= b:
break # a cut-off
if big:
return ret, bret
else:
return ret
def print_statistics():
avg_time = sum(move_times) / len(move_times) if move_times else 0
print("Average time per move: {:.2f} seconds".format(avg_time))
print("Total moves: {}".format(len(moves)//2))
print("Moves: {}".format(' '.join(moves)))
print("Lichess analysis: https://lichess.org/analysis/{}".format(s.board.fen()))
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
print_statistics()
sys.exit(0)
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
signal.signal(signal.SIGINT, signal_handler)
def order_moves(board):
checks = []
captures = []
others = []
for move in board.legal_moves:
if board.is_check():
checks.append(move)
elif board.is_capture(move):
captures.append(move)
else:
others.append(move)
return checks, captures, others
def explore_leaves(s, v):
ret = []
start = time.time()
v.reset()
bval = v(s)
cval, ret = computer_minimax(s, v, 0, a=-MAXVAL, b=MAXVAL, big=True)
eta = time.time() - start
print(
"%.2f -> %.2f: explored %d nodes in %.3f seconds %d/sec"
% (bval, cval, v.count, eta, int(v.count / eta))
)
return ret
s = State()
v = ClassicValuator()
def to_svg(s):
return base64.b64encode(chess.svg.board(board=s.board).encode("utf-8")).decode("utf-8")
app = Flask(__name__)
@app.route("/")
def hello():
ret = open("index.html").read()
return ret.replace("start", s.board.fen())
@app.route("/move_coordinates")
def move_coordinates():
if not s.board.is_game_over():
source = int(request.args.get("from", default=""))
target = int(request.args.get("to", default=""))
promotion = (
True if request.args.get("promotion", default="") == "true" else False
)
move = s.board.san(
chess.Move(
source, target, promotion=chess.QUEEN if promotion else None
)
)
if move is not None and move != "":
try:
start_time = time.time()
s.board.push_san(move)
move_times.append(time.time() - start_time)
moves.append(move) # Append player move to moves list
computer_move(s, v)
except Exception:
traceback.print_exc()
return app.response_class(response="Illegal move", status=400)
return app.response_class(response=s.board.fen(), status=200)
else:
print("GAME IS OVER")
print_statistics() # Print statistics at end of game
return app.response_class(response="game over", status=200)
@app.route("/newgame")
def newgame():
s.board.reset()
response = app.response_class(response=s.board.fen(), status=200)
return response
def computer_move(s, v):
# computer move
move = sorted(explore_leaves(s, v), key=lambda x: x[0], reverse=s.board.turn)
if len(move) == 0:
return
print("top 3:")
for i, m in enumerate(move[0:3]):
print(" ", m)
print(("White" if s.board.turn else "Black"), "moving", move[0][1])
san_move = s.board.san(move[0][1]) # Convert move to SAN before pushing
s.board.push(move[0][1])
moves.append(san_move) # Append AI move to moves list
@app.route("/selfplay")
def selfplay():
while not s.board.is_game_over():
computer_move(s, v)
return "Game over"
if __name__ == "__main__":
app.run(debug=True)