aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-08-22 19:36:18 +0300
committereug-vs <eugene@eug-vs.xyz>2023-08-22 19:36:18 +0300
commitbc88b60cc909f63d1d1389a220c4a43477f2dd7c (patch)
tree527be7fd80387366883a6aaf7f9877d40f2b57a3
parent39e6e29940afdbe3f37a896c80a390f3695fa3b1 (diff)
downloadchessnost-bc88b60cc909f63d1d1389a220c4a43477f2dd7c.tar.gz
feat!: simplify eval function
-rw-r--r--src/grossmeister/evaluation.rs84
1 files changed, 22 insertions, 62 deletions
diff --git a/src/grossmeister/evaluation.rs b/src/grossmeister/evaluation.rs
index 3f94199..d55d9c6 100644
--- a/src/grossmeister/evaluation.rs
+++ b/src/grossmeister/evaluation.rs
@@ -101,12 +101,6 @@ const KING_BONUS_ENGAME: [f32; 64] = [
];
impl Grossmeister {
- pub fn is_endgame(&self) -> bool {
- // No queens on the board
- self.board.piece_sets[Piece::Queen as usize].pop_count() == 0 &&
- self.board.piece_sets[Piece::QueenBlack as usize].pop_count() == 0
- }
-
pub fn passer_mask(&self, color: Color) -> Bitboard {
let black_pawns = self.board.piece_sets[Piece::PawnBlack as usize];
let white_pawns = self.board.piece_sets[Piece::Pawn as usize];
@@ -153,15 +147,6 @@ impl Grossmeister {
minor_pieces <= 1
}
- pub fn evaluate_endgame(&self) -> f32 {
- let color = self.board.color();
- let opponent_color = color.flip();
-
- let opponent_material = self.material(opponent_color, true);
-
- self.material(color, true) - opponent_material
- }
-
/// Return number of pawns above the king (+left/right)
pub fn pawn_shield(&self, color: Color) -> f32 {
let behind_pawns = match color {
@@ -177,19 +162,25 @@ impl Grossmeister {
(behind_pawns & (king | king.west_one() | king.east_one())).pop_count() as f32
}
- pub fn evaluate_middlegame(&self) -> f32 {
+ pub fn evaluate_endgame(&self) -> f32 {
let color = self.board.color();
let opponent_color = color.flip();
let mobility_advantage = self.mobility(color) - self.mobility(opponent_color);
- let opponent_material = self.material(opponent_color, false);
- let material_advantage = self.material(color, false) - opponent_material;
+ let pawn_shield_advantage = self.pawn_shield(color) - self.pawn_shield(opponent_color);
+
+ mobility_advantage * 0.03 + pawn_shield_advantage * 0.20
+ }
- let king_tropism_penalty = self.king_tropism(color) - self.king_tropism(opponent_color);
- material_advantage
- + 0.15 * mobility_advantage
- + king_tropism_penalty * (opponent_material / 40.0) * 0.07
+ pub fn evaluate_middlegame(&self) -> f32 {
+ let color = self.board.color();
+ let opponent_color = color.flip();
+
+ let mobility_advantage = self.mobility(color) - self.mobility(opponent_color);
+ let pawn_shield_advantage = self.pawn_shield(color) - self.pawn_shield(opponent_color);
+
+ mobility_advantage * 0.05 + pawn_shield_advantage * 0.15
}
// Returns a value in [0, 240] representing how
@@ -226,7 +217,7 @@ impl Grossmeister {
let color = self.board.color();
let opponent_color = color.flip();
- let pawn_structure_penalty = self.pawn_structure_penalty(color) - self.pawn_structure_penalty(opponent_color);
+ let material_advantage = self.material(color) - self.material(opponent_color);
let middlegame_eval = self.evaluate_middlegame();
let endgame_eval = self.evaluate_endgame();
@@ -234,45 +225,14 @@ impl Grossmeister {
let phase = self.phase();
let tapered_eval = (middlegame_eval * phase as f32 + endgame_eval * (240 - phase) as f32) / 240.;
- tapered_eval - 0.3 * pawn_structure_penalty
+ material_advantage + tapered_eval
}
- /// Count player pieces' material, giving bonus for pieces standing well
- pub fn material(&self, color: Color, is_endgame: bool) -> f32 {
- let passer_mask = self.passer_mask(color);
-
- let mut material = 0f32;
- for (piece_index, bitboard) in self.board.pieces_by_color(color).iter().enumerate() {
- let piece_type = Piece::from(piece_index);
-
- let bonus_table = match piece_type {
- Piece::Pawn => if bitboard & passer_mask != 0 {
- PAWN_PASSER_BONUS
- } else {
- PAWN_BONUS
- }
- Piece::Knight => KNIGHT_BONUS,
- Piece::Bishop => BISHOP_BONUS,
- Piece::Rook => ROOK_BONUS,
- Piece::King => if is_endgame {
- KING_BONUS_ENGAME
- } else {
- KING_BONUS
- },
- Piece::Queen => QUEEN_BONUS,
- _ => panic!("Unreachable")
- };
-
- material += bitboard.serialize().iter().fold(0., |acc, square| {
- acc + piece_type.static_eval() + bonus_table[
- match color {
- Color::White => square.mirror() as usize,
- Color::Black => *square as usize,
- }
- ]
- });
- }
- material
+ /// Count raw material of the given color
+ pub fn material(&self, color: Color) -> f32 {
+ self.board.pieces_by_color(color).iter().enumerate().fold(0., |acc, (piece_index, bitboard)| {
+ acc + Piece::from(piece_index).static_eval() * bitboard.pop_count() as f32
+ })
}
/// Returns sum of the doubled, blocked and isolated pawns
@@ -489,7 +449,7 @@ mod tests {
fn material() {
let board = Board::new();
let gm = Grossmeister::new(board);
- assert_eq!(gm.material(Color::Black, false), gm.material(Color::White, false));
+ assert_eq!(gm.material(Color::Black), gm.material(Color::White));
}
@@ -527,7 +487,7 @@ mod tests {
#[test]
fn black_winning() {
- let fen = String::from("8/p7/1k4K1/8/4P3/8/PP5r/8 b - - 1 38");
+ let fen = String::from("8/p7/1k4K1/8/4P3/8/PP5r/8 w - - 1 38");
let board = Board::from_FEN(fen);
let gm = Grossmeister::new(board);
let score = gm.evaluate();