aboutsummaryrefslogtreecommitdiff
path: root/old/2021-06-17-cachedata/steps.c
blob: f1001e685d3110cf5ea62764d868914966766b77 (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
#include "steps.h"

/* Check functions ***********************************************************/

static int              check_nothing(Cube cube);
static int              check_eofb_HTM(Cube cube);
static int              check_coud_HTM(Cube cube);
static int              check_coud_URF(Cube cube);
static int              check_corners_HTM(Cube cube);
static int              check_corners_URF(Cube cube);
static int              check_edges_HTM(Cube cube);
static int              check_drud_HTM(Cube cube);
static int              check_optimal_HTM(Cube cube);


/* Index functions ***********************************************************/

static uint64_t         index_eofb(Cube cube);
static uint64_t         index_coud(Cube cube);
static uint64_t         index_corners(Cube cube);
static uint64_t         index_ep(Cube cube);
static uint64_t         index_drud(Cube cube);


/* Steps *********************************************************************/

Step
eofb_HTM = {
	.check     = check_eofb_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM
};

Step
coud_HTM = {
	.check     = check_coud_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM
};

Step
coud_URF = {
	.check     = check_coud_URF,
	.ready     = check_nothing,
	.moveset   = moveset_URF
};

Step
corners_HTM = {
	.check     = check_corners_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM
};

Step
corners_URF = {
	.check     = check_corners_URF,
	.ready     = check_nothing,
	.moveset   = moveset_URF
};

Step
edges_HTM = {
	.check     = check_edges_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM
};

Step
drud_HTM = {
	.check     = check_drud_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM
};

Step
optimal_HTM = {
	.check     = check_optimal_HTM,
	.ready     = check_nothing,
	.moveset   = moveset_HTM,
	.cd        = &cd_optimal_HTM_6
};


/* Cache solutions ***********************************************************/

CacheData
cd_optimal_HTM_6 = {
	.filename  = "cd_optimal_HTM_6",
	.maxind0   = POW3TO7 * FACTORIAL8,
	.len       = 6,
	.nind      = 3,
	.index     = { index_corners, index_eofb, index_ep },
	.moveset   = moveset_HTM
};


/* Pruning tables ************************************************************/

PruneData
pd_eofb_HTM = {
	.filename = "ptable_eofb_HTM",
	.size     = POW2TO11,
	.index    = index_eofb,
	.moveset  = moveset_HTM
};

PruneData
pd_coud_HTM = {
	.filename = "ptable_coud_HTM",
	.size     = POW3TO7,
	.index    = index_coud,
	.moveset  = moveset_HTM
};

PruneData
pd_corners_HTM = {
	.filename = "ptable_corners_HTM",
	.size     = POW3TO7 * FACTORIAL8,
	.index    = index_corners,
	.moveset  = moveset_HTM
};

PruneData
pd_ep_HTM = {
	.filename = "ptable_ep_HTM",
	.size     = FACTORIAL12,
	.index    = index_ep,
	.moveset  = moveset_HTM
};

PruneData
pd_drud_HTM = {
	.filename = "ptable_drud_HTM",
	.size     = POW2TO11 * POW3TO7 * BINOM12ON4,
	.index    = index_drud,
	.moveset  = moveset_HTM
};


/* Check functions implementation ********************************************/

static int
check_nothing(Cube cube)
{
	/* At least we check that it is admissible */
	return is_admissible(cube);
}

static int
check_eofb_HTM(Cube cube)
{
	if (!pd_eofb_HTM.generated)
		generate_ptable(&pd_eofb_HTM);

	return PTABLEVAL(pd_eofb_HTM.ptable, cube.eofb);
}

static int
check_coud_HTM(Cube cube)
{
	if (!pd_coud_HTM.generated)
		generate_ptable(&pd_coud_HTM);

	return PTABLEVAL(pd_coud_HTM.ptable, cube.coud);
}

static int
check_coud_URF(Cube cube)
{
	/* TODO: I can improve this by checking first the orientation of
	 * the corner in DBL and use that as a reference */

	int ud = check_coud_HTM(cube);
	int rl = check_coud_HTM(apply_move(z, cube));
	int fb = check_coud_HTM(apply_move(x, cube));

	return MIN(ud, MIN(rl, fb));
}

static int
check_corners_HTM(Cube cube)
{
	if (!pd_corners_HTM.generated)
		generate_ptable(&pd_corners_HTM);

	return PTABLEVAL(pd_corners_HTM.ptable, index_corners(cube));
}

static int
check_corners_URF(Cube cube)
{
	/* TODO: I can improve this by checking first the corner in DBL
	 * and use that as a reference */

	int ret = 15;
	Trans i;

	for (i = 0; i < NROTATIONS; i++)
		ret = MIN(ret,check_corners_HTM(apply_alg(trans_alg(i),cube)));

	return ret;
}

static int
check_edges_HTM(Cube cube)
{
	int ret = 0;

	if (!pd_ep_HTM.generated)
		generate_ptable(&pd_ep_HTM);

	ret = MAX(ret, PTABLEVAL(pd_ep_HTM.ptable, index_ep(cube)));
	ret = MAX(ret, check_eofb_HTM(cube));
	ret = MAX(ret, check_eofb_HTM(apply_trans(ur, cube)));
	ret = MAX(ret, check_eofb_HTM(apply_trans(fd, cube)));

	return ret;
}

static int
check_drud_HTM(Cube cube)
{
	if (!pd_drud_HTM.generated)
		generate_ptable(&pd_drud_HTM);

	return PTABLEVAL(pd_drud_HTM.ptable, index_drud(cube));
}

static int
check_optimal_HTM(Cube cube)
{
	int dr1, dr2, dr3, drmax, cor; /*ep;*/

	if (!pd_drud_HTM.generated)
		generate_ptable(&pd_drud_HTM);
	if (!pd_corners_HTM.generated)
		generate_ptable(&pd_corners_HTM);
	/*
	 *if (!pd_ep_HTM.generated)
 	 *	generate_ptable(&pd_ep_HTM);
	 */

	dr1 = PTABLEVAL(pd_drud_HTM.ptable, index_drud(cube));
	dr2 = PTABLEVAL(pd_drud_HTM.ptable, index_drud(apply_trans(rf, cube)));
	dr3 = PTABLEVAL(pd_drud_HTM.ptable, index_drud(apply_trans(fd, cube)));

	drmax = MAX(dr1, MAX(dr2, dr3));
	if (dr1 == dr2 && dr2 == dr3 && dr1 != 0)
		drmax++;

	cor = PTABLEVAL(pd_corners_HTM.ptable, index_corners(cube));
	/* ep = PTABLEVAL(pd_ep_HTM.ptable, index_ep(cube)); */

	/*return MAX(drmax, MAX(ep, cor));*/
	if (drmax == 0 && cor == 0)
		return is_solved(cube, false) ? 0 : 1;
	return MAX(drmax, cor); 
}


/* Index functions implementation ********************************************/

static uint64_t
index_eofb(Cube cube)
{
	return cube.eofb;
}

static uint64_t
index_coud(Cube cube)
{
	return cube.coud;
}

static uint64_t
index_corners(Cube cube)
{
	return cube.coud * FACTORIAL8 + cube.cp;
}

static uint64_t
index_ep(Cube cube)
{
	uint64_t a, b, c;

	a = cube.eposs;
	b = (cube.epose % FACTORIAL4) + epos_dependent_cube(cube)*FACTORIAL4;
	c = cube.eposm % FACTORIAL4;

	b *= FACTORIAL4 * BINOM12ON4;
	c *= FACTORIAL4 * BINOM12ON4 * FACTORIAL4 * BINOM8ON4;

	return a + b + c;
}

static uint64_t
index_drud(Cube cube)
{
	uint64_t a, b, c;

	a = cube.eofb;
	b = cube.coud;
	c = cube.epose / FACTORIAL4;

	b *= POW2TO11;
	c *= POW2TO11 * POW3TO7;

	return a + b + c;
}

/* Movesets ******************************************************************/

bool
moveset_HTM(Move m)
{
	return m >= U && m <= B3;
}

bool
moveset_URF(Move m)
{
	Move b = base_move(m);

	return b == U || b == R || b == F;
}

Generated with cgit - Back to sebastiano.tronto.net