-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
185 lines (165 loc) · 4.38 KB
/
main.cc
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
#include "Common.h"
#include "McRaveAgent.h"
#include "Position.h"
struct OpeningEntry {
Bitmask placed;
int wall;
float value;
};
ostream &operator<<(ostream &out, const OpeningEntry &e) {
out << "{0x";
out.precision(6);
out.width(16);
out.fill('0');
out << hex << e.placed << ", " << dec << e.wall << "},//" << e.value;
return out;
}
void generateOpening(const Position &pos, McRaveAgent &agent, int curr,
int length) {
if ((length - curr) % 2 == 0) {
auto bestMove = agent.getBestMove(pos, false).second;
if (length == curr) {
OpeningEntry e{pos.placed, bestMove.wall, agent.eval(pos, bestMove)};
cout << e << endl;
} else {
auto tmpPos = pos;
tmpPos.doMove(bestMove);
generateOpening(tmpPos, agent, curr + 1, length);
}
} else {
for (const Move &move : pos) {
auto tmpPos = pos;
tmpPos.doMove(move);
generateOpening(tmpPos, agent, curr + 1, length);
}
}
}
void generateOpening(int turn) {
Position pos;
McRaveAgent agent;
generateOpening(pos, agent, 0, turn);
}
int main(int argc, char *argv[]) {
if (argc >= 2) {
if (string(argv[1]) == "--opening-0") {
generateOpening(0);
return 0;
}
if (string(argv[1]) == "--opening-1") {
generateOpening(1);
return 0;
}
if (string(argv[1]) == "--opening-2") {
generateOpening(2);
return 0;
}
if (string(argv[1]) == "--opening-3") {
generateOpening(3);
return 0;
}
if (string(argv[1]) == "--opening-4") {
generateOpening(4);
return 0;
}
if (string(argv[1]) == "--opening-5") {
generateOpening(5);
return 0;
}
if (argv[1] == string("--debug")) {
Position pos;
for (int i = 2; i < argc; ++i) {
pos.doMove(argv[i]);
}
McRaveAgent agent;
agent.launchDebugSession(pos);
return 0;
}
if (argv[1] == string("--check-randomness")) {
RNG gen;
Position pos;
int turn;
cout << "Give start turn" << endl;
cin >> turn;
for (int i = 0; i < turn; ++i) {
Move move;
pos.getRandomMove(gen, move);
pos.doMove(move);
}
int c[60];
for (int i = 0; i < 60; ++i) c[i] = 0;
int n = 1000000;
for (int i = 0; i < n; ++i) {
Move move;
pos.getRandomMove(gen, move);
c[move.wall]++;
}
int count = 0;
cout.precision(2);
for (int i = 0; i < 60; ++i) {
if (!c[i]) continue;
cout << i << " => " << 100.f * c[i] / n << "%" << endl;
++count;
}
float p = 100.f / count;
cout << "\nexpecting probability=" << p << "% for each move" << endl;
return 0;
}
if (argv[1] == string("--benchmark-playout")) {
auto start = getTimePoint();
constexpr int count = 100000;
RNG gen;
int wins = 0;
for (int i = 0; i < count; ++i) {
Position pos;
for (Move move; pos.getRandomMove(gen, move); pos.doMove(move)) {
}
wins += pos.turns & 1;
}
auto dt = getDeltaTimeSince(start);
cout.precision(2);
cout.setf(ios::fixed);
cout << "Run " << count << " playouts in " << dt << " seconds" << endl;
cout << 0.001 * count / dt << "k playout/s" << endl;
cout << "w=" << 100.0f * wins / count << "%" << endl;
return 0;
}
if (argv[1] == string("--benchmark-simulation")) {
auto start = getTimePoint();
constexpr int count = 100000;
McRaveAgent agent;
Position pos;
for (int i = 0; i < count; ++i) {
agent.simulate(pos);
}
auto dt = getDeltaTimeSince(start);
cout.precision(2);
cout.setf(ios::fixed);
cout << "Run " << count << " simulations in " << dt << " seconds" << endl;
cout << "Speed=" << 0.001 * count / dt << "k it/s" << endl;
// Thu Jan 21 23:34:04 CET 2021
// Run 100000 simulations in 7.41 seconds
// Speed=13.49k it/s
return 0;
}
}
Position pos;
McRaveAgent agent;
agent.pickTransformation();
for (string s; cin >> s && s != "Quit";) {
if (s != "Start") {
cerr << s << endl;
pos.doMove(s);
}
auto [claimWin, bestMove] = agent.getBestMove(pos);
pos.doMove(bestMove);
cerr << bestMove;
cout << bestMove;
if (claimWin) {
cerr << "!";
cout << "!";
}
cerr << endl;
cout << endl;
}
return 0;
}