-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess50.py
240 lines (199 loc) · 7.56 KB
/
chess50.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
"""
Chess AI
"""
import math
import random
# niklasf/python-chess is licensed under GPL-3.0
import chess
VALUE = {
chess.PAWN: 100,
chess.KNIGHT: 300,
chess.BISHOP: 350,
chess.ROOK: 550,
chess.QUEEN: 950,
chess.KING: 400,
}
# Piece-square tables from Chess Programming Wiki
PIECE_SQUARE = {
chess.PAWN: [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10,-20,-20, 10, 10, 5,
5, -5,-10, 0, 0,-10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0,
],
chess.KNIGHT: [
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 5, 5, 0,-20,-40,
-30, 5, 10, 15, 15, 10, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 10, 15, 15, 10, 0,-30,
-40,-20, 0, 0, 0, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50,
],
chess.BISHOP: [
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 5, 0, 0, 0, 0, 5,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 0, 0, 0, 0, 0, 0,-10,
-20,-10,-10,-10,-10,-10,-10,-20,
],
chess.ROOK:[
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,
],
chess.QUEEN: [
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 5, 0, 0, 0, 0,-10,
-10, 5, 5, 5, 5, 5, 0,-10,
0, 0, 5, 5, 5, 5, 0, -5,
-5, 0, 5, 5, 5, 5, 0, -5,
-10, 0, 5, 5, 5, 5, 0,-10,
-10, 0, 0, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20,
],
chess.KING: [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10,-20,-20,-20,-20,-20,-20,-10,
-20,-30,-30,-40,-40,-30,-30,-20,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
],
}
CHECKMATE_VALUE = 10 ** 6
SEARCH_DEPTH = 3
class ChessAI():
def __init__(self):
# Generate Zobrist table
self.zobrist = []
for _ in range(64):
row = []
for _ in range(12):
row.append(random.randrange(2**64))
self.zobrist.append(row)
# Hash table
self.table = {}
def utility(self, board):
"""
Evaluates the value of the board based on fixed piece valuations
"""
utility = 0
# Increment or decrement piece and piece-position values
for square in board.piece_map():
# For Black's pieces, start indexing the piece-square tables at the end
position = (
square if board.color_at(square) == chess.WHITE else
-square - 1
)
if board.color_at(square) == chess.WHITE:
utility += VALUE[board.piece_type_at(square)]
utility += PIECE_SQUARE[board.piece_type_at(square)][position]
else:
utility -= VALUE[board.piece_type_at(square)]
utility -= PIECE_SQUARE[board.piece_type_at(square)][position]
# Incentive for attacking the opponent's King
if board.is_checkmate():
if board.outcome().winner == chess.WHITE:
utility += CHECKMATE_VALUE
else:
utility -= CHECKMATE_VALUE
return utility
def minimax(self, board, depth=SEARCH_DEPTH):
"""
Returns the optimal action for the current player on the board
by using minimax strategy and alpha-beta pruning
Pseudocode from Russell and Norvig (2021)
"""
if board.turn == chess.WHITE:
return self.max_value(board, depth)
else:
return self.min_value(board, depth)
def max_value(self, board, depth, alpha=-math.inf, beta=math.inf):
# Return value and decision from hash table if board position has already been encountered
if (hash := self.zobrist_hash(board)) in self.table:
if self.table[hash]['depth'] >= depth:
return self.table[hash]['value'], self.table[hash]['decision']
# Base condition
if not depth or board.is_game_over():
return self.utility(board), None
value = -math.inf
for action in board.legal_moves:
board.push(action)
score = self.min_value(board, depth - 1, alpha, beta)[0]
board.pop()
if score > value:
value, decision = score, action
alpha = max(alpha, value)
if value > beta:
self.record(hash, value, decision, depth)
return value, decision
elif value == beta:
if random.randint(0, 1):
self.record(hash, value, decision, depth)
return value, decision
self.record(hash, value, decision, depth)
return value, decision
def min_value(self, board, depth, alpha=-math.inf, beta=math.inf):
# Return value and decision from hash table if board position has already been encountered
if (hash := self.zobrist_hash(board)) in self.table:
if self.table[hash]['depth'] >= depth:
return self.table[hash]['value'], self.table[hash]['decision']
# Base condition
if not depth or board.is_game_over():
return self.utility(board), None
value = math.inf
for action in board.legal_moves:
board.push(action)
score = self.max_value(board, depth - 1, alpha, beta)[0]
board.pop()
if score < value:
value, decision = score, action
beta = min(beta, value)
if value < alpha:
self.record(hash, value, decision, depth)
return value, decision
elif value == alpha:
if random.randint(0, 1):
self.record(hash, value, decision, depth)
return value, decision
self.record(hash, value, decision, depth)
return value, decision
def record(self, hash, value, decision, depth):
"""
Loads board configuration to hash table
"""
if hash not in self.table:
self.table[hash] = {}
# Add or update values
self.table[hash]['value'] = value
self.table[hash]['decision'] = decision
self.table[hash]['depth'] = depth
def zobrist_hash(self, board):
"""
Generate a unique hash for a board position
Hashing algorithm from Zobrist (1970)
"""
pieces = board.piece_map()
hash = 0
for square in chess.SQUARES:
if square in pieces:
piece = pieces[square]
piece_type = piece.piece_type + piece.color * 6 - 1
hash ^= self.zobrist[square][piece_type]
return hash