diff options
| -rw-r--r-- | src/grossmeister/search.rs | 82 | 
1 files changed, 45 insertions, 37 deletions
| diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index c899216..1aa73a0 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -244,33 +244,12 @@ impl Grossmeister {          let mut gradual_widening_counter = 0;          while depth <= max_depth { -            println!("info hashfull {}", 1000 * self.transposition_table.iter().filter(|item| item.is_some()).count() / self.transposition_table.len()); -            println!("info depth {}", depth);              if self.debug {                  println!("info string window {:?}", (alpha, beta));              }              let search_result = self.negamax_search(alpha, beta, depth, 0); -            if search_result.0.abs() >= SCORE_MATE - 128f32 { // TODO: VALUE_WIN - MAX_PLY -                let score = search_result.0; -                let mate_distance = (SCORE_MATE - score.abs()) * if score > 0.0 { -                    1. -                } else { -                    -1. // -N means we are mated in N plies -                }; -                println!("info mate {:.0}", (mate_distance / 2.0).ceil()); - -                result = Some(search_result); - -                if score.abs() == SCORE_MATE { -                    break; -                } else { -                    depth += 1; -                    continue; -                } -            } -              if self.should_halt.load(std::sync::atomic::Ordering::SeqCst) {                  println!("info string halting search");                  break; @@ -280,35 +259,50 @@ impl Grossmeister {                  gradual_widening_counter += 1;                  beta = alpha + window_size * 0.1;                  alpha = search_result.0 - window_size * 2.0f32.powi(gradual_widening_counter); -                println!("info score upperbound {:.0}", beta * 100.0); +                println!("info depth {} score upperbound {:.0}", depth, beta * 100.0);                  continue;              }              if search_result.0 >= beta { // Beta-cutoff                  gradual_widening_counter += 1;                  alpha = beta - window_size * 0.1;                  beta = search_result.0 + window_size * 2.0f32.powi(gradual_widening_counter); -                println!("info score lowerbound {:.0}", alpha * 100.0); +                println!("info depth {} score lowerbound {:.0}", depth, alpha * 100.0);                  continue;              } -            if !search_result.1.is_empty() { -                depth += 1; -                gradual_widening_counter = 0; -                alpha = search_result.0 - window_size; -                beta = search_result.0 + window_size; +            if search_result.1.is_empty() { +                panic!("Why the fuck no moves?"); +            } -                { -                    print!("info score cp {:.0} pv ", search_result.0 * 100.0); -                    for mov in &search_result.1 { -                        print!("{} ", mov); -                    } -                    println!(); -                } +            // Print UCI info +            print!("info depth {} score ", depth); +            if search_result.0.abs() >= SCORE_MATE - 128f32 { // TODO: VALUE_WIN - MAX_PLY +                let mate_distance = (SCORE_MATE - search_result.0.abs()) * if search_result.0 > 0.0 { +                    1. +                } else { +                    -1. // -N means we are mated in N plies +                }; +                print!("mate {:.0} ", (mate_distance / 2.0).ceil()); +            } else { +                print!("cp {:.0} ", search_result.0 * 100.0) +            } +            print!("pv "); +            for mov in &search_result.1 { +                print!("{} ", mov); +            } +            println!(); +            // If our is mate in N, we break at depth N +            if SCORE_MATE - search_result.0.abs() == depth as f32 {                  result = Some(search_result); -            } else { -                panic!("Why the fuck no moves?"); +                break;              } + +            depth += 1; +            gradual_widening_counter = 0; +            alpha = search_result.0 - window_size; +            beta = search_result.0 + window_size; +            result = Some(search_result);          }          if result.is_none() { @@ -316,6 +310,8 @@ impl Grossmeister {              result = Some(self.negamax_search(-INFINITY, INFINITY, 1, 0))          } +        println!("info hashfull {}", 1000 * self.transposition_table.iter().filter(|item| item.is_some()).count() / self.transposition_table.len()); +          match result {              Some(r) => {                  print!("bestmove {}", r.1[0]); @@ -503,4 +499,16 @@ mod tests {          dbg!(score, pv);          assert_eq!(score + SCORE_MATE, 4.0); // Mate in 4 plies      } + +    #[test] +    fn another_mate_in_2() { +        let fen = String::from("7r/2pr1pR1/2p2p2/1p2pN2/p3Pk1P/P2P2b1/1PP1K3/R7 w - - 0 33"); +        let board = Board::from_FEN(fen); + +        let mut gm = Grossmeister::new(board); +        gm.debug = true; +        let (score, pv) = gm.iterative_deepening(5); +        dbg!(score, pv); +        assert_eq!(SCORE_MATE - score, 3.0); // Mate in 3 plies +    }  } | 
