Explorar o código

adding move ordering in quiescence search

Nicolas Winkler %!s(int64=3) %!d(string=hai) anos
pai
achega
eda8271138
Modificáronse 4 ficheiros con 46 adicións e 38 borrados
  1. 35 16
      src/engine.rs
  2. 3 18
      src/interface.rs
  3. 4 2
      src/main.rs
  4. 4 2
      src/search.rs

+ 35 - 16
src/engine.rs

@@ -17,6 +17,7 @@ pub enum EngineMsg {
     SetPiece(Bitboard),
     Search(SearchInfo),
     Print,
+    IsReady,
     Ping,
     Stop,
     NewGame,
@@ -86,34 +87,41 @@ impl Engine {
         while let Some(msg) = self.dequeue_message() {
             match msg {
                 EngineMsg::SetBoard{ pos, moves } => {
-                    self.game = pos;
-                    self.game.zobrist = Some((self.zobrist_table.clone(), self.game.calculate_zobrist(&self.zobrist_table)));
-                    self.move_history.clear();
-                    self.move_history.increment(self.game.calculate_zobrist(&self.zobrist_table));
-                    for mov in moves {
-                        let m = self.game.parse_move(&mov);
-                        if let Ok(mm) = m {
-                            self.game.apply(mm);
-                            self.move_history.increment(self.game.calculate_zobrist(&self.zobrist_table));
-                        }
-                        else {
-                            println!("{}", self.game.beautiful_print());
-                            println!("error parsing move {}", mov);
-                            break;
-                        }
-                    }
+                    self.set_position(pos, &moves);
                 },
                 EngineMsg::Search(ref info) => {
                     self.start_search(info);
                 },
                 EngineMsg::Print => {
                     println!("{}", self.game.beautiful_print());
+                },
+                EngineMsg::IsReady => {
+                    println!("readyok");
                 }
                 _ => {}
             }
         }
     }
 
+    fn set_position(&mut self, pos: Game, moves: &Vec<String>) {
+        self.game = pos;
+        self.game.zobrist = Some((self.zobrist_table.clone(), self.game.calculate_zobrist(&self.zobrist_table)));
+        self.move_history.clear();
+        self.move_history.increment(self.game.calculate_zobrist(&self.zobrist_table));
+        for mov in moves {
+            let m = self.game.parse_move(&mov);
+            if let Ok(mm) = m {
+                self.game.apply(mm);
+                self.move_history.increment(self.game.calculate_zobrist(&self.zobrist_table));
+            }
+            else {
+                println!("{}", self.game.beautiful_print());
+                println!("error parsing move {}", mov);
+                break;
+            }
+        }
+    }
+
     /**
      * blocks until a message is available
      */
@@ -157,6 +165,10 @@ impl Engine {
                         messages.push_back(msg);
                         return true;
                     }
+                    if let EngineMsg::IsReady = msg {
+                        println!("readyok");
+                        return false;
+                    }
                     else {
                         messages.push_back(msg);
                     }
@@ -244,6 +256,13 @@ impl Engine {
                 }
                 depth += 1;
             }
+            else if let SearchResult::Cancelled(Some((bm, bv))) = search_result {
+                if best_move == Move::Nullmove {
+                    best_move = bm;
+                    best_val = bv;
+                }
+                break;
+            }
             else {
                 break;
             }

+ 3 - 18
src/interface.rs

@@ -30,7 +30,7 @@ fn run_command(mut cmd: Vec<&str>, r: &Receiver<InterfaceMsg>, s: &Sender<Engine
         cmd.drain(0..1);
         match command {
             "uci" => cmd_uci(cmd),
-            "isready" => cmd_isready(cmd),
+            "isready" => cmd_isready(cmd, r, s),
             "position" => cmd_position(cmd, r, s),
             "print" => cmd_print(cmd, r, s),
             "go" => cmd_go(cmd, r, s),
@@ -48,8 +48,8 @@ fn cmd_uci(_args: Vec<&str>) {
     println!("uciok");
 }
 
-fn cmd_isready(_args: Vec<&str>) {
-    println!("readyok");
+fn cmd_isready(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
+    s.send(EngineMsg::IsReady).unwrap();
 }
 
 fn cmd_position(mut args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
@@ -118,21 +118,6 @@ fn cmd_go(args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
 
 
     s.send(EngineMsg::Search(si)).unwrap();
-
-    /*if args.len() > 1 && args[0] == "movetime" {
-        let movetime = args[1].parse::<isize>().unwrap_or(1000);
-        s.send(EngineMsg::Search(SearchInfo::Movetime(movetime))).unwrap();
-    }
-    else if args.len() > 1 && args[0] == "depth" {
-        let depth = args[1].parse::<isize>().unwrap_or(1);
-        s.send(EngineMsg::Search(SearchInfo::Depth(depth))).unwrap();
-    }
-    else if args.len() > 1 {
-    //wtime 923997 winc 5000 btime 918725 binc 5000
-    }
-    else {
-        s.send(EngineMsg::Search(SearchInfo::Infinite)).unwrap();
-    }*/
 }
 
 fn cmd_stop(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {

+ 4 - 2
src/main.rs

@@ -14,6 +14,8 @@ extern crate rand;
 
 use std::sync::mpsc;
 use std::{thread, fs::File};
+use log::*;
+use simplelog::*;
 use engine::Engine;
 
 fn main() {
@@ -37,8 +39,8 @@ fn main() {
         println!("t: {}", t);
     }*/
 
-    //let logfile = File::create("/home/nicolas/debug.log").unwrap();
-    //simplelog::WriteLogger::init(LevelFilter::Info, Config::default(), logfile).unwrap();
+    let logfile = File::create("C:\\Users\\Nicolas\\debug.log").unwrap();
+    simplelog::WriteLogger::init(LevelFilter::Info, Config::default(), logfile).unwrap();
 
     let (esend, erecv) = mpsc::channel();
     let (isend, irecv) = mpsc::channel();

+ 4 - 2
src/search.rs

@@ -234,7 +234,6 @@ fn negamax(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha:
 }
 
 fn quiescence_search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> (PosValue, bool) {
-    let val = evaluate(game);
 
     sc.nodes += 1;
     if sc.nodes % 1024 == 0 {
@@ -243,6 +242,7 @@ fn quiescence_search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache,
         }
     }
 
+    let val = evaluate(game);
     if val >= beta {
         return (beta, false);
     }
@@ -256,7 +256,9 @@ fn quiescence_search(game: &mut Game, sc: &mut SearchControl, hash: &mut Cache,
 
     if game.get_piece(KING, game.turn) == 0 { return (-mate(), false); }
 
-    let moves = generate_attacking_moves(game, game.turn);
+    let mut moves = generate_attacking_moves(game, game.turn);
+
+    sort_moves(game, hash, &mut moves);
 
     for mov in moves {
         let undo = game.apply(mov);