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
use super::*;
const CONTROL_CENTER_BONUS: Score = 50;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Default, Debug)]
pub struct EvaluatorNonNNUE;
impl EvaluatorNonNNUE {
pub fn new(_: &ChessPosition) -> Self {
Self
}
pub fn slow_evaluate(position: &ChessPosition) -> Score {
Self::new(position).evaluate(position)
}
fn evaluate_raw(position: &ChessPosition) -> Score {
let material_score = position.get_material_score();
let mut score = material_score;
// Consider additional heuristics
let mut mobility_score = 0;
let mut king_safety_score = 0;
// let mut pawn_structure_score = 0;
let mut center_control_score = 0;
// let mut piece_activity_score = 0;
// let mut threat_score = 0;
for (piece, square) in position.iter() {
let alpha =
position.get_material_score_abs() as f64 / INITIAL_MATERIAL_SCORE_ABS as f64;
let psqt_score = get_psqt_score(piece, square, alpha);
score += if piece.get_color() == White {
psqt_score
} else {
-psqt_score
} as Score;
// Calculate mobility (number of legal moves)
let mobility = position
.generate_masked_legal_moves(square.to_bitboard(), BitBoard::ALL)
.len() as Score;
mobility_score += if piece.get_color() == White {
mobility
} else {
-mobility
};
// Calculate king safety
if piece.get_piece_type() == King {
let king_safety = Self::evaluate_king_safety(position, square);
king_safety_score += if piece.get_color() == White {
king_safety
} else {
-king_safety
};
}
// // Calculate pawn structure
// if piece.get_piece_type() == Pawn {
// let pawn_structure = Self::evaluate_pawn_structure(position, square);
// pawn_structure_score += if piece.get_color() == White {
// pawn_structure
// } else {
// -pawn_structure
// };
// }
// Calculate control of the center
if BB_CENTER.contains(square) {
center_control_score += if piece.get_color() == White {
CONTROL_CENTER_BONUS
} else {
-CONTROL_CENTER_BONUS
};
}
// // Calculate piece activity
// let piece_activity = Self::evaluate_piece_activity(position, piece, square);
// piece_activity_score += if piece.get_color() == White {
// piece_activity
// } else {
// -piece_activity
// };
// // Calculate threats
// let threats = Self::evaluate_threats(position, piece, square);
// threat_score += if piece.get_color() == White {
// threats
// } else {
// -threats
// };
}
// Combine all heuristics into the final score
score += mobility_score;
score += king_safety_score;
// score += pawn_structure_score;
score += center_control_score;
// score += piece_activity_score;
// score += threat_score;
score
}
// Enhanced King Safety
fn evaluate_king_safety(position: &ChessPosition, king_square: Square) -> Score {
let king_color = position.get_piece_at(king_square).unwrap().get_color();
let mut safety_score = 0;
// Evaluate pawn shield (simplified, assuming a standard chessboard)
let Some(forward_king_square) = king_square.forward(king_color) else {
return 0;
};
let pawn_shield_squares = [
forward_king_square.left(),
Some(forward_king_square),
forward_king_square.right(),
]
.into_iter()
.flatten()
.collect_vec();
for &shield_square in &pawn_shield_squares {
if let Some(pawn) = position.get_piece_at(shield_square)
&& pawn.get_piece_type() == Pawn
&& pawn.get_color() == king_color
{
safety_score += 10; // Example value for a pawn in the shield
}
}
// Penalize open files or no pawn shield
if pawn_shield_squares
.iter()
.all(|&sq| position.get_piece_at(sq).is_none())
{
safety_score -= 50; // Example penalty for an exposed king
}
// // Penalize for enemy pieces attacking nearby squares
// let enemy_color = !king_color;
// let nearby_squares = king_square.neighbors();
// for &square in &nearby_squares {
// if position.is_square_attacked(square, enemy_color) {
// safety_score -= 20; // Example penalty for each attack on nearby squares
// }
// }
safety_score
}
// Enhanced Pawn Structure
fn evaluate_pawn_structure(position: &ChessPosition, pawn_square: Square) -> Score {
let mut structure_score = 0;
let pawn = position.get_piece_at(pawn_square).unwrap();
let pawn_color = pawn.get_color();
// Evaluate isolated pawns (no friendly pawns on adjacent files)
let file = pawn_square.get_file();
let adjacent_files = [file.left(), file.right()];
let isolated = adjacent_files.iter().flatten().all(|&adj_file| {
ALL_RANKS.iter().all(|&rank| {
let sq = Square::from_rank_and_file(rank, adj_file);
position
.get_piece_at(sq)
.is_none_or(|p| p.get_piece_type() != Pawn || p.get_color() != pawn_color)
})
});
if isolated {
structure_score -= 20; // Example penalty for isolated pawns
}
// Evaluate doubled pawns (multiple pawns of the same color on the same file)
let file_pawns = ALL_RANKS
.iter()
.filter(|&&rank| {
let sq = Square::from_rank_and_file(rank, file);
position
.get_piece_at(sq)
.is_some_and(|p| p.get_piece_type() == Pawn && p.get_color() == pawn_color)
})
.count();
if file_pawns > 1 {
structure_score -= 10 * (file_pawns as Score - 1) as Score; // Example penalty for each doubled pawn
}
// Evaluate passed pawns (no opposing pawns blocking or attacking the path to promotion)
if position.is_passed_pawn(pawn_square) {
structure_score += 30; // Example bonus for passed pawns
}
// // Evaluate backward pawns (no friendly pawns protecting from behind)
// let backward = adjacent_files.iter().flatten().all(|&adj_file| {
// (0..pawn_square.get_rank()).all(|rank| {
// let sq = Square::from_rank_and_file(rank, adj_file);
// position.get_piece_at(sq).map_or(true, |p| {
// p.get_piece_type() != Pawn || p.get_color() != pawn_color
// })
// })
// });
// if backward {
// structure_score -= 15; // Example penalty for backward pawns
// }
structure_score
}
// Enhanced Piece Activity
fn evaluate_piece_activity(position: &ChessPosition, _piece: Piece, square: Square) -> Score {
let mut activity_score = 0;
// let piece_color = piece.get_color();
// // Simplified example: give a bonus for pieces controlling key squares
// let key_squares = [D4, E4, D5, E5]; // Central squares
// for &key_square in &key_squares {
// if position.is_square_attacked(key_square, piece_color) {
// activity_score += 5; // Example bonus for controlling a key square
// }
// }
// Penalize pieces trapped or blocked by own pawns
let piece_mobility = position
.generate_masked_legal_moves(square.to_bitboard(), BitBoard::ALL)
.len() as Score;
if piece_mobility < 2 {
activity_score -= 10; // Example penalty for low mobility
}
activity_score
}
// Evaluate threats
fn evaluate_threats(position: &ChessPosition, piece: Piece, square: Square) -> Score {
let mut threat_score = 0;
let piece_color = piece.get_color();
// Simplified example: give a bonus for attacking opponent's pieces
for move_ in position.generate_masked_legal_moves(
square.to_bitboard(),
position.occupied_color(!piece_color),
) {
threat_score += match position.get_piece_type_at(move_.get_dest()).unwrap() {
King => 0,
piece_type => piece_type.evaluate(),
}
}
threat_score
}
}
impl PositionEvaluation for EvaluatorNonNNUE {
fn evaluate(&mut self, position: &ChessPosition) -> Score {
let material_score = position.get_material_score();
let mut score = material_score;
for (piece, square) in position.iter() {
let alpha =
position.get_material_score_abs() as f64 / INITIAL_MATERIAL_SCORE_ABS as f64;
let psqt_score = get_psqt_score(piece, square, alpha);
score += if piece.get_color() == White {
psqt_score
} else {
-psqt_score
} as Score;
}
score
// Self::evaluate_raw(position)
}
}