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
use crate::game::{Game, Playout, SingleWinner};
use crate::policies::{
mcts::{BaseMCTSPolicy, MCTSTreeNode, WithMCTSPolicy},
MultiplayerPolicyBuilder,
};
use crate::settings;
use async_trait::async_trait;
use std::f32;
use std::fmt;
use std::iter::*;
use std::sync::{Arc, RwLock};
#[derive(Debug, Clone, Copy)]
pub struct RAVEMoveInfo {
wins: f32,
count: f32,
wins_AMAF: f32,
count_AMAF: f32,
}
#[derive(Debug, Clone, Copy)]
pub struct RAVENodeInfo {
count: f32,
}
pub struct RAVEPolicy_<G: Game> {
color: G::Player,
uct_weight: f32,
}
#[async_trait]
impl<G: super::MCTSGame + SingleWinner> BaseMCTSPolicy<G> for RAVEPolicy_<G> {
type NodeInfo = RAVENodeInfo;
type MoveInfo = RAVEMoveInfo;
type PlayoutInfo = (bool, Vec<G::Move>);
fn get_value(
&self,
board: &G,
_action: &G::Move,
node_info: &Self::NodeInfo,
move_info: &Self::MoveInfo,
_exploration: bool,
) -> f32 {
let optimistic = board.turn() == self.color;
let value = self.eval(*node_info, move_info, optimistic);
let multiplier = if board.turn() == self.color { 1. } else { -1. };
multiplier * value
}
fn default_node(&self, _board: &G) -> Self::NodeInfo {
RAVENodeInfo { count: 0. }
}
fn default_move(&self, _board: &G, _action: &G::Move) -> Self::MoveInfo {
RAVEMoveInfo {
wins: 0.,
wins_AMAF: 0.,
count: 0.,
count_AMAF: 0.,
}
}
fn backpropagate(
&mut self,
leaf: Arc<RwLock<MCTSTreeNode<G, Self>>>,
history: &[G::Move],
(has_won, history_default): Self::PlayoutInfo,
) {
let z = if has_won { 1. } else { 0. };
let mut index = history.len();
let whole_history = [history, &history_default].concat();
let mut current_node = leaf;
while current_node.read().unwrap().parent.is_some() {
let (tree_pointer, action) = current_node
.read()
.unwrap()
.parent
.as_ref()
.map(|(t, a)| (t.upgrade().unwrap(), *a))
.unwrap();
current_node = tree_pointer;
let mut node = current_node.write().unwrap();
node.info.node.count += 1.;
let move_info = node.info.moves.get_mut(&action).unwrap();
move_info.count += 1.;
move_info.wins += (z - move_info.wins) / move_info.count;
index -= 1;
for u in (index + 2..whole_history.len()).step_by(2) {
let action_u = whole_history[u];
if (index..u).step_by(2).all(|i| action_u != whole_history[i]) {
if let Some(mut v_amaf) = node.info.moves.get_mut(&action_u) {
(*v_amaf).count_AMAF += 1.;
(*v_amaf).wins_AMAF += (z - (*v_amaf).wins_AMAF) / (*v_amaf).count_AMAF;
}
}
}
}
}
async fn simulate(&self, board: &G) -> <Self as BaseMCTSPolicy<G>>::PlayoutInfo {
let (s, default, _) = board.playout_history(self.color).await;
let default: Vec<G::Move> = default.iter().map(|(_, m)| *m).collect();
(s.winner() == Some(self.color), default)
}
}
impl<G: super::MCTSGame> RAVEPolicy_<G> {
fn beta(v: &RAVEMoveInfo) -> f32 {
let b = 0.0001;
let mut div = v.count_AMAF + v.count + 4. * v.count_AMAF * v.count * b * b;
if div == 0. {
div = 1.
};
v.count_AMAF / div
}
fn eval(
self: &RAVEPolicy_<G>,
node_info: RAVENodeInfo,
v: &RAVEMoveInfo,
optimistic: bool,
) -> f32 {
let multiplier = if optimistic { 1. } else { -1. };
let v_mean =
v.wins + multiplier * self.uct_weight * (node_info.count.ln() / (1. + v.count)).sqrt();
let v_AMAF = v.wins_AMAF;
let beta = Self::beta(v);
(1. - beta) * v_mean + beta * v_AMAF
}
}
pub type RAVEPolicy<G> = WithMCTSPolicy<G, RAVEPolicy_<G>>;
type RAVE = settings::RAVE;
impl fmt::Display for RAVE {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "RAVE")?;
writeln!(f, "|| uct_weight: {}", self.uct_weight)?;
writeln!(f, "|| N_PLAYOUT: {}", self.playouts)
}
}
impl<G> MultiplayerPolicyBuilder<G> for RAVE
where
G::Move: Send,
G::Player: Send,
G: super::MCTSGame + SingleWinner,
{
type P = RAVEPolicy<G>;
fn create(&self, color: G::Player) -> Self::P {
WithMCTSPolicy::new(
RAVEPolicy_ {
color,
uct_weight: self.uct_weight,
},
self.playouts,
)
}
}