Merge pull request #50 from lemonsqueeze/can_countercap
[pachi.git] / patternplay / patternplay.c
blobd4a857add2c516f270cb6dd32a9947babff469ab
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #include "board.h"
6 #include "debug.h"
7 #include "engine.h"
8 #include "move.h"
9 #include "patternplay/patternplay.h"
10 #include "pattern.h"
11 #include "patternsp.h"
12 #include "patternprob.h"
13 #include "random.h"
16 /* Internal engine state. */
17 struct patternplay {
18 int debug_level;
20 struct pattern_setup pat;
24 static coord_t *
25 patternplay_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
27 struct patternplay *pp = e->data;
29 struct pattern pats[b->flen];
30 floating_t probs[b->flen];
31 pattern_rate_moves(&pp->pat, b, color, pats, probs);
33 int best = 0;
34 for (int f = 0; f < b->flen; f++) {
35 if (pp->debug_level >= 5 && probs[f] >= 0.001) {
36 char s[256]; pattern2str(s, &pats[f]);
37 fprintf(stderr, "\t%s: %.3f %s\n", coord2sstr(b->f[f], b), probs[f], s);
39 if (probs[f] > probs[best])
40 best = f;
43 return coord_copy(b->f[best]);
46 void
47 patternplay_evaluate(struct engine *e, struct board *b, struct time_info *ti, floating_t *vals, enum stone color)
49 struct patternplay *pp = e->data;
51 struct pattern pats[b->flen];
52 pattern_rate_moves(&pp->pat, b, color, pats, vals);
54 #if 0
55 // unused variable 'total' in above call to pattern_rate_moves()
56 floating_t total = pattern_rate_moves(&pp->pat, b, color, pats, vals);
57 /* Rescale properly. */
58 for (int f = 0; f < b->flen; f++) {
59 probs[f] /= total;
61 #endif
63 if (pp->debug_level >= 4) {
64 for (int f = 0; f < b->flen; f++) {
65 if (vals[f] >= 0.001) {
66 char s[256]; pattern2str(s, &pats[f]);
67 fprintf(stderr, "\t%s: %.3f %s\n", coord2sstr(b->f[f], b), vals[f], s);
74 struct patternplay *
75 patternplay_state_init(char *arg)
77 struct patternplay *pp = calloc2(1, sizeof(struct patternplay));
78 bool pat_setup = false;
80 pp->debug_level = debug_level;
82 if (arg) {
83 char *optspec, *next = arg;
84 while (*next) {
85 optspec = next;
86 next += strcspn(next, ",");
87 if (*next) { *next++ = 0; } else { *next = 0; }
89 char *optname = optspec;
90 char *optval = strchr(optspec, '=');
91 if (optval) *optval++ = 0;
93 if (!strcasecmp(optname, "debug")) {
94 if (optval)
95 pp->debug_level = atoi(optval);
96 else
97 pp->debug_level++;
99 } else if (!strcasecmp(optname, "patterns") && optval) {
100 patterns_init(&pp->pat, optval, false, true);
101 pat_setup = true;
103 } else {
104 fprintf(stderr, "patternplay: Invalid engine argument %s or missing value\n", optname);
105 exit(EXIT_FAILURE);
110 if (!pat_setup)
111 patterns_init(&pp->pat, NULL, false, true);
113 return pp;
116 struct engine *
117 engine_patternplay_init(char *arg, struct board *b)
119 struct patternplay *pp = patternplay_state_init(arg);
120 struct engine *e = calloc2(1, sizeof(struct engine));
121 e->name = "PatternPlay Engine";
122 e->comment = "I select moves blindly according to learned patterns. I won't pass as long as there is a place on the board where I can play. When we both pass, I will consider all the stones on the board alive.";
123 e->genmove = patternplay_genmove;
124 e->evaluate = patternplay_evaluate;
125 e->data = pp;
127 return e;