-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.rs
79 lines (64 loc) · 1.67 KB
/
unit.rs
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
use crate::game::{Game, PlayerIndex};
// A trivial game with one move
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Unit(pub bool);
impl std::fmt::Display for Unit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "()")
}
}
#[derive(Clone)]
pub struct UnitGame;
pub struct Player;
impl PlayerIndex for Player {
fn to_index(&self) -> usize {
0
}
}
impl Game for UnitGame {
type S = Unit;
type A = ();
type P = Player;
fn apply(state: Self::S, _m: &Self::A) -> Self::S {
assert!(!state.0);
// assert!(m == ()); // always true
Unit(true)
}
fn generate_actions(state: &Self::S, actions: &mut Vec<Self::A>) {
if !state.0 {
actions.push(());
}
}
fn is_terminal(state: &Self::S) -> bool {
state.0
}
fn notation(_: &Self::S, _: &Self::A) -> String {
"()".to_string()
}
fn winner(_: &Self::S) -> Option<Player> {
Some(Player)
}
fn player_to_move(_: &Self::S) -> Player {
Player
}
fn num_players() -> usize {
1
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::strategies::mcts::{strategy, TreeSearch};
use crate::strategies::Search;
#[test]
pub fn test_unit() {
let mut search: TreeSearch<UnitGame, strategy::Ucb1> = TreeSearch::default();
search.config.max_iterations = 10;
let state = Unit::default();
search.choose_action(&state);
#[allow(clippy::unit_arg)]
let new_state = UnitGame::apply(state, &());
assert!(new_state.0);
assert!(UnitGame::is_terminal(&new_state));
}
}