1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::search::{search as blocking_search, SearchContext, SearchParameters, SearchTerminator};
use crate::{EvalChessBoard, SearchOutcome};
use anyhow::Result;
use myopic_board::Side;
use std::cmp::{max, min};
use std::rc::Rc;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;

const INFINITE_DURATION: Duration = Duration::from_secs(1_000_000);
const INFINITE_DEPTH: usize = 1_000;
const DEFAULT_SEARCH_DURATION: Duration = Duration::from_secs(30);
const DEFAULT_SEARCH_DEPTH: usize = 10;
const DEFAULT_TABLE_SIZE: usize = 100_000;
const MAX_COMPUTED_MOVE_SEARCH_DURATION: Duration = Duration::from_secs(45);

pub type SearchCommandTx<B> = Sender<SearchCommand<B>>;
pub type SearchResultRx = Receiver<Result<SearchOutcome>>;
type CmdRx<B> = Receiver<SearchCommand<B>>;
type ResultTx = Sender<Result<SearchOutcome>>;

#[derive(Debug, Clone, PartialEq)]
pub enum SearchCommand<B: EvalChessBoard> {
    Go,
    GoOnce,
    Stop,
    Close,
    Root(B),
    Infinite,
    Depth(usize),
    Time(usize),
    GameTime {
        w_base: usize,
        w_inc: usize,
        b_base: usize,
        b_inc: usize,
    },
}

/// Create an interactive search running on a separate thread, communication happens
/// via an input channel which accepts a variety of commands and an output channel
/// which transmits the search results.
pub fn search<B: EvalChessBoard + 'static>() -> (SearchCommandTx<B>, SearchResultRx) {
    let (input_tx, input_rx) = mpsc::channel::<SearchCommand<B>>();
    let (output_tx, output_rx) = mpsc::channel::<Result<SearchOutcome>>();
    std::thread::spawn(move || {
        let mut search = InteractiveSearch::new(input_rx, output_tx);
        loop {
            match &search.input_rx.recv() {
                Err(_) => continue,
                Ok(input) => match input.to_owned() {
                    SearchCommand::Close => break,
                    SearchCommand::Stop => (),
                    SearchCommand::Go => search.execute_then_send(),
                    SearchCommand::Root(root) => search.root = Some(root),
                    SearchCommand::Depth(max_depth) => search.max_depth = max_depth,
                    SearchCommand::Time(max_time) => search.set_max_time(max_time),
                    SearchCommand::GameTime {
                        w_base,
                        w_inc,
                        b_base,
                        b_inc,
                    } => search.set_game_time(w_base, w_inc, b_base, b_inc),
                    SearchCommand::Infinite => {
                        search.max_time = INFINITE_DURATION;
                        search.max_depth = INFINITE_DEPTH;
                    }
                    SearchCommand::GoOnce => {
                        search.execute_then_send();
                        break;
                    }
                },
            }
        }
    });
    (input_tx, output_rx)
}

struct InteractiveSearch<B: EvalChessBoard> {
    input_rx: Rc<CmdRx<B>>,
    output_tx: ResultTx,
    root: Option<B>,
    max_depth: usize,
    max_time: Duration,
    transposition_table_size: usize,
}

impl<B: EvalChessBoard + 'static> InteractiveSearch<B> {
    pub fn new(input_rx: CmdRx<B>, output_tx: ResultTx) -> InteractiveSearch<B> {
        InteractiveSearch {
            input_rx: Rc::new(input_rx),
            root: None,
            output_tx,
            max_depth: DEFAULT_SEARCH_DEPTH,
            max_time: DEFAULT_SEARCH_DURATION,
            transposition_table_size: DEFAULT_TABLE_SIZE,
        }
    }

    pub fn set_max_time(&mut self, time: usize) {
        self.max_time = Duration::from_millis(time as u64);
    }

    // TODO This lets time run out with an increment...
    pub fn set_game_time(&mut self, w_base: usize, w_inc: usize, b_base: usize, b_inc: usize) {
        if self.root.is_some() {
            let active = self.root.as_ref().unwrap().active();
            let mut time = max(
                500,
                match active {
                    Side::White => w_inc,
                    _ => b_inc,
                },
            );
            time += match active {
                Side::White => w_base / 10,
                Side::Black => b_base / 10,
            };
            self.set_max_time(min(
                time,
                MAX_COMPUTED_MOVE_SEARCH_DURATION.as_millis() as usize,
            ));
        }
    }

    pub fn execute_then_send(&self) -> () {
        if self.root.is_some() {
            match self.output_tx.send(self.execute()) {
                _ => (),
            }
        }
    }

    pub fn execute(&self) -> Result<SearchOutcome> {
        let tracker = InteractiveSearchTerminator {
            max_depth: self.max_depth,
            max_time: self.max_time,
            stop_signal: self.input_rx.clone(),
        };
        blocking_search(
            self.root.clone().unwrap(),
            SearchParameters {
                terminator: tracker,
                table_size: self.transposition_table_size,
            },
        )
    }
}

struct InteractiveSearchTerminator<B: EvalChessBoard> {
    max_time: Duration,
    max_depth: usize,
    stop_signal: Rc<CmdRx<B>>,
}

impl<B: EvalChessBoard> SearchTerminator for InteractiveSearchTerminator<B> {
    fn should_terminate(&self, ctx: &SearchContext) -> bool {
        ctx.start_time.elapsed() > self.max_time
            || ctx.depth_remaining >= self.max_depth
            || match self.stop_signal.try_recv() {
                Ok(SearchCommand::Stop) => true,
                _ => false,
            }
    }
}