UCB1: Fix parity usage in descend equation
[pachi.git] / playout.c
blob90fd170d293f433004cefb718a32e305f7031aa2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "board.h"
6 #include "debug.h"
7 #include "engine.h"
8 #include "move.h"
9 #include "playout.h"
11 int
12 play_random_game(struct board *b, enum stone starting_color, int gamelen,
13 struct playout_amafmap *amafmap,
14 struct playout_policy *policy)
16 gamelen = gamelen - b->moves;
17 if (gamelen < 10)
18 gamelen = 10;
20 enum stone color = starting_color;
21 enum stone policy_color = stone_other(starting_color);
22 coord_t urgent;
24 if (amafmap)
25 amafmap->color = color;
27 int passes = is_pass(b->last_move.coord);
29 while (gamelen-- && passes < 2) {
30 urgent = policy->choose(policy, b, policy_color);
32 coord_t coord;
34 if (!is_pass(urgent)) {
35 struct move m;
36 m.coord = urgent; m.color = color;
37 if (board_play(b, &m) < 0) {
38 if (DEBUGL(8)) {
39 fprintf(stderr, "Urgent move %d,%d is ILLEGAL:\n", coord_x(urgent, b), coord_y(urgent, b));
40 board_print(b, stderr);
42 goto play_random;
44 coord = urgent;
45 } else {
46 play_random:
47 board_play_random(b, color, &coord);
50 #if 0
51 /* For UCT, superko test here is downright harmful since
52 * in superko-likely situation we throw away literarily
53 * 95% of our playouts; UCT will deal with this fine by
54 * itself. */
55 if (unlikely(b->superko_violation)) {
56 /* We ignore superko violations that are suicides. These
57 * are common only at the end of the game and are
58 * rather harmless. (They will not go through as a root
59 * move anyway.) */
60 if (group_at(b, coord)) {
61 if (DEBUGL(3)) {
62 fprintf(stderr, "Superko fun at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
63 if (DEBUGL(4))
64 board_print(b, stderr);
66 return -1;
67 } else {
68 if (DEBUGL(6)) {
69 fprintf(stderr, "Ignoring superko at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
70 board_print(b, stderr);
72 b->superko_violation = false;
75 #endif
77 if (DEBUGL(7)) {
78 fprintf(stderr, "%s %s\n", stone2str(color), coord2sstr(coord, b));
79 if (DEBUGL(8))
80 board_print(b, stderr);
83 if (amafmap && amafmap->map[coord] == S_NONE)
84 amafmap->map[coord] = color;
86 if (unlikely(is_pass(coord))) {
87 passes++;
88 } else {
89 passes = 0;
92 color = stone_other(color);
95 float score = board_fast_score(b);
96 bool result = (starting_color == S_WHITE ? (score > 0) : (score < 0));
98 if (DEBUGL(6)) {
99 fprintf(stderr, "Random playout result: %d (W %f)\n", result, score);
100 if (DEBUGL(7))
101 board_print(b, stderr);
104 return result;