Quellcode durchsuchen

adding pvmove to TT

Nicolas Winkler vor 2 Jahren
Ursprung
Commit
e23dc02faa
3 geänderte Dateien mit 17 neuen und 12 gelöschten Zeilen
  1. 0 1
      src/game.rs
  2. 9 3
      src/hash.rs
  3. 8 8
      src/search.rs

+ 0 - 1
src/game.rs

@@ -21,7 +21,6 @@ pub struct Game
     /// 
     pub castling_rights: [bool; 4],
 
-    
     //
     // non-hashed part
     // vvv

+ 9 - 3
src/hash.rs

@@ -4,6 +4,8 @@ use std::collections::{HashMap};
 use log::info;
 use zobrist;
 
+use crate::movegen::Move;
+
 #[derive(Clone)]
 pub enum EntryType {
     Value,
@@ -14,30 +16,34 @@ pub enum EntryType {
 #[derive(Clone)]
 pub struct CacheEntry {
     pub entry_type: EntryType,
+    pub mov: Move,
     pub depth: i32,
     pub value: PosValue,
 }
 
 impl CacheEntry {
-    pub fn new_value(depth: i32, value: PosValue) -> Self {
+    pub fn new_value(depth: i32, mov: Move, value: PosValue) -> Self {
         CacheEntry {
             entry_type: EntryType::Value,
+            mov,
             depth,
             value,
         }
     }
 
-    pub fn new_upper(depth: i32, beta: PosValue) -> Self {
+    pub fn new_upper(depth: i32, mov: Move, beta: PosValue) -> Self {
         CacheEntry {
             entry_type: EntryType::UpperBound,
+            mov,
             depth,
             value: beta,
         }
     }
 
-    pub fn new_lower(depth: i32, alpha: PosValue) -> Self {
+    pub fn new_lower(depth: i32, mov: Move, alpha: PosValue) -> Self {
         CacheEntry {
             entry_type: EntryType::LowerBound,
+            mov,
             depth,
             value: alpha,
         }

+ 8 - 8
src/search.rs

@@ -93,6 +93,8 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
         let undo = game.apply(mov);
 
         let val = -negamax(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
+
+        //info!("moveval {} -> {}\n", mov.to_string(), val);
         
         game.undo_move(undo);
 
@@ -119,7 +121,7 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
             return SearchResult::Cancelled(Some((chosen_mov.0, -chosen_mov.1)));
         }
         else {
-            hash.cache(game, CacheEntry::new_value(depth, chosen_mov.1));
+            hash.cache(game, CacheEntry::new_value(depth, chosen_mov.0, chosen_mov.1));
             sc.pv[0] = chosen_mov.0;
             return SearchResult::Finished(chosen_mov.0, -chosen_mov.1);
         }
@@ -129,8 +131,9 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
     }
 }
 
-pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> PosValue {
+pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, mut beta: PosValue, depth: i32) -> PosValue {
 
+    let mut pvmove: Option<Move> = None;
     if let Some(e) = hash.lookup(game) {
         if e.depth >= depth {
             //println!("TABLE HIT!");
@@ -214,7 +217,6 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
 
         let val = -negamax(game, sc, hash, -decrease_mate_in(beta), -decrease_mate_in(alpha), depth - 1);
         game.undo_move(undo);
-        //val = increase_mate_in(val);
 
         // return if the search has been cancelled
         if sc.stopping {
@@ -222,9 +224,7 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
         }
 
         if val >= beta {
-            hash.cache(game, CacheEntry::new_upper(depth, val));
-            //println!("but ret {}: {}", depth, beta);
-            //info!("{} causes beta cutoff at {} ", mov.to_string(), val);
+            hash.cache(game, CacheEntry::new_upper(depth, mov, val));
             if !mov.is_capture() {
                 sc.insert_killer(ply_depth, mov);
             }
@@ -239,12 +239,12 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
 
 
     if alpha_is_exact {
-        hash.cache(game, CacheEntry::new_value(depth, alpha));
+        hash.cache(game, CacheEntry::new_value(depth, best_move, alpha));
         let cur_depth = (sc.initial_depth - depth) as usize;
         sc.pv[cur_depth] = best_move;
     }
     else {
-        hash.cache(game, CacheEntry::new_lower(depth, alpha));
+        //hash.cache(game, CacheEntry::new_lower(depth, alpha));
     }
     //info!("best alpha {}", alpha);
     return alpha;