-
Notifications
You must be signed in to change notification settings - Fork 1
/
chesstakov_negamax_vs_random.py
129 lines (100 loc) · 3.2 KB
/
chesstakov_negamax_vs_random.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
import chess
import random
import numpy as np
import sys
from parser import position_to_vector
from keras.models import load_model
CHECKMATE_SCORE = 10
def choose_random_position(board):
moves = []
for move in board.legal_moves:
board.push(chess.Move.from_uci(str(move)))
if (board.is_checkmate()):
board.pop()
return (str(move))
moves.append(str(move))
board.pop()
return random.choice(moves)
def compare_positions(model, p1, p2):
X1 = np.array([p1])
X2 = np.array([p2])
r = model.predict(X1, X2)
if (r[0][0] > 0.5):
return 0
else:
return 1
def negamax_base(model, board):
base_position = position_to_vector(board)
alpha = float('-inf')
beta = float('inf')
score, move = negamax(model, base_position, board, 3, alpha, beta, 0)
return move
def negamax(model, base_position, board, depth, alpha, beta, color):
moves = []
X = []
for move in board.legal_moves:
moves.append(str(move))
board.push(chess.Move.from_uci(str(move)))
X.append(position_to_vector(board))
board.pop()
if (len(X) <= 0):
return Exception(''), Exception()
X_base = np.repeat([base_position], len(X), axis=0)
scores = model.predict([np.array(X), X_base])
scores = scores[:, color]
for i, move in enumerate(moves):
board.push(chess.Move.from_uci(move))
if board.is_checkmate():
scores[i] = CHECKMATE_SCORE
board.pop()
child_nodes = sorted(zip(scores, moves), reverse=True)
best_value = float('-inf')
best_move = None
for score, move in child_nodes:
if depth == 1 or score == CHECKMATE_SCORE:
value = score
else:
board.push(chess.Move.from_uci(move))
neg_value, _ = negamax(model, base_position, board, depth - 1, -alpha, -beta, 1 - color)
value = -neg_value
board.pop()
if value > best_value:
best_value = value
best_move = move
if value > alpha:
alpha = value
if alpha > beta:
break
return best_value, best_move
def main(model_filename):
chesstakov = load_model(model_filename)
white = .0
black = .0
tie = .0
while True:
board = chess.Board()
turn = chess.WHITE
number_turns = 0
while (not board.result() != '*'):
play = None
if (turn == chess.WHITE):
number_turns = number_turns + 1
play = negamax_base(chesstakov, board)
turn = chess.BLACK
else:
play = choose_random_position(board)
turn = chess.WHITE
board.push(chess.Move.from_uci(play))
r = board.result()
if (r == '1-0'):
white = white + 1
elif (r == '0-1'):
black = black + 1
else:
tie = tie + 1
total = white + black + tie
print 'white: %d (%.3f) black: %d (%.3f) tie: %d (%.3f)' % \
(white, (100 * white / total), black, 100 * black / total, tie, 100 * tie / total)
if __name__ == '__main__':
if (len(sys.argv) == 2):
main(sys.argv[1])