-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.cpp
308 lines (275 loc) · 7.39 KB
/
simulator.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "simulator.h"
#include "game.h"
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <set>
using namespace std;
YGO::condition_set_t YGO::Simulator::Topic::get_wanted_hand_conds() const {
condition_set_t res_set;
for (auto &combo : m_combos) {
for (auto p_cond : combo.hand_conditions) {
res_set.emplace(p_cond);
}
}
return res_set;
}
YGO::condition_set_t YGO::Simulator::Topic::get_wanted_grave_conds() const {
condition_set_t res_set;
for (auto& combo : m_combos) {
for (auto p_cond : combo.grave_conditions) {
res_set.emplace(p_cond);
}
}
return res_set;
}
YGO::Simulator::Simulator(YAML::Node simulate)
{
auto count_node = simulate["count"];
m_count = count_node.IsDefined() ? count_node.as<int>() : 1000;
auto debug_node = simulate["debug"];
m_debug = debug_node.IsDefined() ? debug_node.as<bool>() : false;
auto CI_node = simulate["confidence-interval"];
m_show_ci = CI_node.IsDefined() ? CI_node.as<bool>() : false;
auto tests = simulate["tests"];
if (tests.IsDefined())
{
for (auto it = tests.begin(); it != tests.end(); ++it)
{
Topic topic;
topic.name = it->first.as<t_string>();
auto topic_node = it->second;
auto start_card_node = topic_node["start-card"];
topic.m_start_card = start_card_node.IsDefined() ? start_card_node.as<int>() : 5;
auto exec_program_node = topic_node["exec-program"];
topic.m_exec_program = exec_program_node.IsDefined() ? exec_program_node.as<bool>() : false;
auto header_node = topic_node["header"];
topic.m_header = header_node.IsDefined() ? header_node.as<string>() : "";
auto topic_combos_node = topic_node["combos"];
for (auto jt = topic_combos_node.begin(); jt != topic_combos_node.end(); ++jt)
{
Combo combo;
combo.name = jt->first.as<t_string>();
auto combo_node = jt->second;
if (combo_node["score"].IsDefined()) {
combo.score = combo_node["score"].as<string>();
}
if (combo_node["condition"].IsDefined()) {
combo.condition = combo_node["condition"].as<string>();
}
auto combo_hand_node = combo_node["hand"];
if (combo_hand_node.IsDefined() && combo_hand_node.IsSequence())
{
for (int j = 0; j < combo_hand_node.size(); j++)
{
t_string cond_k = combo_hand_node[j].as<t_string>();
combo.hand_condition_strings.push_back(cond_k);
}
}
auto combo_grave_node = combo_node["grave"];
if (combo_grave_node.IsDefined() && combo_grave_node.IsSequence())
{
for (int j = 0; j < combo_grave_node.size(); j++)
{
t_string cond_k = combo_grave_node[j].as<t_string>();
combo.grave_condition_strings.push_back(cond_k);
}
}
topic.m_combos.emplace_back(std::move(combo));
}
m_topics.emplace_back(std::move(topic));
}
}
}
void YGO::Simulator::run(const Deck& deck_template, Context& context)
{
for (int i = 0; i < m_topics.size(); i++)
{
m_topics[i].bind(context);
m_topics[i].print(std::cout);
cout << "\n";
}
cout << "\n";
int num_topic = m_topics.size();
vector<vector<int>> success(num_topic); //topic, combo
vector<int> total_success(num_topic); //topic
vector<vector<double>> topic_scores(num_topic, vector<double>(m_count)); //topic, scores
int max_num_combo = 0;
for (int i = 0; i < num_topic; i++)
{
int num_combo = m_topics[i].m_combos.size();
success[i] = vector<int>(num_combo);
max_num_combo = max(max_num_combo, num_combo);
}
std::random_device rd;
printf("Simulate %d times...\n", m_count);
auto start_time = chrono::high_resolution_clock::now();
for (int k = 0; k < m_count; k++)
{
for (int i = 0; i < m_topics.size(); i++)
{
Game g(deck_template, m_topics[i], m_debug);
g.runHeader();
if (m_topics[i].m_exec_program) {
g.run();
}
const int num_combo = m_topics[i].m_combos.size();
bool any_success = false;
int max_topic_score = 0;
for (int j = 0; j < num_combo; j++)
{
int combo_score = m_topics[i].m_combos[j].test(g);
if (combo_score > 0) {
success[i][j]++;
any_success = true;
max_topic_score = std::max<int>(max_topic_score, combo_score);
}
}
total_success[i] += any_success;
topic_scores[i][k] = max_topic_score;
}
}
auto finish_time = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(finish_time - start_time);
std::cout << "Time used: " << duration.count() << "ms\n";
for (int i = 0; i < num_topic; i++)
{
std::cout << "Topic: " << m_topics[i].name << endl;
const int num_combo = m_topics[i].m_combos.size();
double p_success = (double)total_success[i] / m_count;
printf("First turn average success rate: %.2lf%%", p_success * 100.0);
if (m_show_ci) {
printf(" +- %.2lf%%", compute_ci(p_success, m_count) * 100);
}
printf(" ");
double mean_score = compute_mean(topic_scores[i]);
printf("average score: %.2lf", mean_score);
if (m_show_ci) {
double std_score = compute_std(topic_scores[i]);
printf(" +- %.2lf", 1.96 * (std_score / sqrt(m_count)));
}
printf("\n");
for (int j = 0; j < num_combo; j++)
{
double p = (double)success[i][j] / m_count;
printf(" %s: %.2lf%%", m_topics[i].m_combos[j].name.c_str(), p * 100.0);
if (m_show_ci) {
printf(" +- %.2lf%%", compute_ci(p, m_count) * 100);
}
printf(" ");
}
printf("\n\n");
}
}
void YGO::Simulator::Combo::bind(Context& context)
{
hand_conditions.clear();
for (auto condition_str: hand_condition_strings)
{
auto *cond = context.getCondition(condition_str);
if (!cond)
{
cond = Utils::parse(context, condition_str);
}
hand_conditions.push_back(cond);
}
grave_conditions.clear();
for (auto condition_str : grave_condition_strings)
{
auto* cond = context.getCondition(condition_str);
if (!cond)
{
cond = Utils::parse(context, condition_str);
}
grave_conditions.push_back(cond);
}
}
int YGO::Simulator::Combo::test(Game &g)
{
if (condition.size() && g.compute_number(condition) == 0) {
return 0;
}
auto hand_cards = g.m_collections["H"]->to_vector();
vector<bool> used(hand_cards.size());
for (auto cond : hand_conditions)
{
int found = false;
for (int i = 0; i < hand_cards.size(); i++)
{
if (!used[i] && cond->match(hand_cards[i]))
{
used[i] = true;
found = true;
break;
}
}
if (!found)
{
return 0;
}
}
auto grave_cards = g.m_collections["B"]->to_vector();
used = vector<bool>(grave_cards.size());
for (auto cond : grave_conditions)
{
int found = false;
for (int i = 0; i < grave_cards.size(); i++)
{
if (!used[i] && cond->match(grave_cards[i]))
{
used[i] = true;
found = true;
break;
}
}
if (!found)
{
return 0;
}
}
return g.compute_number(score);
}
void YGO::Simulator::Combo::print(ostream& os)
{
os << name << ": score: " << score << "; ";
if (hand_condition_strings.size()) {
os << "hand: ";
for (auto cond : hand_condition_strings)
{
os << cond << " ";
}
}
if (grave_condition_strings.size()) {
os << "grave: ";
for (auto cond : grave_condition_strings)
{
os << cond << " ";
}
}
}
int YGO::Simulator::Topic::test(Game& g)
{
int max_score = 0;
for (auto& comb : m_combos)
{
max_score = max(max_score, comb.test(g));
}
return max_score;
}
void YGO::Simulator::Topic::bind(Context& context)
{
for (auto& comb : m_combos)
{
comb.bind(context);
}
}
void YGO::Simulator::Topic::print(std::ostream& os)
{
os << name << ": " << endl;
for (auto& comb : m_combos)
{
comb.print(os);
os << "\n";
}
}