aboutsummaryrefslogtreecommitdiff
path: root/src/board/engine.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-27 23:42:45 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-27 23:42:45 +0300
commitdbf3b8649c45aca6b80d9659363d3e878d54f4ef (patch)
treea0182e11edb7f3e12744090794cc2ffa39198684 /src/board/engine.rs
parentedf252ab85bb790c2fabaa048350d2bd7dc75ada (diff)
downloadchessnost-dbf3b8649c45aca6b80d9659363d3e878d54f4ef.tar.gz
fix: scale king tropism by opponent material
Diffstat (limited to 'src/board/engine.rs')
-rw-r--r--src/board/engine.rs74
1 files changed, 39 insertions, 35 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index 1e2c7c1..ed533df 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -81,41 +81,39 @@ impl Board {
}
/// Compute material advantage relative to the current player
- pub fn material_advantage(&self) -> f32 {
- let mut eval = 0f32;
- for (piece_index, bitboard) in self.pieces.iter().enumerate() {
+ pub fn material(&self, color: Color) -> f32 {
+ let mut material = 0f32;
+ for (piece_index, bitboard) in self.pieces_by_color(color).iter().enumerate() {
let piece_type = PieceType::from(piece_index);
- let sign = if Color::from_piece(piece_type) == self.color() {
- 1.
- } else {
- -1.
- };
-
- eval += sign * match piece_type {
+ material += match piece_type {
PieceType::Pawn => {
- serialize_bitboard(*bitboard).iter().fold(0., |acc, square| {
- acc + match (*square).rank() {
- 6 => 3.,
- 5 => 2.,
- _ => 1.,
- }
- })
- }
- PieceType::PawnBlack => {
- serialize_bitboard(*bitboard).iter().fold(0., |acc, square| {
- acc + match (*square).rank() {
- 1 => 3.,
- 2 => 2.,
- _ => 1.,
+ match color {
+ Color::White => {
+ serialize_bitboard(*bitboard).iter().fold(0., |acc, square| {
+ acc + match (*square).rank() {
+ 6 => 3.,
+ 5 => 2.,
+ _ => 1.,
+ }
+ })
+ },
+ Color::Black => {
+ serialize_bitboard(*bitboard).iter().fold(0., |acc, square| {
+ acc + match (*square).rank() {
+ 1 => 3.,
+ 2 => 2.,
+ _ => 1.,
+ }
+ })
}
- })
+ }
}
_ => {
piece_type.static_eval() * pop_count(*bitboard) as f32
}
};
}
- eval
+ material
}
/// Returns sum of the doubled, blocked and isolated pawns
@@ -180,20 +178,24 @@ impl Board {
/// Evaluate a position relative to the current player
pub fn evaluate(&self, precomputed_mobility: Option<f32>) -> f32 {
- let opponent_mobility = self.mobility(self.color().flip());
+ let color = self.color();
+ let opponent_color = color.flip();
+
+ let opponent_mobility = self.mobility(opponent_color);
let player_mobility = match precomputed_mobility {
Some(m) => m,
- None => self.mobility(self.color()),
+ None => self.mobility(color),
};
let mobility_advantage = player_mobility - opponent_mobility as f32;
- let material_advantage = self.material_advantage();
+ let opponent_material = self.material(opponent_color);
+ let material_advantage = self.material(color) - opponent_material;
- let pawn_structure_penalty = self.pawn_structure_penalty(self.color()) - self.pawn_structure_penalty(self.color().flip());
+ let pawn_structure_penalty = self.pawn_structure_penalty(color) - self.pawn_structure_penalty(opponent_color);
- let king_tropism_penalty = self.king_tropism(self.color()) - self.king_tropism(self.color().flip());
+ let king_tropism_penalty = self.king_tropism(color) - self.king_tropism(opponent_color);
- material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty - 0.015 * king_tropism_penalty
+ material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty - king_tropism_penalty * opponent_material / 3000.0
}
/// Evaluate move for move ordering, prioritizing efficient captures
@@ -413,7 +415,7 @@ impl Board {
#[cfg(test)]
mod tests {
- use crate::board::{Board, engine::PerftResult};
+ use crate::board::{Board, engine::PerftResult, Color};
#[test]
fn perft() {
@@ -449,9 +451,11 @@ mod tests {
}
#[test]
- fn material_advantage() {
+ fn material() {
let board = Board::new();
- assert_eq!(board.material_advantage(), 0.0, "Material advantage should be 0 at starting position");
+ assert_eq!(board.material(Color::Black), 38.0);
+ assert_eq!(board.material(Color::White), 38.0);
}
+
}