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
use crate::game::{Game, NoFeatures};
use crate::settings;
use async_trait::async_trait;
use std::fmt::Display;
pub mod flat;
pub mod mcts;
pub mod nmcs;
pub mod nrpa;
pub mod ppa;
#[async_trait]
pub trait MultiplayerPolicy<T: Game> {
async fn play(&mut self, board: &T) -> T::Move;
}
pub trait MultiplayerPolicyBuilder<T: Game>: Display {
type P: MultiplayerPolicy<T>;
fn create(&self, color: T::Player) -> Self::P;
}
pub trait DynMultiplayerPolicyBuilder<'a, T: Game>: Display {
fn create(&self, color: T::Player) -> Box<dyn MultiplayerPolicy<T> + Send + Sync + 'a>;
}
impl<'a, G, PB> DynMultiplayerPolicyBuilder<'a, G> for PB
where
G: Game,
PB: MultiplayerPolicyBuilder<G>,
PB::P: 'a + Send + Sync,
{
fn create(&self, color: G::Player) -> Box<dyn MultiplayerPolicy<G> + Send + Sync + 'a> {
Box::new(PB::create(self, color))
}
}
pub trait SingleplayerPolicyBuilder<T: Game> {
type P: SingleplayerPolicy<T>;
fn create(&self) -> Self::P;
}
#[async_trait]
pub trait SingleplayerPolicy<T: Game> {
async fn solve(&mut self, board: &T) -> Vec<T::Move>;
}
use super::game;
pub fn get_multi<'a, G>(
config: settings::Config,
name: &str,
) -> Box<dyn DynMultiplayerPolicyBuilder<'a, G> + Sync + Send + 'a>
where
G: mcts::MCTSGame + game::SingleWinner + 'a + std::hash::Hash + Eq,
G::Move: Send,
{
match name {
"rand" => Box::new(flat::Random {}),
"flat" => Box::new(config.policies.flat),
"flat_ucb" => Box::new(config.policies.flat_ucb),
"uct" => Box::new(config.policies.uct),
"rave" => Box::new(config.policies.rave),
"ppa" => Box::new(ppa::PPA::<_, NoFeatures>::new(config.policies.ppa)),
"nmcs" => Box::new(nmcs::MultiNMCS::default()),
_ => panic!("Policy '{}' not found.", name),
}
}