-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
488 lines (379 loc) · 10.5 KB
/
engine.c
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#define _USE_MATH_DEFINES
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include "inttypes.h"
#include "rogo.h"
#include "engine.h"
#include "utils.h"
#include "state.h"
#include "goban.h"
#include "program.h"
#include "rand.h"
#include "timing.h"
#include "hash.h"
#define USE_TARGET NODETYPE_TARGET
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#ifndef EXAGGERATE_RATE
#define EXAGGERATE_RATE 5
#endif
#ifndef MAXDEPTH
#define MAXDEPTH 100
#endif
#ifndef UCT_K
#define UCT_K 2
#endif
#ifndef UPDATE_SPECIAL_RATE
#define UPDATE_SPECIAL_RATE 1
#endif
#ifndef MIN_SPECIAL_VISITS
#define MIN_SPECIAL_VISITS 2
#endif
#ifndef ADD_RANDOM
#define ADD_RANDOM 0.02
#endif
#define LOG_MOVES_RATE 1
int cc=1;
int mode = 0;
// general stats for a pos
struct PosStat {
int total;
int won;
int lost;
};
struct PosStat pos_stat[POSCOUNT];
// the stats for a single move
// in a single position
struct MoveInfo {
double wins;
double visits;
double winrate;
double parent_visits;
double uct;
};
static int tries = 0;
static int success = 0;
static int target;
int bestmove;
void engine_init()
{
hash_init();
srand((unsigned)time(NULL));
tries = 0;
success = 0;
memset(pos_stat, 0, sizeof(pos_stat));
}
// makes high winrate higher and low winrate lower
double exaggerate_winrate(double winrate, int count) {
int n;
for (n=0; n < count; n++) {
winrate = sin((winrate - 0.5) * M_PI) / 2.0 + 0.5;
}
return winrate;
}
// determine stability
// 1 = always win/lose
// 0 = varable
static double stab(int pos)
{
double min = (double)MIN(pos_stat[pos].won, pos_stat[pos].lost);
double max = (double)MAX(pos_stat[pos].won, pos_stat[pos].lost);
return min/max;
}
// classic utc
static double getuct(double winrate, int visits, int parent_visits, int nodetype)
{
return winrate + sqrt((log((double)parent_visits+1))
/ (UCT_K * (double) visits+1));
}
// gets the currently determined best-move from the hashtables
int engine_getbestmove() {
int bestmove = 0;
double bestwinrate = 0;
int x,y;
for(y = state.boardsize; y >= 1; y--)
{
for (x = 1; x <= state.boardsize; x++)
{
struct Node *node = hash_get_node(hash_get(
POS(x,y), 0, goban.hash,
NODETYPE_POS|NODETYPE_MOVE|NODETYPE_SPECIAL));
double winrate = (double) node->wins / (double) node->visits;
if (winrate > bestwinrate)
{
bestwinrate = winrate;
bestmove = POS(x,y);
}
}
}
return bestmove;
}
// move_info should contain parentvisits; others are filled in
void get_move_value(int move, struct MoveInfo *move_info, int nodetype)
{
uint64_t hash;
struct Node * node;
if (move_info->parent_visits == 0)
{
move_info->uct = 0;
/*
hash = hash_get(move, target, goban.hash, nodetype);
node = hash_get_node(hash);
assert(node->wins == 0);
assert(node->visits == 0);
*/
return;
}
hash = hash_get(move, target, goban.hash, NODETYPE_POS|USE_TARGET|NODETYPE_MOVE);
node = hash_get_node(hash);
move_info->wins = node->wins;
move_info->visits = node->visits;
if (move_info->visits > 0)
move_info->winrate = (double)move_info->wins / ((double)move_info->visits);
else
move_info->winrate = 0;
//if (move_info->parent_visits > state.boardsize * state.boardsize * 10)
if (0)
{
hash = hash_get(move, target, goban.hash, NODETYPE_POS|NODETYPE_MOVE|NODETYPE_SPECIAL);
node = hash_get_node(hash);
if (node->visits > 0)
move_info->winrate = (double)node->wins / ((double)node->visits+1);
}
//assert(move_info->visits <= move_info->parent_visits);
move_info->uct = getuct(move_info->winrate, move_info->visits, move_info->parent_visits, nodetype);
if (0)
{
// get the winrate from the special node
hash = hash_get(move, target, goban.hash, nodetype);
node = hash_get_node(hash);
if (node->visits == 0)
{
move_info->wins = 0;
move_info->visits = 0;
move_info->winrate = 0.0;
}
else
{
move_info->winrate = (double)node->wins / ((double)node->visits+1);
hash = hash_get(move, target, goban.hash,
NODETYPE_POS|NODETYPE_TARGET|NODETYPE_MOVE);
node = hash_get_node(hash);
move_info->wins = node->wins;
move_info->visits = node->visits;
}
move_info->uct = getuct(move_info->winrate, move_info->visits, move_info->parent_visits, nodetype);
}
}
// get a current best move to try in a try-variation
int get_best_move(struct Node *parent, int nodetype, FILE *f) {
int x,y;
double bestuct = 0;
int bestmove = 0;
static double cache_uct[POSCOUNT];
static int cache_status = 0;
for(y=state.boardsize; y >= 1; y--)
{
for(x=1; x <= state.boardsize; x++)
{
struct MoveInfo move_info = { 0 };
if (!ISLEGAL(POS(x,y)))
{
if (goban.is[POS(x,y)].state & STONE)
if (goban.is[POS(x,y)].state & MINE)
out(f, " *** ");
else
out(f, " ### ");
else
out(f, " ... ");
continue;
}
if (0 && (nodetype == (NODETYPE_TARGET | NODETYPE_MOVE)) && cache_status) {
move_info.uct = cache_uct[POS(x,y)];
}
else
{
move_info.parent_visits = parent->visits;
get_move_value(POS(x,y), &move_info, nodetype);
//cache_uct[POS(x,y)] = move_info.uct;
out(f, "%4.1f ", move_info.winrate * state.boardsize * state.boardsize);
}
move_info.uct += rnd_d() / 1000000;
if (move_info.uct > bestuct)
{
bestmove = POS(x,y);
bestuct = move_info.uct;
}
}
out(f, "\n");
}
cache_status = (nodetype == (NODETYPE_TARGET | NODETYPE_MOVE));
//cache_status = 0;
return bestmove;
}
void update_special_node(int move) {
// store special node
int x,y;
double winrate = 0;
double c = 0;
for(y = state.boardsize; y >= 1; y--)
{
for(x = 1; x <= state.boardsize; x++)
{
struct Node * nodeThis =
hash_get_node(hash_get(move, POS(x,y), goban.hash,
NODETYPE_POS|NODETYPE_TARGET|NODETYPE_MOVE));
struct Node *nodeParent =
hash_get_node(hash_get(0, POS(x,y), goban.hash, NODETYPE_POS|NODETYPE_TARGET));
if (nodeThis->visits)
{
double wr = ((double)nodeThis->wins / (double)nodeThis->visits);
double wrPar = ((double)nodeParent->wins / (double)nodeParent->visits);
/*double stb = stab(POS(x,y));
stb = pow(stb, 2);
stb=1;
c+=stb;*/
c += 1;
wr = exaggerate_winrate(wr, EXAGGERATE_RATE) ;
wrPar = 1;//exaggerate_winrate(wrPar, EXAGGERATE_RATE) ;
winrate += wr / wrPar;//((wr - wrPar) + 1) / 2.0;// 0...1 - get_average_rating(move(x,y));
//out(fileGTP, "Winrate for move %p, at target %p = %f - %f\n",
// move, POS(x,y), wr, wrPar);
}
}
}
winrate /= c;
//out(fileGTP, "Winrate for move %p, %f\n", move, POS(x,y), winrate);
hash_set_node_special(hash_get(move, 0, goban.hash,
NODETYPE_POS|NODETYPE_MOVE|NODETYPE_SPECIAL), winrate);
}
// recursive function to try a game
int trygame(int depth, int doUpdate) {
struct Node *parent = (struct Node*)0;
uint64_t goban_hash = goban.hash;
int myturn = goban.turn;
int result;
int pos;
int win;
int x,y;
int haspoints;
uint64_t hash;
if (depth == MAXDEPTH)
return goban_getOwner(target);
// see if we can use nt3
hash = hash_get(0, target, goban.hash, NODETYPE_POS |USE_TARGET);
parent = hash_get_node(hash);
pos = get_best_move(
parent,
(NODETYPE_POS| USE_TARGET| NODETYPE_MOVE),
NULL
);
if (parent->visits >= MIN_SPECIAL_VISITS
&& parent->visits % UPDATE_SPECIAL_RATE == 0)
{
update_special_node(pos);
}
if (mode == 1)
{
int p;
for(y = state.boardsize; y >= 1; y--)
for(x = 1; x <= state.boardsize; x++)
update_special_node(POS(x,y));
p = engine_getbestmove();
if (p) pos = p;
}
goban_move(pos);
#if LOG_MOVES_RATE > 0
if (depth == 0 && tries % LOG_MOVES_RATE == 0)
{
out(fileGTP, "Target %p ", target);
}
if (depth < 10 && tries % LOG_MOVES_RATE == 0)
out(fileGTP, "%d%p ", 0, pos);
#endif
result = trygame(depth+1, parent->visits > 0);
#if LOG_MOVES_RATE > 0
if (depth == 0 && tries % LOG_MOVES_RATE == 0)
out(fileGTP, " res=%d, %r\n", result, goban.score);
#endif
haspoints = 0;
for(y = state.boardsize; y >= 1; y--)
for(x = 1; x <= state.boardsize; x++)
{
int targ = POS(x,y);
if (targ != target) continue;
result = goban_getOwner(targ);
if (!result) continue;
haspoints = 1;
}
if (!haspoints)
{
//out(fileInfo, "Failure getOwner(target)=%d\n", goban_getOwner(POS(7,5)));
//outboard(fileInfo);
}
if (haspoints && doUpdate)
for(y = state.boardsize; y >= 1; y--)
for(x=1; x <= state.boardsize; x++)
{
int targ = POS(x,y);
if (targ != target) continue;
result = goban_getOwner(targ);
if (!result) continue;
win = (result == myturn ? 1 : 0);
// store nt 3
hash_set_node(hash_get(pos, targ, goban_hash,
NODETYPE_POS|NODETYPE_TARGET|NODETYPE_MOVE), win);
hash_set_node(hash_get(pos, targ, goban_hash,
NODETYPE_POS|NODETYPE_TARGET), win);
}
// update target-agnostic nodes (generic uct)
if (haspoints && doUpdate && goban.score)
{
win = (myturn * goban.score > 0 ? 1 : 0);
hash_set_node(hash_get(pos, 0, goban_hash,
NODETYPE_POS|NODETYPE_MOVE), win);
hash_set_node(hash_get(pos, 0, goban_hash,
NODETYPE_POS), win);
}
if (haspoints && depth == 0)
{
for(y = state.boardsize; y >= 1; y--)
for(x=1; x <= state.boardsize; x++)
{
int targ = POS(x,y);
result = goban_getOwner(targ);
pos_stat[targ].total++;
if (result == myturn)
pos_stat[targ].won++;
else if (result == -myturn)
pos_stat[targ].lost++;
}
//outboard(fileGTP);
//update_special_node(pos);
return 1;
}
return 0;
return result;
}
int engine_tryvariation(int forcetarget)
{
int n;
if (forcetarget)
target = forcetarget;
else
target = POS(1+ rand() % state.boardsize, 1+ rand() % state.boardsize);
//target = POS(7,3);
n = trygame(0, -1);
if (n)
success++;
tries++;
goban_restore();
return n;
}
// special dbg funcs
#include "engine_dbg.h"