-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
336 lines (272 loc) · 10.8 KB
/
environment.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from random import choice
import chess
import gym
import numpy as np
import pygame
import torch
from gym import spaces
from constants import N_ACTIONS, device
# Constants for the chessboard
BOARD_SIZE = 1000
SQUARE_SIZE = BOARD_SIZE // 8
COLORS = {
"white": (255, 255, 255),
"black": (150, 150, 150),
"highlight": (100, 200, 100),
}
SYMBOL_TO_PIECE_NAME = {
"k": "king",
"q": "queen",
"r": "rook",
"b": "bishop",
"n": "knight",
"p": "pawn",
}
class ChessEnv(gym.Env):
def __init__(self, max_turns=1e9):
self.board = chess.Board()
# Actions: 64 possible 'from' squares * 64 possible 'to' squares * 5 possible promotions
self.action_space = spaces.Discrete(N_ACTIONS)
# Observation: 8x8 board with integer values representing pieces
self.observation_space = spaces.Box(low=-6, high=6, shape=(8, 8), dtype=np.int8)
self.render_function = self.init_render
self.max_turns = max_turns
self.current_turn = 0
# Define piece values
self.piece_values = {
chess.PAWN: 1,
chess.KNIGHT: 3,
chess.BISHOP: 3,
chess.ROOK: 5,
chess.QUEEN: 9,
chess.KING: 0,
}
def __del__(self):
pygame.quit()
def calculate_material_balance(self):
"""Calculate the material difference (white - black)"""
white_material = 0
black_material = 0
for _, piece in self.board.piece_map().items():
value = self.piece_values[piece.piece_type]
if piece.color == chess.WHITE:
white_material += value
else:
black_material += value
return white_material - black_material
def move_to_action(self, move):
from_square = move.from_square
to_square = move.to_square
promotion = move.promotion
# Total number of possible moves without promotions
non_promotion_moves = 64 * 63
if promotion is None:
# Non-promotion move
action = from_square * 63 + to_square
if to_square > from_square:
action -= 1
return action
# Promotion move
from_file = chess.square_file(from_square)
to_file = chess.square_file(to_square)
promotion_map = {
chess.QUEEN: 0,
chess.ROOK: 1,
chess.BISHOP: 2,
chess.KNIGHT: 3,
}
action = (
non_promotion_moves
+ (from_file * 32)
+ (to_file * 4)
+ promotion_map[promotion]
)
return action
def turn(self):
return self.board.turn
# Helper function to convert UCI string to Move object
@staticmethod
def uci_to_move(uci_string):
return chess.Move.from_uci(uci_string)
def sample(self):
move = choice(list(self.board.legal_moves))
return self.move_to_action(move)
def init_graphics(self):
pygame.display.set_mode((BOARD_SIZE, BOARD_SIZE))
pygame.display.set_caption("Chess Environment")
def render_no_init(self, _="human"):
self._draw_board()
pygame.display.flip()
def init_render(self, _="human"):
self.init_graphics()
self.render_no_init()
self.render_function = self.render_no_init
def render(self, mode="human"):
self.render_function(mode)
def _draw_board(self):
screen = pygame.display.get_surface()
screen.fill(COLORS["black"])
for i in range(8):
for j in range(8):
color = COLORS["white"] if (i + j) % 2 == 0 else COLORS["black"]
pygame.draw.rect(
screen,
color,
(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE),
)
for square, piece in self.board.piece_map().items():
self._draw_piece(piece, square)
def _draw_piece(self, piece, square):
color = "black"
if piece.color:
color = "white"
screen = pygame.display.get_surface()
image = pygame.image.load(
"images/"
+ SYMBOL_TO_PIECE_NAME[piece.symbol().lower()]
+ "_"
+ color
+ ".png"
)
image = pygame.transform.scale(image, (SQUARE_SIZE, SQUARE_SIZE))
rank, file = divmod(square, 8)
screen.blit(image, (file * SQUARE_SIZE, (7 - rank) * SQUARE_SIZE))
def reset(self, *, seed=None, options=None, max_turns=1e9):
if seed is not None or options is not None:
print("WARNING: seed and option args not used")
self.max_turns = max_turns
self.current_turn = 0
self.board.reset()
return self._get_observation()
def step(self, action):
# Convert the action to a chess move
from_square, to_square, promotion = self.action_to_move(action)
move = chess.Move(from_square, to_square, promotion=promotion)
self.current_turn += 1
# Check if the move is legal
if move in self.board.legal_moves:
# Perform the move on the chess board
self.board.push(move)
# Check if the game is over
done = self.board.is_game_over()
# Calculate the reward based on the game outcome
if self.board.is_checkmate():
reward = 100000.0 if self.board.turn == chess.WHITE else -1.0
elif self.board.is_stalemate() or self.board.is_insufficient_material():
reward = 0.0
elif self.current_turn >= self.max_turns:
done = True
reward = self.calculate_material_balance()
else:
reward = self.calculate_material_balance()
# Get the new observation after the move
observation = self._get_observation()
return observation, reward, done, {}
# Invalid move, return the current observation and a negative reward
return self._get_observation(), -1.0, False, {}
def action_to_move(self, action):
# Total number of possible moves without promotions
non_promotion_moves = 64 * 63
if action < non_promotion_moves:
# Non-promotion move
from_square = action // 63
to_square = action % 63
if to_square >= from_square:
to_square += 1
return (chess.SQUARES[from_square], chess.SQUARES[to_square], None)
# Promotion move
action -= non_promotion_moves
from_file = action // (8 * 4)
to_file = (action % (8 * 4)) // 4
# Determine the rank based on the color to play
from_rank = 6 if self.board.turn == chess.WHITE else 1
to_rank = 7 if self.board.turn == chess.WHITE else 0
from_square = chess.square(from_file, from_rank)
to_square = chess.square(to_file, to_rank)
return (from_square, to_square, chess.QUEEN)
def _get_observation(self):
# Convert the board state to a feature representation
feature_matrix = np.zeros((8, 8), dtype=np.int64)
for square, piece in self.board.piece_map().items():
rank, file = divmod(square, 8)
piece_type = piece.piece_type
color = piece.color
# Assign integer values based on piece type and color
piece_value = piece_type
if color == chess.BLACK:
piece_value *= -1
feature_matrix[rank, file] = piece_value
return feature_matrix.flatten()
def explain_action(self, action):
if action is None:
return "Invalid move"
from_square, to_square, promotion = self.action_to_move(action)
# Get the piece at the from_square
piece = self.board.piece_at(from_square)
# Convert the square indices to algebraic notation
from_square_str = chess.SQUARE_NAMES[from_square]
to_square_str = chess.SQUARE_NAMES[to_square]
if piece is None:
return f"Invalid move: {from_square_str}{to_square_str}"
# Get the piece type and color
piece_type = chess.PIECE_NAMES[piece.piece_type]
color = "White" if piece.color == chess.WHITE else "Black"
# Create the move explanation
move_explanation = (
f"{color} {piece_type} moves from {from_square_str} to {to_square_str}"
)
# Handle promotion
if promotion is not None:
promotion_piece = chess.PIECE_NAMES[promotion]
move_explanation += f" and promotes to {promotion_piece}"
return move_explanation
def notation_to_action(self, notation):
try:
# Parse the notation using python-chess
move = chess.Move.from_uci(notation)
return self.move_to_action(move)
except ValueError:
return None
def get_legal_moves_mask(self):
"""Create a mask of legal moves in the current position"""
mask = torch.zeros(N_ACTIONS, device=device)
for legal_move in self.board.legal_moves:
action = self.move_to_action(legal_move)
mask[action] = 1
return mask
class InteractiveEnvironment(ChessEnv):
def play(self):
while not self.board.is_game_over():
self.render()
action = self.notation_to_action(input("Make a move: "))
print(self.explain_action(action))
if action is None:
continue
_, reward, done, _ = self.step(action)
print(f"Reward: {reward}, Done: {done}")
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# pygame.quit()
# return
# elif event.type == pygame.MOUSEBUTTONDOWN:
# if event.button == 1: # Left mouse button
# from_square = self._get_square_from_mouse(event.pos)
# to_square = self._get_square_from_mouse(event.pos)
# if from_square != to_square:
# move = chess.Move(from_square, to_square)
# if move in self.board.legal_moves:
# observation, reward, done, _ = self.step(move)
# print(f"Reward: {reward}, Done: {done}")
self.render()
result = self.board.result()
print(f"Game Over. Result: {result}")
def _get_square_from_mouse(self, pos):
file, rank = pos[0] // SQUARE_SIZE, 7 - pos[1] // SQUARE_SIZE
return chess.square(file, rank)
if __name__ == "__main__":
env = InteractiveEnvironment()
for i in range(N_ACTIONS):
from_square, to_square, promotion = env.action_to_move(i)
check = env.move_to_action(chess.Move(from_square, to_square, promotion))
assert check == i
# env.play()