-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation.go
296 lines (249 loc) · 7.34 KB
/
evaluation.go
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
package main
import "github.com/0hq/chess"
// "fmt"
// "log"
// "math"
// "github.com/0hq/chess"
/*
Version 1 piece values.
*/
// piece weights
const pawn_v1 int = 100
const knight_v1 int = 320
const bishop_v1 int = 330
const rook_v1 int = 500
const queen_v1 int = 900
const king_v1 int = 20000
// piece map
var piece_map_v1 map[chess.PieceType]int = map[chess.PieceType]int{
1: king_v1,
2: queen_v1,
3: rook_v1,
4: bishop_v1,
5: knight_v1,
6: pawn_v1,
}
/*
Version 1 of the evaluation function.
Sums simple material values and detects checkmate and stalemate.
*/
func evaluate_position_v1(pos *chess.Position) int {
squares := pos.Board().SquareMap()
var material int = 0
for _, piece := range squares {
var sign int = 1
if piece.Color() == chess.Black {
sign = -1
}
material += piece_map_v1[piece.Type()] * sign
}
// faster than doing two comparisons
if pos.Status() != chess.NoMethod {
if pos.Status() == chess.Stalemate {
return 0
}
if pos.Status() == chess.Checkmate {
if pos.Turn() == chess.White {
return -CHECKMATE_VALUE
} else {
return CHECKMATE_VALUE
}
}
}
return material
}
/*
Version 2 of the evaluation function.
Sums simple material values and detects checkmate and stalemate.
Penalizes checkmates based on ply.
*/
func evaluate_position_v2(pos *chess.Position, engine_ply int, current_ply int, flip int) int {
squares := pos.Board().SquareMap()
var material int = 0
for _, piece := range squares {
var sign int = 1
if piece.Color() == chess.Black {
sign = -1
}
material += piece_map_v1[piece.Type()] * sign
}
// faster than doing two comparisons
if pos.Status() != chess.NoMethod {
if pos.Status() == chess.Stalemate {
return 0
}
if pos.Status() == chess.Checkmate {
// calculate ply penalty
ply_penalty := (engine_ply - current_ply) * 1
if pos.Turn() == chess.White {
return -CHECKMATE_VALUE * flip + ply_penalty
} else {
return CHECKMATE_VALUE * flip + ply_penalty
}
}
}
return material * flip
}
/*
Version 1 Piece-Square tables.
Used in evaluation version 2.
Stored as from black's perspective.
*/
// Flip the board and return the piece square table for the other side's perspective.
var FLIP = []int{
56, 57, 58, 59, 60, 61, 62, 63,
48, 49, 50, 51, 52, 53, 54, 55,
40, 41, 42, 43, 44, 45, 46, 47,
32, 33, 34, 35, 36, 37, 38, 39,
24, 25, 26, 27, 28, 29, 30, 31,
16, 17, 18, 19, 20, 21, 22, 23,
8, 9, 10, 11, 12, 13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7,
};
var KING_MG_V1 = []int{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 20, 20, 0, 0, 0,
0, 0, 0, 20, 20, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -10, -10, 0, 0, 0,
0, 0, 20, -10, -10, 0, 20, 0,
};
var QUEEN_MG_V1 = []int{
-30, -20, -10, -10, -10, -10, -20, -30,
-20, -10, -5, -5, -5, -5, -10, -20,
-10, -5, 10, 10, 10, 10, -5, -10,
-10, -5, 10, 20, 20, 10, -5, -10,
-10, -5, 10, 20, 20, 10, -5, -10,
-10, -5, -5, -5, -5, -5, -5, -10,
-20, -10, -5, -5, -5, -5, -10, -20,
-30, -20, -10, -10, -10, -10, -20, -30,
};
var ROOK_MG_V1 = []int{
0, 0, 0, 0, 0, 0, 0, 0,
15, 15, 15, 20, 20, 15, 15, 15,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 10, 10, 0, 0,
};
var BISHOP_MG_V1 = []int{
-20, 0, 0, 0, 0, 0, 0, -20,
-15, 0, 0, 0, 0, 0, 0, -15,
-10, 0, 0, 5, 5, 0, 0, -10,
-10, 10, 10, 30, 30, 10, 10, -10,
5, 5, 10, 25, 25, 10, 5, 5,
5, 5, 5, 10, 10, 5, 5, 5,
-10, 5, 5, 10, 10, 5, 5, -10,
-20, -10, -10, -10, -10, -10, -10, -20,
};
var KNIGHT_MG_V1 = []int{
-20, -10, -10, -10, -10, -10, -10, -20,
-10, -5, -5, -5, -5, -5, -5, -10,
-10, -5, 15, 15, 15, 15, -5, -10,
-10, -5, 15, 15, 15, 15, -5, -10,
-10, -5, 15, 15, 15, 15, -5, -10,
-10, -5, 10, 15, 15, 15, -5, -10,
-10, -5, -5, -5, -5, -5, -5, -10,
-20, 0, -10, -10, -10, -10, 0, -20,
};
var PAWN_MG_V1 = []int{
0, 0, 0, 0, 0, 0, 0, 0,
60, 60, 60, 60, 70, 60, 60, 60,
40, 40, 40, 50, 60, 40, 40, 40,
20, 20, 20, 40, 50, 20, 20, 20,
5, 5, 15, 30, 40, 10, 5, 5,
5, 5, 10, 20, 30, 5, 5, 5,
5, 5, 5, -30, -30, 5, 5, 5,
0, 0, 0, 0, 0, 0, 0, 0,
};
/*
Version 3 of the evaluation function.
Sums simple material values and detects checkmate and stalemate.
Penalizes checkmates based on ply.
Uses piece-square tables.
*/
var piece_square_map_v1 = map[chess.PieceType][]int{
chess.Pawn: PAWN_MG_V1,
chess.Knight: KNIGHT_MG_V1,
chess.Bishop: BISHOP_MG_V1,
chess.Rook: ROOK_MG_V1,
chess.Queen: QUEEN_MG_V1,
chess.King: KING_MG_V1,
}
func evaluate_position_v3(pos *chess.Position, engine_ply int, current_ply int, flip int) int {
squares := pos.Board().SquareMap()
var eval int = 0
for square, piece := range squares {
if piece.Color() == chess.Black {
eval += piece_square_map_v1[piece.Type()][square] * -1
eval += piece_map_v1[piece.Type()] * -1
} else {
eval += piece_square_map_v1[piece.Type()][FLIP[square]]
eval += piece_map_v1[piece.Type()]
}
}
// faster than doing two comparisons
if pos.Status() != chess.NoMethod {
if pos.Status() == chess.Stalemate {
return 0
}
if pos.Status() == chess.Checkmate {
// calculate ply penalty
ply_penalty := (engine_ply - current_ply) * 1
if pos.Turn() == chess.White {
return -CHECKMATE_VALUE * flip + ply_penalty
} else {
return CHECKMATE_VALUE * flip + ply_penalty
}
}
}
return eval * flip
}
/*
Version 4 of the evaluation function.
Considers mobility.
*/
func evaluate_position_v4(pos *chess.Position, engine_ply int, current_ply int, flip int) int {
// faster than doing two comparisons
if pos.Status() != chess.NoMethod {
if pos.Status() == chess.Stalemate {
return 0
}
if pos.Status() == chess.Checkmate {
// calculate ply penalty
ply_penalty := (engine_ply - current_ply) * 1
if pos.Turn() == chess.White {
return -CHECKMATE_VALUE * flip + ply_penalty
} else {
return CHECKMATE_VALUE * flip + ply_penalty
}
}
}
squares := pos.Board().SquareMap()
var eval int = 0
for square, piece := range squares {
if piece.Color() == chess.Black {
eval += piece_square_map_v1[piece.Type()][square] * -1
eval += piece_map_v1[piece.Type()] * -1
} else {
eval += piece_square_map_v1[piece.Type()][FLIP[square]]
eval += piece_map_v1[piece.Type()]
}
}
// mobility
mobility := 0
if pos.Turn() == chess.White {
mobility += len(pos.ValidMoves())
mobility -= len(pos.NullMove().ValidMoves())
} else {
mobility += len(pos.NullMove().ValidMoves())
mobility -= len(pos.ValidMoves())
}
eval += mobility * 1
// out("mobility:", mobility)
return eval * flip
}