-
Notifications
You must be signed in to change notification settings - Fork 14
/
Agents.py
379 lines (314 loc) · 12.5 KB
/
Agents.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from chess import Move, Board
import ai.ai as ai
import Evaluators as ev
import random
import functools
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Manager
avg = []
timer_on = False
def timer(func):
def wrapper(*args, **kwargs):
if not timer_on:
return func(*args, **kwargs)
start = time.perf_counter()
result = func(*args, **kwargs)
fn_time = time.perf_counter() - start
print(f"Time taken in seconds : {fn_time:0.4f}")
avg.append(fn_time)
print(f"Average time per move : {sum(avg)/len(avg):0.4f}")
return result
return wrapper
class Agent():
def __init__(self) -> None:
self.evaluator = ev.nn_eval
self.depth = 4
def get_move(self, board_state: Board) -> Move:
pass
def log_info(self, board_state: Board):
pass
class MinimaxAgent(Agent):
def get_move(self, board_state: Board) -> Move:
def minimax(board, depth, Player): #Player will be a value true if it is the player false if it is the oponenet
# return evaluation if depth is zero or if the board state is gameover
if depth == 0 or board.is_game_over():
boardfen = board.fen()
return self.evaluator(board)
if board_state.turn == Player:
max_eval = float("-inf")
for move in list(board.legal_moves):
child_board = board.copy()
child_board.push(move)
evaluation = minimax(child_board , depth - 1, False)
max_eval = max(max_eval, evaluation)
return max_eval
else:
min_eval = float("inf")
for move in list(board.legal_moves):
child_board = board.copy()
child_board.push(move)
evaluation = minimax(child_board , depth - 1, True)
min_eval = min(min_eval, evaluation)
return min_eval
best_move = None;
best_score = float('-inf')
for move in list(board_state.legal_moves):
test_board = board_state.copy()
test_board.push(move)
score = minimax(test_board, self.depth, board_state.turn)
if score > best_score:
best_score = score
best_move = move
return best_move;
def log_info(self, board_state: Board):
pass
class AlphaBetaAgent(Agent):
# @timer
def get_move(self, board_state: Board) -> Move:
def minimax(board, depth, alpha, beta, Player):
if depth == 0 or board.is_game_over(): # Add your game_over function
return self.evaluator(board)
if Player:
max_eval = float("-inf")
for move in board.legal_moves:
board.push(move)
evaluation = minimax(board, depth - 1, alpha, beta, False)
board.pop()
max_eval = max(max_eval, evaluation)
alpha = max(alpha, evaluation)
if beta <= alpha:
break
return max_eval
else:
min_eval = float("inf")
for move in board.legal_moves:
board.push(move)
evaluation = minimax(board, depth - 1, alpha, beta, True)
board.pop()
min_eval = min(min_eval, evaluation)
beta = min(beta, evaluation)
if beta <= alpha:
break
return min_eval
best_move = None;
best_score = float('-inf')
for move in list(board_state.legal_moves):
test_board = board_state.copy()
test_board.push(move)
score = minimax(test_board, self.depth, float("-inf"), float("inf"), board_state.turn)
if score > best_score:
best_score = score
best_move = move
return best_move;
def log_info(self, board_state: Board):
pass
class TranspositionAgent(Agent):
def __init__(self):
super().__init__()
self.transposition_table = {}
def minimax(self, board, depth, alpha, beta, Player):
board_hash = board.fen()
if board_hash in self.transposition_table:
return self.transposition_table[board_hash]
if depth == 0 or board.is_game_over():
score = self.evaluator(board)
self.transposition_table[board_hash] = (score, None)
return (score, None)
best_move = None
if Player:
max_eval = float("-inf")
for move in board.legal_moves:
board.push(move)
evaluation, _ = self.minimax(board, depth - 1, alpha, beta, False)
board.pop()
if evaluation > max_eval:
max_eval = evaluation
best_move = move
alpha = max(alpha, evaluation)
if beta <= alpha:
break
else:
max_eval = float("inf")
for move in board.legal_moves:
board.push(move)
evaluation, _ = self.minimax(board, depth - 1, alpha, beta, True)
board.pop()
if evaluation < max_eval:
max_eval = evaluation
best_move = move
beta = min(beta, evaluation)
if beta <= alpha:
break
self.transposition_table[board_hash] = (max_eval, best_move)
return (max_eval, best_move)
def get_move(self, board_state: Board) -> Move:
self.transposition_table = {}
best_score, best_move = self.minimax(board_state, self.depth, float("-inf"), float("inf"), board_state.turn)
return best_move
class SortedTranspositionAgent(Agent):
def __init__(self):
super().__init__()
self.transposition_table = {}
self.things_done = 0
# @functools.lru_cache(maxsize=100000)
def move_sorter(self, board):
scores = {}
for move in board.legal_moves:
if board.is_capture(move):
scores[move] = 10
elif board.gives_check(move):
scores[move] = 9
else:
scores[move] = 1
return scores
# @functools.lru_cache(maxsize=100000)
def minimax(self, board, depth, alpha, beta, maximizing):
# self.things_done += 1
board_hash = str(board.fen())
if board_hash in self.transposition_table:
return self.transposition_table[board_hash]
if depth == 0 or board.is_game_over():
score = self.evaluator(board)
self.transposition_table[board_hash] = (score, None)
return (score, None)
best_move = None
moves = list(board.legal_moves)
move_scores = self.move_sorter(board)
moves.sort(key=lambda move: move_scores.get(move, 0), reverse=True)
if maximizing:
min_eval = float("-inf")
for move in moves:
board.push(move)
evaluation, _ = self.minimax(board, depth - 1, alpha, beta, False)
board.pop()
if evaluation > min_eval:
min_eval = evaluation
best_move = move
alpha = max(alpha, evaluation)
if beta <= alpha:
break
else:
min_eval = float("inf")
for move in moves:
board.push(move)
evaluation, _ = self.minimax(board, depth - 1, alpha, beta, True)
board.pop()
if evaluation < min_eval:
min_eval = evaluation
best_move = move
beta = min(beta, evaluation)
if beta <= alpha:
break
self.transposition_table[board_hash] = (min_eval, best_move)
return (min_eval, best_move)
@timer
# @functools.lru_cache(maxsize=100000)
def get_move(self, board_state: Board) -> Move:
self.transposition_table = {}
best_move = None
# curr = self.things_done
_, best_move = self.minimax(board_state, self.depth, float("-inf"), float("inf"), board_state.turn)
# print(self.things_done - 0, "computations done")
return best_move
# class MultiProcessAgent(Agent):
# def __init__(self):
# super().__init__()
# # self.transposition_table = {}
# manager = Manager()
# self.transposition_table = manager.dict()
# # self.things_done = 0
# def move_sorter(self, board):
# scores = {}
# for move in board.legal_moves:
# if board.is_capture(move):
# scores[move] = 10
# elif board.gives_check(move):
# scores[move] = 9
# else:
# scores[move] = 1
# return scores
# def minimax(self, board, depth, alpha, beta, maximizing):
# board_hash = str(board.fen())
# if board_hash in self.transposition_table:
# return self.transposition_table[board_hash]
# if depth == 0 or board.is_game_over():
# score = self.evaluator(board)
# self.transposition_table[board_hash] = (score, None)
# return (score, None)
# best_move = None
# moves = list(board.legal_moves)
# move_scores = self.move_sorter(board)
# moves.sort(key=lambda move: move_scores.get(move, 0), reverse=True)
# min_eval = float("-inf") if maximizing else float("inf")
# with ProcessPoolExecutor() as executor:
# # Create future to move mapping
# future_to_move = {}
# for move in moves:
# new_board = board.copy()
# new_board.push(move)
# future = executor.submit(self.minimax, new_board, depth - 1, alpha, beta, not maximizing)
# future_to_move[future] = move
# for future in as_completed(future_to_move):
# move = future_to_move[future]
# try:
# evaluation, _ = future.result()
# except Exception as e:
# print(f"Exception: {e}")
# continue
# if maximizing:
# if evaluation > min_eval:
# min_eval = evaluation
# best_move = move
# alpha = max(alpha, evaluation)
# else:
# if evaluation < min_eval:
# min_eval = evaluation
# best_move = move
# beta = min(beta, evaluation)
# if beta <= alpha:
# break
# self.transposition_table[board_hash] = (min_eval, best_move)
# return (min_eval, best_move)
# @timer
# def get_move(self, board_state: Board) -> Move:
# self.transposition_table = {}
# best_move = None
# _, best_move = self.minimax(board_state, self.depth, float("-inf"), float("inf"), board_state.turn)
# return best_move
class FirstAgent(Agent):
def get_move(self, board_state: Board) -> Move:
return list(board_state.legal_moves)[0]
def log_info(self, board_state: Board):
pass
class RandomAgent(Agent):
# @timer
def get_move(self, board_state: Board) -> Move:
options = list(board_state.legal_moves)
return random.choice(options)
def log_info(self, board_state: Board):
pass
class MouseAgent(Agent):
def __init__(self, ui):
self.ui = ui
if not self.ui:
raise Exception("MouseAgent requires a UI to be passed in")
def get_move(self, board_state: Board) -> Move:
return self.ui.get_user_request()
def log_info(self, board_state: Board):
pass
class StockfishAgent(Agent):
def __init__(self) -> None:
self.skill_level = 4
# self.ai = ai
ai.stockfish.set_skill_level(self.skill_level)
def get_move(self, board_state: Board) -> Move:
best_move = ai.getMove(board_state.fen())
best_move = Move.from_uci(best_move)
return best_move
def set_skill(self, skill):
if skill < 20:
self.skill_level = skill
# self.ai.stockfish.set_skill_level(skill)
def log_info(self, board_state: Board):
pass