-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathevalsgf.cpp
More file actions
228 lines (193 loc) · 7.89 KB
/
Copy pathevalsgf.cpp
File metadata and controls
228 lines (193 loc) · 7.89 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
#include "core/global.h"
#include "core/config_parser.h"
#include "core/timer.h"
#include "dataio/sgf.h"
#include "search/asyncbot.h"
#include "program/setup.h"
#include "main.h"
using namespace std;
#define TCLAP_NAMESTARTSTRING "-" //Use single dashes for all flags
#include <tclap/CmdLine.h>
int MainCmds::evalSgf(int argc, const char* const* argv) {
Board::initHash();
Rand seedRand;
string configFile;
string modelFile;
string sgfFile;
int moveNum;
string printBranch;
string extraMoves;
try {
TCLAP::CmdLine cmd("Run a search on a position from an sgf file", ' ', "1.0",true);
TCLAP::ValueArg<string> configFileArg("","config-file","Config file to use (see configs/gtp_example.cfg)",true,string(),"FILE");
TCLAP::ValueArg<string> modelFileArg("","model-file","Neural net model file to use",true,string(),"FILE");
TCLAP::UnlabeledValueArg<string> sgfFileArg("","Sgf file to analyze",true,string(),"FILE");
TCLAP::ValueArg<int> moveNumArg("","move-num","Sgf move num to analyze, 1-indexed",true,0,"MOVENUM");
TCLAP::ValueArg<string> printBranchArg("","print-branch","Move branch in search tree to print",false,string(),"MOVE MOVE ...");
TCLAP::ValueArg<string> printArg("","print","Alias for -print-branch",false,string(),"MOVE MOVE ...");
TCLAP::ValueArg<string> extraMovesArg("","extra-moves","Alias for -extra-moves",false,string(),"MOVE MOVE ...");
TCLAP::ValueArg<string> movesArg("","moves","Alias for -extra-moves",false,string(),"MOVE MOVE ...");
cmd.add(configFileArg);
cmd.add(modelFileArg);
cmd.add(sgfFileArg);
cmd.add(moveNumArg);
cmd.add(printBranchArg);
cmd.add(printArg);
cmd.add(extraMovesArg);
cmd.add(movesArg);
cmd.parse(argc,argv);
configFile = configFileArg.getValue();
modelFile = modelFileArg.getValue();
sgfFile = sgfFileArg.getValue();
moveNum = moveNumArg.getValue();
printBranch = printBranchArg.getValue();
string print = printArg.getValue();
extraMoves = extraMovesArg.getValue();
string moves = movesArg.getValue();
if(printBranch.length() > 0 && print.length() > 0) {
cerr << "Error: -print-branch and -print both specified" << endl;
return 1;
}
if(printBranch.length() <= 0)
printBranch = print;
if(extraMoves.length() > 0 && moves.length() > 0) {
cerr << "Error: -extra-moves and -moves both specified" << endl;
return 1;
}
if(extraMoves.length() <= 0)
extraMoves = moves;
}
catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
return 1;
}
//Parse config and rules -------------------------------------------------------------------
ConfigParser cfg(configFile);
Rules initialRules;
{
string koRule = cfg.getString("koRule", Rules::koRuleStrings());
string scoringRule = cfg.getString("scoringRule", Rules::scoringRuleStrings());
bool multiStoneSuicideLegal = cfg.getBool("multiStoneSuicideLegal");
float komi = 7.5f; //Default komi, sgf will generally override this
initialRules.koRule = Rules::parseKoRule(koRule);
initialRules.scoringRule = Rules::parseScoringRule(scoringRule);
initialRules.multiStoneSuicideLegal = multiStoneSuicideLegal;
initialRules.komi = komi;
}
//Parse sgf file and board ------------------------------------------------------------------
CompactSgf* sgf = CompactSgf::loadFile(sgfFile);
Board board;
Player nextPla;
BoardHistory hist;
sgf->setupInitialBoardAndHist(initialRules, board, nextPla, hist);
vector<Move>& moves = sgf->moves;
if(moveNum < 0)
throw StringError("Move num " + Global::intToString(moveNum) + " requested but must be non-negative");
if(moveNum > moves.size())
throw StringError("Move num " + Global::intToString(moveNum) + " requested but sgf has only " + Global::intToString(moves.size()));
for(int i = 0; i<moveNum; i++) {
if(!board.isLegal(moves[i].loc,moves[i].pla,hist.rules.multiStoneSuicideLegal)) {
cerr << board << endl;
cerr << "SGF Illegal move " << (i+1) << " for " << colorToChar(moves[i].pla) << ": " << Location::toString(moves[i].loc,board) << endl;
return 1;
}
hist.makeBoardMoveAssumeLegal(board,moves[i].loc,moves[i].pla,NULL);
nextPla = getOpp(moves[i].pla);
}
//Parse move sequence arguments------------------------------------------
PrintTreeOptions options;
options = options.maxDepth(1);
if(printBranch.length() > 0)
options = options.onlyBranch(board,printBranch);
vector<Loc> extraMoveLocs = Location::parseSequence(extraMoves,board);
for(size_t i = 0; i<extraMoveLocs.size(); i++) {
Loc loc = extraMoveLocs[i];
if(!board.isLegal(loc,nextPla,hist.rules.multiStoneSuicideLegal)) {
cerr << board << endl;
cerr << "Extra illegal move for " << colorToChar(nextPla) << ": " << Location::toString(loc,board) << endl;
return 1;
}
hist.makeBoardMoveAssumeLegal(board,loc,nextPla,NULL);
nextPla = getOpp(nextPla);
}
//Load neural net and start bot------------------------------------------
Logger logger;
logger.setLogToStdout(true);
logger.write("Engine starting...");
NNEvaluator* nnEval;
{
Setup::initializeSession(cfg);
vector<NNEvaluator*> nnEvals = Setup::initializeNNEvaluators({modelFile},cfg,logger,seedRand);
assert(nnEvals.size() == 1);
nnEval = nnEvals[0];
}
logger.write("Loaded neural net");
SearchParams params;
{
vector<SearchParams> paramss = Setup::loadParams(cfg);
if(paramss.size() != 1)
throw StringError("Can only specify exactly one bot in for searching in an sgf");
params = paramss[0];
}
string searchRandSeed;
if(cfg.contains("searchRandSeed"))
searchRandSeed = cfg.getString("searchRandSeed");
else
searchRandSeed = Global::uint64ToString(seedRand.nextUInt64());
//Check for unused config keys
{
vector<string> unusedKeys = cfg.unusedKeys();
for(size_t i = 0; i<unusedKeys.size(); i++) {
string msg = "WARNING: Unused key '" + unusedKeys[i] + "' in " + configFile;
logger.write(msg);
cerr << msg << endl;
}
}
AsyncBot* bot = new AsyncBot(params, nnEval, &logger, searchRandSeed);
bot->setPosition(nextPla,board,hist);
//Print initial state----------------------------------------------------------------
Search* search = bot->getSearch();
ostringstream sout;
Board::printBoard(sout, board, Board::NULL_LOC, &(hist.moveHistory));
if(options.branch_.size() > 0) {
Board copy = board;
BoardHistory copyHist = hist;
Player pla = nextPla;
for(int i = 0; i<options.branch_.size(); i++) {
Loc loc = options.branch_[i];
if(!copy.isLegal(loc,pla,copyHist.rules.multiStoneSuicideLegal)) {
cerr << board << endl;
cerr << "Branch Illegal move for " << colorToChar(pla) << ": " << Location::toString(loc,board) << endl;
return 1;
}
copyHist.makeBoardMoveAssumeLegal(copy,loc,pla,NULL);
pla = getOpp(pla);
}
Board::printBoard(sout, copy, Board::NULL_LOC, &(copyHist.moveHistory));
}
sout << "\n";
logger.write(sout.str());
sout.clear();
//Search!----------------------------------------------------------------
ClockTimer timer;
nnEval->clearStats();
Loc loc = bot->genMoveSynchronous(bot->getSearch()->rootPla);
(void)loc;
//Postprocess------------------------------------------------------------
sout << "Time taken: " << timer.getSeconds() << "\n";
sout << "Root visits: " << search->numRootVisits() << "\n";
sout << "NN rows: " << nnEval->numRowsProcessed() << endl;
sout << "NN batches: " << nnEval->numBatchesProcessed() << endl;
sout << "NN avg batch size: " << nnEval->averageProcessedBatchSize() << endl;
sout << "PV: ";
search->printPV(sout, search->rootNode, 25);
sout << "\n";
sout << "Tree:\n";
search->printTree(sout, search->rootNode, options);
logger.write(sout.str());
delete bot;
delete nnEval;
NeuralNet::globalCleanup();
delete sgf;
return 0;
}