-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_net.py
213 lines (167 loc) · 5.84 KB
/
chess_net.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
import numpy as np
import re
import torch
from torch import nn
from torch.nn import functional as F
import chess
import chess.pgn
import collections
class ChessNet(nn.Module):
def __init__(self, hidden_layers=4, hidden_size=200):
super(ChessNet, self).__init__()
self.hidden_layers = hidden_layers
self.input_layer = nn.Conv2d(6, hidden_size, 3, stride=1, padding=1)
self.module_list = nn.ModuleList(
[module(hidden_size) for i in range(hidden_layers)]
)
self.output_layer = nn.Conv2d(hidden_size, 2, 3, stride=1, padding=1)
def forward(self, x):
x = self.input_layer(x)
x = F.relu(x)
for i in range(self.hidden_layers):
x = self.module_list[i](x)
x = self.output_layer(x)
return x
class module(nn.Module):
def __init__(self, hidden_size):
super(module, self).__init__()
self.conv1 = nn.Conv2d(hidden_size, hidden_size, 3, stride=1, padding=1)
self.conv2 = nn.Conv2d(hidden_size, hidden_size, 3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(hidden_size)
self.bn2 = nn.BatchNorm2d(hidden_size)
self.activation1 = nn.SELU()
self.activation2 = nn.SELU()
def forward(self, x):
x_input = torch.clone(x)
x = self.conv1(x)
x = self.bn1(x)
x = self.activation1(x)
x = self.conv2(x)
x = self.bn2(x)
x = x + x_input # residual connections
x = self.activation2(x)
return x
letter_to_num = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7}
num_to_letter = {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h"}
def board_to_rep(board):
pieces = ["p", "r", "n", "k", "q", "b"]
layers = []
for piece in pieces:
layers.append(create_rep_layer(board, piece))
board_rep = np.stack(layers)
return board_rep
def create_rep_layer(board, type):
s = str(board)
s = re.sub(f"[^{type}{type.upper()} \n]", ".", s)
s = re.sub(f"{type}", "-1", s)
s = re.sub(f"{type.upper()}", "1", s)
s = re.sub(f"\.", "0", s)
board_mat = []
for row in s.split("\n"):
row = row.split(" ")
row = [int(x) for x in row]
board_mat.append(row)
return np.array(board_mat)
def move_to_rep(move, board):
board.push_san(move).uci()
move = str(board.pop())
from_output_layer = np.zeros((8, 8))
from_row = 8 - int(move[1])
from_column = letter_to_num[move[0]]
from_output_layer[from_row, from_column] = 1
to_output_layer = np.zeros((8, 8))
to_row = 8 - int(move[3])
to_column = letter_to_num[move[2]]
to_output_layer[to_row, to_column] = 1
return np.stack([from_output_layer, to_output_layer])
def create_move_list(s):
return re.sub("\d*\. ", "", s).split(" ")[:-1]
def check_mate_single(board):
board = board.copy()
legal_moves = list(board.legal_moves)
for move in legal_moves:
board.push_uci(str(move))
if board.is_checkmate():
move = board.pop()
return move
_ = board.pop()
def distribution_over_moves(vals):
probs = np.array(vals)
probs = np.exp(probs)
probs = probs / probs.sum()
probs = probs**3
probs = probs / probs.sum()
return probs
def choose_semi_random_move(model, board, color, device):
legal_moves = list(board.legal_moves)
move = check_mate_single(board)
if move is not None:
return move
x = torch.Tensor(board_to_rep(board)).float().to(device)
if color == chess.BLACK:
x *= 1
x = x.unsqueeze(0)
move = predict(model, x)
vals = []
froms = [str(legal_move)[:2] for legal_move in legal_moves]
froms = list(set(froms))
for from_ in froms:
# val = move[0, :, :][8 - int(from_[1]), letter_to_num[from_[0]]]
val = move[0, 0, 8 - int(from_[1]), letter_to_num[from_[0]]]
vals.append(val)
probs = distribution_over_moves(vals)
chosen_from = str(np.random.choice(froms, size=1, p=probs)[0])[:2]
vals = []
for legal_move in legal_moves:
from_ = str(legal_move)[:2]
if from_ == chosen_from:
to = str(legal_move)[2:]
# val = move[1, :, :][8 - int(to[1]), letter_to_num[to[0]]]
val = move[0, 1, 8 - int(to[1]), letter_to_num[to[0]]]
vals.append(val)
else:
vals.append(0)
chosen_move = legal_moves[np.argmax(vals)]
return chosen_move
def predict(model, x):
model.eval()
with torch.no_grad():
outputs = model(x)
return outputs
def simulate_game_between_models(model1, model2, device):
board = chess.Board()
color = chess.WHITE
while not board.is_game_over():
if color is chess.WHITE:
move = choose_semi_random_move(model1, board, color, device)
color = chess.BLACK
else:
move = choose_semi_random_move(model2, board, color, device)
color = chess.WHITE
board.push(move)
return board_to_game(board)
def board_to_game(board):
game = chess.pgn.Game()
# Undo all moves.
switchyard = collections.deque()
while board.move_stack:
switchyard.append(board.pop())
game.setup(board)
node = game
# Replay all moves.
while switchyard:
move = switchyard.pop()
node = node.add_variation(move)
board.push(move)
game.headers["Result"] = board.result()
return game
if __name__ == "__main__":
if torch.cuda.is_available():
device = torch.device("cuda")
print("CUDA is available. Training on GPU.")
else:
device = torch.device("cpu")
print("CUDA not available. Training on CPU.")
model1 = ChessNet(hidden_layers=4, hidden_size=200).to(device)
model1.load_state_dict(torch.load("chess_model_epoch_300.pth", map_location=device))
print(simulate_game_between_models(model1, model1, device))