patternplay_evaluate(): Skip normalization; more useful values for debugging
[pachi.git] / patternplay / patternplay.c
blob1d1313038a920c56d4f6f1094417d3d52f1acb9e
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_config pc;
21 pattern_spec ps;
22 struct pattern_pdict *pd;
26 static coord_t *
27 patternplay_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
29 struct patternplay *pp = e->data;
31 struct pattern pats[b->flen];
32 floating_t probs[b->flen];
33 pattern_rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, probs);
35 int best = 0;
36 for (int f = 0; f < b->flen; f++) {
37 if (pp->debug_level >= 5 && probs[f] >= 0.001) {
38 char s[256]; pattern2str(s, &pats[f]);
39 fprintf(stderr, "\t%s: %.3f %s\n", coord2sstr(b->f[f], b), probs[f], s);
41 if (probs[f] > probs[best])
42 best = f;
45 return coord_copy(b->f[best]);
48 void
49 patternplay_evaluate(struct engine *e, struct board *b, struct time_info *ti, floating_t *vals, enum stone color)
51 struct patternplay *pp = e->data;
53 struct pattern pats[b->flen];
54 floating_t total = pattern_rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, vals);
56 #if 0
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));
79 pp->debug_level = debug_level;
80 pp->pc = DEFAULT_PATTERN_CONFIG;
81 pp->pc.spat_dict = spatial_dict_init(false, false);
82 memcpy(&pp->ps, PATTERN_SPEC_MATCH_DEFAULT, sizeof(pattern_spec));
84 if (arg) {
85 char *optspec, *next = arg;
86 while (*next) {
87 optspec = next;
88 next += strcspn(next, ",");
89 if (*next) { *next++ = 0; } else { *next = 0; }
91 char *optname = optspec;
92 char *optval = strchr(optspec, '=');
93 if (optval) *optval++ = 0;
95 if (!strcasecmp(optname, "debug")) {
96 if (optval)
97 pp->debug_level = atoi(optval);
98 else
99 pp->debug_level++;
101 /* See pattern.h:pattern_config for description and
102 * pattern.c:DEFAULT_PATTERN_CONFIG for default values
103 * of the following options. */
104 } else if (!strcasecmp(optname, "bdist_max") && optval) {
105 pp->pc.bdist_max = atoi(optval);
106 } else if (!strcasecmp(optname, "spat_min") && optval) {
107 pp->pc.spat_min = atoi(optval);
108 } else if (!strcasecmp(optname, "spat_max") && optval) {
109 pp->pc.spat_max = atoi(optval);
110 } else if (!strcasecmp(optname, "spat_largest")) {
111 pp->pc.spat_largest = !optval || atoi(optval);
113 } else {
114 fprintf(stderr, "patternplay: Invalid engine argument %s or missing value\n", optname);
115 exit(EXIT_FAILURE);
120 pp->pd = pattern_pdict_init(NULL, &pp->pc);
122 return pp;
125 struct engine *
126 engine_patternplay_init(char *arg, struct board *b)
128 struct patternplay *pp = patternplay_state_init(arg);
129 struct engine *e = calloc2(1, sizeof(struct engine));
130 e->name = "PatternPlay Engine";
131 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.";
132 e->genmove = patternplay_genmove;
133 e->evaluate = patternplay_evaluate;
134 e->data = pp;
136 return e;