-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathengine.c
More file actions
455 lines (382 loc) · 10.7 KB
/
Copy pathengine.c
File metadata and controls
455 lines (382 loc) · 10.7 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define DEBUG
#include <unistd.h>
#include "engine.h"
#include "debug.h"
#include "pachi.h"
/* engine headers */
#include "uct/uct.h"
#include "distributed/distributed.h"
#include "engines/replay.h"
#include "engines/montecarlo.h"
#include "engines/random.h"
#include "engines/external.h"
#include "dcnn/dcnn_engine.h"
#include "dcnn/blunderscan.h"
#include "pattern/patternscan.h"
#include "pattern/pattern_engine.h"
#include "joseki/joseki_engine.h"
#include "joseki/josekiload.h"
#include "josekifix/josekifix_engine.h"
#include "josekifix/josekifixload.h"
#include "josekifix/josekifixscan.h"
/**************************************************************************************************/
/* Engines list */
typedef struct {
int id;
char *name;
engine_init_t init;
bool show;
} engine_map_t;
/* Must match order in engine.h */
engine_map_t engines[] = {
{ E_UCT, "uct", uct_engine_init, 1 },
#ifdef DCNN
{ E_DCNN, "dcnn", dcnn_engine_init, 1 },
#ifdef EXTRA_ENGINES
{ E_BLUNDERSCAN, "blunderscan", blunderscan_engine_init, 0 },
#endif
#endif
{ E_PATTERN, "pattern", pattern_engine_init, 1 },
#ifdef EXTRA_ENGINES
{ E_PATTERNSCAN, "patternscan", patternscan_engine_init, 0 },
#endif
{ E_JOSEKI, "joseki", joseki_engine_init, 1 },
{ E_JOSEKILOAD, "josekiload", josekiload_engine_init, 0 },
#ifdef JOSEKIFIX
{ E_JOSEKIFIX, "josekifix", josekifix_engine_init, 0 },
{ E_JOSEKIFIXLOAD, "josekifixload", josekifixload_engine_init, 0 },
#ifdef EXTRA_ENGINES
{ E_JOSEKIFIXSCAN, "josekifixscan", josekifixscan_engine_init, 0 },
#endif
#endif
{ E_RANDOM, "random", random_engine_init, 1 },
{ E_REPLAY, "replay", replay_engine_init, 1 },
{ E_MONTECARLO, "montecarlo", montecarlo_engine_init, 1 },
#ifdef DISTRIBUTED
{ E_DISTRIBUTED, "distributed", distributed_engine_init, 1 },
#endif
#ifdef JOSEKIFIX
{ E_EXTERNAL, "external", external_engine_init, 0 },
#endif
/* Alternative names */
{ E_PATTERN, "patternplay", pattern_engine_init, 1 }, /* backwards compatibility */
{ E_JOSEKI, "josekiplay", joseki_engine_init, 1 },
{ 0, 0, 0, 0 }
};
void
engine_init_checks(void)
{
/* Check engines list is sane. */
for (int i = 0; i < E_MAX; i++)
assert(engines[i].name && engines[i].id == i);
}
enum engine_id
engine_name_to_id(const char *name)
{
for (int i = 0; engines[i].name; i++)
if (!strcmp(name, engines[i].name))
return engines[i].id;
return E_MAX;
}
char*
supported_engines(bool show_all)
{
static_strbuf(buf, 512);
for (int i = 0; i < E_MAX; i++) /* Don't list alt names */
if (show_all || engines[i].show)
strbuf_printf(buf, "%s%s", engines[i].name, (engines[i+1].name ? ", " : ""));
return buf->str;
}
/**************************************************************************************************/
/* Engine options */
/* Parse comma separated @arg, fill @options. */
static void
engine_options_parse(const char *arg, options_t *options)
{
options->n = 0;
if (!arg) return;
option_t *option = &options->o[0];
char *tmp_arg = strdup(arg);
char *next = tmp_arg;
for (options->n = 0; *next; options->n++, option++) {
assert(options->n < ENGINE_OPTIONS_MAX);
char *optspec = next;
next += strcspn(next, ",");
if (*next) *next++ = 0; else *next = 0;
char *optname = optspec;
char *optval = strchr(optspec, '=');
if (optval) *optval++ = 0;
option->name = strdup(optname);
option->val = (optval ? strdup(optval) : NULL);
}
free(tmp_arg);
}
static void
engine_options_free(options_t *options)
{
for (int i = 0; i < options->n; i++) {
free(options->o[i].name);
free(options->o[i].val);
}
}
/* Add option, overwriting previous value if any. */
void
engine_options_add(options_t *options, const char *name, const char *val)
{
/* Overwrite existing option ? */
for (int i = 0; i < options->n; i++) {
assert(i < ENGINE_OPTIONS_MAX);
if (!strcmp(options->o[i].name, name)) {
free(options->o[i].val);
options->o[i].val = (val ? strdup(val) : NULL);
return;
}
}
/* Ok, new option. */
options->o[options->n].name = strdup(name);
options->o[options->n].val = (val ? strdup(val) : NULL);
options->n++;
}
void
engine_options_copy(options_t *dest, options_t *src)
{
memcpy(dest, src, sizeof(*src));
}
void
engine_options_print(options_t *options)
{
fprintf(stderr, "engine options:\n");
for (int i = 0; i < options->n; i++)
if (options->o[i].val) fprintf(stderr, " %s=%s\n", options->o[i].name, options->o[i].val);
else fprintf(stderr, " %s\n", options->o[i].name);
}
option_t *
engine_options_lookup(options_t *options, const char *name)
{
for (int i = 0; i < options->n; i++)
if (!strcmp(options->o[i].name, name))
return &options->o[i];
return NULL;
}
void
engine_options_concat(strbuf_t *buf, options_t *options)
{
option_t *option = &options->o[0];
for (int i = 0; i < options->n; i++, option++)
if (option->val) sbprintf(buf, "%s%s=%s", (i ? "," : ""), option->name, option->val);
else sbprintf(buf, "%s%s", (i ? "," : ""), option->name);
}
/**************************************************************************************************/
/* Engine init */
/* init from scratch, preserving options. */
void
engine_init_(engine_t *e, int id, board_t *b)
{
assert(id >= 0 && id < E_MAX);
options_t options;
//engine_options_print(&e->options);
engine_options_copy(&options, &e->options);
memset(e, 0, sizeof(*e));
engine_options_copy(&e->options, &options);
e->id = id;
engines[id].init(e, b);
}
/* init from scratch */
void
engine_init(engine_t *e, int id, const char *e_arg, board_t *b)
{
engine_options_parse(e_arg, &e->options);
engine_init_(e, id, b);
}
void
engine_done(engine_t *e)
{
if (e->done) e->done(e);
if (e->data) free(e->data);
engine_options_free(&e->options);
memset(e, 0, sizeof(*e));
}
engine_t*
new_engine(int id, const char *e_arg, board_t *b)
{
engine_t *e = malloc2(engine_t);
engine_init(e, id, e_arg, b);
return e;
}
void
delete_engine(engine_t **e)
{
engine_done(*e);
free(*e);
*e = NULL;
}
void
engine_reset(engine_t *e, board_t *b)
{
/* Engine implements its own reset logic ? Don't pull rug under it. */
if (e->reset) {
e->reset(e, b);
return;
}
int engine_id = e->id;
options_t options;
engine_options_copy(&options, &e->options); /* Save options. */
e->options.n = 0;
engine_done(e);
engine_options_copy(&e->options, &options); /* Restore options. */
engine_init_(e, engine_id, b);
}
void
engine_board_print(engine_t *e, board_t *b, FILE *f)
{
(e->board_print ? e->board_print(e, b, f) : board_print(b, f));
}
void
engine_best_moves(engine_t *e, board_t *b, time_info_t *ti, enum stone color, best_moves_t *best)
{
assert(is_player_color(color));
e->best_moves(e, b, ti, color, best);
}
ownermap_t*
engine_ownermap(engine_t *e, board_t *b)
{
return (e->ownermap ? e->ownermap(e, b) : NULL);
}
static void
print_dead_groups(board_t *b, mq_t *dead)
{
if (!DEBUGL(1)) return;
for (int i = 0; i < dead->moves; i++) {
fprintf(stderr, " ");
foreach_in_group(b, dead->move[i]) {
fprintf(stderr, "%s ", coord2sstr(c));
} foreach_in_group_end;
fprintf(stderr, "\n");
}
}
/* Ask engine for dead stones */
bool
engine_dead_groups(engine_t *e, board_t *b, mq_t *q)
{
mq_init(q);
/* Tell engine to stop pondering, the game is probably over. */
if (e->stop) e->stop(e);
bool safe = true;
if (e->dead_groups)
safe = e->dead_groups(e, b, q);
/* else we return empty list - i.e. engine not supporting
* this assumes all stones alive at the game end. */
print_dead_groups(b, q); /* log output */
return safe;
}
/* For engines internal use, ensures optval is properly strdup'ed / freed
* since engine may change it. Must be preserved for next engine reset. */
bool
engine_setoption(engine_t *e, board_t *b, option_t *option, char **err, bool setup, bool *reset)
{
char *optval = (option->val ? strdup(option->val) : NULL);
bool r = e->setoption(e, b, option->name, optval, err, setup, reset);
free(optval);
return r;
}
bool
engine_setoptions(engine_t *e, board_t *b, const char *arg, char **err)
{
assert(arg);
options_t options;
engine_options_parse(arg, &options);
/* Reset engine if engine doesn't implement setoption(). */
bool reset = true;
if (e->setoption) {
reset = false;
/* Don't save options until we know they're all good. */
for (int i = 0; i < options.n; i++)
if (!engine_setoption(e, b, &options.o[i], err, false, &reset))
if (!reset) return false; /* Failed, err is error msg */
}
/* Ok, save. */
for (int i = 0; i < options.n; i++)
engine_options_add(&e->options, options.o[i].name, options.o[i].val);
/* Engine reset needed ? */
if (reset) engine_reset(e, b);
engine_options_free(&options);
return true;
}
/**************************************************************************************************/
void
best_moves_init(best_moves_t *best, coord_t c[], float r[], int size)
{
best->n = 0;
best->size = size;
best->c = c;
best->r = r;
best->d = NULL;
memset(c, 0, size * sizeof(*c));
memset(r, 0, size * sizeof(*r));
}
void
best_moves_init_full(best_moves_t *best, coord_t c[], float r[], void* d[], int size)
{
best->n = 0;
best->size = size;
best->c = c;
best->r = r;
best->d = d;
memset(c, 0, size * sizeof(*c));
memset(r, 0, size * sizeof(*r));
memset(d, 0, size * sizeof(*d));
}
/* For engines best_move(): Add move @c with prob @r to best moves */
void
best_moves_add(best_moves_t *best, coord_t c, float r)
{
int i;
for (i = 0; i < best->n; i++)
if (r > best->r[i]) {
/* Found position, shift following moves */
for (int j = MIN(best->n, best->size - 1); j > i; j--) {
best->r[j] = best->r[j - 1];
best->c[j] = best->c[j - 1];
}
break;
}
/* Add move if we found a spot or there is room left. */
if (i < best->size) {
best->r[i] = r;
best->c[i] = c;
best->n = MIN(best->n + 1, best->size);
}
}
void
best_moves_add_full(best_moves_t *best, coord_t c, float r, void *d)
{
int i;
for (i = 0; i < best->n; i++)
if (r > best->r[i]) {
/* Found position, shift following moves */
for (int j = MIN(best->n, best->size - 1); j > i; j--) {
best->r[j] = best->r[j - 1];
best->c[j] = best->c[j - 1];
best->d[j] = best->d[j - 1];
}
break;
}
/* Add move if we found a spot or there is room left. */
if (i < best->size) {
best->r[i] = r;
best->c[i] = c;
best->d[i] = d;
best->n = MIN(best->n + 1, best->size);
}
}
int
best_moves_print(best_moves_t *best, char *str)
{
fprintf(stderr, "%s[ ", str);
for (int i = 0; i < best->n; i++)
fprintf(stderr, "%-3s ", coord2sstr(best->c[i]));
for (int i = best->n; i < best->size; i++)
fprintf(stderr, "%-3s ", "");
fprintf(stderr, "]\n");
return strlen(str);
}