Ver Fonte

added basic move ordering

Nicolas Winkler há 3 anos atrás
pai
commit
568f1d377e
2 ficheiros alterados com 16 adições e 25 exclusões
  1. 11 2
      src/movegen.rs
  2. 5 23
      src/search.rs

+ 11 - 2
src/movegen.rs

@@ -2,6 +2,7 @@ use bitboard::Bitboard;
 use bitboard::Square;
 use bitboard::*;
 use game::Game;
+use hash::Cache;
 use log::info;
 
 pub type Side = bool;
@@ -194,10 +195,18 @@ pub fn generate_attacking_moves(game: &Game, side: Side) -> Vec<Move> {
 }
 
 
-pub fn sort_moves(game: &Game, move_list: &mut Vec<Move>) {
+pub fn sort_moves(game: &Game, hash: &mut Cache, move_list: &mut Vec<Move>) {
     let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
 
-    move_list.sort_by_key(|mov| {
+    move_list.sort_by_cached_key(|mov| {
+        let new_game = crate::search::apply_move(game, *mov);
+        if let Some(e) = hash.lookup(&new_game) {
+            return e.value;
+        }
+        else {
+            return crate::evaluate::evaluate(&new_game);
+        }
+
         match mov {
             Move::Default { mov, piece_type: _, captured } => if let None = captured { 0 } else { -10 }, //if (from_square(mov.to) & all_pieces) != 0 { -10 } else { 0 },
             Move::Castling { side: _, left: _ } => 0,

+ 5 - 23
src/search.rs

@@ -31,18 +31,8 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, depth:
     }
 
     let mut moves = generate_legal_moves(game, game.turn);
-    //info!("mov list: {:?}", moves.iter().map(|x| x.to_string()).collect::<Vec<String>>());
-
     let mut rng = rand::thread_rng();
-
-    /*moves.shuffle(&mut rng);
-
-    // return random move
-    if moves.len() >= 1 {
-        return moves[0];
-    }*/
-
-    sort_moves(game, &mut moves);
+    sort_moves(game, hash, &mut moves);
     
     info!("moves: {:?}", moves.iter().map(|mv| mv.to_string()).collect::<Vec<String>>());
 
@@ -143,7 +133,6 @@ fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha:
 
     let mut is_corr: PosValue = -1;
 
-
     let hash_entry = hash.lookup(game);
     if let Some(e) = hash_entry {
         if e.depth >= depth {
@@ -163,13 +152,11 @@ fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha:
         }
     }
 
-    let mut best = MIN_VALUE;
-    //let mut best_move = Move::default();
-    //info!(" -> generate_legal_moves");
-
     if game.get_piece(KING, game.turn) == 0 { return (MIN_VALUE, false); }
 
-    let moves = generate_legal_moves(game, game.turn);
+    let mut moves = generate_legal_moves(game, game.turn);
+
+    sort_moves(game, hash, &mut moves);
 
     if moves.len() == 0 {
         if is_check(game, game.turn) {
@@ -211,11 +198,6 @@ fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha:
             alpha = val;//(val as f64 * 0.95) as _;
             alpha_is_exact = true;
         }
-        //info!(" -> negamaxed {} -> {}", mov.to_string(), depth - 1);
-        if val > best {
-            best = val;
-            //best_move = mov;
-        }
         game.undo_move(undo);
     }
 
@@ -245,7 +227,7 @@ fn quiescence_search(game: &mut Game, sc: &mut SearchControl, mut alpha: PosValu
         return alpha;
     }
 
-    if game.get_piece(KING, game.turn) == 0 { return MIN_VALUE; }
+    if game.get_piece(KING, game.turn) == 0 { return -mate(); }
 
     let moves = generate_attacking_moves(game, game.turn);