-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlzparse.cpp
More file actions
250 lines (217 loc) · 6.63 KB
/
Copy pathlzparse.cpp
File metadata and controls
250 lines (217 loc) · 6.63 KB
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
#include <zstr/src/zstr.hpp>
#include <cstdlib>
#include "../dataio/lzparse.h"
LZSample::LZSample()
:emptyBoard(19,19),plaStones(),oppStones(),sideStr(),policyStr(),resultStr()
{}
LZSample::~LZSample()
{}
static void getLine(istream& in, string& buf) {
std::getline(in,buf);
size_t len = buf.length();
if(len > 0 && buf[len-1] == '\r') //just in case
buf.pop_back();
}
static void setStone(Color* stones, int tensorPos, Color stone) {
int x = tensorPos % 19;
int y = tensorPos / 19;
stones[Location::getLoc(x,y,19)] = stone;
}
static int parseHexChar(char c) {
int d;
if(c >= '0' && c <= '9')
d = c-'0';
else if (c >= 'a' && c <= 'f')
d = c-'a'+10;
else
assert(false);
return d;
}
static void decodeStones(const string& linePla, const string& lineOpp, Color* stones, Player pla)
{
assert(linePla.length() == 91);
assert(lineOpp.length() == 91);
Player opp = getOpp(pla);
//The first 90 characters are a hex-encoding of the first 360 points
for(int i = 0; i<90; i++) {
int dPla = parseHexChar(linePla[i]);
int dOpp = parseHexChar(lineOpp[i]);
Color stone;
if(dPla & 0x8) stone = pla; else if(dOpp & 0x8) stone = opp; else stone = C_EMPTY;
setStone(stones,i*4+0,stone);
if(dPla & 0x4) stone = pla; else if(dOpp & 0x4) stone = opp; else stone = C_EMPTY;
setStone(stones,i*4+1,stone);
if(dPla & 0x2) stone = pla; else if(dOpp & 0x2) stone = opp; else stone = C_EMPTY;
setStone(stones,i*4+2,stone);
if(dPla & 0x1) stone = pla; else if(dOpp & 0x1) stone = opp; else stone = C_EMPTY;
setStone(stones,i*4+3,stone);
}
//The last character is either 0 or 1
{
int cPla = linePla[90];
int cOpp = lineOpp[90];
assert(cPla == '1' || cPla == '0');
assert(cOpp == '1' || cOpp == '0');
Color stone;
if(cPla == '1') stone = pla; else if(cOpp == '1') stone = opp; else stone = C_EMPTY;
setStone(stones,90*4,stone);
}
}
static Move inferMove(Color* board, Color* prev, Player whoMoved, Color stones[8][Board::MAX_ARR_SIZE], int stonesIdx, const short adj_offsets[8]) {
//Search to find if there is a stone of the player who moved that is newly placed
for(int y = 0; y<19; y++) {
for(int x = 0; x<19; x++) {
Loc loc = Location::getLoc(x,y,19);
if(board[loc] == whoMoved && prev[loc] != whoMoved)
return Move(loc,whoMoved);
}
}
//Search to find if there as a suicide
for(int y = 0; y<19; y++) {
for(int x = 0; x<19; x++) {
Loc loc = Location::getLoc(x,y,19);
if(board[loc] != whoMoved && prev[loc] == whoMoved) {
//Look around for an empty spot to play, next to where our stones vanished.
for(int i = 0; i < 4; i++)
{
Loc adj = loc + adj_offsets[i];
if(prev[adj] == C_EMPTY)
return Move(adj,whoMoved);
}
}
}
}
//Otherwise it must have been a pass
for(int y = 0; y<19; y++) {
for(int x = 0; x<19; x++) {
Loc loc = Location::getLoc(x,y,19);
if(board[loc] != prev[loc]) {
for(int i = 0; i<8; i++) {
for(int y2 = 0; y2<19; y2++) {
for(int x2 = 0; x2<19; x2++) {
Loc loc2 = Location::getLoc(x2,y2,19);
assert(stones[i][loc2] >= 0 && stones[i][loc2] <= 2);
cout << (stones[i][loc2] == 0 ? '.' : stones[i][loc2] == 1 ? 'X' : 'O');
}
cout << endl;
}
cout << endl;
}
cout << "Problem getting to index " << stonesIdx << " from " << (stonesIdx+1) << endl;
throw IOError(string("Bad leela zero board consistency"));
}
}
}
return Move(Board::PASS_LOC,whoMoved);
}
void LZSample::iterSamples(
const string& gzippedFile,
std::function<void(const LZSample&,const string&,int)> f
) {
LZSample sample;
zstr::ifstream in(gzippedFile);
int sampleCount = 0;
while(in.good()) {
//First 8 lines are pla stones, second 8 lines are opp stones
//Most recent states are first
for(int i = 0; i<8; i++) {
getLine(in,sample.plaStones[i]);
}
for(int i = 0; i<8; i++) {
getLine(in,sample.oppStones[i]);
}
if(!in.good())
break;
//Next line is which color, 0 = black, 1 = white
getLine(in,sample.sideStr);
assert(sample.sideStr.length() == 1);
//Next we have 362 floats indicating moves
getLine(in,sample.policyStr);
//Next we have one line indicating whether the current player won or lost (+1 or -1).
getLine(in,sample.resultStr);
f(sample,gzippedFile,sampleCount);
sampleCount++;
}
}
void LZSample::parse(
Board& board,
BoardHistory& hist,
vector<Move>& moves,
float policyTarget[362],
Player& nextPlayer,
Player& winner
) const {
if(moves.size() != 8)
moves.resize(8);
Player pla;
if(sideStr[0] == '0')
pla = P_BLACK;
else if(sideStr[0] == '1')
pla = P_WHITE;
else
assert(false);
Player opp = getOpp(pla);
//Parse all stones
Color stones[8][Board::MAX_ARR_SIZE];
for(int i = 0; i<8; i++)
decodeStones(plaStones[i], oppStones[i], stones[i], pla);
//Infer the moves based on the stones
for(int i = 0; i<7; i++)
{
Color* current = stones[i];
Color* prev = stones[i+1];
Player whoMoved = (i % 2 == 0) ? opp : pla;
Move move = inferMove(current,prev,whoMoved,stones,i,emptyBoard.adj_offsets);
moves[7-i-1] = move;
}
//Generate the boards from the stones and the moves
board = emptyBoard;
for(int y = 0; y<19; y++) {
for(int x = 0; x<19; x++) {
Loc loc = Location::getLoc(x,y,19);
board.setStone(loc,stones[7][loc]);
}
}
hist.clear(board,opp,Rules::getTrompTaylorish());
for(int i = 6; i>=0; i--)
{
Move move = moves[7-i-1];
bool multiStoneSuicideLegal = true; //True for LZ
bool suc = board.isLegal(move.loc,move.pla,multiStoneSuicideLegal);
if(!suc)
throw IOError(string("Leela zero illegal implied move"));
hist.makeBoardMoveAssumeLegal(board,move.loc,move.pla,NULL);
}
{
const char* start = policyStr.c_str();
const char* s = start;
char* end = NULL;
float maxProb = 0;
int maxI = 0;
for(int i = 0; i<362; i++) {
float prob = std::strtod(s,&end);
policyTarget[i] = prob;
s = end;
if(prob > maxProb) {
maxProb = prob;
maxI = i;
}
}
assert(end == start + policyStr.length());
//Fill in the "next" move to be the argmax of the policyTarget
if(maxI == 361)
moves[7] = Move(Board::PASS_LOC,pla);
else {
int x = maxI % 19;
int y = maxI / 19;
moves[7] = Move(Location::getLoc(x,y,19),pla);
}
}
if(resultStr == "1")
winner = pla;
else if(resultStr == "-1")
winner = opp;
else
assert(false);
nextPlayer = pla;
}