-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_best_move.py
377 lines (313 loc) · 16 KB
/
get_best_move.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
# from domino_game_analyzer import GameState, DominoTile, PlayerPosition, PlayerPosition_SOUTH, PlayerPosition_NORTH
import math
from dataclasses import dataclass
from typing import FrozenSet
type PlayerPosition = int
PlayerPosition_SOUTH = 0
PlayerPosition_EAST = 1
PlayerPosition_NORTH = 2
PlayerPosition_WEST = 3
def next_player(pos: PlayerPosition)-> PlayerPosition:
return (pos + 1) % 4
PlayerPosition_names = ['SOUTH', 'EAST', 'NORTH', 'WEST']
@dataclass(frozen=True)
class DominoTile:
top: int
bottom: int
@classmethod
def new_tile(cls, top: int, bottom: int) -> 'DominoTile':
return cls(min(top, bottom), max(top, bottom))
@classmethod
def loi_to_domino_tiles(cls, tuple_list: list[tuple[int, int]]) -> list['DominoTile']:
return [DominoTile.new_tile(left, right) for left, right in tuple_list]
def __repr__(self):
return f"{self.top}|{self.bottom}"
def can_connect(self, end: int|None) -> bool:
if end is None: # This allows any tile to be played on an empty board
return True
return self.top == end or self.bottom == end
def get_other_end(self, connected_end: int) -> int:
return self.bottom if connected_end == self.top else self.top
def get_pip_sum(self) -> int:
return self.top + self.bottom
@dataclass(frozen=True)
class GameState:
player_hands: tuple[FrozenSet[DominoTile], ...]
current_player: PlayerPosition
left_end: int|None
right_end: int|None
consecutive_passes: int
def __hash__(self):
return hash((self.player_hands, self.current_player, self.left_end, self.right_end, self.consecutive_passes))
@classmethod
def new_game(cls, player_hands: list[list[DominoTile]]):
return cls(
# player_hands=tuple(frozenset(DominoTile(top, bottom) for top, bottom in hand) for hand in player_hands),
player_hands=tuple(frozenset(tile for tile in hand) for hand in player_hands),
current_player=PlayerPosition_SOUTH,
left_end=None,
right_end=None,
consecutive_passes=0
)
def play_hand(self, tile: DominoTile, left: bool) -> 'GameState':
new_hands = list(self.player_hands)
new_hands[self.current_player] = self.player_hands[self.current_player] - frozenset([tile])
if self.left_end is None or self.right_end is None:
new_left_end, new_right_end = tile.top, tile.bottom
elif left:
new_left_end = tile.get_other_end(self.left_end)
new_right_end = self.right_end
else:
new_left_end = self.left_end
new_right_end = tile.get_other_end(self.right_end)
return GameState(
player_hands=tuple(new_hands),
# current_player=self.current_player.next(),
current_player=next_player(self.current_player),
left_end=new_left_end,
right_end=new_right_end,
consecutive_passes=0
)
def pass_turn(self) -> 'GameState':
return GameState(
player_hands=self.player_hands,
# current_player=self.current_player.next(),
current_player=next_player(self.current_player),
left_end=self.left_end,
right_end=self.right_end,
consecutive_passes=self.consecutive_passes + 1
)
def is_game_over(self) -> bool:
return any(len(hand) == 0 for hand in self.player_hands) or self.consecutive_passes == 4
# def is_game_over(self) -> bool:
# cy_state = GameStateCy(
# self.player_hands,
# self.current_player.value,
# self.left_end if self.left_end is not None else -1,
# self.right_end if self.right_end is not None else -1,
# self.consecutive_passes
# )
# return cy_state.is_game_over()
# def is_game_over(self) -> bool:
# return GameStateCy.static_is_game_over(self.player_hands, self.consecutive_passes)
def get_current_hand(self) -> FrozenSet[DominoTile]:
# return self.player_hands[self.current_player.value]
return self.player_hands[self.current_player]
def min_max_alpha_beta(state: GameState, depth: int, alpha: float, beta: float, cache: dict = {}, best_path_flag: bool = True) -> tuple[tuple[DominoTile, bool]|None, float, list[tuple[PlayerPosition, tuple[DominoTile, bool]|None]]]:
"""
Implement the min-max algorithm with alpha-beta pruning for the domino game, including the optimal path.
:param state: The current GameState
:param depth: The depth to search in the game tree
:param alpha: The best value that the maximizer currently can guarantee at that level or above
:param beta: The best value that the minimizer currently can guarantee at that level or above
:param cache: The cache dictionary to use for memoization
:param best_path_flag: Flag to indicate if best_path is needed or not
:return: A tuple of (best_move, best_score, optimal_path)
"""
if depth == 0 or state.is_game_over():
_, total_score = count_game_stats(state, print_stats=False, cache=cache)
return None, total_score, []
current_player = state.current_player
# is_maximizing = current_player in (PlayerPosition.NORTH, PlayerPosition.SOUTH)
is_maximizing = current_player in (PlayerPosition_NORTH, PlayerPosition_SOUTH)
best_move = None
best_path = []
possible_moves = list_possible_moves(state, cache, include_stats=False)
if is_maximizing:
best_score = -math.inf
for move in possible_moves:
tile_and_loc_info, _, _ = move
# tile, is_left = tile_info if tile_info is not None else (None, None)
# if tile is None: # Pass move
if tile_and_loc_info is None: # Pass move
new_state = state.pass_turn()
else:
# assert is_left is not None
tile, is_left = tile_and_loc_info
new_state = state.play_hand(tile, is_left)
_, score, path = min_max_alpha_beta(new_state, depth - 1, alpha, beta, cache)
if score > best_score:
best_score = score
# best_move = (tile, is_left)
# best_path = [(current_player, (tile, is_left))] + path
best_move = tile_and_loc_info
if best_path_flag:
best_path = [(current_player, tile_and_loc_info)] + path
alpha = max(alpha, best_score)
if beta <= alpha:
break # Beta cut-off
else:
best_score = math.inf
for move in possible_moves:
tile_and_loc_info, _, _ = move
# tile, is_left = tile_and_loc_info if tile_and_loc_info is not None else (None, None)
# if tile is None: # Pass move
if tile_and_loc_info is None: # Pass move
new_state = state.pass_turn()
else:
# assert is_left is not None
tile, is_left = tile_and_loc_info
new_state = state.play_hand(tile, is_left)
_, score, path = min_max_alpha_beta(new_state, depth - 1, alpha, beta, cache)
if score < best_score:
best_score = score
# best_move = (tile, is_left)
best_move = tile_and_loc_info
# best_path = [(current_player, (tile, is_left))] + path
if best_path_flag:
best_path = [(current_player, tile_and_loc_info)] + path
beta = min(beta, best_score)
if beta <= alpha:
break # Alpha cut-off
return best_move, best_score, best_path
def get_best_move_alpha_beta(state: GameState, depth: int, cache: dict = {}, best_path_flag: bool = True) -> tuple[tuple[DominoTile, bool]|None, float, list[tuple[PlayerPosition, tuple[DominoTile, bool]|None]]]:
"""
Get the best move for the current player using the min-max algorithm with alpha-beta pruning, including the optimal path.
:param state: The current GameState
:param depth: The depth to search in the game tree
:param cache: The cache dictionary to use for memoization
:param best_path_flag: Flag to indicate if best_path is needed or not
:return: A tuple of (best_move, best_score, optimal_path)
"""
return min_max_alpha_beta(state, depth, -math.inf, math.inf, cache, best_path_flag)
cache_hit: int = 0
cache_miss: int = 0
def count_game_stats(initial_state: GameState, print_stats: bool = True, cache: dict = {}) -> tuple[int, float]:
global cache_hit, cache_miss
# stack: list[tuple[GameState, list[tuple[DominoTile, bool]]]] = [(initial_state, [])] # Stack contains (state, path) pairs
stack: list[tuple[GameState, list[GameState]]] = [(initial_state, [])] # Stack contains (state, path) pairs
winning_stats = {-1: 0, 0: 0, 1: 0}
while stack:
state, path = stack.pop()
if state in cache:
cache_hit += 1
total_games, total_score = cache[state]
# Update all states in the path with this result
for path_state in reversed(path):
if path_state in cache:
cache[path_state] = (
cache[path_state][0] + total_games,
cache[path_state][1] + total_score
)
else:
cache[path_state] = (total_games, total_score)
continue
cache_miss += 1
if state.is_game_over():
winner, pair_0_pips, pair_1_pips = determine_winning_pair(state)
winning_stats[winner] += 1
score = 0 if winner == -1 else (pair_0_pips + pair_1_pips) * (1 if winner == 0 else -1)
total_games, total_score = 1, score
# Cache the result for this terminal state
cache[state] = (total_games, total_score)
# Update all states in the path with this result
for path_state in reversed(path):
if path_state in cache:
cache[path_state] = (
cache[path_state][0] + total_games,
cache[path_state][1] + total_score
)
else:
cache[path_state] = (total_games, total_score)
else:
current_hand = state.get_current_hand()
moves = []
# Generate possible moves
if state.right_end is None and state.left_end is None:
moves = [(tile, True) for tile in current_hand]
else:
for tile in current_hand:
if tile.can_connect(state.left_end):
moves.append((tile, True))
if tile.can_connect(state.right_end) and state.left_end != state.right_end:
moves.append((tile, False))
# If no moves are possible, pass the turn
if not moves:
new_state = state.pass_turn()
stack.append((new_state, path + [state]))
else:
for tile, left in moves:
new_state = state.play_hand(tile, left)
stack.append((new_state, path + [state]))
# Calculate final statistics
total_games, total_score = cache[initial_state]
exp_score = total_score / total_games if total_games > 0 else 0
if print_stats:
print(f"Number of possible game outcomes: {total_games}")
print('Winning stats:', winning_stats)
print(f'Expected score: {exp_score:.4f}')
print(f'Cache hits: {cache_hit}')
print(f'Cache misses: {cache_miss}')
print(f'Total cached states: {len(cache)}')
return total_games, exp_score
def list_possible_moves(state: GameState, cache: dict = {}, include_stats: bool = True) -> list[tuple[tuple[DominoTile, bool]|None, int|None, float|None]]:
"""
List all possible moves for the current player in the given game state,
optionally including the number of possible outcomes and expected score for each move.
:param state: The current GameState
:param cache: The cache dictionary to use for memoization
:param include_stats: Whether to include possible outcomes and expected score (default True)
:return: A list of tuples (tile, is_left, possible_outcomes, expected_score)
If include_stats is False, possible_outcomes and expected_score will be None
"""
current_hand = state.get_current_hand()
possible_moves: list[tuple[tuple[DominoTile, bool]|None, int|None, float|None]] = []
# If the board is empty, the first player can play any tile
if state.right_end is None and state.left_end is None:
for tile in current_hand:
if include_stats:
new_state = state.play_hand(tile, left=True) # Direction doesn't matter for the first tile
total_games, total_score = count_game_stats(new_state, print_stats=False, cache=cache)
expected_score = total_score / total_games if total_games > 0 else 0
possible_moves.append(((tile, True), total_games, expected_score))
else:
possible_moves.append(((tile, True), None, None))
else:
# Try playing each tile in the current player's hand
for tile in current_hand:
if tile.can_connect(state.left_end):
if include_stats:
new_state = state.play_hand(tile, left=True)
total_games, total_score = count_game_stats(new_state, print_stats=False, cache=cache)
expected_score = total_score / total_games if total_games > 0 else 0
possible_moves.append(((tile, True), total_games, expected_score))
else:
possible_moves.append(((tile, True), None, None))
if tile.can_connect(state.right_end) and state.left_end != state.right_end:
if include_stats:
new_state = state.play_hand(tile, left=False)
total_games, total_score = count_game_stats(new_state, print_stats=False, cache=cache)
expected_score = total_score / total_games if total_games > 0 else 0
possible_moves.append(((tile, False), total_games, expected_score))
else:
possible_moves.append(((tile, False), None, None))
# If the player can't play, include the option to pass
if not possible_moves:
if include_stats:
new_state = state.pass_turn()
total_games, total_score = count_game_stats(new_state, print_stats=False, cache=cache)
expected_score = total_score / total_games if total_games > 0 else 0
possible_moves.append((None, total_games, expected_score))
# possible_moves.append((None, None, total_games, expected_score))
else:
# possible_moves.append((None, None, None, None))
possible_moves.append((None, None, None))
# Sort moves by expected score (descending order) if stats are included
if include_stats:
# possible_moves.sort(key=lambda x: x[3] if x[3] is not None else float('-inf'), reverse=True)
possible_moves.sort(key=lambda x: x[2] if x[2] is not None else float('-inf'), reverse=True)
return possible_moves
def determine_winning_pair(state: GameState) -> tuple[int, int, int]:
pair_0_pips = sum(tile.get_pip_sum() for hand in state.player_hands[::2] for tile in hand)
pair_1_pips = sum(tile.get_pip_sum() for hand in state.player_hands[1::2] for tile in hand)
# Check if a player has run out of tiles
for i, hand in enumerate(state.player_hands):
if len(hand) == 0:
# print(f'player {i} domino')
return i % 2, pair_0_pips, pair_1_pips
# If we're here, the game must be blocked
if pair_1_pips == pair_0_pips:
result = -1
else:
result = 1 if pair_1_pips < pair_0_pips else 0
return result, pair_0_pips, pair_1_pips