-
Notifications
You must be signed in to change notification settings - Fork 27
/
evaluate.py
217 lines (187 loc) · 6.57 KB
/
evaluate.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
import chess
# this module implement's Tomasz Michniewski's Simplified Evaluation Function
# https://www.chessprogramming.org/Simplified_Evaluation_Function
# note that the board layouts have been flipped and the top left square is A1
# fmt: off
piece_value = {
chess.PAWN: 100,
chess.ROOK: 500,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.QUEEN: 900,
chess.KING: 20000
}
pawnEvalWhite = [
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
]
pawnEvalBlack = list(reversed(pawnEvalWhite))
knightEval = [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50
]
bishopEvalWhite = [
-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
]
bishopEvalBlack = list(reversed(bishopEvalWhite))
rookEvalWhite = [
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
]
rookEvalBlack = list(reversed(rookEvalWhite))
queenEval = [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
]
kingEvalWhite = [
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
]
kingEvalBlack = list(reversed(kingEvalWhite))
kingEvalEndGameWhite = [
50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50
]
kingEvalEndGameBlack = list(reversed(kingEvalEndGameWhite))
# fmt: on
def move_value(board: chess.Board, move: chess.Move, endgame: bool) -> float:
"""
How good is a move?
A promotion is great.
A weaker piece taking a stronger piece is good.
A stronger piece taking a weaker piece is bad.
Also consider the position change via piece-square table.
"""
if move.promotion is not None:
return -float("inf") if board.turn == chess.BLACK else float("inf")
_piece = board.piece_at(move.from_square)
if _piece:
_from_value = evaluate_piece(_piece, move.from_square, endgame)
_to_value = evaluate_piece(_piece, move.to_square, endgame)
position_change = _to_value - _from_value
else:
raise Exception(f"A piece was expected at {move.from_square}")
capture_value = 0.0
if board.is_capture(move):
capture_value = evaluate_capture(board, move)
current_move_value = capture_value + position_change
if board.turn == chess.BLACK:
current_move_value = -current_move_value
return current_move_value
def evaluate_capture(board: chess.Board, move: chess.Move) -> float:
"""
Given a capturing move, weight the trade being made.
"""
if board.is_en_passant(move):
return piece_value[chess.PAWN]
_to = board.piece_at(move.to_square)
_from = board.piece_at(move.from_square)
if _to is None or _from is None:
raise Exception(
f"Pieces were expected at _both_ {move.to_square} and {move.from_square}"
)
return piece_value[_to.piece_type] - piece_value[_from.piece_type]
def evaluate_piece(piece: chess.Piece, square: chess.Square, end_game: bool) -> int:
piece_type = piece.piece_type
mapping = []
if piece_type == chess.PAWN:
mapping = pawnEvalWhite if piece.color == chess.WHITE else pawnEvalBlack
if piece_type == chess.KNIGHT:
mapping = knightEval
if piece_type == chess.BISHOP:
mapping = bishopEvalWhite if piece.color == chess.WHITE else bishopEvalBlack
if piece_type == chess.ROOK:
mapping = rookEvalWhite if piece.color == chess.WHITE else rookEvalBlack
if piece_type == chess.QUEEN:
mapping = queenEval
if piece_type == chess.KING:
# use end game piece-square tables if neither side has a queen
if end_game:
mapping = (
kingEvalEndGameWhite
if piece.color == chess.WHITE
else kingEvalEndGameBlack
)
else:
mapping = kingEvalWhite if piece.color == chess.WHITE else kingEvalBlack
return mapping[square]
def evaluate_board(board: chess.Board) -> float:
"""
Evaluates the full board and determines which player is in a most favorable position.
The sign indicates the side:
(+) for white
(-) for black
The magnitude, how big of an advantage that player has
"""
total = 0
end_game = check_end_game(board)
for square in chess.SQUARES:
piece = board.piece_at(square)
if not piece:
continue
value = piece_value[piece.piece_type] + evaluate_piece(piece, square, end_game)
total += value if piece.color == chess.WHITE else -value
return total
def check_end_game(board: chess.Board) -> bool:
"""
Are we in the end game?
Per Michniewski:
- Both sides have no queens or
- Every side which has a queen has additionally no other pieces or one minorpiece maximum.
"""
queens = 0
minors = 0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece and piece.piece_type == chess.QUEEN:
queens += 1
if piece and (
piece.piece_type == chess.BISHOP or piece.piece_type == chess.KNIGHT
):
minors += 1
if queens == 0 or (queens == 2 and minors <= 1):
return True
return False