My Project
eval.c
Go to the documentation of this file.
1
11#include "eval.h"
12
13#include "bit.h"
14#include "board.h"
15#include "options.h"
16#include "move.h"
17#include "util.h"
18
19#include <stdlib.h>
20#include <assert.h>
21
23typedef struct CoordinateToFeature {
25 struct {
26 int i;
27 int x;
28 } feature[16];
30
36
38static const FeatureToCoordinate EVAL_F2X[] = {
39 { 9, {A1, B1, A2, B2, C1, A3, C2, B3, C3}},
40 { 9, {H1, G1, H2, G2, F1, H3, F2, G3, F3}},
41 { 9, {A8, A7, B8, B7, A6, C8, B6, C7, C6}},
42 { 9, {H8, H7, G8, G7, H6, F8, G6, F7, F6}},
43
44 {10, {A5, A4, A3, A2, A1, B2, B1, C1, D1, E1}},
45 {10, {H5, H4, H3, H2, H1, G2, G1, F1, E1, D1}},
46 {10, {A4, A5, A6, A7, A8, B7, B8, C8, D8, E8}},
47 {10, {H4, H5, H6, H7, H8, G7, G8, F8, E8, D8}},
48
49 {10, {B2, A1, B1, C1, D1, E1, F1, G1, H1, G2}},
50 {10, {B7, A8, B8, C8, D8, E8, F8, G8, H8, G7}},
51 {10, {B2, A1, A2, A3, A4, A5, A6, A7, A8, B7}},
52 {10, {G2, H1, H2, H3, H4, H5, H6, H7, H8, G7}},
53
54 {10, {A1, C1, D1, C2, D2, E2, F2, E1, F1, H1}},
55 {10, {A8, C8, D8, C7, D7, E7, F7, E8, F8, H8}},
56 {10, {A1, A3, A4, B3, B4, B5, B6, A5, A6, A8}},
57 {10, {H1, H3, H4, G3, G4, G5, G6, H5, H6, H8}},
58
59 { 8, {A2, B2, C2, D2, E2, F2, G2, H2}},
60 { 8, {A7, B7, C7, D7, E7, F7, G7, H7}},
61 { 8, {B1, B2, B3, B4, B5, B6, B7, B8}},
62 { 8, {G1, G2, G3, G4, G5, G6, G7, G8}},
63
64 { 8, {A3, B3, C3, D3, E3, F3, G3, H3}},
65 { 8, {A6, B6, C6, D6, E6, F6, G6, H6}},
66 { 8, {C1, C2, C3, C4, C5, C6, C7, C8}},
67 { 8, {F1, F2, F3, F4, F5, F6, F7, F8}},
68
69 { 8, {A4, B4, C4, D4, E4, F4, G4, H4}},
70 { 8, {A5, B5, C5, D5, E5, F5, G5, H5}},
71 { 8, {D1, D2, D3, D4, D5, D6, D7, D8}},
72 { 8, {E1, E2, E3, E4, E5, E6, E7, E8}},
73
74 { 8, {A1, B2, C3, D4, E5, F6, G7, H8}},
75 { 8, {A8, B7, C6, D5, E4, F3, G2, H1}},
76
77 { 7, {B1, C2, D3, E4, F5, G6, H7}},
78 { 7, {H2, G3, F4, E5, D6, C7, B8}},
79 { 7, {A2, B3, C4, D5, E6, F7, G8}},
80 { 7, {G1, F2, E3, D4, C5, B6, A7}},
81
82 { 6, {C1, D2, E3, F4, G5, H6}},
83 { 6, {A3, B4, C5, D6, E7, F8}},
84 { 6, {F1, E2, D3, C4, B5, A6}},
85 { 6, {H3, G4, F5, E6, D7, C8}},
86
87 { 5, {D1, E2, F3, G4, H5}},
88 { 5, {A4, B5, C6, D7, E8}},
89 { 5, {E1, D2, C3, B4, A5}},
90 { 5, {H4, G5, F6, E7, D8}},
91
92 { 4, {D1, C2, B3, A4}},
93 { 4, {A5, B6, C7, D8}},
94 { 4, {E1, F2, G3, H4}},
95 { 4, {H5, G6, F7, E8}},
96
97 { 0, {NOMOVE}}
98};
99
102 {7, {{ 0, 6561}, { 4, 243}, { 8, 6561}, {10, 6561}, {12, 19683}, {14, 19683}, {28, 2187}}}, /* a1 */
103 {5, {{ 0, 2187}, { 4, 27}, { 8, 2187}, {18, 2187}, {30, 729}}}, /* b1 */
104 {6, {{ 0, 81}, { 4, 9}, { 8, 729}, {12, 6561}, {22, 2187}, {34, 243}}}, /* c1 */
105 {7, {{ 4, 3}, { 5, 1}, { 8, 243}, {12, 2187}, {26, 2187}, {38, 81}, {42, 27}}}, /* d1 */
106 {7, {{ 4, 1}, { 5, 3}, { 8, 81}, {12, 9}, {27, 2187}, {40, 81}, {44, 27}}}, /* e1 */
107 {6, {{ 1, 81}, { 5, 9}, { 8, 27}, {12, 3}, {23, 2187}, {36, 243}}}, /* f1 */
108 {5, {{ 1, 2187}, { 5, 27}, { 8, 9}, {19, 2187}, {33, 729}}}, /* g1 */
109 {7, {{ 1, 6561}, { 5, 243}, { 8, 3}, {11, 6561}, {12, 1}, {15, 19683}, {29, 1}}}, /* h1 */
110 {5, {{ 0, 729}, { 4, 729}, {10, 2187}, {16, 2187}, {32, 729}}}, /* a2 */
111 {7, {{ 0, 243}, { 4, 81}, { 8, 19683}, {10, 19683}, {16, 729}, {18, 729}, {28, 729}}}, /* b2 */
112 {6, {{ 0, 9}, {12, 729}, {16, 243}, {22, 729}, {30, 243}, {42, 9}}}, /* c2 */
113 {5, {{12, 243}, {16, 81}, {26, 729}, {34, 81}, {40, 27}}}, /* d2 */
114 {5, {{12, 81}, {16, 27}, {27, 729}, {36, 81}, {38, 27}}}, /* e2 */
115 {6, {{ 1, 9}, {12, 27}, {16, 9}, {23, 729}, {33, 243}, {44, 9}}}, /* f2 */
116 {7, {{ 1, 243}, { 5, 81}, { 8, 1}, {11, 19683}, {16, 3}, {19, 729}, {29, 3}}}, /* g2 */
117 {5, {{ 1, 729}, { 5, 729}, {11, 2187}, {16, 1}, {31, 729}}}, /* h2 */
118 {6, {{ 0, 27}, { 4, 2187}, {10, 729}, {14, 6561}, {20, 2187}, {35, 243}}}, /* a3 */
119 {6, {{ 0, 3}, {14, 729}, {18, 243}, {20, 729}, {32, 243}, {42, 3}}}, /* b3 */
120 {5, {{ 0, 1}, {20, 243}, {22, 243}, {28, 243}, {40, 9}}}, /* c3 */
121 {4, {{20, 81}, {26, 243}, {30, 81}, {36, 27}}}, /* d3 */
122 {4, {{20, 27}, {27, 243}, {33, 81}, {34, 27}}}, /* e3 */
123 {5, {{ 1, 1}, {20, 9}, {23, 243}, {29, 9}, {38, 9}}}, /* f3 */
124 {6, {{ 1, 3}, {15, 729}, {19, 243}, {20, 3}, {31, 243}, {44, 3}}}, /* g3 */
125 {6, {{ 1, 27}, { 5, 2187}, {11, 729}, {15, 6561}, {20, 1}, {37, 243}}}, /* h3 */
126 {7, {{ 4, 6561}, { 6, 19683}, {10, 243}, {14, 2187}, {24, 2187}, {39, 81}, {42, 1}}}, /* a4 */
127 {5, {{14, 243}, {18, 81}, {24, 729}, {35, 81}, {40, 3}}}, /* b4 */
128 {4, {{22, 81}, {24, 243}, {32, 81}, {36, 9}}}, /* c4 */
129 {4, {{24, 81}, {26, 81}, {28, 81}, {33, 27}}}, /* d4 */
130 {4, {{24, 27}, {27, 81}, {29, 27}, {30, 27}}}, /* e4 */
131 {4, {{23, 81}, {24, 9}, {31, 81}, {34, 9}}}, /* f4 */
132 {5, {{15, 243}, {19, 81}, {24, 3}, {37, 81}, {38, 3}}}, /* g4 */
133 {7, {{ 5, 6561}, { 7, 19683}, {11, 243}, {15, 2187}, {24, 1}, {41, 81}, {44, 1}}}, /* h4 */
134 {7, {{ 4, 19683}, { 6, 6561}, {10, 81}, {14, 9}, {25, 2187}, {40, 1}, {43, 27}}}, /* a5 */
135 {5, {{14, 81}, {18, 27}, {25, 729}, {36, 3}, {39, 27}}}, /* b5 */
136 {4, {{22, 27}, {25, 243}, {33, 9}, {35, 27}}}, /* c5 */
137 {4, {{25, 81}, {26, 27}, {29, 81}, {32, 27}}}, /* d5 */
138 {4, {{25, 27}, {27, 27}, {28, 27}, {31, 27}}}, /* e5 */
139 {4, {{23, 27}, {25, 9}, {30, 9}, {37, 27}}}, /* f5 */
140 {5, {{15, 81}, {19, 27}, {25, 3}, {34, 3}, {41, 27}}}, /* g5 */
141 {7, {{ 5, 19683}, { 7, 6561}, {11, 81}, {15, 9}, {25, 1}, {38, 1}, {45, 27}}}, /* h5 */
142 {6, {{ 2, 81}, { 6, 2187}, {10, 27}, {14, 3}, {21, 2187}, {36, 1}}}, /* a6 */
143 {6, {{ 2, 9}, {14, 27}, {18, 9}, {21, 729}, {33, 3}, {43, 9}}}, /* b6 */
144 {5, {{ 2, 1}, {21, 243}, {22, 9}, {29, 243}, {39, 9}}}, /* c6 */
145 {4, {{21, 81}, {26, 9}, {31, 9}, {35, 9}}}, /* d6 */
146 {4, {{21, 27}, {27, 9}, {32, 9}, {37, 9}}}, /* e6 */
147 {5, {{ 3, 1}, {21, 9}, {23, 9}, {28, 9}, {41, 9}}}, /* f6 */
148 {6, {{ 3, 9}, {15, 27}, {19, 9}, {21, 3}, {30, 3}, {45, 9}}}, /* g6 */
149 {6, {{ 3, 81}, { 7, 2187}, {11, 27}, {15, 3}, {21, 1}, {34, 1}}}, /* h6 */
150 {5, {{ 2, 2187}, { 6, 729}, {10, 9}, {17, 2187}, {33, 1}}}, /* a7 */
151 {7, {{ 2, 243}, { 6, 81}, { 9, 19683}, {10, 1}, {17, 729}, {18, 3}, {29, 729}}}, /* b7 */
152 {6, {{ 2, 3}, {13, 729}, {17, 243}, {22, 3}, {31, 3}, {43, 3}}}, /* c7 */
153 {5, {{13, 243}, {17, 81}, {26, 3}, {37, 3}, {39, 3}}}, /* d7 */
154 {5, {{13, 81}, {17, 27}, {27, 3}, {35, 3}, {41, 3}}}, /* e7 */
155 {6, {{ 3, 3}, {13, 27}, {17, 9}, {23, 3}, {32, 3}, {45, 3}}}, /* f7 */
156 {7, {{ 3, 243}, { 7, 81}, { 9, 1}, {11, 1}, {17, 3}, {19, 3}, {28, 3}}}, /* g7 */
157 {5, {{ 3, 2187}, { 7, 729}, {11, 9}, {17, 1}, {30, 1}}}, /* h7 */
158 {7, {{ 2, 6561}, { 6, 243}, { 9, 6561}, {10, 3}, {13, 19683}, {14, 1}, {29, 2187}}}, /* a8 */
159 {5, {{ 2, 729}, { 6, 27}, { 9, 2187}, {18, 1}, {31, 1}}}, /* b8 */
160 {6, {{ 2, 27}, { 6, 9}, { 9, 729}, {13, 6561}, {22, 1}, {37, 1}}}, /* c8 */
161 {7, {{ 6, 3}, { 7, 1}, { 9, 243}, {13, 2187}, {26, 1}, {41, 1}, {43, 1}}}, /* d8 */
162 {7, {{ 6, 1}, { 7, 3}, { 9, 81}, {13, 9}, {27, 1}, {39, 1}, {45, 1}}}, /* e8 */
163 {6, {{ 3, 27}, { 7, 9}, { 9, 27}, {13, 3}, {23, 1}, {35, 1}}}, /* f8 */
164 {5, {{ 3, 729}, { 7, 27}, { 9, 9}, {19, 1}, {32, 1}}}, /* g8 */
165 {7, {{ 3, 6561}, { 7, 243}, { 9, 3}, {11, 3}, {13, 1}, {15, 1}, {28, 1}}}, /* h8 */
166 {0, {{ 0, 0}}} // <- PASS
167};
168
170static const int EVAL_SIZE[] = {19683, 59049, 59049, 59049, 6561, 6561, 6561, 6561, 2187,729, 243, 81, 1};
171
173static const int EVAL_PACKED_SIZE[] = {10206, 29889, 29646, 29646, 3321, 3321, 3321, 3321, 1134, 378, 135, 45, 1};
174
176static const int EVAL_OFFSET[] = {
177 0, 0, 0, 0,
178 19683, 19683, 19683, 19683,
179 78732, 78732, 78732, 78732,
180 137781, 137781, 137781, 137781,
181 196830, 196830, 196830, 196830,
182 203391, 203391, 203391, 203391,
183 209952, 209952, 209952, 209952,
184 216513, 216513,
185 223074, 223074, 223074, 223074,
186 225261, 225261, 225261, 225261,
187 225990, 225990, 225990, 225990,
188 226233, 226233, 226233, 226233,
189 226314,
190};
191
192static const int EVAL_MAX_VALUE[] = {
193 19682, 19682, 19682, 19682,
194 78731, 78731, 78731, 78731,
195 137780, 137780, 137780, 137780,
196 196829, 196829, 196829, 196829,
197 203390, 203390, 203390, 203390,
198 209951, 209951, 209951, 209951,
199 216512, 216512,
200 223073, 223073, 223073, 223073,
201 225260, 225260, 225260, 225260,
202 225989, 225989, 225989, 225989,
203 226232, 226232, 226232, 226232,
204 226313, 226313, 226313, 226313,
205 226314,
206};
207
209static int EVAL_C10[2][59049];
210static int EVAL_S10[2][59049];
211static int EVAL_C9[2][19683];
212static int EVAL_S8[2][6561];
213static int EVAL_S7[2][2187];
214static int EVAL_S6[2][729];
215static int EVAL_S5[2][243];
216static int EVAL_S4[2][81];
217
219static const int EVAL_N_WEIGHT = 226315;
220
222static const int EVAL_N_PLY = 61;
223
225static const int EVAL_N_FEATURE = 47;
226
228static int EVAL_LOADED = 0;
229
231short ***EVAL_WEIGHT;
232
235
236
245static int opponent_feature(int l, int d)
246{
247 static const int o[] = {1, 0, 2};
248 int f = o[l % 3];
249
250 if (d > 1) f += opponent_feature(l / 3, d - 1) * 3;
251
252 return f;
253}
254
265void eval_open(const char* file)
266{
267 unsigned int edax_header, eval_header;
268 unsigned int version, release, build;
269 double date;
270 const int n_w = 114364;
271 int *T;
272 int ply, i, j, k, l, n;
273 int r;
274 int offset;
275 FILE* f;
276 short *w = NULL;
277
278 if (EVAL_LOADED++) return;
279
280 // the following is assumed:
281 // -(unsigned) int are 32 bits
282 if (sizeof (int) != 4) fatal_error("int size is not compatible with Edax.\n");
283 // -(unsigned) short are 16 bits
284 if (sizeof (short) != 2) fatal_error("short size is not compatible with Edax.\n");
285
286 // create unpacking tables
287 T = (int*) malloc(59049 * sizeof (*T));
288 if (T == NULL) fatal_error("Cannot allocate temporary table variable.\n");
289
290 for (l = n = 0; l < 6561; l++){ /* 8 squares : 6561 -> 3321 */
291 k=((l / 2187) % 3) + ((l / 729) % 3) * 3 + ((l / 243) % 3) * 9 +
292 ((l / 81) % 3) * 27 + ((l / 27) % 3) * 81 + ((l / 9) % 3) * 243 +
293 ((l / 3) % 3) * 729 + (l % 3) * 2187;
294 if (k < l) T[l] = T[k];
295 else T[l] = n++;
296 EVAL_S8[0][l] = T[l];
297 EVAL_S8[1][opponent_feature(l, 8)] = T[l];
298 }
299 for (l = n = 0; l < 2187; l++){ /* 7 squares : 2187 -> 1134 */
300 k=((l / 729) % 3) + ((l / 243) % 3) * 3 + ((l / 81) % 3) * 9 +
301 ((l / 27) % 3) * 27 + ((l / 9) % 3) * 81 + ((l / 3) % 3) * 243 +
302 (l % 3) * 729;
303 if (k < l) T[l] = T[k];
304 else T[l] = n++;
305 EVAL_S7[0][l] = T[l];
306 EVAL_S7[1][opponent_feature(l, 7)] = T[l];
307 }
308 for (l = n = 0; l < 729; l++){ /* 6 squares : 729 -> 378 */
309 k=((l / 243) % 3) + ((l / 81) % 3) * 3 + ((l / 27) % 3) * 9 +
310 ((l / 9) % 3) * 27 + ((l / 3) % 3) * 81 + (l % 3) * 243;
311 if (k < l) T[l]=T[k];
312 else T[l] = n++;
313 EVAL_S6[0][l] = T[l];
314 EVAL_S6[1][opponent_feature(l, 6)] = T[l];
315 }
316 for (l = n = 0; l < 243; l++) { /* 5 squares : 243 -> 135 */
317 k=((l / 81) % 3)+((l / 27) % 3) * 3 + ((l / 9) % 3) * 9+
318 ((l / 3) % 3) * 27 + (l % 3) * 81;
319 if (k < l) T[l] = T[k];
320 else T[l] = n++;
321 EVAL_S5[0][l] = T[l];
322 EVAL_S5[1][opponent_feature(l, 5)] = T[l];
323 }
324 for (l = n = 0; l < 81; l++) { /* 4 squares : 81 -> 45 */
325 k=((l / 27) % 3) + ((l / 9) % 3) * 3 + ((l / 3) % 3) * 9 + (l % 3) * 27;
326 if (k < l) T[l] = T[k];
327 else T[l] = n++;
328 EVAL_S4[0][l] = T[l];
329 EVAL_S4[1][opponent_feature(l, 4)] = T[l];
330 }
331 for (l = n = 0;l < 19683; l++) { /* 9 corner squares : 19683 -> 10206 */
332 k = ((l / 6561) % 3) * 6561 + ((l / 729) % 3) * 2187 +
333 ((l / 2187) % 3) * 729 + ((l / 243) % 3) * 243 +((l / 27) % 3) * 81 +
334 ((l / 81) % 3) * 27 + ((l / 3) % 3) * 9 + ((l / 9) % 3) * 3 + (l % 3);
335 if (k < l) T[l] = T[k];
336 else T[l] = n++;
337 EVAL_C9[0][l] = T[l];
338 EVAL_C9[1][opponent_feature(l, 9)] = T[l];
339 }
340 for (l = n = 0; l < 59049; l++) { /* 10 squares (edge +X ) : 59049 -> 29646 */
341 k=((l / 19683) % 3) + ((l / 6561) % 3) * 3+((l / 2187) % 3) * 9 +
342 ((l / 729) % 3) * 27 + ((l / 243) % 3) * 81 + ((l / 81) % 3) * 243 +
343 ((l / 27) % 3) * 729 + ((l / 9) % 3) * 2187 + ((l / 3) % 3) * 6561 +
344 (l % 3) * 19683;
345 if (k < l) T[l] = T[k];
346 else T[l] = n++;
347 EVAL_S10[0][l] = T[l];
348 EVAL_S10[1][opponent_feature(l, 10)] = T[l];
349 }
350 for (l = n = 0; l < 59049; l++) { /* 10 squares (angle + X) : 59049 -> 29889 */
351 k=((l / 19683) % 3) + ((l / 6561) % 3) * 3+((l / 2187) % 3) * 9 +
352 ((l / 729) % 3) * 27 + ((l / 243) % 3) * 243 + ((l / 81) % 3) * 81 +
353 ((l / 27) % 3) * 729 + ((l / 9) % 3) * 2187 + ((l / 3) % 3) * 6561 +
354 (l % 3) * 19683;
355 if (k < l) T[l] = T[k];
356 else T[l] = n++;
357 EVAL_C10[0][l] = T[l];
358 EVAL_C10[1][opponent_feature(l, 10)] = T[l];
359 }
360 free(T);
361
362 // allocation
363 EVAL_WEIGHT = (short***) malloc(2 * sizeof (*EVAL_WEIGHT));
364 if (EVAL_WEIGHT == NULL) fatal_error("Cannot evaluation weights.\n");
365 EVAL_WEIGHT[0] = (short**) malloc(2 * EVAL_N_PLY * sizeof (**EVAL_WEIGHT));
366 if (EVAL_WEIGHT[0] == NULL) fatal_error("Cannot evaluation weights.\n");
368 EVAL_WEIGHT[0][0] = (short*) malloc(2 * EVAL_N_PLY * EVAL_N_WEIGHT * sizeof (***EVAL_WEIGHT));
369 if (EVAL_WEIGHT[0][0] == NULL) fatal_error("Cannot evaluation weights.\n");
371 for (ply = 1; ply < EVAL_N_PLY; ply++) {
372 EVAL_WEIGHT[0][ply] = EVAL_WEIGHT[0][ply - 1] + EVAL_N_WEIGHT;
373 EVAL_WEIGHT[1][ply] = EVAL_WEIGHT[1][ply - 1] + EVAL_N_WEIGHT;
374 }
375
376 // data reading
377 w = (short*) malloc(n_w * sizeof (*w)); // a temporary to read packed weights
378 f = fopen(file, "rb");
379 if (f == NULL) {
380 fprintf(stderr, "Cannot open %s", file);
381 exit(EXIT_FAILURE);
382 }
383
384 // File header
385 r = fread(&edax_header, sizeof (int), 1, f);
386 r += fread(&eval_header, sizeof (int), 1, f);
387 if (r != 2 || (!(edax_header == EDAX || eval_header == EVAL) && !(edax_header == XADE || eval_header == LAVE))) fatal_error("%s is not an Edax evaluation file\n", file);
388 r = fread(&version, sizeof (int), 1, f);
389 r += fread(&release, sizeof (int), 1, f);
390 r += fread(&build, sizeof (int), 1, f);
391 r += fread(&date, sizeof (double), 1, f);
392 if (r != 4) fatal_error("Cannot read version info from %s\n", file);
393 if (edax_header == XADE) {
395 release = bswap_int(release);
396 build = bswap_int(build);
397 }
398 // Weights : read & unpacked them
399 for (ply = 0; ply < EVAL_N_PLY; ply++) {
400 r = fread(w, sizeof (short), n_w, f);
401 if (r != n_w) fatal_error("Cannot read evaluation weight from %s\n", file);
402 if (edax_header == XADE) for (i = 0; i < n_w; ++i) w[i] = bswap_short(w[i]);
403 i = j = offset = 0;
404 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
405 EVAL_WEIGHT[0][ply][j] = w[EVAL_C9[0][k] + offset];
406 EVAL_WEIGHT[1][ply][j] = w[EVAL_C9[1][k] + offset];
407 }
408 offset += EVAL_PACKED_SIZE[i];
409 i++;
410 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
411 EVAL_WEIGHT[0][ply][j] = w[EVAL_C10[0][k] + offset];
412 EVAL_WEIGHT[1][ply][j] = w[EVAL_C10[1][k] + offset];
413 }
414 offset += EVAL_PACKED_SIZE[i];
415 i++;
416 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
417 EVAL_WEIGHT[0][ply][j] = w[EVAL_S10[0][k] + offset];
418 EVAL_WEIGHT[1][ply][j] = w[EVAL_S10[1][k] + offset];
419 }
420 offset += EVAL_PACKED_SIZE[i];
421 i++;
422 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
423 EVAL_WEIGHT[0][ply][j] = w[EVAL_S10[0][k] + offset];
424 EVAL_WEIGHT[1][ply][j] = w[EVAL_S10[1][k] + offset];
425 }
426 offset += EVAL_PACKED_SIZE[i];
427 i++;
428 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
429 EVAL_WEIGHT[0][ply][j] = w[EVAL_S8[0][k] + offset];
430 EVAL_WEIGHT[1][ply][j] = w[EVAL_S8[1][k] + offset];
431 }
432 offset += EVAL_PACKED_SIZE[i];
433 i++;
434 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
435 EVAL_WEIGHT[0][ply][j] = w[EVAL_S8[0][k] + offset];
436 EVAL_WEIGHT[1][ply][j] = w[EVAL_S8[1][k] + offset];
437 }
438 offset += EVAL_PACKED_SIZE[i];
439 i++;
440 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
441 EVAL_WEIGHT[0][ply][j] = w[EVAL_S8[0][k] + offset];
442 EVAL_WEIGHT[1][ply][j] = w[EVAL_S8[1][k] + offset];
443 }
444 offset += EVAL_PACKED_SIZE[i];
445 i++;
446 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
447 EVAL_WEIGHT[0][ply][j] = w[EVAL_S8[0][k] + offset];
448 EVAL_WEIGHT[1][ply][j] = w[EVAL_S8[1][k] + offset];
449 }
450 offset += EVAL_PACKED_SIZE[i];
451 i++;
452 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
453 EVAL_WEIGHT[0][ply][j] = w[EVAL_S7[0][k] + offset];
454 EVAL_WEIGHT[1][ply][j] = w[EVAL_S7[1][k] + offset];
455 }
456 offset += EVAL_PACKED_SIZE[i];
457 i++;
458 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
459 EVAL_WEIGHT[0][ply][j] = w[EVAL_S6[0][k] + offset];
460 EVAL_WEIGHT[1][ply][j] = w[EVAL_S6[1][k] + offset];
461 }
462 offset += EVAL_PACKED_SIZE[i];
463 i++;
464 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
465 EVAL_WEIGHT[0][ply][j] = w[EVAL_S5[0][k] + offset];
466 EVAL_WEIGHT[1][ply][j] = w[EVAL_S5[1][k] + offset];
467 }
468 offset += EVAL_PACKED_SIZE[i];
469 i++;
470 for (k = 0; k < EVAL_SIZE[i]; k++,j++) {
471 EVAL_WEIGHT[0][ply][j] = w[EVAL_S4[0][k] + offset];
472 EVAL_WEIGHT[1][ply][j] = w[EVAL_S4[1][k] + offset];
473 }
474 offset += EVAL_PACKED_SIZE[i];
475 i++;
476 EVAL_WEIGHT[0][ply][j] = w[offset];
477 EVAL_WEIGHT[1][ply][j] = w[offset];
478 }
479
480 fclose(f);
481 free(w);
482
483 /*if (version == 3 && release == 2 && build == 5)*/ {
484 EVAL_A = -0.10026799, EVAL_B = 0.31027733, EVAL_C = -0.57772603;
485 EVAL_a = 0.07585621, EVAL_b = 1.16492647, EVAL_c = 5.4171698;
486 }
487
488 info("<Evaluation function weights version %u.%u.%u loaded>\n", version, release, build);
489}
490
494void eval_close(void)
495{
496 free(EVAL_WEIGHT[0][0]);
497 free(EVAL_WEIGHT[0]);
498 free(EVAL_WEIGHT);
499 EVAL_WEIGHT = NULL;
500}
501
509void eval_init(Eval *eval)
510{
511 eval->feature = (int*) malloc(EVAL_N_FEATURE * sizeof (*eval->feature));
512}
513
519void eval_free(Eval *eval)
520{
521 free(eval->feature);
522}
523
530void eval_set(Eval *eval, const Board *board)
531{
532 int i, j, c;
533
534 for (i = 0; i < EVAL_N_FEATURE; ++i) {
535 eval->feature[i] = 0;
536 for (j = 0; j < EVAL_F2X[i].n_square; j++) {
537 c = board_get_square_color(board, EVAL_F2X[i].x[j]);
538 eval->feature[i] = eval->feature[i] * 3 + c;
539 }
540 eval->feature[i] += EVAL_OFFSET[i];
541 }
542 eval->player = 0;
543}
544
550static void eval_swap(Eval *eval)
551{
552 eval->player ^= 1;
553}
554
555
562static void eval_update_0(Eval *eval, const Move *move)
563{
564 const CoordinateToFeature *s = EVAL_X2F + move->x;
565 register int x;
566 int *feature = eval->feature;
567 unsigned long long f = move->flipped;
568
569#ifdef DEBUG
570
571 int i, j;
572 for (i = 0; i < s->n_feature; ++i) {
573 j = s->feature[i].i;
574 assert(0 <= j && j < EVAL_N_FEATURE);
575 feature[j] -= 2 * s->feature[i].x;
576 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
577 }
578
579 foreach_bit (x, f) {
580 s = EVAL_X2F + x;
581 for (i = 0; i < s->n_feature; ++i) {
582 j = s->feature[i].i;
583 assert(0 <= j && j < EVAL_N_FEATURE);
584 feature[j] -= s->feature[i].x;
585 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
586 }
587 }
588
589#else
590
591 switch (s->n_feature) {
592 case 7: feature[s->feature[6].i] -= 2 * s->feature[6].x;
593 case 6: feature[s->feature[5].i] -= 2 * s->feature[5].x;
594 case 5: feature[s->feature[4].i] -= 2 * s->feature[4].x;
595 case 4: feature[s->feature[3].i] -= 2 * s->feature[3].x;
596 case 3: feature[s->feature[2].i] -= 2 * s->feature[2].x;
597 case 2: feature[s->feature[1].i] -= 2 * s->feature[1].x;
598 case 1: feature[s->feature[0].i] -= 2 * s->feature[0].x;
599 case 0: break;
600 }
601 foreach_bit (x, f) {
602 s = EVAL_X2F + x;
603 switch (s->n_feature) {
604 case 7: feature[s->feature[6].i] -= s->feature[6].x;
605 case 6: feature[s->feature[5].i] -= s->feature[5].x;
606 case 5: feature[s->feature[4].i] -= s->feature[4].x;
607 case 4: feature[s->feature[3].i] -= s->feature[3].x;
608 case 3: feature[s->feature[2].i] -= s->feature[2].x;
609 case 2: feature[s->feature[1].i] -= s->feature[1].x;
610 case 1: feature[s->feature[0].i] -= s->feature[0].x;
611 case 0: break;
612 }
613 }
614
615#endif
616
617}
618
625static void eval_update_1(Eval *eval, const Move *move)
626{
627 const CoordinateToFeature *s = EVAL_X2F + move->x;
628 register int x;
629 int *feature = eval->feature;
630 unsigned long long f = move->flipped;
631
632#ifdef DEBUG
633
634 int i, j;
635 for (i = 0; i < s->n_feature; ++i) {
636 j = s->feature[i].i;
637 assert(0 <= j && j < EVAL_N_FEATURE);
638 feature[j] -= s->feature[i].x;
639 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
640 }
641
642 foreach_bit (x, f) {
643 s = EVAL_X2F + x;
644 for (i = 0; i < s->n_feature; ++i) {
645 j = s->feature[i].i;
646 assert(0 <= j && j < EVAL_N_FEATURE);
647 feature[j] += s->feature[i].x;
648 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
649 }
650 }
651
652#else
653
654 switch (s->n_feature) {
655 case 7: feature[s->feature[6].i] -= s->feature[6].x;
656 case 6: feature[s->feature[5].i] -= s->feature[5].x;
657 case 5: feature[s->feature[4].i] -= s->feature[4].x;
658 case 4: feature[s->feature[3].i] -= s->feature[3].x;
659 case 3: feature[s->feature[2].i] -= s->feature[2].x;
660 case 2: feature[s->feature[1].i] -= s->feature[1].x;
661 case 1: feature[s->feature[0].i] -= s->feature[0].x;
662 case 0: break;
663 }
664 foreach_bit (x, f) {
665 s = EVAL_X2F + x;
666 switch (s->n_feature) {
667 case 7: feature[s->feature[6].i] += s->feature[6].x;
668 case 6: feature[s->feature[5].i] += s->feature[5].x;
669 case 5: feature[s->feature[4].i] += s->feature[4].x;
670 case 4: feature[s->feature[3].i] += s->feature[3].x;
671 case 3: feature[s->feature[2].i] += s->feature[2].x;
672 case 2: feature[s->feature[1].i] += s->feature[1].x;
673 case 1: feature[s->feature[0].i] += s->feature[0].x;
674 case 0: break;
675 }
676 }
677#endif
678
679}
680
681void eval_update(Eval *eval, const Move *move)
682{
683 static void (*eval_update_f[])(Eval*, const Move*) = {eval_update_0, eval_update_1};
684 assert(move->flipped);
685 assert(WHITE == eval->player || BLACK == eval->player);
686 eval_update_f[eval->player](eval, move);
687 eval_swap(eval);
688}
689
696static void eval_restore_0(Eval *eval, const Move *move)
697{
698 const CoordinateToFeature *s = EVAL_X2F + move->x;
699 register int x;
700 int *feature = eval->feature;
701 unsigned long long f = move->flipped;
702
703#ifdef DEBUG
704
705 int i, j;
706 for (i = 0; i < s->n_feature; ++i) {
707 j = s->feature[i].i;
708 assert(0 <= j && j < EVAL_N_FEATURE);
709 feature[j] += 2 * s->feature[i].x;
710 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
711 }
712
713 foreach_bit (x, f) {
714 s = EVAL_X2F + x;
715 for (i = 0; i < s->n_feature; ++i) {
716 j = s->feature[i].i;
717 assert(0 <= j && j < EVAL_N_FEATURE);
718 feature[j] += s->feature[i].x;
719 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
720 }
721 }
722
723#else
724
725 switch (s->n_feature) {
726 case 7: feature[s->feature[6].i] += 2 * s->feature[6].x;
727 case 6: feature[s->feature[5].i] += 2 * s->feature[5].x;
728 case 5: feature[s->feature[4].i] += 2 * s->feature[4].x;
729 case 4: feature[s->feature[3].i] += 2 * s->feature[3].x;
730 case 3: feature[s->feature[2].i] += 2 * s->feature[2].x;
731 case 2: feature[s->feature[1].i] += 2 * s->feature[1].x;
732 case 1: feature[s->feature[0].i] += 2 * s->feature[0].x;
733 case 0: break;
734 }
735 foreach_bit (x, f) {
736 s = EVAL_X2F + x;
737 switch (s->n_feature) {
738 case 7: feature[s->feature[6].i] += s->feature[6].x;
739 case 6: feature[s->feature[5].i] += s->feature[5].x;
740 case 5: feature[s->feature[4].i] += s->feature[4].x;
741 case 4: feature[s->feature[3].i] += s->feature[3].x;
742 case 3: feature[s->feature[2].i] += s->feature[2].x;
743 case 2: feature[s->feature[1].i] += s->feature[1].x;
744 case 1: feature[s->feature[0].i] += s->feature[0].x;
745 case 0: break;
746 }
747 }
748
749#endif
750
751}
752
753static void eval_restore_1(Eval *eval, const Move *move)
754{
755 const CoordinateToFeature *s = EVAL_X2F + move->x;
756 register int x;
757 int *feature = eval->feature;
758 unsigned long long f = move->flipped;
759
760#ifdef DEBUG
761
762 int i, j;
763 for (i = 0; i < s->n_feature; ++i) {
764 j = s->feature[i].i;
765 assert(0 <= j && j < EVAL_N_FEATURE);
766 feature[j] += s->feature[i].x;
767 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
768 }
769
770 foreach_bit (x, f) {
771 s = EVAL_X2F + x;
772 for (i = 0; i < s->n_feature; ++i) {
773 j = s->feature[i].i;
774 assert(0 <= j && j < EVAL_N_FEATURE);
775 feature[j] -= s->feature[i].x;
776 assert(EVAL_OFFSET[j] <= feature[j] && feature[j] <= EVAL_MAX_VALUE[j]);
777 }
778 }
779
780#else
781
782 switch (s->n_feature) {
783 case 7: feature[s->feature[6].i] += s->feature[6].x;
784 case 6: feature[s->feature[5].i] += s->feature[5].x;
785 case 5: feature[s->feature[4].i] += s->feature[4].x;
786 case 4: feature[s->feature[3].i] += s->feature[3].x;
787 case 3: feature[s->feature[2].i] += s->feature[2].x;
788 case 2: feature[s->feature[1].i] += s->feature[1].x;
789 case 1: feature[s->feature[0].i] += s->feature[0].x;
790 case 0: break;
791 }
792 foreach_bit (x, f) {
793 s = EVAL_X2F + x;
794 switch (s->n_feature) {
795 case 7: feature[s->feature[6].i] -= s->feature[6].x;
796 case 6: feature[s->feature[5].i] -= s->feature[5].x;
797 case 5: feature[s->feature[4].i] -= s->feature[4].x;
798 case 4: feature[s->feature[3].i] -= s->feature[3].x;
799 case 3: feature[s->feature[2].i] -= s->feature[2].x;
800 case 2: feature[s->feature[1].i] -= s->feature[1].x;
801 case 1: feature[s->feature[0].i] -= s->feature[0].x;
802 case 0: break;
803 }
804 }
805
806#endif
807
808}
809
810void eval_restore(Eval *eval, const Move *move)
811{
812 static void (*eval_restore_f[])(Eval*, const Move*) = {eval_restore_0, eval_restore_1};
813 assert(move->flipped);
814 eval_swap(eval);
815 assert(WHITE == eval->player || BLACK == eval->player);
816 eval_restore_f[eval->player](eval, move);
817}
818
824void eval_pass(Eval *eval)
825{
826 eval_swap(eval);
827}
828
829
843double eval_sigma(const int n_empty, const int depth, const int probcut_depth)
844{
845 double sigma;
846
847 sigma = EVAL_A * n_empty + EVAL_B * depth + EVAL_C * probcut_depth;
848 sigma = EVAL_a * sigma * sigma + EVAL_b * sigma + EVAL_c;
849
850 return sigma;
851}
852
unsigned int bswap_int(unsigned int i)
Mirror the unsigned int (little <-> big endian).
Definition bit.c:401
unsigned short bswap_short(unsigned short s)
Swap bytes of a short (little <-> big endian).
Definition bit.c:391
#define foreach_bit(i, b)
Definition bit.h:39
int board_get_square_color(const Board *board, const int x)
Get square color.
Definition board.c:1168
#define XADE
Definition const.h:93
#define LAVE
Definition const.h:94
#define EVAL
Definition const.h:92
#define EDAX
Definition const.h:91
@ B3
Definition const.h:31
@ D7
Definition const.h:35
@ B6
Definition const.h:34
@ F3
Definition const.h:31
@ D4
Definition const.h:32
@ NOMOVE
Definition const.h:37
@ E8
Definition const.h:36
@ E4
Definition const.h:32
@ E6
Definition const.h:34
@ C4
Definition const.h:32
@ A6
Definition const.h:34
@ F5
Definition const.h:33
@ C5
Definition const.h:33
@ D1
Definition const.h:29
@ H4
Definition const.h:32
@ G5
Definition const.h:33
@ B8
Definition const.h:36
@ E7
Definition const.h:35
@ E3
Definition const.h:31
@ A2
Definition const.h:30
@ A3
Definition const.h:31
@ C7
Definition const.h:35
@ H5
Definition const.h:33
@ B7
Definition const.h:35
@ F1
Definition const.h:29
@ C2
Definition const.h:30
@ C8
Definition const.h:36
@ G6
Definition const.h:34
@ E5
Definition const.h:33
@ B4
Definition const.h:32
@ F7
Definition const.h:35
@ A1
Definition const.h:29
@ A7
Definition const.h:35
@ H1
Definition const.h:29
@ D2
Definition const.h:30
@ F4
Definition const.h:32
@ G3
Definition const.h:31
@ B2
Definition const.h:30
@ D5
Definition const.h:33
@ G8
Definition const.h:36
@ F6
Definition const.h:34
@ F2
Definition const.h:30
@ H6
Definition const.h:34
@ A5
Definition const.h:33
@ G1
Definition const.h:29
@ E1
Definition const.h:29
@ H7
Definition const.h:35
@ H3
Definition const.h:31
@ D6
Definition const.h:34
@ H2
Definition const.h:30
@ F8
Definition const.h:36
@ E2
Definition const.h:30
@ B1
Definition const.h:29
@ A4
Definition const.h:32
@ C3
Definition const.h:31
@ H8
Definition const.h:36
@ D3
Definition const.h:31
@ C6
Definition const.h:34
@ G7
Definition const.h:35
@ C1
Definition const.h:29
@ D8
Definition const.h:36
@ G2
Definition const.h:30
@ G4
Definition const.h:32
@ B5
Definition const.h:33
@ A8
Definition const.h:36
@ WHITE
Definition const.h:43
@ BLACK
Definition const.h:42
void version(void)
Print version & copyright.
Definition main.c:27
static double EVAL_c
Definition eval.c:234
static const int EVAL_SIZE[]
Definition eval.c:170
static const int EVAL_N_PLY
Definition eval.c:222
void eval_free(Eval *eval)
Free resources used by the evaluation function.
Definition eval.c:519
struct FeatureToCoordinate FeatureToCoordinate
void eval_close(void)
Free global resources allocated to the evaluation function.
Definition eval.c:494
static const int EVAL_N_FEATURE
Definition eval.c:225
static void eval_update_1(Eval *eval, const Move *move)
Update the features after a player's move.
Definition eval.c:625
struct CoordinateToFeature CoordinateToFeature
static int EVAL_S6[2][729]
Definition eval.c:214
static int opponent_feature(int l, int d)
Opponent feature.
Definition eval.c:245
void eval_restore(Eval *eval, const Move *move)
Definition eval.c:810
static int EVAL_S4[2][81]
Definition eval.c:216
static const CoordinateToFeature EVAL_X2F[]
Definition eval.c:101
void eval_pass(Eval *eval)
Update/Restore the features after a passing move.
Definition eval.c:824
static int EVAL_S7[2][2187]
Definition eval.c:213
static void eval_update_0(Eval *eval, const Move *move)
Update the features after a player's move.
Definition eval.c:562
static const int EVAL_N_WEIGHT
Definition eval.c:219
static int EVAL_S5[2][243]
Definition eval.c:215
void eval_update(Eval *eval, const Move *move)
Definition eval.c:681
static const int EVAL_PACKED_SIZE[]
Definition eval.c:173
void eval_open(const char *file)
Load the evaluation function features' weights.
Definition eval.c:265
static void eval_restore_0(Eval *eval, const Move *move)
Restore the features as before a player's move.
Definition eval.c:696
void eval_init(Eval *eval)
Initialize a new evaluation function.
Definition eval.c:509
static double EVAL_A
Definition eval.c:234
static int EVAL_LOADED
Definition eval.c:228
static int EVAL_C10[2][59049]
Definition eval.c:209
static double EVAL_b
Definition eval.c:234
static int EVAL_S8[2][6561]
Definition eval.c:212
static void eval_swap(Eval *eval)
Swap player's feature.
Definition eval.c:550
static double EVAL_C
Definition eval.c:234
void eval_set(Eval *eval, const Board *board)
Set up evaluation features from a board.
Definition eval.c:530
static double EVAL_a
Definition eval.c:234
static void eval_restore_1(Eval *eval, const Move *move)
Definition eval.c:753
static const FeatureToCoordinate EVAL_F2X[]
Definition eval.c:38
double eval_sigma(const int n_empty, const int depth, const int probcut_depth)
Compute the error-type of the evaluation function according to the depths.
Definition eval.c:843
short *** EVAL_WEIGHT
Definition eval.c:231
static int EVAL_C9[2][19683]
Definition eval.c:211
static const int EVAL_MAX_VALUE[]
Definition eval.c:192
static int EVAL_S10[2][59049]
Definition eval.c:210
static const int EVAL_OFFSET[]
Definition eval.c:176
static double EVAL_B
Definition eval.c:234
Definition board.h:26
Definition eval.c:23
struct CoordinateToFeature::@8 feature[16]
int i
Definition eval.c:26
int n_feature
Definition eval.c:24
int x
Definition eval.c:27
evaluation function
Definition eval.h:18
int player
Definition eval.h:20
int * feature
Definition eval.h:19
Definition eval.c:32
int x[16]
Definition eval.c:34
int n_square
Definition eval.c:33
Definition move.h:20
unsigned long long flipped
Definition move.h:21
int x
Definition move.h:22
Miscellaneous utilities header.
#define fatal_error(...)
Display an error message as "FATAL_ERROR : file name : function name : line number : ....
Definition util.h:349
#define info(...)
Display a message.
Definition util.h:382