forked from Cryolite/kanachan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.cpp
213 lines (195 loc) · 6.75 KB
/
simulate.cpp
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
#include "simulate.hpp"
#include "simulation/game.hpp"
#include "simulation/paishan.hpp"
#include "simulation/simulator.hpp"
#include "simulation/decision_maker.hpp"
#include "simulation/utility.hpp"
#include "common/assert.hpp"
#include "common/throw.hpp"
#include <boost/python/import.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/list.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/str.hpp>
#include <boost/python/long.hpp>
#include <boost/python/object.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/errors.hpp>
#include <Python.h>
#include <random>
#include <algorithm>
#include <vector>
#include <array>
#include <string>
#include <functional>
#include <tuple>
#include <utility>
#include <memory>
#include <stdexcept>
#include <exception>
#include <cstdint>
#include <limits>
#include <cstddef>
namespace{
using std::placeholders::_1;
namespace python = boost::python;
} // namespace `anonymous`
namespace Kanachan{
python::list simulate(
std::string const &device, python::object dtype,
long baseline_grade, python::object baseline_model,
long proposed_grade, python::object proposed_model,
long simulation_mode, long num_simulation_sets, long batch_size, long concurrency)
try {
if (dtype.is_none()) {
KANACHAN_THROW<std::invalid_argument>("`dtype` must not be `None`.");
}
if (baseline_grade < 0 || 16 <= baseline_grade) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< baseline_grade << ": An invalid value for `baseline_grade`.";
}
if (baseline_model.is_none()) {
KANACHAN_THROW<std::invalid_argument>("`baseline_model` must not be `None`.");
}
if (proposed_grade < 0 || 16 <= proposed_grade) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< proposed_grade << ": An invalid value for `proposed_grade`.";
}
if (proposed_model.is_none()) {
KANACHAN_THROW<std::invalid_argument>("`proposed_model` must not be `None`.");
}
Kanachan::Simulator simulator(
device, dtype, baseline_grade, baseline_model, proposed_grade, proposed_model,
simulation_mode, num_simulation_sets, batch_size, concurrency);
return simulator.run();
}
catch (std::exception const &) {
throw;
}
catch (python::error_already_set const &) {
Kanachan::translatePythonException();
}
catch (...) {
std::terminate();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
}
#pragma GCC diagnostic pop
python::dict test(
python::long_ const simulation_mode, python::tuple const grades,
python::object test_model, python::list const test_paishan_list)
try {
if (simulation_mode.is_none()) {
KANACHAN_THROW<std::invalid_argument>(_1) << "`simulation_mode` is `None`.";
}
long simulation_mode_ = python::extract<long>(simulation_mode);
bool const no_duplicate = simulation_mode_ & 1u;
bool const dong_feng_zhan = simulation_mode_ & 2u;
bool const one_versus_three = simulation_mode_ & 4u;
std::uint_fast8_t room = std::numeric_limits<std::uint_fast8_t>::max();
if ((simulation_mode_ & 8u) != 0u) {
room = 0u;
}
if ((simulation_mode_ & 16u) != 0u) {
if (room != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "simulation_mode: Multiple rooms specified.";
}
room = 1u;
}
if ((simulation_mode_ & 32u) != 0u) {
if (room != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "simulation_mode: Multiple rooms specified.";
}
room = 2u;
}
if ((simulation_mode_ & 64u) != 0u) {
if (room != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "simulation_mode: Multiple rooms specified.";
}
room = 3u;
}
if ((simulation_mode_ & 128u) != 0u) {
if (room != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "simulation_mode: Multiple rooms specified.";
}
room = 4u;
}
if (room == std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "simulation_mode: Room not specified.";
}
if (grades.is_none()) {
KANACHAN_THROW<std::invalid_argument>(_1) << "`grades` is `None`.";
}
if (python::len(grades) != 4) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< python::len(grades) << ": An invalid length.";
}
if (test_model.is_none()) {
KANACHAN_THROW<std::invalid_argument>(_1) << "`grades` is `None`.";
}
auto p_test_decision_maker = std::make_shared<Kanachan::DecisionMaker>(
"cpu", python::import("torch").attr("float64"), test_model, 1u);
if (test_paishan_list.is_none()) {
KANACHAN_THROW<std::invalid_argument>(_1) << "`test_paishan_list` is `None`.";
}
if (python::len(test_paishan_list) == 0) {
KANACHAN_THROW<std::invalid_argument>(_1) << "`test_paishan_list` is empty.";
}
std::vector<Kanachan::Paishan> test_paishan_list_;
for (std::size_t i = 0; i < python::len(test_paishan_list); ++i) {
python::list paishan = [test_paishan_list, i]() -> python::list {
python::object o = test_paishan_list[i];
python::extract<python::list> paishan_(o);
if (!paishan_.check()) {
KANACHAN_THROW<std::invalid_argument>("test_paishan_list: A type error.");
}
return paishan_();
}();
if (python::len(paishan) != 136u) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "test_paishan_list: An wrong length (" << python::len(paishan) << ").";
}
Kanachan::Paishan paishan_(paishan);
test_paishan_list_.push_back(std::move(paishan_));
}
python::dict result;
std::vector<std::uint_least32_t> seed = Kanachan::getRandomSeed();
std::seed_seq ss(seed.cbegin(), seed.cend());
std::mt19937 urng(ss);
using Seat = std::pair<std::uint_fast8_t, std::shared_ptr<Kanachan::DecisionMaker>>;
std::array<Seat, 4u> seats = [&]() -> std::array<Seat, 4u>
{
python::extract<python::long_> grade0(grades[0]);
python::extract<long> grade0_(grade0());
python::extract<python::long_> grade1(grades[1]);
python::extract<long> grade1_(grade1());
python::extract<python::long_> grade2(grades[2]);
python::extract<long> grade2_(grade2());
python::extract<python::long_> grade3(grades[3]);
python::extract<long> grade3_(grade3());
return std::array{
Seat(grade0_(), p_test_decision_maker),
Seat(grade1_(), p_test_decision_maker),
Seat(grade2_(), p_test_decision_maker),
Seat(grade3_(), p_test_decision_maker)
};
}();
return Kanachan::simulateGame(
seed, room, dong_feng_zhan, seats, test_paishan_list_, std::stop_token());
}
catch (std::exception const &) {
throw;
}
catch (python::error_already_set const &) {
Kanachan::translatePythonException();
}
catch (...) {
std::terminate();
}
} // namespace Kanachan