UCT: Add policy= parameter, explore_p is now policy argument
[pachi.git] / playout.c
blobb32c42c770976b68a9af0d43467700ae4c712e37
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 playout_policeman policeman, void *policy)
15 gamelen = gamelen - b->moves;
16 if (gamelen < 10)
17 gamelen = 10;
19 enum stone color = starting_color;
20 enum stone policy_color = stone_other(starting_color);
21 coord_t urgent;
23 int passes = is_pass(b->last_move.coord);
25 while (gamelen-- && passes < 2) {
26 urgent = policeman(policy, b, policy_color);
28 coord_t coord;
30 if (!is_pass(urgent)) {
31 struct move m;
32 m.coord = urgent; m.color = color;
33 if (board_play(b, &m) < 0) {
34 if (DEBUGL(8)) {
35 fprintf(stderr, "Urgent move %d,%d is ILLEGAL:\n", coord_x(urgent, b), coord_y(urgent, b));
36 board_print(b, stderr);
38 goto play_random;
40 coord = urgent;
41 } else {
42 play_random:
43 board_play_random(b, color, &coord);
46 #if 0
47 /* For UCT, superko test here is downright harmful since
48 * in superko-likely situation we throw away literarily
49 * 95% of our playouts; UCT will deal with this fine by
50 * itself. */
51 if (unlikely(b->superko_violation)) {
52 /* We ignore superko violations that are suicides. These
53 * are common only at the end of the game and are
54 * rather harmless. (They will not go through as a root
55 * move anyway.) */
56 if (group_at(b, coord)) {
57 if (DEBUGL(3)) {
58 fprintf(stderr, "Superko fun at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
59 if (DEBUGL(4))
60 board_print(b, stderr);
62 return -1;
63 } else {
64 if (DEBUGL(6)) {
65 fprintf(stderr, "Ignoring superko at %d,%d in\n", coord_x(coord, b), coord_y(coord, b));
66 board_print(b, stderr);
68 b->superko_violation = false;
71 #endif
73 if (DEBUGL(8)) {
74 char *cs = coord2str(coord, b);
75 fprintf(stderr, "%s %s\n", stone2str(color), cs);
76 free(cs);
79 if (unlikely(is_pass(coord))) {
80 passes++;
81 } else {
82 passes = 0;
85 color = stone_other(color);
88 float score = board_fast_score(b);
89 bool result = (starting_color == S_WHITE ? (score > 0) : (score < 0));
91 if (DEBUGL(6)) {
92 fprintf(stderr, "Random playout result: %d (W %f)\n", result, score);
93 if (DEBUGL(7))
94 board_print(b, stderr);
97 return result;