-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphaBetaNoTransposition.py
138 lines (120 loc) · 4.95 KB
/
alphaBetaNoTransposition.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
import time
import chess
def alphabeta_policy(cpu_time):
piece_values = {
"p": -1,
"n": -3.05,
"b": -3.33,
"r": -5.63,
"q": -9.5,
"k": 0,
"P": 1,
"N": 3.05,
"B": 3.33,
"R": 5.63,
"Q": 9.5,
"K": 0,
}
def policy(state):
start_time = time.time()
current_depth = 1
best_move = None
best_value = None
states_evaluated = 0
while True:
if time.time() - start_time > cpu_time:
break
best_value = float("-inf") if state.actor() == chess.WHITE else float("inf")
def alphabeta(state, depth, alpha, beta, maximizing_player, move_depth=1):
nonlocal states_evaluated
if time.time() - start_time > cpu_time:
raise TimeoutError
if depth == 0 or state.is_terminal():
states_evaluated += 1
if state.is_terminal():
payoff = state.payoff()
if payoff != 0: # Checkmate or draw detected
return payoff + (1000 if payoff > 0 else -1000) * (
100 / move_depth
)
return payoff
return state.heuristic_evaluation()
def order_moves(board, moves):
scores = []
for move in moves:
score = 0
moving_piece = board.piece_at(move.from_square)
captured_piece = board.piece_at(move.to_square)
if moving_piece:
moving_piece_value = piece_values[moving_piece.symbol()]
if captured_piece:
captured_piece_value = piece_values[captured_piece.symbol()]
score = 10 * captured_piece_value - moving_piece_value
if moving_piece.symbol().lower() == "p" and move.promotion:
promotion_piece_value = piece_values[
chess.Piece(move.promotion, board.turn).symbol()
]
score += promotion_piece_value
scores.append((move, score))
scores.sort(key=lambda x: x[1], reverse=True)
sorted_moves = [move for move, score in scores]
return sorted_moves
moves = order_moves(state.board, list(state.board.legal_moves))
if maximizing_player:
value = float("-inf")
for move in moves:
successor = state.successor(move)
value = max(
value,
alphabeta(
successor, depth - 1, alpha, beta, False, move_depth + 1
),
)
alpha = max(alpha, value)
if alpha >= beta:
break
return value
else:
value = float("inf")
for move in moves:
successor = state.successor(move)
value = min(
value,
alphabeta(
successor, depth - 1, alpha, beta, True, move_depth + 1
),
)
beta = min(beta, value)
if alpha >= beta:
break
return value
try:
for move in state.get_actions():
successor = state.successor(move)
value = alphabeta(
successor,
current_depth,
float("-inf"),
float("inf"),
state.actor() != chess.WHITE,
)
if state.actor() == chess.WHITE and value > best_value:
best_value = value
best_move = move
elif state.actor() != chess.WHITE and value < best_value:
best_value = value
best_move = move
current_depth += 1
except TimeoutError:
break
# Debugging prints
print(f"States evaluated: {states_evaluated}")
print(f"Depth reached: {current_depth - 1}")
print(f"Best move: {best_move}")
print(f"Best value: {best_value}")
if best_move:
b = state.successor(best_move)
print(b.board)
print(f"Heuristic evaluation of final position: {b.heuristic_evaluation()}")
return best_move
return policy