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
234
235
236
237
238
239
240
241
242
243
244
extern crate connect6;
extern crate cpython;
extern crate rand;
#[macro_use]
pub mod macro_def;
pub mod pybind;
use connect6::{agent, policy};
use cpython::*;
py_module_initializer!(
libpyconnect6,
initlibpyconnect6,
PyInit_pyconnect6,
|py, m| {
try!(m.add(
py,
"__doc__",
"This module is implemented in Rust, for Simulating Connect6"
));
try!(m.add(
py,
"self_play",
py_fn!(
py,
self_play(
object: PyObject,
num_simulation: i32,
epsilon: f32,
dirichlet_alpha: f64,
c_puct: f32,
debug: bool,
num_game_thread: i32
)
)
));
try!(m.add(
py,
"play_with",
py_fn!(
py,
play_with(
object: PyObject,
num_simulation: i32,
epsilon: f32,
dirichlet_alpha: f64,
c_puct: f32
)
)
));
try!(m.add(
py,
"test_echo_pyeval",
py_fn!(
py,
test_echo_pyeval(object: PyObject, player: PyObject, boards: PyObject)
)
));
Ok(())
}
);
fn self_play(
py: Python,
object: PyObject,
num_simulation: i32,
epsilon: f32,
dirichlet_alpha: f64,
c_puct: f32,
debug: bool,
num_game_thread: i32,
) -> PyResult<PyTuple> {
let param = policy::HyperParameter {
num_simulation,
epsilon,
dirichlet_alpha,
c_puct,
};
if num_game_thread == 1 {
let pyeval = Box::new(pybind::PyEval::new(object));
let mut policy = policy::AlphaZero::with_param(pyeval, param);
let result = if debug {
agent::Agent::debug(&mut policy).play()
} else {
agent::Agent::new(&mut policy).play()
};
Ok(pybind::RunResultWrapper(&result.unwrap()).to_py_object(py))
} else {
let result = py.allow_threads(move || {
let policy_gen = || {
let object = {
let gil = Python::acquire_gil();
let py = gil.python();
object.clone_ref(py)
};
let pyeval = Box::new(pybind::PyEval::new(object));
policy::AlphaZero::with_param(pyeval, param)
};
let async_agent = if debug {
agent::AsyncAgent::debug(policy_gen)
} else {
agent::AsyncAgent::new(policy_gen)
};
async_agent.run(num_game_thread)
});
let py_result = result
.iter()
.map(|x| pybind::RunResultWrapper(x).to_py_object(py).into_object())
.collect::<Vec<_>>();
Ok(PyTuple::new(py, py_result.as_slice()))
}
}
fn play_with(
py: Python,
object: PyObject,
num_simulation: i32,
epsilon: f32,
dirichlet_alpha: f64,
c_puct: f32,
) -> PyResult<PyTuple> {
let param = policy::HyperParameter {
num_simulation,
epsilon,
dirichlet_alpha,
c_puct,
};
let pyeval = Box::new(pybind::PyEval::new(object));
let mut py_policy = policy::AlphaZero::with_param(pyeval, param);
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let mut io_policy = policy::IoPolicy::new(&mut stdin, &mut stdout);
let mut multi_policy = policy::MultiPolicy::new(&mut py_policy, &mut io_policy);
let result = agent::Agent::debug(&mut multi_policy).play();
Ok(pybind::RunResultWrapper(&result.unwrap()).to_py_object(py))
}
fn test_echo_pyeval(
py: Python,
object: PyObject,
player: PyObject,
boards: PyObject,
) -> PyResult<PyTuple> {
use connect6::{game::Player, policy::Evaluator, BOARD_CAPACITY, BOARD_SIZE};
let player = Player::from(player.extract::<i32>(py).ok().unwrap());
let boards = pybind::pyiter_to_vec::<i32>(py, boards).unwrap();
let len = boards.len() / BOARD_CAPACITY;
let mut recovered = Vec::new();
let mut idx = 0;
for _ in 0..len {
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for j in 0..BOARD_SIZE {
for k in 0..BOARD_SIZE {
board[j][k] = Player::from(boards[idx]);
idx += 1;
}
}
recovered.push(board);
}
let pyeval = pybind::PyEval::new(object);
let (value, policy) = pyeval.eval(player, &recovered).unwrap();
let value = value
.into_iter()
.map(|x| x.to_py_object(py))
.collect::<Vec<_>>();
let value = value.into_py_object(py).into_object();
let float_seq = |x: [[f32; BOARD_SIZE]; BOARD_SIZE]| {
let mut ordered = Vec::with_capacity(BOARD_CAPACITY);
for i in 0..BOARD_SIZE {
for j in 0..BOARD_SIZE {
ordered.push(x[i][j].to_py_object(py));
}
}
ordered
};
let policy = policy.into_iter().map(float_seq).collect::<Vec<_>>();
let policy = policy.into_py_object(py).into_object();
Ok(PyTuple::new(py, vec![value, policy].as_slice()))
}