Bladeren bron

adding parts of aspiration window

Nicolas Winkler 3 jaren geleden
bovenliggende
commit
421f115eea
3 gewijzigde bestanden met toevoegingen van 31 en 9 verwijderingen
  1. 18 1
      src/engine.rs
  2. 4 4
      src/movegen.rs
  3. 9 4
      src/search.rs

+ 18 - 1
src/engine.rs

@@ -181,13 +181,30 @@ impl Engine {
         let mut sc = SearchControl{ nodes: 0, check: &mut check_fn, move_history: &mut self.move_history, initial_depth: 0 };
         let mut best_move = Move::default();
         let mut best_val: PosValue;
+
+        let mut alpha = crate::evaluate::MIN_VALUE;
+        let mut beta = crate::evaluate::MAX_VALUE;
+        let window_size = 50;
         loop {
 
             sc.initial_depth = depth;
-            let search_result = search(&mut self.game, &mut sc, &mut self.hash, depth as i32);
+
+            let search_result = search(&mut self.game, &mut sc, &mut self.hash, alpha, beta, depth as i32);
 
             if let SearchResult::Finished(bm, bv) = search_result {
                 info!("depth: {} bm: {}, bv: {}", depth, bm.to_string(), bv);
+
+                if bv >= beta || bv <= alpha {
+                    alpha = crate::evaluate::MIN_VALUE;
+                    beta = crate::evaluate::MAX_VALUE;
+                    //println!("repeat");
+                    continue;
+                }
+                else {
+                    //alpha = bv - window_size;
+                    //beta = bv + window_size;
+                }
+
                 best_move = bm;
                 best_val = bv;
 

+ 4 - 4
src/movegen.rs

@@ -100,10 +100,10 @@ impl Move {
             },
             Move::Promotion{ mov, promote_to, captured: _c } => {
                 Self::square_to_string(mov.from) + &Self::square_to_string(mov.to) + 
-                if *promote_to == QUEEN { "Q" }
-                else if *promote_to == ROOK { "R" }
-                else if *promote_to == KNIGHT { "N" }
-                else if *promote_to == BISHOP { "B" }
+                if *promote_to == QUEEN { "q" }
+                else if *promote_to == ROOK { "r" }
+                else if *promote_to == KNIGHT { "n" }
+                else if *promote_to == BISHOP { "b" }
                 else { "" }
             },
             Move::Nullmove => {

+ 9 - 4
src/search.rs

@@ -30,7 +30,7 @@ pub enum SearchResult {
 /**
  * searches for moves and returns the best move found plus its value
  */
-pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, depth: i32) -> SearchResult {
+pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> SearchResult {
     if depth == 0 {
         return SearchResult::Invalid;
     }
@@ -41,8 +41,8 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, depth:
     
     info!("moves: {:?}", moves.iter().map(|mv| mv.to_string()).collect::<Vec<String>>());
 
-    let mut alpha: PosValue = MIN_VALUE;
-    let mut beta: PosValue = MAX_VALUE;
+    //let mut alpha: PosValue = MIN_VALUE;
+    //let mut beta: PosValue = MAX_VALUE;
 
     // use a slight offset for the alpha value in the root node in order to
     // determine possibly multiple good moves
@@ -175,9 +175,14 @@ fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha:
         let nmov = Move::Nullmove;
         let undo = game.apply(nmov);
 
-        let (mut val, ret) = negamax(game, sc, hash, -beta, -alpha, depth / 2 - 2);
+        let (mut val, ret) = negamax(game, sc, hash, -beta, -alpha, depth / 2 - 1);
         val = -val;
         val = increase_mate_in(val);
+        
+        if ret {
+            game.undo_move(undo);
+            return (alpha, ret)
+        }
 
         if val >= beta {
             game.undo_move(undo);