-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveAlphaBeta.py
289 lines (239 loc) · 11 KB
/
NaiveAlphaBeta.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
import time
import gzip
import json
from typing import Dict
from random import shuffle
from Goban import Board
from playerInterface import PlayerInterface
def eval_fn(board: Board, point_of_view: int) -> float:
""" Naive evaluation function, comparing scores of both players. """
black_score, white_score = board.compute_score()
if board.is_game_over():
if black_score > white_score:
winner = Board._BLACK
elif black_score < white_score:
winner = Board._WHITE
else:
winner = Board._EMPTY
if winner == point_of_view:
return float('inf')
else:
return float('-inf')
score_feature = black_score - white_score if point_of_view == Board._BLACK else white_score - black_score
liberty_feature = 0
ennemy_color = Board._BLACK if point_of_view == Board._WHITE else Board._WHITE
for fcoord in range(len(board)): # makes use of __len__
cell = board[fcoord] # makes use of __getitem__
if cell == ennemy_color:
liberty_feature -= board._stringLiberties[board._getStringOfStone(fcoord)]
elif cell == point_of_view:
liberty_feature += board._stringLiberties[board._getStringOfStone(fcoord)]
return score_feature * 10 + liberty_feature
POSITION_SCORE = [0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 2, 2, 1, 2, 2, 1, 0,
0, 1, 2, 1, 1, 1, 2, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 2, 1, 1, 1, 2, 1, 0,
0, 1, 2, 2, 1, 2, 2, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]
def evaluate(board: Board, point_of_view: int) -> float:
""" Multivariate evaluation function. """
black_score, white_score = board.compute_score()
if board.is_game_over():
if black_score > white_score:
winner = Board._BLACK
elif black_score < white_score:
winner = Board._WHITE
else:
winner = Board._EMPTY
if winner == point_of_view:
return float('inf')
else:
return float('-inf')
score_feature = black_score - white_score if point_of_view == Board._BLACK else white_score - black_score
liberty_feature = 0
position_feature = 0
ennemy_color = Board._BLACK if point_of_view == Board._WHITE else Board._WHITE
for fcoord in range(len(board)): # makes use of __len__
cell = board[fcoord] # makes use of __getitem__
if cell == ennemy_color:
liberty_feature -= board._stringLiberties[board._getStringOfStone(fcoord)]
position_feature -= POSITION_SCORE[fcoord]
elif cell == point_of_view:
liberty_feature += board._stringLiberties[board._getStringOfStone(fcoord)]
position_feature += POSITION_SCORE[fcoord]
return score_feature * 5 + liberty_feature * 2 + position_feature
with gzip.open("samples-9x9.json.gz") as fz:
tables = json.loads(fz.read().decode("utf-8"))
def search_in_tables(move_history):
len_history = len(move_history)
next_to_play = "black" if len_history % 2 == 0 else "white"
best_table = None
best_wr = -1
for table in tables:
if len_history >= table['depth']:
continue
if move_history == table['list_of_moves'][:len_history] \
and table[next_to_play + "_wins"] / table['rollouts'] > best_wr:
best_table = table
best_wr = table[next_to_play + "_wins"] / table['rollouts']
return best_table['list_of_moves'][len_history] if best_table else None
def get_book_move(board: Board) -> str:
"""
Return the next move from the opening book.
"""
move_history = board._historyMoveNames
move = search_in_tables(move_history)
return move
class IterativeDeepeningAlphaBeta:
"""
Iterative Deepening Alpha Beta with move ordering after each depth
"""
def __init__(self, time_per_move: float, eval_fn):
self.time_per_move = time_per_move
self.eval_fn = eval_fn
self.start_time = 0
self.transposition_table = {} # hash -> {depth: int, score: float}
def is_out_of_time(self):
return time.monotonic() - self.start_time > self.time_per_move
def alpha_beta(self, board: Board, depth: int, alpha: float, beta: float, maximizing_player: bool,
point_of_view: int, activate_timer: bool = True) -> float:
if activate_timer and self.is_out_of_time():
return 0 # The return value is not used anyway in this case.
transposition = self.transposition_table.get(str(board._currentHash))
if transposition is not None and transposition['depth'] >= depth:
# Use the transposition table to speed up the search.
return transposition['score']
if depth == 0 or board.is_game_over():
# We are at the leaf of the tree.
utility = self.eval_fn(board, point_of_view)
self.transposition_table[str(board._currentHash)] = {'depth': depth, 'score': utility}
return utility
if maximizing_player:
best_value = -float('inf')
for move in board.legal_moves():
board.push(move)
value = self.alpha_beta(board, depth - 1, alpha, beta, False, point_of_view, activate_timer)
board.pop()
best_value = max(best_value, value)
alpha = max(alpha, best_value)
if beta <= alpha:
break
# Store the best value in the transposition table.
self.transposition_table[str(board._currentHash)] = {'depth': depth, 'score': best_value}
return best_value
else:
best_value = float('inf')
for move in board.legal_moves():
board.push(move)
value = self.alpha_beta(board, depth - 1, alpha, beta, True, point_of_view, activate_timer)
board.pop()
best_value = min(best_value, value)
beta = min(beta, best_value)
if beta <= alpha:
break
# Store the best value in the transposition table.
self.transposition_table[str(board._currentHash)] = {'depth': depth, 'score': best_value}
return best_value
def get_action(self, board: Board, color_to_play: int, max_depth: int = 20, activate_timer: bool = True) -> int:
"""
Returns the best action for the current board using iterative deepening on alpha beta pruning.
Move ordering is done after each depth.
"""
self.start_time = time.monotonic()
legal_moves = board.legal_moves()
shuffle(legal_moves) # Randomize the order of the moves, so that the moves aren't always the same.
current_depth_evaluations: Dict[int, float] = {} # {move: evaluation}
depth = 0
prev_best_move = legal_moves[0]
while depth <= max_depth: # Iterative deepening.
self.transposition_table = {}
best_move = legal_moves[0]
best_value = -float('inf')
# Analyze the current depth
for move in legal_moves:
board.push(move)
value = self.alpha_beta(board, depth, -float('inf'), float('inf'), False, color_to_play,
activate_timer and depth != 1)
board.pop()
if self.is_out_of_time() and depth != 1:
print(f"Played until depth {depth - 1}")
return prev_best_move
current_depth_evaluations[move] = value
# Order legal_moves according to the new evaluations
legal_moves = sorted(legal_moves, key=lambda m: current_depth_evaluations[m], reverse=True)
# Update best_move and best_value
for move in legal_moves:
if current_depth_evaluations[move] > best_value:
best_move = move
best_value = current_depth_evaluations[move]
prev_best_move = best_move
# Update depth
depth += 1
print(f"Played to depth {depth - 1}")
return prev_best_move
class myPlayer(PlayerInterface):
def __init__(self):
self.state: Board = Board()
self.player_fallback = IterativeDeepeningAlphaBeta(time_per_move=1000,
eval_fn=evaluate) # For this player, time won't matter anyway, as we won't activate it.
self.player_early = IterativeDeepeningAlphaBeta(time_per_move=7, eval_fn=eval_fn)
self.player_mid = IterativeDeepeningAlphaBeta(time_per_move=25, eval_fn=eval_fn)
self.color = None
self.turn = 0
self.activate_book = True
self.time_left = 15 * 60
def getPlayerName(self):
return "Dans La Légende"
def _getMove(self, state: Board, color: int, player: IterativeDeepeningAlphaBeta,
max_depth: int = 20, activate_timer: bool = True) -> int:
"""
Returns the best move for the current board using iterative deepening on alpha beta pruning.
Move ordering is done after each depth.
"""
if self.activate_book:
move = get_book_move(state)
else:
move = None
if move is None:
self.activate_book = False # Don't use the book in the following turns.
return player.get_action(state, color, max_depth=max_depth, activate_timer=activate_timer)
else:
print("Using book move.")
return Board.name_to_flat(move)
def getPlayerMove(self):
self.turn += 1
if self.state.is_game_over():
return "PASS"
start_time = time.monotonic()
if self.time_left < 5:
move = self._getMove(self.state, self.color, self.player_fallback, max_depth=0, activate_timer=False)
elif self.turn < 20:
move = self._getMove(self.state, self.color, self.player_fallback, max_depth=0, activate_timer=False)
elif self.turn < 50:
move = self._getMove(self.state, self.color, self.player_early)
else:
move = self._getMove(self.state, self.color, self.player_mid)
end_time = time.monotonic()
self.time_left -= (end_time - start_time)
print("Time left:", self.time_left)
self.state.push(move)
return Board.flat_to_name(move)
def playOpponentMove(self, move):
self.turn += 1
self.state.push(Board.name_to_flat(move))
def newGame(self, color):
self.color = color
self.state = Board()
self.turn = 0
def endGame(self, winner):
if winner == self.color:
print("Plus je me rapproche du sommet, plus j’entends le ciel qui gronde.")
else:
print("Recherche du bonheur, j’m’enfonce dans le vide.")
if __name__ == "__main__":
player = myPlayer()
player.newGame(Board._BLACK)
player.getPlayerMove()