Merge branch 'master' into libmap
[pachi.git] / mq.h
blob6f455867acd30d405165e438e23f1b9063f26197
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 /* Warning: move_queue is further specialized upon in libmap_mq.
10 * Take care of that when doing any changes here. */
12 #include "move.h"
13 #include "random.h"
15 #define MQL 512 /* XXX: On larger board this might not be enough. */
16 struct move_queue {
17 unsigned int moves;
18 coord_t move[MQL];
19 /* Each move can have an optional tag or set of tags.
20 * The usage of these is user-dependent. */
21 unsigned char tag[MQL];
24 /* Pick a random move from the queue. */
25 static coord_t mq_pick(struct move_queue *q);
27 /* Add a move to the queue. */
28 static void mq_add(struct move_queue *q, coord_t c, unsigned char tag);
30 /* Cat two queues together. */
31 static void mq_append(struct move_queue *qd, struct move_queue *qs);
33 /* Check if the last move in queue is not a dupe, and remove it
34 * in that case. */
35 static void mq_nodup(struct move_queue *q);
37 /* Print queue contents on stderr. */
38 static void mq_print(struct move_queue *q, struct board *b, char *label);
41 /* Variations of the above that allow move weighting. */
42 /* XXX: The "kinds of move queue" issue (it's even worse in some other
43 * branches) is one of the few good arguments for C++ in Pachi...
44 * At least rewrite it to be less hacky and maybe make a move_gamma_queue
45 * that encapsulates move_queue. */
47 static coord_t mq_gamma_pick(struct move_queue *q, fixp_t *gammas);
48 static void mq_gamma_add(struct move_queue *q, fixp_t *gammas, coord_t c, double gamma, unsigned char tag);
49 static void mq_gamma_print(struct move_queue *q, fixp_t *gammas, struct board *b, char *label);
52 static inline coord_t
53 mq_pick(struct move_queue *q)
55 return q->moves ? q->move[fast_random(q->moves)] : pass;
58 static inline void
59 mq_add(struct move_queue *q, coord_t c, unsigned char tag)
61 assert(q->moves < MQL);
62 q->tag[q->moves] = tag;
63 q->move[q->moves++] = c;
66 static inline void
67 mq_append(struct move_queue *qd, struct move_queue *qs)
69 assert(qd->moves + qs->moves < MQL);
70 memcpy(&qd->tag[qd->moves], qs->tag, qs->moves * sizeof(*qs->tag));
71 memcpy(&qd->move[qd->moves], qs->move, qs->moves * sizeof(*qs->move));
72 qd->moves += qs->moves;
75 static inline void
76 mq_nodup(struct move_queue *q)
78 for (unsigned int i = 1; i < 4; i++) {
79 if (q->moves <= i)
80 return;
81 if (q->move[q->moves - 1 - i] == q->move[q->moves - 1]) {
82 q->tag[q->moves - 1 - i] |= q->tag[q->moves - 1];
83 q->moves--;
84 return;
89 static inline void
90 mq_print(struct move_queue *q, struct board *b, char *label)
92 fprintf(stderr, "%s candidate moves: ", label);
93 for (unsigned int i = 0; i < q->moves; i++) {
94 fprintf(stderr, "%s ", coord2sstr(q->move[i], b));
96 fprintf(stderr, "\n");
99 static inline coord_t
100 mq_gamma_pick(struct move_queue *q, fixp_t *gammas)
102 if (!q->moves)
103 return pass;
104 fixp_t total = 0;
105 for (unsigned int i = 0; i < q->moves; i++) {
106 total += gammas[i];
108 if (!total)
109 return pass;
110 fixp_t stab = fast_irandom(total);
111 for (unsigned int i = 0; i < q->moves; i++) {
112 if (stab < gammas[i])
113 return q->move[i];
114 stab -= gammas[i];
116 assert(0);
117 return pass;
120 static inline void
121 mq_gamma_add(struct move_queue *q, fixp_t *gammas, coord_t c, double gamma, unsigned char tag)
123 mq_add(q, c, tag);
124 gammas[q->moves - 1] = double_to_fixp(gamma);
127 static inline void
128 mq_gamma_print(struct move_queue *q, fixp_t *gammas, struct board *b, char *label)
130 fprintf(stderr, "%s candidate moves: ", label);
131 for (unsigned int i = 0; i < q->moves; i++) {
132 fprintf(stderr, "%s(%.3f) ", coord2sstr(q->move[i], b), fixp_to_double(gammas[i]));
134 fprintf(stderr, "\n");
137 #endif