-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
220 lines (187 loc) · 7 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
218
219
220
"""
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
"""
import chess
piece_value = {
chess.PAWN: 100,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.ROOK: 500,
chess.QUEEN: 900,
chess.KING: 20000
}
pawn_eval_white = [
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,
]
pawn_eval_black = list(reversed(pawn_eval_white))
knight_eval = [
-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,
]
bishop_eval_white = [
-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,
]
bishop_eval_black = list(reversed(bishop_eval_white))
rook_eval_white = [
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,
]
rook_eval_black = list(reversed(rook_eval_white))
queen_eval = [
-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,
]
king_eval_white = [
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,
]
king_eval_black = list(reversed(king_eval_white))
king_eval_end_game_white = [
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,
]
king_eval_end_game_black = list(reversed(king_eval_end_game_white))
def move_value(board: chess.Board, move: chess.Move, endgame: bool) -> float:
"""
Determine how good a move is as determined by its value
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, weigh 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:
"""
Given a piece and a square, return the value of the piece on that square
"""
piece_type = piece.piece_type
mapping = []
if piece_type == chess.PAWN:
mapping = pawn_eval_white if piece.color == chess.WHITE else pawn_eval_black
if piece_type == chess.KNIGHT:
mapping = knight_eval
if piece_type == chess.BISHOP:
mapping = bishop_eval_white if piece.color == chess.WHITE else bishop_eval_black
if piece_type == chess.ROOK:
mapping = rook_eval_white if piece.color == chess.WHITE else rook_eval_black
if piece_type == chess.QUEEN:
mapping = queen_eval
if piece_type == chess.KING:
# Use end game piece-square tables if neither side has a queen
if end_game:
mapping = (
king_eval_end_game_white if piece.color == chess.WHITE else king_eval_end_game_black
)
else:
mapping = king_eval_white if piece.color == chess.WHITE else king_eval_black
return mapping[square]
def evaluate_board(board: chess.Board) -> float:
"""
Evaluates the full board and determines which player is in the most favorable position
The sign indicates the side:
(+) for white
(-) for black
The magnitude is 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:
"""
We are in the end-game if:
- Both sides have no queens
or
- Every side which has a queen has additionally no other pieces or only a
maximum of one minor piece
"""
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