Browse Source

improved move ordering; taking TT's pv into consideration

Nicolas Winkler 2 years ago
parent
commit
e21fed4cbb
2 changed files with 20 additions and 24 deletions
  1. 10 21
      src/movegen.rs
  2. 10 3
      src/search.rs

+ 10 - 21
src/movegen.rs

@@ -4,6 +4,8 @@ use bitboard::*;
 use game::Game;
 use hash::{Cache, EntryType};
 
+use crate::hash::CacheEntry;
+
 pub type Side = bool;
 
 pub const WHITE: Side = false;
@@ -202,7 +204,7 @@ pub fn generate_legal_moves(game: &mut Game, side: Side) -> Vec<Move> {
 }
 
 
-pub fn generate_legal_sorted_moves(game: &mut Game, hash: &mut Cache, killers: &[Move], side: Side) -> Vec<Move> {
+pub fn generate_legal_sorted_moves(game: &mut Game, hash: &mut Cache, killers: &[Move], ce: Option<CacheEntry>, side: Side) -> Vec<Move> {
     let moves = generate_moves(game, side);
 
     let illegal_value = crate::evaluate::MAX_VALUE;
@@ -216,27 +218,14 @@ pub fn generate_legal_sorted_moves(game: &mut Game, hash: &mut Cache, killers: &
             return illegal_value;
         }
 
-        if let Some(e) = hash.lookup(game) {
-            game.undo_move(undo);
-            if let EntryType::Value = e.entry_type {
-                return e.value;
-            }
-            else if let EntryType::LowerBound = e.entry_type {
-                return e.value - 1000;
-            }
-            else if let EntryType::UpperBound = e.entry_type {
-                return e.value + 1000;
-            }
-            else {
-                return crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
-            }
-        }
-        else {
-            let eval = crate::evaluate::evaluate(game) - if killers.contains(mov) { 200 } else { 0 };
-            game.undo_move(undo);
-            return eval;
-        }
+        let pv_bonus = if let Some(c) = &ce { if &c.mov == mov { 1000 } else { 0 } } else { 0 };
+        let killer_bonus = if killers.contains(mov) { 200 } else { 0 };
+
+        let eval = crate::evaluate::evaluate(game) - killer_bonus - pv_bonus;
+        game.undo_move(undo);
+        return eval;
     };
+
     let mut amovs: Vec<_> = moves.iter().map(|m| (*m, mov_val_and_legality(m))).collect();
     amovs.sort_unstable_by_key(|x| x.1);
 

+ 10 - 3
src/search.rs

@@ -133,8 +133,9 @@ pub fn search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alp
 
 pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, mut beta: PosValue, depth: i32) -> PosValue {
 
-    let mut pvmove: Option<Move> = None;
-    if let Some(e) = hash.lookup(game) {
+    let cache_entry = hash.lookup(game);
+
+    if let Some(e) = &cache_entry {
         if e.depth >= depth {
             //println!("TABLE HIT!");
             match e.entry_type {
@@ -166,7 +167,13 @@ pub fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut al
     }
 
     let ply_depth = (sc.initial_depth - depth) as usize;
-    let moves = generate_legal_sorted_moves(game, hash, &sc.killer_moves[ply_depth], game.turn);
+    let mut moves = generate_legal_sorted_moves(
+        game,
+        hash,
+        &sc.killer_moves[ply_depth],
+        cache_entry,
+        game.turn);
+
     //info!("nega moves: {:?}", moves.iter().map(|mv| mv.to_string()).collect::<Vec<String>>());
 
     let check = is_check(game, game.turn);