-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtpsolver.cpp
346 lines (274 loc) · 11.7 KB
/
gtpsolver.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "havannahgtp.h"
string HavannahGTP::solve_str(int outcome) const {
switch(outcome){
case -2: return "black_or_draw";
case -1: return "white_or_draw";
case 0: return "draw";
case 1: return "white";
case 2: return "black";
default: return "unknown";
}
}
string HavannahGTP::solve_str(const Solver & solve){
string ret = "";
ret += solve_str(solve.outcome) + " ";
ret += move_str(solve.bestmove) + " ";
ret += to_str(solve.maxdepth) + " ";
ret += to_str(solve.nodes_seen);
return ret;
}
GTPResponse HavannahGTP::gtp_solve_ab(vecstr args){
double time = 60;
if(args.size() >= 1)
time = from_str<double>(args[0]);
solverab.solve(time);
logerr("Finished in " + to_str(solverab.time_used*1000, 0) + " msec\n");
return GTPResponse(true, solve_str(solverab));
}
GTPResponse HavannahGTP::gtp_solve_ab_params(vecstr args){
if(args.size() == 0)
return GTPResponse(true, string("\n") +
"Update the alpha-beta solver settings, eg: ab_params -m 100 -s 1 -d 3\n"
" -m --memory Memory limit in Mb (0 to disable the TT) [" + to_str(solverab.memlimit/(1024*1024)) + "]\n"
" -s --scout Whether to scout ahead for the true minimax value [" + to_str(solverab.scout) + "]\n"
" -d --depth Starting depth [" + to_str(solverab.startdepth) + "]\n"
);
for(unsigned int i = 0; i < args.size(); i++) {
string arg = args[i];
if((arg == "-m" || arg == "--memory") && i+1 < args.size()){
int mem = from_str<int>(args[++i]);
solverab.set_memlimit(mem);
}else if((arg == "-s" || arg == "--scout") && i+1 < args.size()){
solverab.scout = from_str<bool>(args[++i]);
}else if((arg == "-d" || arg == "--depth") && i+1 < args.size()){
solverab.startdepth = from_str<int>(args[++i]);
}else{
return GTPResponse(false, "Missing or unknown parameter");
}
}
return true;
}
GTPResponse HavannahGTP::gtp_solve_ab_stats(vecstr args){
string s = "";
Board board = game.getboard();
for(unsigned int i = 0; i < args.size(); i++)
board.move(Move(args[i]));
int value;
for(Board::MoveIterator move = board.moveit(true); !move.done(); ++move){
value = solverab.tt_get(board.test_hash(*move));
s += move->to_s() + "," + to_str(value) + "\n";
}
return GTPResponse(true, s);
}
GTPResponse HavannahGTP::gtp_solve_ab_clear(vecstr args){
solverab.clear_mem();
return true;
}
GTPResponse HavannahGTP::gtp_solve_pns(vecstr args){
double time = 60;
if(args.size() >= 1)
time = from_str<double>(args[0]);
solverpns.solve(time);
logerr("Finished in " + to_str(solverpns.time_used*1000, 0) + " msec\n");
return GTPResponse(true, solve_str(solverpns));
}
GTPResponse HavannahGTP::gtp_solve_pns_params(vecstr args){
if(args.size() == 0)
return GTPResponse(true, string("\n") +
"Update the pns solver settings, eg: pns_params -m 100 -s 0 -d 1 -e 0.25 -a 2 -l 0\n"
" -m --memory Memory limit in Mb [" + to_str(solverpns.memlimit/(1024*1024)) + "]\n"
" -s --ties Which side to assign ties to, 0 = handle, 1 = p1, 2 = p2 [" + to_str(solverpns.ties) + "]\n"
// " -t --threads How many threads to run
// " -o --ponder Ponder in the background
" -d --df Use depth-first thresholds [" + to_str(solverpns.df) + "]\n"
" -e --epsilon How big should the threshold be [" + to_str(solverpns.epsilon) + "]\n"
" -a --abdepth Run an alpha-beta search of this size at each leaf [" + to_str(solverpns.ab) + "]\n"
" -l --lbdist Initialize with the lower bound on distance to win [" + to_str(solverpns.lbdist) + "]\n"
);
for(unsigned int i = 0; i < args.size(); i++) {
string arg = args[i];
if((arg == "-m" || arg == "--memory") && i+1 < args.size()){
uint64_t mem = from_str<uint64_t>(args[++i]);
if(mem < 1) return GTPResponse(false, "Memory can't be less than 1mb");
solverpns.set_memlimit(mem*1024*1024);
}else if((arg == "-s" || arg == "--ties") && i+1 < args.size()){
solverpns.ties = from_str<int>(args[++i]);
solverpns.clear_mem();
}else if((arg == "-d" || arg == "--df") && i+1 < args.size()){
solverpns.df = from_str<bool>(args[++i]);
}else if((arg == "-e" || arg == "--epsilon") && i+1 < args.size()){
solverpns.epsilon = from_str<float>(args[++i]);
}else if((arg == "-a" || arg == "--abdepth") && i+1 < args.size()){
solverpns.ab = from_str<int>(args[++i]);
}else if((arg == "-l" || arg == "--lbdist") && i+1 < args.size()){
solverpns.lbdist = from_str<bool>(args[++i]);
}else{
return GTPResponse(false, "Missing or unknown parameter");
}
}
return true;
}
GTPResponse HavannahGTP::gtp_solve_pns_stats(vecstr args){
string s = "";
SolverPNS::PNSNode * node = &(solverpns.root);
for(unsigned int i = 0; i < args.size(); i++){
Move m(args[i]);
SolverPNS::PNSNode * c = node->children.begin(),
* cend = node->children.end();
for(; c != cend; c++){
if(c->move == m){
node = c;
break;
}
}
}
SolverPNS::PNSNode * child = node->children.begin(),
* childend = node->children.end();
for( ; child != childend; child++){
if(child->move == M_NONE)
continue;
s += child->move.to_s() + "," + to_str(child->phi) + "," + to_str(child->delta) + "\n";
}
return GTPResponse(true, s);
}
GTPResponse HavannahGTP::gtp_solve_pns_clear(vecstr args){
solverpns.clear_mem();
return true;
}
GTPResponse HavannahGTP::gtp_solve_pns2(vecstr args){
double time = 60;
if(args.size() >= 1)
time = from_str<double>(args[0]);
solverpns2.solve(time);
logerr("Finished in " + to_str(solverpns2.time_used*1000, 0) + " msec\n");
return GTPResponse(true, solve_str(solverpns2));
}
GTPResponse HavannahGTP::gtp_solve_pns2_params(vecstr args){
if(args.size() == 0)
return GTPResponse(true, string("\n") +
"Update the pns solver settings, eg: pns_params -m 100 -s 0 -d 1 -e 0.25 -a 2 -l 0\n"
" -m --memory Memory limit in Mb [" + to_str(solverpns2.memlimit/(1024*1024)) + "]\n"
" -s --ties Which side to assign ties to, 0 = handle, 1 = p1, 2 = p2 [" + to_str(solverpns2.ties) + "]\n"
" -t --threads How many threads to run [" + to_str(solverpns2.numthreads) + "]\n"
// " -o --ponder Ponder in the background
" -d --df Use depth-first thresholds [" + to_str(solverpns2.df) + "]\n"
" -e --epsilon How big should the threshold be [" + to_str(solverpns2.epsilon) + "]\n"
" -a --abdepth Run an alpha-beta search of this size at each leaf [" + to_str(solverpns2.ab) + "]\n"
" -l --lbdist Initialize with the lower bound on distance to win [" + to_str(solverpns2.lbdist) + "]\n"
);
for(unsigned int i = 0; i < args.size(); i++) {
string arg = args[i];
if((arg == "-t" || arg == "--threads") && i+1 < args.size()){
solverpns2.numthreads = from_str<int>(args[++i]);
solverpns2.reset_threads();
}else if((arg == "-m" || arg == "--memory") && i+1 < args.size()){
uint64_t mem = from_str<uint64_t>(args[++i]);
if(mem < 1) return GTPResponse(false, "Memory can't be less than 1mb");
solverpns2.set_memlimit(mem*1024*1024);
}else if((arg == "-s" || arg == "--ties") && i+1 < args.size()){
solverpns2.ties = from_str<int>(args[++i]);
solverpns2.clear_mem();
}else if((arg == "-d" || arg == "--df") && i+1 < args.size()){
solverpns2.df = from_str<bool>(args[++i]);
}else if((arg == "-e" || arg == "--epsilon") && i+1 < args.size()){
solverpns2.epsilon = from_str<float>(args[++i]);
}else if((arg == "-a" || arg == "--abdepth") && i+1 < args.size()){
solverpns2.ab = from_str<int>(args[++i]);
}else if((arg == "-l" || arg == "--lbdist") && i+1 < args.size()){
solverpns2.lbdist = from_str<bool>(args[++i]);
}else{
return GTPResponse(false, "Missing or unknown parameter");
}
}
return true;
}
GTPResponse HavannahGTP::gtp_solve_pns2_stats(vecstr args){
string s = "";
SolverPNS2::PNSNode * node = &(solverpns2.root);
for(unsigned int i = 0; i < args.size(); i++){
Move m(args[i]);
SolverPNS2::PNSNode * c = node->children.begin(),
* cend = node->children.end();
for(; c != cend; c++){
if(c->move == m){
node = c;
break;
}
}
}
SolverPNS2::PNSNode * child = node->children.begin(),
* childend = node->children.end();
for( ; child != childend; child++){
if(child->move == M_NONE)
continue;
s += child->move.to_s() + "," + to_str(child->phi) + "," + to_str(child->delta) + "," + to_str(child->work) + "\n";
}
return GTPResponse(true, s);
}
GTPResponse HavannahGTP::gtp_solve_pns2_clear(vecstr args){
solverpns2.clear_mem();
return true;
}
GTPResponse HavannahGTP::gtp_solve_pnstt(vecstr args){
double time = 60;
if(args.size() >= 1)
time = from_str<double>(args[0]);
solverpnstt.solve(time);
logerr("Finished in " + to_str(solverpnstt.time_used*1000, 0) + " msec\n");
return GTPResponse(true, solve_str(solverpnstt));
}
GTPResponse HavannahGTP::gtp_solve_pnstt_params(vecstr args){
if(args.size() == 0)
return GTPResponse(true, string("\n") +
"Update the pnstt solver settings, eg: pnstt_params -m 100 -s 0 -d 1 -e 0.25 -a 2 -l 0\n"
" -m --memory Memory limit in Mb [" + to_str(solverpnstt.memlimit/(1024*1024)) + "]\n"
" -s --ties Which side to assign ties to, 0 = handle, 1 = p1, 2 = p2 [" + to_str(solverpnstt.ties) + "]\n"
// " -t --threads How many threads to run
// " -o --ponder Ponder in the background
" -d --df Use depth-first thresholds [" + to_str(solverpnstt.df) + "]\n"
" -e --epsilon How big should the threshold be [" + to_str(solverpnstt.epsilon) + "]\n"
" -a --abdepth Run an alpha-beta search of this size at each leaf [" + to_str(solverpnstt.ab) + "]\n"
" -c --copy Try to copy a proof to this many siblings, <0 quit early [" + to_str(solverpnstt.copyproof) + "]\n"
// " -l --lbdist Initialize with the lower bound on distance to win [" + to_str(solverpnstt.lbdist) + "]\n"
);
for(unsigned int i = 0; i < args.size(); i++) {
string arg = args[i];
if((arg == "-m" || arg == "--memory") && i+1 < args.size()){
int mem = from_str<int>(args[++i]);
if(mem < 1) return GTPResponse(false, "Memory can't be less than 1mb");
solverpnstt.set_memlimit(mem*1024*1024);
}else if((arg == "-s" || arg == "--ties") && i+1 < args.size()){
solverpnstt.ties = from_str<int>(args[++i]);
solverpnstt.clear_mem();
}else if((arg == "-d" || arg == "--df") && i+1 < args.size()){
solverpnstt.df = from_str<bool>(args[++i]);
}else if((arg == "-e" || arg == "--epsilon") && i+1 < args.size()){
solverpnstt.epsilon = from_str<float>(args[++i]);
}else if((arg == "-a" || arg == "--abdepth") && i+1 < args.size()){
solverpnstt.ab = from_str<int>(args[++i]);
}else if((arg == "-c" || arg == "--copy") && i+1 < args.size()){
solverpnstt.copyproof = from_str<int>(args[++i]);
// }else if((arg == "-l" || arg == "--lbdist") && i+1 < args.size()){
// solverpnstt.lbdist = from_str<bool>(args[++i]);
}else{
return GTPResponse(false, "Missing or unknown parameter");
}
}
return true;
}
GTPResponse HavannahGTP::gtp_solve_pnstt_stats(vecstr args){
string s = "";
Board board = game.getboard();
for(unsigned int i = 0; i < args.size(); i++)
board.move(Move(args[i]));
SolverPNSTT::PNSNode * child = NULL;
for(Board::MoveIterator move = board.moveit(true); !move.done(); ++move){
child = solverpnstt.tt(board, *move);
s += move->to_s() + "," + to_str(child->phi) + "," + to_str(child->delta) + "\n";
}
return GTPResponse(true, s);
}
GTPResponse HavannahGTP::gtp_solve_pnstt_clear(vecstr args){
solverpnstt.clear_mem();
return true;
}