-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPlayout.cpp
217 lines (173 loc) · 5.91 KB
/
Playout.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
#include <vector>
#include <array>
#include <cstdlib>
#include <cassert>
#include <thread>
#include <algorithm>
#include "config.h"
#include "Timing.h"
#include "GameState.h"
#include "Playout.h"
#include "Utils.h"
#include "MCOTable.h"
#include "Random.h"
#include "GTP.h"
using namespace Utils;
Playout::Playout() :
m_run(false), m_eval_valid(false) {
m_sq[0].reset();
m_sq[1].reset();
}
float Playout::get_score() const {
assert(m_run);
assert(m_score > -2.00f && m_score < 2.00f);
return m_score;
}
float Playout::get_territory() const {
assert(m_run);
return m_territory;
}
void Playout::set_eval(float eval) {
m_blackeval = eval;
m_eval_valid = true;
}
float Playout::get_eval() const {
assert(m_eval_valid == true);
return m_blackeval;
}
bool Playout::has_eval() const {
return m_eval_valid;
}
void Playout::run(FastState & state, bool postpassout, bool resigning,
PolicyTrace * trace) {
assert(!m_run);
const int boardsize = state.board.get_boardsize();
const int resign = (boardsize * boardsize) / 3;
const int playoutlen = (boardsize * boardsize) * 2;
// 2 passes end the game, except when we're marking
const int maxpasses = postpassout ? 4 : 2;
int counter = 0;
// do the main loop
while (state.get_passes() < maxpasses
&& state.get_movenum() < playoutlen
&& (!resigning || abs(state.estimate_mc_score()) < resign)) {
int vtx = state.play_random_move(state.get_to_move(), trace);
if (counter < 30 && vtx != FastBoard::PASS) {
int color = !state.get_to_move();
if (!m_sq[!color][vtx]) {
m_sq[color][vtx] = true;
}
}
counter++;
}
// get ownership info
bitboard_t blackowns;
for (int i = 0; i < boardsize; i++) {
for (int j = 0; j < boardsize; j++) {
int vtx = state.board.get_vertex(i, j);
if (state.board.get_square(vtx) == FastBoard::BLACK) {
blackowns[vtx] = true;
} else if (state.board.get_square(vtx) == FastBoard::EMPTY) {
if (state.board.is_eye(FastBoard::BLACK, vtx)) {
blackowns[vtx] = true;
}
}
}
}
float board_score = state.calculate_mc_score();
// update MCO in one swoop
bool blackwon;
if (board_score == 0.0f) {
blackwon = (Random::get_Rng()->randfix<2>() == 0);
} else {
blackwon = (board_score > 0.0f);
}
MCOwnerTable::get_MCO()->update_owns(blackowns, blackwon, board_score);
m_run = true;
m_territory = board_score;
// Scale to -1.0 <--> 1.0
m_score = board_score / (boardsize * boardsize);
}
bool Playout::passthrough(int color, int vertex) {
assert(m_run);
if (vertex == FastBoard::PASS) {
return false;
}
return m_sq[color][vertex];
}
void Playout::do_playout_benchmark(GameState & game) {
int cpus = cfg_num_threads;
int iters_per_thread = (AUTOGAMES + (cpus - 1)) / cpus;
std::atomic<float> len{0.0f};
std::atomic<float> board_score{0.0f};
const int boardsize = game.board.get_boardsize();
const int resign = (boardsize * boardsize) / 3;
const int playoutlen = (boardsize * boardsize) * 2;
Time start;
ThreadGroup tg(thread_pool);
for (int i = 0; i < cpus; i++) {
tg.add_task([iters_per_thread, &game, &len, &board_score,
playoutlen, resign]() {
GameState mygame = game;
float thread_len = 0.0f;
float thread_board_score = 0.0f;
for (int i = 0; i < iters_per_thread; i++) {
do {
mygame.play_random_move(mygame.get_to_move());
} while (mygame.get_passes() < 2
&& mygame.get_movenum() < playoutlen
&& abs(mygame.estimate_mc_score()) < resign);
thread_len += mygame.get_movenum();
thread_board_score += mygame.calculate_mc_score();
mygame.reset_game();
}
atomic_add(board_score, thread_board_score);
atomic_add(len, thread_len);
});
};
tg.wait_all();
Time end;
float games_per_sec = (float)AUTOGAMES/((float)Time::timediff(start,end)/100.0);
myprintf("%d games in %5.2f seconds -> %d g/s (%d g/s per thread)\n",
AUTOGAMES,
(float)Time::timediff(start,end)/100.0,
(int)games_per_sec,(int)(games_per_sec/(float)cpus));
myprintf("Avg Len: %5.2f Score: %f\n", len/(float)AUTOGAMES, board_score/AUTOGAMES);
}
float Playout::mc_owner(FastState & state, const int iterations, float* points) {
int cpus = cfg_num_threads;
int iters_per_thread = (iterations + (cpus - 1)) / cpus;
std::atomic<float> bwins{0.0f};
std::atomic<float> board_score{0.0f};
ThreadGroup tg(thread_pool);
for (int i = 0; i < cpus; i++) {
tg.add_task([iters_per_thread, &state,
&bwins, &board_score]() {
float thread_bwins = 0.0f;
float thread_board_score = 0.0f;
for (int i = 0; i < iters_per_thread; i++) {
FastState tmp = state;
Playout p;
p.run(tmp, true, false);
float score = p.get_score();
if (score == 0.0f) {
thread_bwins += 0.5f;
} else if (score > 0.0f) {
thread_bwins += 1.0f;
}
thread_board_score += p.get_territory();
}
atomic_add(bwins, thread_bwins);
atomic_add(board_score, thread_board_score);
});
}
tg.wait_all();
float score = bwins / (float)iterations;
if (state.get_to_move() != FastBoard::BLACK) {
score = 1.0f - score;
}
if (points != nullptr) {
*points = board_score / (float)iterations;
}
return score;
}