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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use crate::game::*;
use ndarray::s;
use std::fmt;
use std::iter::FromIterator;
use ggpf_gym::*;
#[derive(Clone)]
pub struct Gym {
env: GymRunnerClient,
game: String,
possible_moves: Vec<usize>,
is_done: bool,
current_state: Array<f32, Ix3>,
features: (Vec<usize>, Ix3, Ix1),
}
impl fmt::Debug for Gym {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TODO")
}
}
fn convert_to_3D(shape: &[usize]) -> Ix3 {
if shape.len() > 3 {
panic!("Observation dimension is too big.")
};
let mut dim = Ix3(1, 1, 1);
for i in 0..shape.len() {
dim[3 - shape.len() + i] = shape[i]
}
dim
}
fn interpolate(image: &Array<f32, Ix3>, x_t: usize, y_t: usize) -> Array<f32, Ix3> {
let x_o = image.shape()[0] as f32;
let y_o = image.shape()[1] as f32;
let mut result = Array::zeros([x_t, y_t, 3]);
for x in 0..x_t {
for y in 0..y_t {
let x_r = (x as f32 / x_t as f32) * x_o;
let x_0 = x_r.floor();
let x_1 = x_r.ceil();
let y_r = (y as f32 / y_t as f32) * y_o;
let y_0 = y_r.floor();
let y_1 = y_r.ceil();
let f_0_0 = image.slice(s![x_0 as usize, y_0 as usize, ..]);
let f_0_1 = image.slice(s![x_0 as usize, y_1 as usize, ..]);
let f_1_0 = image.slice(s![x_1 as usize, y_0 as usize, ..]);
let f_1_1 = image.slice(s![x_1 as usize, y_1 as usize, ..]);
let dx = x_r - x_0;
let dy = y_r - y_0;
let delta_f_x = &f_1_0 - &f_0_0;
let delta_f_y = &f_0_1 - &f_0_0;
let delta_f_xy = &f_1_1 + &f_0_0 - f_1_0 - f_0_1;
result
.slice_mut(s![x, y, ..])
.assign(&(dx * delta_f_x + dy * delta_f_y + dx * dy * delta_f_xy + f_0_0));
}
}
result
}
use tarpc::context;
impl Gym {
pub async fn new(mut env: GymRunnerClient, game: String) -> Self {
let possible_moves = match env.action_space(context::current()).await.unwrap() {
gym::SpaceTemplate::DISCRETE { n } => (0..n).collect::<Vec<_>>(),
x => panic!("Unsupported action space. {:?}", x),
};
let init_state = env.reset(context::current()).await.unwrap();
let state_dimension = match env.observation_space(context::current()).await.unwrap() {
gym::SpaceTemplate::BOX { shape, .. } => convert_to_3D(&shape),
_ => panic!("..."),
};
let obs_state = init_state
.get_box()
.unwrap()
.mapv(|x| x as f32)
.into_shape(state_dimension)
.expect("Unable to reshape observation.");
let action_dimension = Ix1(possible_moves.len());
Self {
env,
possible_moves: possible_moves.clone(),
is_done: false,
current_state: obs_state,
features: (possible_moves, state_dimension, action_dimension),
game,
}
}
}
impl Base for Gym {
type Move = usize;
fn possible_moves(&self) -> Vec<Self::Move> {
self.possible_moves.clone()
}
fn is_finished(&self) -> bool {
self.is_done
}
}
use async_trait::async_trait;
#[async_trait]
impl Playable for Gym {
#[allow(clippy::trivially_copy_pass_by_ref)]
async fn play(&mut self, action: &usize) -> f32 {
let next_state = self.env.play(context::current(), *action).await.unwrap();
self.is_done = next_state.is_done;
next_state.reward as f32
}
}
use ndarray::{Ix1, Ix3};
impl Singleplayer for Gym {}
impl Features for Gym {
type StateDim = Ix3;
type ActionDim = Ix1;
type Descriptor = (Vec<usize>, Ix3, Ix1);
fn get_features(&self) -> Self::Descriptor {
let (pm, st, ac) = self.features.clone();
if self.game == "Breakout-v0" {
(pm, ndarray::Dim([96, 96, 3]), ac)
} else {
(pm, st, ac)
}
}
fn state_dimension(descr: &Self::Descriptor) -> Self::StateDim {
descr.1
}
fn action_dimension(descr: &Self::Descriptor) -> Self::ActionDim {
descr.2
}
fn state_to_feature(&self, _pov: Self::Player) -> Array<f32, Self::StateDim> {
let res = self.current_state.clone();
if self.game == "Breakout-v0" {
interpolate(&res, 96, 96)
} else {
res
}
}
fn all_possible_moves(descr: &Self::Descriptor) -> Vec<Self::Move> {
descr.0.clone()
}
fn moves_to_feature(
descr: &Self::Descriptor,
moves: &HashMap<Self::Move, f32>,
) -> Array<f32, Self::ActionDim> {
let mut res = Array::zeros(descr.2);
for (i, v) in moves.iter() {
res[*i] = *v;
}
res
}
fn all_feature_to_moves(
_descr: &Self::Descriptor,
features: &Array<f32, Self::ActionDim>,
) -> HashMap<Self::Move, f32> {
HashMap::from_iter(features.iter().cloned().enumerate())
}
fn feature_to_moves(&self, features: &Array<f32, Self::ActionDim>) -> HashMap<Self::Move, f32> {
HashMap::from_iter(features.iter().cloned().enumerate())
}
}
#[derive(Clone, Debug)]
pub struct GymBuilder {
pub address: String,
pub game_name: String,
pub render: bool,
}
use tarpc::client;
#[async_trait]
impl SingleplayerGameBuilder for GymBuilder {
type G = Gym;
async fn create(&self) -> Gym {
let conn = tarpc::serde_transport::tcp::connect(&self.address, BinCodec::default());
let conn = conn.await.unwrap();
let mut runner = GymRunnerClient::new(client::Config::default(), conn)
.spawn()
.unwrap();
runner
.init(context::current(), self.game_name.clone(), self.render)
.await
.unwrap();
runner.reset(context::current()).await.unwrap();
Gym::new(runner, self.game_name.clone()).await
}
}