Quellcode durchsuchen

adding delta pruning

Nicolas Winkler vor 2 Jahren
Ursprung
Commit
c4ec8447cf
3 geänderte Dateien mit 39 neuen und 12 gelöschten Zeilen
  1. 2 1
      src/engine.rs
  2. 4 0
      src/movegen.rs
  3. 33 11
      src/search.rs

+ 2 - 1
src/engine.rs

@@ -183,7 +183,8 @@ impl Engine {
                 else if self.board.turn == BLACK {
                     sc.remaining_time = si.btime.map(|ms| Duration::from_millis(ms as _));
                 }
-                let search_rtn = move || sc.iterative_deepening(None);
+                let depth = si.depth;
+                let search_rtn = move || sc.iterative_deepening(depth);
                 thread::spawn(search_rtn)
             };
 

+ 4 - 0
src/movegen.rs

@@ -579,6 +579,10 @@ fn mvv_lva_score(mov: &Move) -> i32 {
     }
 }
 
+fn see_score(board: &mut Board, sq: Square, side: Side) -> i32 {
+    0
+}
+
 pub fn sort_moves_least_valuable_attacker(_game: &mut Board, move_list: &mut Vec<Move>) {
     move_list.sort_by_cached_key(mvv_lva_score)
 }

+ 33 - 11
src/search.rs

@@ -212,8 +212,10 @@ impl SearchControl {
                 }
             }
 
-            if Some(depth) == max_depth {
-                break;
+            if let Some(d) = max_depth {
+                if depth >= d {
+                    break;
+                }
             }
         }
 
@@ -453,8 +455,7 @@ pub fn negamax(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut a
     return alpha;
 }
 
-fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> PosValue {
-
+fn quiescence_search(board: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> PosValue {
     sc.nodes += 1;
     if sc.nodes % 1024 == 0 {
         if sc.check_stop() {
@@ -463,7 +464,7 @@ fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache,
         }
     }
 
-    let val = evaluate(game);
+    let val = evaluate(board);
     if val >= beta {
         return beta;
     }
@@ -475,18 +476,39 @@ fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache,
         return alpha;
     }
 
-    if game.get_piece(KING, game.turn) == 0 { return checkmated(); }
+    if board.get_piece(KING, board.turn) == 0 { return checkmated(); }
 
     //let mut moves = generate_legal_sorted_moves(game, hash, &[], None, true, game.turn);
-    let mut moves = generate_legal_moves(game, game.turn, true);
+    let mut moves = generate_legal_moves(board, board.turn, true);
 
     //sort_moves_no_hash(game, &mut moves);
-    sort_moves_least_valuable_attacker(game, &mut moves);
+    sort_moves_least_valuable_attacker(board, &mut moves);
+
+    let apply_delta_pruning = game_lateness(board) < 50;
 
     for mov in moves {
-        let undo = game.apply(mov);
-        let val = -quiescence_search(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
-        game.undo_move(undo);
+
+        if apply_delta_pruning {
+            const PIECE_VALUES: [i32; 6] = [
+                100, // Pawn
+                220, // Knight
+                320, // Bishop
+                400, // Rook
+                800, // Queen
+                100000 // King
+            ];
+            const SAFETY_MARGIN: i32 = 200;
+            let target_sq = mov.to_simple().to;
+            if let Some((piece_type, side)) = board.get_square(target_sq) {
+                if val + PIECE_VALUES[piece_type as usize] + SAFETY_MARGIN < alpha {
+                    continue;
+                }
+            }
+        }
+
+        let undo = board.apply(mov);
+        let val = -quiescence_search(board, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
+        board.undo_move(undo);
 
         if sc.stopping {
             return alpha