aboutsummaryrefslogtreecommitdiff
path: root/src/pruning.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/pruning.c271
1 files changed, 271 insertions, 0 deletions
diff --git a/src/pruning.c b/src/pruning.c
new file mode 100644
index 0000000..bc9dd59
--- /dev/null
+++ b/src/pruning.c
@@ -0,0 +1,271 @@
1#include "pruning.h"
2
3static void genptable_bfs(PruneData *pd, int d, Move *ms);
4static void genptable_branch(PruneData *pd, uint64_t i, int d, Move *m);
5static void ptable_update(PruneData *pd, Cube cube, int m);
6static void ptable_update_index(PruneData *pd, uint64_t ind, int m);
7static int ptableval_index(PruneData *pd, uint64_t ind);
8static bool read_ptable_file(PruneData *pd);
9static bool write_ptable_file(PruneData *pd);
10
11PruneData
12pd_eofb_HTM = {
13 .filename = "pt_eofb_HTM",
14 .coord = &coord_eofb,
15 .moveset = moveset_HTM,
16};
17
18PruneData
19pd_coud_HTM = {
20 .filename = "pt_coud_HTM",
21 .coord = &coord_coud,
22 .moveset = moveset_HTM,
23};
24
25PruneData
26pd_cornershtr_HTM = {
27 .filename = "pt_cornershtr_HTM",
28 .coord = &coord_cornershtr,
29 .moveset = moveset_HTM,
30};
31
32PruneData
33pd_corners_HTM = {
34 .filename = "pt_corners_HTM",
35 .coord = &coord_corners,
36 .moveset = moveset_HTM,
37};
38
39PruneData
40pd_drud_sym16_HTM = {
41 .filename = "pt_drud_sym16_HTM",
42 .coord = &coord_drud_sym16,
43 .moveset = moveset_HTM,
44};
45
46PruneData
47pd_drud_eofb = {
48 .filename = "pt_drud_eofb",
49 .coord = &coord_drud_eofb,
50 .moveset = moveset_eofb,
51};
52
53PruneData
54pd_drudfin_noE_sym16_drud = {
55 .filename = "pt_drudfin_noE_sym16_drud",
56 .coord = &coord_drudfin_noE_sym16,
57 .moveset = moveset_drud,
58};
59
60PruneData
61pd_htr_drud = {
62 .filename = "pt_htr_drud",
63 .coord = &coord_htr_drud,
64 .moveset = moveset_drud,
65};
66
67PruneData
68pd_htrfin_htr = {
69 .filename = "pt_htrfin_htr",
70 .coord = &coord_htrfin,
71 .moveset = moveset_htr,
72};
73
74PruneData
75pd_khuge_HTM = {
76 .filename = "pt_khuge_HTM",
77 .coord = &coord_khuge,
78 .moveset = moveset_HTM,
79};
80
81void
82genptable(PruneData *pd)
83{
84 Move ms[NMOVES];
85 int d;
86 uint64_t j, oldn;
87
88 if (pd->generated)
89 return;
90
91 /* TODO: check if memory is enough, otherwise maybe exit gracefully? */
92 pd->ptable = malloc(ptablesize(pd) * sizeof(uint8_t));
93
94 if (read_ptable_file(pd)) {
95 pd->generated = true;
96 return;
97 }
98 pd->generated = true;
99
100 fprintf(stderr, "Cannot load %s, generating it\n", pd->filename);
101
102 moveset_to_list(pd->moveset, ms);
103
104 /* We use 4 bits per value, so any distance >= 15 is set to 15 */
105 for (j = 0; j < pd->coord->max; j++)
106 ptable_update_index(pd, j, 15);
107
108 for (j = 0; j < pd->coord->max; j++)
109 if (ptableval_index(pd, j) != 15) {
110 printf("Error, non-max value at index %lu!\n", j);
111 break;
112 }
113 printf("Table set, ready to start\n");
114
115 ptable_update(pd, (Cube){0}, 0);
116 pd->n = 1;
117 oldn = 0;
118 fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
119 0, pd->n - oldn, pd->n, pd->coord->max);
120 oldn = 1;
121 for (d = 0; d < 15 && pd->n < pd->coord->max; d++) {
122 genptable_bfs(pd, d, ms);
123 fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
124 d+1, pd->n - oldn, pd->n, pd->coord->max);
125 oldn = pd->n;
126 }
127
128 if (!write_ptable_file(pd))
129 fprintf(stderr, "Error writing ptable file\n");
130}
131
132static void
133genptable_bfs(PruneData *pd, int d, Move *ms)
134{
135 uint64_t i;
136
137 for (i = 0; i < pd->coord->max; i++)
138 if (ptableval_index(pd, i) == d)
139 genptable_branch(pd, i, d, ms);
140}
141
142static void
143genptable_branch(PruneData *pd, uint64_t ind, int d, Move *ms)
144{
145 int i, j;
146 Cube ci, cc, c;
147
148 /*
149 * This is the only line of the whole program where we REALLY need an
150 * anti-indexer function. We could get rid of it if only we could save
151 * a cube object for each index value as we go, but then we would need
152 * an incredible amount of memory to generate each ptable: assuming
153 * fields in struct cube are 32 bit ints that would take 88 times the
154 * memory of the table to be generated, more than 120Gb for
155 * ptable_khuge for example!
156 */
157 ci = pd->coord->cube(ind);
158
159 for (i = 0; i < pd->coord->ntrans; i++) {
160 /* For simplicity trans[] is NULL when ntrans = 1 */
161 c = i == 0 ? ci :
162 apply_trans(pd->coord->trans[i], ci);
163 for (j = 0; ms[j] != NULLMOVE; j++) {
164 cc = apply_move(ms[j], c);
165 if (ptableval(pd, cc) > d+1)
166 ptable_update(pd, cc, d+1);
167 }
168 }
169}
170
171void
172print_ptable(PruneData *pd)
173{
174 uint64_t i, a[16];
175
176 for (i = 0; i < 16; i++)
177 a[i] = 0;
178
179 if (!pd->generated)
180 genptable(pd);
181
182 for (i = 0; i < pd->coord->max; i++)
183 a[ptableval_index(pd, i)]++;
184
185 fprintf(stderr, "Values for table %s\n", pd->filename);
186 for (i = 0; i < 16; i++)
187 printf("%2lu\t%10lu\n", i, a[i]);
188}
189
190uint64_t
191ptablesize(PruneData *pd)
192{
193 return (pd->coord->max + 1) / 2;
194}
195
196static void
197ptable_update(PruneData *pd, Cube cube, int n)
198{
199 uint64_t ind = pd->coord->index(cube);
200 ptable_update_index(pd, ind, n);
201}
202
203static void
204ptable_update_index(PruneData *pd, uint64_t ind, int n)
205{
206 uint8_t oldval2 = pd->ptable[ind/2];
207 int other = (ind % 2) ? oldval2 % 16 : oldval2 / 16;
208
209 pd->ptable[ind/2] = (ind % 2) ? 16*n + other : 16*other + n;
210 pd->n++;
211}
212
213int
214ptableval(PruneData *pd, Cube cube)
215{
216 return ptableval_index(pd, pd->coord->index(cube));
217}
218
219static int
220ptableval_index(PruneData *pd, uint64_t ind)
221{
222 if (!pd->generated)
223 genptable(pd);
224
225 return (ind % 2) ? pd->ptable[ind/2] / 16 : pd->ptable[ind/2] % 16;
226}
227
228static bool
229read_ptable_file(PruneData *pd)
230{
231 init_env();
232
233 FILE *f;
234 char fname[strlen(tabledir)+100];
235 uint64_t r;
236
237 strcpy(fname, tabledir);
238 strcat(fname, "/");
239 strcat(fname, pd->filename);
240
241 if ((f = fopen(fname, "rb")) == NULL)
242 return false;
243
244 r = fread(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
245 fclose(f);
246
247 return r == ptablesize(pd);
248}
249
250static bool
251write_ptable_file(PruneData *pd)
252{
253 init_env();
254
255 FILE *f;
256 char fname[strlen(tabledir)+100];
257 uint64_t written;
258
259 strcpy(fname, tabledir);
260 strcat(fname, "/");
261 strcat(fname, pd->filename);
262
263 if ((f = fopen(fname, "wb")) == NULL)
264 return false;
265
266 written = fwrite(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
267 fclose(f);
268
269 return written == ptablesize(pd);
270}
271

Generated with cgit - Back to sebastiano.tronto.net