aboutsummaryrefslogtreecommitdiff
path: root/old/2021-11-10-beforeremovingchecker/pruning.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--old/2021-11-10-beforeremovingchecker/pruning.c272
1 files changed, 272 insertions, 0 deletions
diff --git a/old/2021-11-10-beforeremovingchecker/pruning.c b/old/2021-11-10-beforeremovingchecker/pruning.c
new file mode 100644
index 0000000..86363f4
--- /dev/null
+++ b/old/2021-11-10-beforeremovingchecker/pruning.c
@@ -0,0 +1,272 @@
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_withcosets_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 /*TODO: change, set to 0 for every solved state (might be more than 1)*/
116 ptable_update(pd, (Cube){0}, 0);
117 pd->n = 1;
118 oldn = 0;
119 fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
120 0, pd->n - oldn, pd->n, pd->coord->max);
121 oldn = 1;
122 for (d = 0; d < 15 && pd->n < pd->coord->max; d++) {
123 genptable_bfs(pd, d, ms);
124 fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
125 d+1, pd->n - oldn, pd->n, pd->coord->max);
126 oldn = pd->n;
127 }
128
129 if (!write_ptable_file(pd))
130 fprintf(stderr, "Error writing ptable file\n");
131}
132
133static void
134genptable_bfs(PruneData *pd, int d, Move *ms)
135{
136 uint64_t i;
137
138 for (i = 0; i < pd->coord->max; i++)
139 if (ptableval_index(pd, i) == d)
140 genptable_branch(pd, i, d, ms);
141}
142
143static void
144genptable_branch(PruneData *pd, uint64_t ind, int d, Move *ms)
145{
146 int i, j;
147 Cube ci, cc, c;
148
149 /*
150 * This is the only line of the whole program where we REALLY need an
151 * anti-indexer function. We could get rid of it if only we could save
152 * a cube object for each index value as we go, but then we would need
153 * an incredible amount of memory to generate each ptable: assuming
154 * fields in struct cube are 32 bit ints that would take 88 times the
155 * memory of the table to be generated, more than 120Gb for
156 * ptable_khuge for example!
157 */
158 ci = pd->coord->cube(ind);
159
160 for (i = 0; i < pd->coord->ntrans; i++) {
161 /* For simplicity trans[] is NULL when ntrans = 1 */
162 c = i == 0 ? ci :
163 apply_trans(pd->coord->trans[i], ci);
164 for (j = 0; ms[j] != NULLMOVE; j++) {
165 cc = apply_move(ms[j], c);
166 if (ptableval(pd, cc) > d+1)
167 ptable_update(pd, cc, d+1);
168 }
169 }
170}
171
172void
173print_ptable(PruneData *pd)
174{
175 uint64_t i, a[16];
176
177 for (i = 0; i < 16; i++)
178 a[i] = 0;
179
180 if (!pd->generated)
181 genptable(pd);
182
183 for (i = 0; i < pd->coord->max; i++)
184 a[ptableval_index(pd, i)]++;
185
186 fprintf(stderr, "Values for table %s\n", pd->filename);
187 for (i = 0; i < 16; i++)
188 printf("%2lu\t%10lu\n", i, a[i]);
189}
190
191uint64_t
192ptablesize(PruneData *pd)
193{
194 return (pd->coord->max + 1) / 2;
195}
196
197static void
198ptable_update(PruneData *pd, Cube cube, int n)
199{
200 uint64_t ind = pd->coord->index(cube);
201 ptable_update_index(pd, ind, n);
202}
203
204static void
205ptable_update_index(PruneData *pd, uint64_t ind, int n)
206{
207 uint8_t oldval2 = pd->ptable[ind/2];
208 int other = (ind % 2) ? oldval2 % 16 : oldval2 / 16;
209
210 pd->ptable[ind/2] = (ind % 2) ? 16*n + other : 16*other + n;
211 pd->n++;
212}
213
214int
215ptableval(PruneData *pd, Cube cube)
216{
217 return ptableval_index(pd, pd->coord->index(cube));
218}
219
220static int
221ptableval_index(PruneData *pd, uint64_t ind)
222{
223 if (!pd->generated)
224 genptable(pd);
225
226 return (ind % 2) ? pd->ptable[ind/2] / 16 : pd->ptable[ind/2] % 16;
227}
228
229static bool
230read_ptable_file(PruneData *pd)
231{
232 init_env();
233
234 FILE *f;
235 char fname[strlen(tabledir)+100];
236 uint64_t r;
237
238 strcpy(fname, tabledir);
239 strcat(fname, "/");
240 strcat(fname, pd->filename);
241
242 if ((f = fopen(fname, "rb")) == NULL)
243 return false;
244
245 r = fread(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
246 fclose(f);
247
248 return r == ptablesize(pd);
249}
250
251static bool
252write_ptable_file(PruneData *pd)
253{
254 init_env();
255
256 FILE *f;
257 char fname[strlen(tabledir)+100];
258 uint64_t written;
259
260 strcpy(fname, tabledir);
261 strcat(fname, "/");
262 strcat(fname, pd->filename);
263
264 if ((f = fopen(fname, "wb")) == NULL)
265 return false;
266
267 written = fwrite(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
268 fclose(f);
269
270 return written == ptablesize(pd);
271}
272

Generated with cgit - Back to sebastiano.tronto.net