diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-09-04 06:32:17 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-09-04 06:32:17 +0300 |
commit | 30326ce2dafb57fd1b6b46b8e7561383ed404641 (patch) | |
tree | 157f2f10ee856ba04a3a1796e8ec3dfca339f968 /src/grossmeister | |
parent | cc3f6674fa1662626a95995b32890c8ddd6255a0 (diff) | |
download | chessnost-30326ce2dafb57fd1b6b46b8e7561383ed404641.tar.gz |
feat: add grain of 1cp to the score
Diffstat (limited to 'src/grossmeister')
-rw-r--r-- | src/grossmeister/evaluation.rs | 27 | ||||
-rw-r--r-- | src/grossmeister/search.rs | 22 |
2 files changed, 27 insertions, 22 deletions
diff --git a/src/grossmeister/evaluation.rs b/src/grossmeister/evaluation.rs index 6fc4149..1b57f5a 100644 --- a/src/grossmeister/evaluation.rs +++ b/src/grossmeister/evaluation.rs @@ -4,7 +4,10 @@ use crate::{board::{piece::Piece, color::Color, CastlingSide}, bitboard::{Bitboa use super::Grossmeister; -const PAWN_BONUS: [f32; 64] = [ +/// Score in pawns (not centipawns) +pub type Score = f32; + +const PAWN_BONUS: [Score; 64] = [ // A B C D E F G H 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, @@ -16,7 +19,7 @@ const PAWN_BONUS: [f32; 64] = [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 ]; -const PAWN_BONUS_PASSER_ENDGAME: [f32; 64] = [ +const PAWN_BONUS_PASSER_ENDGAME: [Score; 64] = [ // A B C D E F G H 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, @@ -28,7 +31,7 @@ const PAWN_BONUS_PASSER_ENDGAME: [f32; 64] = [ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, ]; -const KNIGHT_BONUS: [f32; 64] = [ +const KNIGHT_BONUS: [Score; 64] = [ // A B C D E F G H -0.50, -0.40, -0.30, -0.30, -0.30, -0.30, -0.40, -0.50, -0.40, -0.20, 0.00, 0.00, 0.00, 0.00, -0.20, -0.40, @@ -40,7 +43,7 @@ const KNIGHT_BONUS: [f32; 64] = [ -0.50, -0.40, -0.30, -0.30, -0.30, -0.30, -0.40, -0.50, ]; -const BISHOP_BONUS: [f32; 64] = [ +const BISHOP_BONUS: [Score; 64] = [ // A B C D E F G H -0.20, -0.10, -0.10, -0.10, -0.10, -0.10, -0.10, -0.20, -0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.10, @@ -52,7 +55,7 @@ const BISHOP_BONUS: [f32; 64] = [ -0.20, -0.10, -0.10, -0.10, -0.10, -0.10, -0.10, -0.20, ]; -const ROOK_BONUS: [f32; 64] = [ +const ROOK_BONUS: [Score; 64] = [ // A B C D E F G H 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.05, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.05, @@ -64,7 +67,7 @@ const ROOK_BONUS: [f32; 64] = [ 0.00, 0.00, 0.00, 0.05, 0.05, 0.00, 0.00, 0.00 ]; -const QUEEN_BONUS: [f32; 64] = [ +const QUEEN_BONUS: [Score; 64] = [ // A B C D E F G H -0.20, -0.10, -0.10, -0.05, -0.05, -0.10, -0.10, -0.20, -0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.10, @@ -76,7 +79,7 @@ const QUEEN_BONUS: [f32; 64] = [ -0.20, -0.10, -0.10, -0.05, -0.05, -0.10, -0.10, -0.20 ]; -const KING_BONUS: [f32; 64] = [ +const KING_BONUS: [Score; 64] = [ // A B C D E F G H -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, @@ -88,7 +91,7 @@ const KING_BONUS: [f32; 64] = [ 0.20, 0.10, 0.30, -0.20, 0.00, 0.10, 0.30, 0.20 ]; -const KING_BONUS_ENGAME: [f32; 64] = [ +const KING_BONUS_ENGAME: [Score; 64] = [ // A B C D E F G H -0.50, -0.40, -0.30, -0.20, -0.20, -0.30, -0.40, -0.50, -0.30, -0.20, -0.10, 0.00, 0.00, -0.10, -0.20, -0.30, @@ -209,7 +212,7 @@ impl Grossmeister { /// Evaluate a position relative to the current player - pub fn evaluate(&self) -> f32 { + pub fn evaluate(&self) -> Score { let color = self.board.color(); let opponent_color = color.flip(); @@ -243,9 +246,11 @@ impl Grossmeister { let phase = self.phase(); let tapered_eval = (middlegame_eval * phase as f32 + endgame_eval * (240 - phase) as f32) / 240.; - (material_advantage + tapered_eval) + let eval = (material_advantage + tapered_eval) .min(if winnable { INFINITY } else { 0.0 }) // Can not score > 0 if not winnable - .max(if loseable { -INFINITY } else { 0.0 }) // Can not score < 0 if not loseable + .max(if loseable { -INFINITY } else { 0.0 }); // Can not score < 0 if not loseable + + (eval * 100.0).round() / 100.0 } /// Count pseudo-legal moves without actually generating them diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index d3dc0b4..455fb6f 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -2,9 +2,9 @@ use std::f32::INFINITY; use crate::{moves::{Move, MoveKind}, board::io::IO}; -use super::{Grossmeister, ttable::{NodeType, TTABLE_SIZE, TranspositionTableItem}}; +use super::{Grossmeister, ttable::{NodeType, TTABLE_SIZE, TranspositionTableItem}, evaluation::Score}; -const SCORE_MATE: f32 = 20_000.0; +const SCORE_MATE: Score = 20_000.0; #[derive(Debug, Default, PartialEq)] pub struct PerftResult { @@ -18,9 +18,9 @@ pub struct PerftResult { impl Grossmeister { // Mate distance pruning // If a non-zero value (mating score) is returned, branch could be safely pruned - pub fn MDP(alpha: &mut f32, beta: &mut f32, root_distance: u8) -> f32 { + pub fn MDP(alpha: &mut Score, beta: &mut Score, root_distance: u8) -> Score { { // Update bound in case we are winning - let mating_score = SCORE_MATE - root_distance as f32; + let mating_score = SCORE_MATE - root_distance as Score; if mating_score < *beta { *beta = mating_score; if *alpha >= mating_score { @@ -29,7 +29,7 @@ impl Grossmeister { } } { // Update bound in case we are losing - let mating_score = -SCORE_MATE + root_distance as f32; + let mating_score = -SCORE_MATE + root_distance as Score; if mating_score > *alpha { *alpha = mating_score; if *beta <= mating_score { @@ -40,7 +40,7 @@ impl Grossmeister { 0.0 } - pub fn negamax_search(&mut self, mut alpha: f32, mut beta: f32, depth_left: u8, root_distance: u8) -> (f32, Vec<Move>) { + pub fn negamax_search(&mut self, mut alpha: Score, mut beta: Score, depth_left: u8, root_distance: u8) -> (Score, Vec<Move>) { let mut principal_variation = Vec::new(); let color = self.board.color(); @@ -163,7 +163,7 @@ impl Grossmeister { if !legal_move_found { if self.board.is_king_in_check(color) { - return (-SCORE_MATE + root_distance as f32, principal_variation); + return (-SCORE_MATE + root_distance as Score, principal_variation); } else { return (0.0, principal_variation); } @@ -172,7 +172,7 @@ impl Grossmeister { (alpha, principal_variation) } - pub fn quiscence(&mut self, mut alpha: f32, mut beta: f32, root_distance: u8) -> f32 { + pub fn quiscence(&mut self, mut alpha: Score, mut beta: Score, root_distance: u8) -> Score { let color = self.board.color(); if self.board.threefold_repetition() { @@ -227,13 +227,13 @@ impl Grossmeister { } if !legal_move_found && self.board.is_king_in_check(color) { - return -SCORE_MATE + root_distance as f32 + return -SCORE_MATE + root_distance as Score } alpha } - pub fn iterative_deepening(&mut self, max_depth: u8) -> Option<(f32, Vec<Move>)> { + pub fn iterative_deepening(&mut self, max_depth: u8) -> Option<(Score, Vec<Move>)> { let mut result = None; let mut depth = 1; let mut alpha = -INFINITY; @@ -291,7 +291,7 @@ impl Grossmeister { println!(); // If our score is mate in N, we break at depth N - if depth as f32 >= SCORE_MATE - search_result.0.abs() { + if depth as Score >= SCORE_MATE - search_result.0.abs() { result = Some(search_result); break; } |