Prechádzať zdrojové kódy

adding killer heuristic

Nicolas Winkler 3 rokov pred
rodič
commit
ed9c18df54
3 zmenil súbory, kde vykonal 25 pridanie a 10 odobranie
  1. 4 4
      src/engine.rs
  2. 3 3
      src/movegen.rs
  3. 18 3
      src/search.rs

+ 4 - 4
src/engine.rs

@@ -181,14 +181,14 @@ impl Engine {
                 }
                 else if side == WHITE {
                     if let Some(wtime) = si.wtime {
-                        if before.elapsed() > Duration::from_millis((wtime / 35) as _) {
+                        if before.elapsed() > Duration::from_millis((wtime / 17) as _) {
                             return true;
                         }
                     }
                 }
                 else if side == BLACK {
                     if let Some(btime) = si.btime {
-                        if before.elapsed() > Duration::from_millis((btime / 35) as _) {
+                        if before.elapsed() > Duration::from_millis((btime / 17) as _) {
                             return true;
                         }
                     }
@@ -216,7 +216,7 @@ impl Engine {
 
         let mut alpha = crate::evaluate::MIN_VALUE;
         let mut beta = crate::evaluate::MAX_VALUE;
-        let window_size = 100;
+        //let window_size = 100;
         loop {
 
             sc.initial_depth = depth;
@@ -267,7 +267,7 @@ impl Engine {
                 }
                 depth += 1;
             }
-            else if let SearchResult::Cancelled(Some((bm, bv))) = search_result {
+            else if let SearchResult::Cancelled(Some((bm, _bv))) = search_result {
                 if best_move == Move::Nullmove {
                     best_move = bm;
                     //best_val = bv;

+ 3 - 3
src/movegen.rs

@@ -204,7 +204,7 @@ pub fn generate_attacking_moves(game: &Game, side: Side) -> Vec<Move> {
 }
 
 
-pub fn sort_moves(game: &mut Game, hash: &mut Cache, move_list: &mut Vec<Move>) {
+pub fn sort_moves(game: &mut Game, hash: &mut Cache, killers: &[Move], move_list: &mut Vec<Move>) {
     move_list.sort_by_cached_key(|mov| {
         let undo = game.apply(*mov);
         if let Some(e) = hash.lookup(game) {
@@ -219,11 +219,11 @@ pub fn sort_moves(game: &mut Game, hash: &mut Cache, move_list: &mut Vec<Move>)
                 return e.value + 1000;
             }
             else {
-                return crate::evaluate::evaluate(game);
+                return crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
             }
         }
         else {
-            let eval = crate::evaluate::evaluate(game);
+            let eval = crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
             game.undo_move(undo);
             return eval;
         }

+ 18 - 3
src/search.rs

@@ -11,6 +11,7 @@ pub struct SearchControl<'a> {
     pub nodes: usize,
 
     pub pv: Vec<Move>,
+    pub killer_moves: [Move; 3],
 
     /// function to check if the search should be exited
     pub check: &'a mut dyn FnMut() -> bool,
@@ -33,12 +34,24 @@ impl<'a> SearchControl<'a> {
         SearchControl {
             nodes: 0,
             pv: Vec::with_capacity(depth as usize),
+            killer_moves: [Move::Nullmove; 3],
             check,
             stopping: false,
             move_history,
             initial_depth: depth
         }
     }
+
+    pub fn insert_killer(&mut self, killer: Move) {
+        for i in 1..self.killer_moves.len() {
+            self.killer_moves[i - 1] = self.killer_moves[i];
+        }
+        self.killer_moves[self.killer_moves.len() - 1] = killer;
+    }
+
+    pub fn is_killer(&self, killer: &Move) -> bool {
+        self.killer_moves.contains(killer)
+    }
 }
 
 
@@ -52,7 +65,7 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
 
     let mut moves = generate_legal_moves(game, game.turn);
     let mut rng = rand::thread_rng();
-    sort_moves(game, hash, &mut moves);
+    sort_moves(game, hash, &sc.killer_moves, &mut moves);
     
     info!("moves: {:?}", moves.iter().map(|mv| mv.to_string()).collect::<Vec<String>>());
 
@@ -191,7 +204,8 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
     }
 
 
-    sort_moves(game, hash, &mut moves);
+    sort_moves(game, hash, &sc.killer_moves, &mut moves);
+    //moves.sort_by_cached_key(|m| if sc.is_killer(*m) { -1 } else { 0 });
 
     let mut alpha_is_exact = false;
     let mut best_move = Move::Nullmove;
@@ -217,6 +231,7 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
             hash.cache(game, CacheEntry::new_upper(depth, val));
             //println!("but ret {}: {}", depth, beta);
             //info!("{} causes beta cutoff at {} ", mov.to_string(), val);
+            sc.insert_killer(mov);
             return beta;
         }
         if increase_mate_in(val) > alpha {
@@ -269,7 +284,7 @@ fn quiescence_search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache,
 
     for mov in moves {
         let undo = game.apply(mov);
-        let mut val = -quiescence_search(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
+        let val = -quiescence_search(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
         game.undo_move(undo);
 
         if sc.stopping {