aboutsummaryrefslogtreecommitdiff
path: root/src/pruning.c
blob: ca6f57a1ebf10def56bbf642aeda4a4f7faf6da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "pruning.h"

/*
 * The commented functions are a way to generate a pruning table
 * without using anti-indexes. It does not matter too much because
 * we still need anti-indexes in gensym.
 */
/*
static bool        dfs_get_visited(PruneData *pd, DfsData *dd, Cube c);
static bool        dfs_get_visited_index(DfsData *dd, uint64_t ind);
static void        dfs_set_visited(PruneData *pd, DfsData *dd, Cube c, bool b);
static void        dfs_set_visited_index(DfsData *dd, uint64_t ind, bool b);
static void        genptable_dfs(Cube c, PruneData *pd, DfsData *dd);
*/

static void        genptable_bfs(PruneData *pd, int d, Move *ms);
static void        genptable_branch(PruneData *pd,uint64_t ind,int d,Move *ms);
static void        ptable_update(PruneData *pd, Cube cube, int m);
static void        ptable_update_index(PruneData *pd, uint64_t ind, int m);
static int         ptableval_index(PruneData *pd, uint64_t ind);
static bool        read_ptable_file(PruneData *pd);
static bool        write_ptable_file(PruneData *pd);

PruneData
pd_eofb_HTM = {
	.filename = "pt_eofb_HTM",
	.coord    = &coord_eofb,
	.moveset  = moveset_HTM,
};

PruneData
pd_coud_HTM = {
	.filename = "pt_coud_HTM",
	.coord    = &coord_coud,
	.moveset  = moveset_HTM,
};

PruneData
pd_cornershtr_HTM = {
	.filename = "pt_cornershtr_HTM",
	.coord    = &coord_cornershtr,
	.moveset  = moveset_HTM,
};

PruneData
pd_corners_HTM = {
	.filename = "pt_corners_HTM",
	.coord    = &coord_corners,
	.moveset  = moveset_HTM,
};

PruneData
pd_drud_sym16_HTM = {
	.filename = "pt_drud_sym16_HTM",
	.coord    = &coord_drud_sym16,
	.moveset  = moveset_HTM,
};

PruneData
pd_drud_eofb = {
	.filename = "pt_drud_eofb",
	.coord    = &coord_drud_eofb,
	.moveset  = moveset_eofb,
};

PruneData
pd_drudfin_noE_sym16_drud = {
	.filename = "pt_drudfin_noE_sym16_drud",
	.coord    = &coord_drudfin_noE_sym16,
	.moveset  = moveset_drud,
};

PruneData
pd_htr_drud = {
	.filename = "pt_htr_drud",
	.coord    = &coord_htr_drud,
	.moveset  = moveset_drud,
};

PruneData
pd_htrfin_htr = {
	.filename = "pt_htrfin_htr",
	.coord    = &coord_htrfin,
	.moveset  = moveset_htr,
};

PruneData
pd_khuge_HTM = {
	.filename = "pt_khuge_HTM",
	.coord    = &coord_khuge,
	.moveset  = moveset_HTM,
};

/*
void
genptable(PruneData *pd)
{
	Move ms[NMOVES];
	uint64_t j, oldn;
	DfsData dd;

	if (pd->generated)
		return;

	pd->ptable = malloc(ptablesize(pd) * sizeof(uint8_t));

	if (read_ptable_file(pd)) {
		pd->generated = true;
		return;
	}
	pd->generated = true;

	fprintf(stderr, "Cannot load %s, generating it\n", pd->filename); 

	moveset_to_list(pd->moveset, ms);

	for (j = 0; j < pd->coord->max; j++)
		ptable_update_index(pd, j, 15);

	dd = (DfsData) { .m = 0 };
	dd.visited = malloc((ptablesize(pd)/4 + 1) * sizeof(uint8_t));
	moveset_to_list(pd->moveset, dd.sorted_moves);
	oldn = 0;
	pd->n = 0;

	for (dd.d = 0; dd.d < 15 && pd->n < pd->coord->max; dd.d++) {
		for (j = 0; j < pd->coord->max; j++)
			dfs_set_visited_index(&dd, j, false);
		genptable_dfs((Cube){0}, pd, &dd);
		fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
			dd.d+1, pd->n - oldn, pd->n, pd->coord->max);
		oldn = pd->n;
	}

	if (!write_ptable_file(pd))
		fprintf(stderr, "Error writing ptable file\n");

	free(dd.visited);
}
*/

void
genptable(PruneData *pd)
{
	Move ms[NMOVES];
	int d;
	uint64_t j, oldn;

	if (pd->generated)
		return;

	/* TODO: check if memory is enough, otherwise maybe exit gracefully? */
	pd->ptable = malloc(ptablesize(pd) * sizeof(uint8_t));

	if (read_ptable_file(pd)) {
		pd->generated = true;
		return;
	}
	pd->generated = true;

	fprintf(stderr, "Cannot load %s, generating it\n", pd->filename); 

	moveset_to_list(pd->moveset, ms);

	/* We use 4 bits per value, so any distance >= 15 is set to 15 */
	for (j = 0; j < pd->coord->max; j++)
		ptable_update_index(pd, j, 15);

	ptable_update(pd, (Cube){0}, 0);
	pd->n = 1;
	oldn = 0;
	fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
		0, pd->n - oldn, pd->n, pd->coord->max);
	oldn = 1;
	for (d = 0; d < 15 && pd->n < pd->coord->max; d++) {
		genptable_bfs(pd, d, ms);
		fprintf(stderr, "Depth %d done, generated %lu\t(%lu/%lu)\n",
			d+1, pd->n - oldn, pd->n, pd->coord->max);
		oldn = pd->n;
	}

	if (!write_ptable_file(pd))
		fprintf(stderr, "Error writing ptable file\n");

}

/*
static void
genptable_dfs(Cube c, PruneData *pd, DfsData *dd)
{
	int i, j, pv;
	Move mm;
	Cube cc;

	if (pd->coord->index(c) > 2*ptablesize(pd)) {
		printf("error! %lu > %lu\n", pd->coord->index(c), 2*ptablesize(pd));
		print_cube(c);
		exit(1);
	}
	pv = ptableval(pd, c);

	if (pv < dd->m || dd->m > dd->d)
		return;

	if (dfs_get_visited(pd, dd, c))
		return;
	dfs_set_visited(pd, dd, c, true);

	if (pv != dd->m)
		ptable_update(pd, c, dd->m);

	for (i = 0; i < pd->coord->ntrans; i++) {
		cc = i == 0 ? c :
			      apply_trans(pd->coord->trans[i], c);
		for (j = 0; dd->sorted_moves[j] != NULLMOVE; j++) {
			mm = dd->sorted_moves[j];
			dd->m++;
			genptable_dfs(apply_move(mm, cc), pd, dd);
			dd->m--;
		}
	}
}
*/

/*
static bool
dfs_get_visited(PruneData *pd, DfsData *dd, Cube c)
{
	return dfs_get_visited_index(dd, pd->coord->index(c));
}
*/

/*
static bool
dfs_get_visited_index(DfsData *dd, uint64_t ind)
{
	return dd->visited[ind/8] & ((uint8_t)1 << (ind % 8));
}
*/

/*
static void
dfs_set_visited(PruneData *pd, DfsData *dd, Cube c, bool b)
{
	dfs_set_visited_index(dd, pd->coord->index(c), b);
}
*/

/*
static void
dfs_set_visited_index(DfsData *dd, uint64_t ind, bool b)
{
	if (b)
		dd->visited[ind/8] |= ((uint8_t)1 << (ind % 8));
	else
		dd->visited[ind/8] &= ~((uint8_t)1 << (ind % 8));
}
*/

static void
genptable_bfs(PruneData *pd, int d, Move *ms)
{
	uint64_t i;

	for (i = 0; i < pd->coord->max; i++)
		if (ptableval_index(pd, i) == d)
			genptable_branch(pd, i, d, ms);
}

static void
genptable_branch(PruneData *pd, uint64_t ind, int d, Move *ms)
{
	int i, j;
	Cube ci, cc, c;

	ci = pd->coord->cube(ind);

	for (i = 0; i < pd->coord->ntrans; i++) {
		c = i == 0 ? ci :
			     apply_trans(pd->coord->trans[i], ci);
		for (j = 0; ms[j] != NULLMOVE; j++) {
			cc = apply_move(ms[j], c);
			if (ptableval(pd, cc) > d+1)
				ptable_update(pd, cc, d+1);
		}
	}
}

void
print_ptable(PruneData *pd)
{
	uint64_t i, a[16];
	
	for (i = 0; i < 16; i++)
		a[i] = 0;

	if (!pd->generated)
		genptable(pd);

	for (i = 0; i < pd->coord->max; i++)
		a[ptableval_index(pd, i)]++;
		
	fprintf(stderr, "Values for table %s\n", pd->filename);
	for (i = 0; i < 16; i++)
		printf("%2lu\t%10lu\n", i, a[i]);
}

uint64_t
ptablesize(PruneData *pd)
{
	return (pd->coord->max + 1) / 2;
}

static void
ptable_update(PruneData *pd, Cube cube, int n)
{
	uint64_t ind = pd->coord->index(cube);
	ptable_update_index(pd, ind, n);
}

static void
ptable_update_index(PruneData *pd, uint64_t ind, int n)
{
	uint8_t oldval2 = pd->ptable[ind/2];
	int other = (ind % 2) ? oldval2 % 16 : oldval2 / 16;

	pd->ptable[ind/2] = (ind % 2) ? 16*n + other : 16*other + n;
	pd->n++;
}

int
ptableval(PruneData *pd, Cube cube)
{
	return ptableval_index(pd, pd->coord->index(cube));
}

static int
ptableval_index(PruneData *pd, uint64_t ind)
{
	if (!pd->generated)
		genptable(pd);

	return (ind % 2) ? pd->ptable[ind/2] / 16 : pd->ptable[ind/2] % 16;
}

static bool
read_ptable_file(PruneData *pd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t r;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, pd->filename);

	if ((f = fopen(fname, "rb")) == NULL)
		return false;

	r = fread(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
	fclose(f);

	return r == ptablesize(pd);
}

static bool
write_ptable_file(PruneData *pd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t written;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, pd->filename);

	if ((f = fopen(fname, "wb")) == NULL)
		return false;

	written = fwrite(pd->ptable, sizeof(uint8_t), ptablesize(pd), f);
	fclose(f);

	return written == ptablesize(pd);
}

Generated with cgit - Back to sebastiano.tronto.net