9 #include "patternplay/patternplay.h"
11 #include "patternsp.h"
12 #include "patternprob.h"
16 /* Internal engine state. */
20 struct pattern_config pc
;
22 struct pattern_pdict
*pd
;
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
);
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
])
45 return coord_copy(b
->f
[best
]);
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
);
57 /* Rescale properly. */
58 for (int f
= 0; f
< b
->flen
; f
++) {
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
);
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
));
85 char *optspec
, *next
= arg
;
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")) {
97 pp
->debug_level
= atoi(optval
);
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
);
114 fprintf(stderr
, "patternplay: Invalid engine argument %s or missing value\n", optname
);
120 pp
->pd
= pattern_pdict_init(NULL
, &pp
->pc
);
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
;