README: Simplify strength info, add link to pachi-tr
[pachi.git] / mq.h
blobd8caa22701de08c9df9ae042f7442217f79ac538
1 #ifndef PACHI_MQ_H
2 #define PACHI_MQ_H
4 /* Move queues; in fact, they are more like move lists, usually used
5 * to accumulate equally good move candidates, then choosing from them
6 * randomly. But they are also used to juggle group lists (using the
7 * fact that coord_t == group_t). */
9 #include "move.h"
10 #include "random.h"
12 #define MQL 512 /* XXX: On larger board this might not be enough. */
13 struct move_queue {
14 unsigned int moves;
15 coord_t move[MQL];
16 /* Each move can have an optional tag or set of tags.
17 * The usage of these is user-dependent. */
18 unsigned char tag[MQL];
21 /* Pick a random move from the queue. */
22 static coord_t mq_pick(struct move_queue *q);
24 /* Add a move to the queue. */
25 static void mq_add(struct move_queue *q, coord_t c, unsigned char tag);
27 /* Cat two queues together. */
28 static void mq_append(struct move_queue *qd, struct move_queue *qs);
30 /* Check if the last move in queue is not a dupe, and remove it
31 * in that case. */
32 static void mq_nodup(struct move_queue *q);
34 /* Print queue contents on stderr. */
35 static void mq_print(struct move_queue *q, struct board *b, char *label);
38 /* Variations of the above that allow move weighting. */
39 /* XXX: The "kinds of move queue" issue (it's even worse in some other
40 * branches) is one of the few good arguments for C++ in Pachi...
41 * At least rewrite it to be less hacky and maybe make a move_gamma_queue
42 * that encapsulates move_queue. */
44 static coord_t mq_gamma_pick(struct move_queue *q, fixp_t *gammas);
45 static void mq_gamma_add(struct move_queue *q, fixp_t *gammas, coord_t c, double gamma, unsigned char tag);
46 static void mq_gamma_print(struct move_queue *q, fixp_t *gammas, struct board *b, char *label);
49 static inline coord_t
50 mq_pick(struct move_queue *q)
52 return q->moves ? q->move[fast_random(q->moves)] : pass;
55 static inline void
56 mq_add(struct move_queue *q, coord_t c, unsigned char tag)
58 assert(q->moves < MQL);
59 q->tag[q->moves] = tag;
60 q->move[q->moves++] = c;
63 static inline void
64 mq_append(struct move_queue *qd, struct move_queue *qs)
66 assert(qd->moves + qs->moves < MQL);
67 memcpy(&qd->tag[qd->moves], qs->tag, qs->moves * sizeof(*qs->tag));
68 memcpy(&qd->move[qd->moves], qs->move, qs->moves * sizeof(*qs->move));
69 qd->moves += qs->moves;
72 static inline void
73 mq_nodup(struct move_queue *q)
75 for (unsigned int i = 1; i < 4; i++) {
76 if (q->moves <= i)
77 return;
78 if (q->move[q->moves - 1 - i] == q->move[q->moves - 1]) {
79 q->tag[q->moves - 1 - i] |= q->tag[q->moves - 1];
80 q->moves--;
81 return;
86 static inline void
87 mq_print(struct move_queue *q, struct board *b, char *label)
89 fprintf(stderr, "%s candidate moves: ", label);
90 for (unsigned int i = 0; i < q->moves; i++) {
91 fprintf(stderr, "%s ", coord2sstr(q->move[i], b));
93 fprintf(stderr, "\n");
96 static inline coord_t
97 mq_gamma_pick(struct move_queue *q, fixp_t *gammas)
99 if (!q->moves)
100 return pass;
101 fixp_t total = 0;
102 for (unsigned int i = 0; i < q->moves; i++) {
103 total += gammas[i];
105 if (!total)
106 return pass;
107 fixp_t stab = fast_irandom(total);
108 for (unsigned int i = 0; i < q->moves; i++) {
109 if (stab < gammas[i])
110 return q->move[i];
111 stab -= gammas[i];
113 assert(0);
114 return pass;
117 static inline void
118 mq_gamma_add(struct move_queue *q, fixp_t *gammas, coord_t c, double gamma, unsigned char tag)
120 mq_add(q, c, tag);
121 gammas[q->moves - 1] = double_to_fixp(gamma);
124 static inline void
125 mq_gamma_print(struct move_queue *q, fixp_t *gammas, struct board *b, char *label)
127 fprintf(stderr, "%s candidate moves: ", label);
128 for (unsigned int i = 0; i < q->moves; i++) {
129 fprintf(stderr, "%s(%.3f) ", coord2sstr(q->move[i], b), fixp_to_double(gammas[i]));
131 fprintf(stderr, "\n");
134 #endif