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
use std::fmt::Debug;
use ego_tree::{NodeId, Tree};
use crate::aliases::{LazyMctsNode, LazyMctsTree, Num};
use crate::mcts_node::MctsNode;
use crate::Nat;
pub trait GameTrait: Clone {
type Player: Debug + Clone + Eq;
type Move: Debug + Clone;
fn legals_moves(&self) -> Vec<Self::Move>;
fn player_turn(&self) -> Self::Player;
fn hash(&self) -> u64;
fn is_final(&self) -> bool;
fn do_move(&mut self, m: &Self::Move);
fn get_winner(&self) -> Self::Player;
}
pub trait Evaluator<State: GameTrait, Reward: Clone, AdditionalInfo: Clone + Default> {
type Args;
type EvalResult: Clone;
fn eval_child(
child: &LazyMctsNode<State, Reward, AdditionalInfo>,
turn: &State::Player,
parent_visits: Nat,
args: &Self::Args,
) -> Num;
fn evaluate_leaf(child: State, turn: &State::Player) -> Self::EvalResult;
}
pub trait Playout<State> {
type Args;
fn playout(state: State, args: Self::Args) -> State;
}
pub trait LazyTreePolicy<
State: GameTrait,
EV: Evaluator<State, Reward, A>,
A: Clone + Default,
Reward: Clone,
>
{
fn tree_policy(
tree: &mut LazyMctsTree<State, Reward, A>,
root_state: State,
evaluator_args: &EV::Args,
) -> (NodeId, State);
fn update_state(mut root_state: State, historic: &[State::Move]) -> State {
for x in historic {
root_state.do_move(x)
}
return root_state;
}
fn best_child(
tree: &LazyMctsTree<State, Reward, A>,
turn: &State::Player,
parent_id: NodeId,
evaluator_args: &EV::Args,
) -> NodeId;
}
pub trait BackPropPolicy<
State: Clone,
Move: Clone,
Reward: Clone,
AdditionalInfo: Clone + Default,
EvalResult = Reward,
>
{
fn backprop(
tree: &mut Tree<MctsNode<State, Move, Reward, AdditionalInfo>>,
leaf: NodeId,
playout_result: EvalResult,
);
}