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
use crate::deep::evaluator::{dynamics, DynamicsEvaluatorChannel};
use crate::game::*;
use ndarray::Ix3;
use ndarray::{Array, Dimension};
use tokio::sync::mpsc;
pub struct DynamicsNetworkOutput<H: Dimension> {
pub reward: f32,
pub repr_state: Array<f32, H>,
}
pub struct Simulated<G>
where
G: Features,
{
turn: G::Player,
repr_state: Array<f32, Ix3>,
possible_moves: Vec<G::Move>,
total_reward: f32,
dynamics_evaluator: mpsc::Sender<DynamicsEvaluatorChannel>,
repr_dimension: Ix3,
game_descriptor: G::Descriptor,
support_size: usize,
}
impl<G> Clone for Simulated<G>
where
G: Features,
{
fn clone(&self) -> Self {
Self {
turn: self.turn,
possible_moves: self.possible_moves.clone(),
repr_state: self.repr_state.clone(),
dynamics_evaluator: self.dynamics_evaluator.clone(),
total_reward: self.total_reward,
repr_dimension: self.repr_dimension,
game_descriptor: self.game_descriptor.clone(),
support_size: self.support_size,
}
}
}
impl<G> Simulated<G>
where
G: Features,
{
pub fn new(
turn: G::Player,
repr_state: Array<f32, Ix3>,
game_descriptor: G::Descriptor,
initial_possible_moves: Vec<G::Move>,
dynamics_evaluator: mpsc::Sender<DynamicsEvaluatorChannel>,
support_size: usize,
) -> Self {
let repr_dimension = repr_state.raw_dim();
Simulated {
turn,
possible_moves: initial_possible_moves,
repr_state,
total_reward: 0.,
dynamics_evaluator,
repr_dimension,
game_descriptor,
support_size,
}
}
}
use std::fmt::*;
impl<G> Debug for Simulated<G>
where
G: Features,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"Simulated: {:?}\n{:?}",
self.total_reward, self.repr_state
)
}
}
impl<G> Base for Simulated<G>
where
G: Features + 'static,
{
type Move = G::Move;
fn possible_moves(&self) -> Vec<Self::Move> {
self.possible_moves.clone()
}
}
#[async_trait]
impl<G> Playable for Simulated<G>
where
G: Features + 'static,
{
async fn play(&mut self, action: &<Self as Base>::Move) -> f32 {
let mut move_as_prob: HashMap<<Self as Base>::Move, f32> = HashMap::new();
move_as_prob.insert(*action, 1.);
let move_encoded = G::moves_to_feature(&self.game_descriptor, &move_as_prob);
let network_output = dynamics(
self.dynamics_evaluator.clone(),
&self.repr_state,
&move_encoded,
self.support_size,
)
.await;
self.repr_state = network_output.repr_state;
self.possible_moves = G::all_possible_moves(&self.game_descriptor).to_vec();
self.turn = G::player_after(self.turn);
network_output.reward
}
}
impl<G> Game for Simulated<G>
where
G: Features + 'static,
{
type Player = G::Player;
fn players() -> Vec<Self::Player> {
G::players()
}
fn player_after(player: Self::Player) -> Self::Player {
G::player_after(player)
}
fn turn(&self) -> Self::Player {
self.turn
}
}
impl<G> Features for Simulated<G>
where
G: Features + 'static,
{
type StateDim = Ix3;
type ActionDim = G::ActionDim;
type Descriptor = (Ix3, G::Descriptor);
fn get_features(&self) -> Self::Descriptor {
let ft = self.game_descriptor.clone();
(self.repr_dimension, ft)
}
fn state_dimension(descr: &Self::Descriptor) -> Self::StateDim {
descr.0
}
fn action_dimension(descr: &Self::Descriptor) -> Self::ActionDim {
G::action_dimension(&descr.1)
}
fn state_to_feature(&self, _pov: Self::Player) -> Array<f32, Self::StateDim> {
self.repr_state.clone()
}
fn moves_to_feature(
descr: &Self::Descriptor,
moves: &HashMap<Self::Move, f32>,
) -> Array<f32, Self::ActionDim> {
G::moves_to_feature(&descr.1, moves)
}
fn feature_to_moves(&self, features: &Array<f32, Self::ActionDim>) -> HashMap<Self::Move, f32> {
G::all_feature_to_moves(&self.game_descriptor, features)
}
fn all_possible_moves(descr: &Self::Descriptor) -> Vec<Self::Move> {
G::all_possible_moves(&descr.1)
}
fn all_feature_to_moves(
descr: &Self::Descriptor,
features: &Array<f32, Self::ActionDim>,
) -> HashMap<Self::Move, f32> {
G::all_feature_to_moves(&descr.1, features)
}
}