ai-backgammon 0.1.0

A compact cubeless Backgammon rules and expectimax engine
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::{DICE_OUTCOMES, Dice, Equity, Play, PlayOutcome, Position, evaluate_position};
#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
use wasm_bindgen::prelude::*;

const DEFAULT_TT_ENTRIES: usize = 1 << 15;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Limits for one iterative-deepening expectimax search.
pub struct SearchOptions {
    /// Maximum number of complete future checker turns.
    pub max_depth: u8,
    /// Maximum decision nodes; zero is treated as one to preserve a fallback.
    pub node_limit: u64,
    /// Soft wall-clock limit in milliseconds; zero disables it.
    pub soft_time_ms: u32,
}

impl Default for SearchOptions {
    fn default() -> Self {
        SEARCH_PRESETS[SEARCH_PRESETS.len() - 1].options
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SearchPreset {
    pub name: &'static str,
    pub options: SearchOptions,
}

pub const SEARCH_PRESETS: [SearchPreset; 6] = [
    preset("beginner", 0, 1, 20),
    preset("easy", 1, 1_000, 50),
    preset("medium", 2, 5_000, 120),
    preset("hard", 2, 20_000, 300),
    preset("expert", 3, 80_000, 650),
    preset("maximum", 4, 250_000, 1_000),
];

const fn preset(
    name: &'static str,
    max_depth: u8,
    node_limit: u64,
    soft_time_ms: u32,
) -> SearchPreset {
    SearchPreset {
        name,
        options: SearchOptions {
            max_depth,
            node_limit,
            soft_time_ms,
        },
    }
}

pub fn search_preset(name: &str) -> Option<SearchPreset> {
    SEARCH_PRESETS
        .iter()
        .copied()
        .find(|preset| preset.name == name)
}

#[derive(Clone, Debug, PartialEq)]
/// The last fully completed iteration and a legal fallback if search stopped early.
pub struct SearchReport {
    /// Selected complete play, or `None` when the supplied position is terminal.
    pub best_play: Option<Play>,
    /// Outcome probabilities from the root side-to-move's perspective.
    pub equity: Equity,
    /// Deepest fully completed iteration.
    pub depth: u8,
    /// Visited decision nodes.
    pub nodes: u64,
    /// Enumerated dice outcomes.
    pub chance_nodes: u64,
    /// Exact-depth transposition-table hits.
    pub tt_hits: u64,
    /// Whether a node, time, or caller cancellation limit interrupted search.
    pub stopped: bool,
}

#[derive(Clone, Copy, Debug)]
struct Entry {
    key: u64,
    depth: u8,
    equity: Equity,
    occupied: bool,
}

impl Default for Entry {
    fn default() -> Self {
        Self {
            key: 0,
            depth: 0,
            equity: Equity::zero(),
            occupied: false,
        }
    }
}

pub struct Searcher {
    table: Vec<Entry>,
    nodes: u64,
    node_limit: u64,
    chance_nodes: u64,
    tt_hits: u64,
    stopped: bool,
    deadline: Deadline,
}

impl Default for Searcher {
    fn default() -> Self {
        Self::new()
    }
}

impl Searcher {
    pub fn new() -> Self {
        Self::with_table_entries(DEFAULT_TT_ENTRIES)
    }

    /// Creates a searcher with a fixed-size table; zero disables the table.
    pub fn with_table_entries(entries: usize) -> Self {
        Self {
            table: vec![Entry::default(); entries],
            nodes: 0,
            node_limit: 0,
            chance_nodes: 0,
            tt_hits: 0,
            stopped: false,
            deadline: Deadline::disabled(),
        }
    }

    /// Searches complete plays for known `dice` using iterative deepening.
    pub fn search(
        &mut self,
        position: Position,
        dice: Dice,
        options: SearchOptions,
    ) -> SearchReport {
        self.search_until(position, dice, options, || false)
    }

    /// Searches until a normal limit or a caller-provided cancellation signal fires.
    pub fn search_until<F: Fn() -> bool>(
        &mut self,
        position: Position,
        dice: Dice,
        options: SearchOptions,
        should_stop: F,
    ) -> SearchReport {
        self.nodes = 0;
        self.node_limit = options.node_limit.max(1);
        self.chance_nodes = 0;
        self.tt_hits = 0;
        self.stopped = false;
        self.deadline = Deadline::after(options.soft_time_ms);
        self.table.fill(Entry::default());

        if position.game_outcome().is_some() {
            return self.report(None, evaluate_position(position), 0);
        }

        let outcomes = position.legal_outcomes(dice);
        let Some(fallback) = outcomes.first() else {
            return self.report(None, evaluate_position(position), 0);
        };
        let mut best_play = fallback.representative().clone();
        let mut best_equity = evaluate_position(fallback.position()).reversed();
        let mut completed_depth = 0;

        for depth in 1..=options.max_depth {
            if self.should_stop(&should_stop) {
                break;
            }
            let Some((play, equity)) = self.search_root(position, dice, depth, &should_stop) else {
                break;
            };
            best_play = play;
            best_equity = equity;
            completed_depth = depth;
            if best_equity.expected_points().abs() == 3.0 {
                break;
            }
        }

        self.report(Some(best_play), best_equity, completed_depth)
    }

    fn search_root<F: Fn() -> bool>(
        &mut self,
        position: Position,
        dice: Dice,
        depth: u8,
        should_stop: &F,
    ) -> Option<(Play, Equity)> {
        let outcomes = ordered_outcomes(position, dice);
        let mut best: Option<(Play, Equity)> = None;
        for outcome in outcomes {
            if !self.visit(should_stop) {
                return None;
            }
            let equity = self
                .chance_value(outcome.position(), depth - 1, should_stop)?
                .reversed();
            if better(equity, outcome.representative(), best.as_ref()) {
                best = Some((outcome.representative().clone(), equity));
            }
        }
        best
    }

    fn chance_value<F: Fn() -> bool>(
        &mut self,
        position: Position,
        depth: u8,
        should_stop: &F,
    ) -> Option<Equity> {
        let key = position.hash();
        if let Some(equity) = self.probe(key, depth) {
            return Some(equity);
        }
        if position.game_outcome().is_some() || depth == 0 {
            let equity = evaluate_position(position);
            self.store(key, depth, equity);
            return Some(equity);
        }

        let mut total = Equity::zero();
        for outcome in DICE_OUTCOMES {
            if self.should_stop(should_stop) {
                return None;
            }
            self.chance_nodes += 1;
            let equity = self.decision_value(position, outcome.dice, depth, should_stop)?;
            total.add_weighted(equity, f32::from(outcome.weight) / 36.0);
        }
        self.store(key, depth, total);
        Some(total)
    }

    fn decision_value<F: Fn() -> bool>(
        &mut self,
        position: Position,
        dice: Dice,
        depth: u8,
        should_stop: &F,
    ) -> Option<Equity> {
        let mut best: Option<(Play, Equity)> = None;
        for outcome in ordered_outcomes(position, dice) {
            if !self.visit(should_stop) {
                return None;
            }
            let equity = self
                .chance_value(outcome.position(), depth - 1, should_stop)?
                .reversed();
            if better(equity, outcome.representative(), best.as_ref()) {
                best = Some((outcome.representative().clone(), equity));
            }
        }
        best.map(|(_, equity)| equity)
    }

    fn visit<F: Fn() -> bool>(&mut self, should_stop: &F) -> bool {
        if self.nodes >= self.node_limit || self.should_stop(should_stop) {
            self.stopped = true;
            return false;
        }
        self.nodes += 1;
        true
    }

    fn should_stop<F: Fn() -> bool>(&mut self, should_stop: &F) -> bool {
        if self.stopped || self.deadline.expired() || should_stop() {
            self.stopped = true;
            true
        } else {
            false
        }
    }

    fn probe(&mut self, key: u64, depth: u8) -> Option<Equity> {
        if self.table.is_empty() {
            return None;
        }
        let entry = self.table[key as usize % self.table.len()];
        if entry.occupied && entry.key == key && entry.depth == depth {
            self.tt_hits += 1;
            Some(entry.equity)
        } else {
            None
        }
    }

    fn store(&mut self, key: u64, depth: u8, equity: Equity) {
        if self.table.is_empty() {
            return;
        }
        let index = key as usize % self.table.len();
        self.table[index] = Entry {
            key,
            depth,
            equity,
            occupied: true,
        };
    }

    fn report(&self, best_play: Option<Play>, equity: Equity, depth: u8) -> SearchReport {
        SearchReport {
            best_play,
            equity,
            depth,
            nodes: self.nodes,
            chance_nodes: self.chance_nodes,
            tt_hits: self.tt_hits,
            stopped: self.stopped,
        }
    }
}

fn ordered_outcomes(position: Position, dice: Dice) -> Vec<PlayOutcome> {
    let mut outcomes = position.legal_outcomes(dice);
    outcomes.sort_by(|left, right| {
        evaluate_position(right.position())
            .reversed()
            .expected_points()
            .total_cmp(
                &evaluate_position(left.position())
                    .reversed()
                    .expected_points(),
            )
            .then_with(|| left.representative().cmp(right.representative()))
    });
    outcomes
}

fn better(equity: Equity, play: &Play, best: Option<&(Play, Equity)>) -> bool {
    let Some((best_play, best_equity)) = best else {
        return true;
    };
    equity.expected_points() > best_equity.expected_points()
        || (equity.expected_points() == best_equity.expected_points() && play < best_play)
}

#[cfg(not(target_arch = "wasm32"))]
struct Deadline(Option<Instant>);

#[cfg(not(target_arch = "wasm32"))]
impl Deadline {
    fn disabled() -> Self {
        Self(None)
    }

    fn after(milliseconds: u32) -> Self {
        Self(
            (milliseconds > 0)
                .then(|| Instant::now() + Duration::from_millis(u64::from(milliseconds))),
        )
    }

    fn expired(&self) -> bool {
        self.0.is_some_and(|deadline| Instant::now() >= deadline)
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = performance, js_name = now)]
    fn performance_now() -> f64;
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
struct Deadline(Option<f64>);

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
impl Deadline {
    fn disabled() -> Self {
        Self(None)
    }

    fn after(milliseconds: u32) -> Self {
        Self((milliseconds > 0).then(|| performance_now() + f64::from(milliseconds)))
    }

    fn expired(&self) -> bool {
        self.0.is_some_and(|deadline| performance_now() >= deadline)
    }
}

#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
struct Deadline;

#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
impl Deadline {
    const fn disabled() -> Self {
        Self
    }

    const fn after(_milliseconds: u32) -> Self {
        Self
    }

    const fn expired(&self) -> bool {
        false
    }
}