aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/coord/solve.h
blob: fb996ac24423986639539a68d9b587298fea42f1 (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
typedef struct {
	cube_t cube;
	cube_t inverse;
	uint8_t target_depth;
	solution_moves_t *solution_moves;
	solution_settings_t *solution_settings;
	solution_list_t *solution_list;
	uint8_t nissflag;
	bool lastisnormal;
	coord_t *coord;
	const unsigned char *coord_data;
	const unsigned char *ptable;
} dfsarg_solve_coord_t;

STATIC int64_t solve_coord(cube_t, coord_t [static 1], uint8_t, uint8_t,
    uint8_t, uint8_t, uint64_t, uint8_t, uint8_t, uint64_t,
    const unsigned char *, size_t n, char [n]);
STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t,
    uint8_t, uint64_t, uint8_t, uint8_t, uint64_t, const unsigned char *,
    size_t n, char [n]);
STATIC bool coord_solution_admissible(const dfsarg_solve_coord_t [static 1]);
STATIC bool solve_coord_dfs_stop(const dfsarg_solve_coord_t [static 1]);
STATIC bool coord_continue_onnormal(const dfsarg_solve_coord_t [static 1]);
STATIC bool coord_continue_oninverse(const dfsarg_solve_coord_t [static 1]);
STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t [static 1]);

STATIC bool
coord_solution_admissible(const dfsarg_solve_coord_t arg[static 1])
{
	uint8_t n;

	n = arg->solution_moves->nmoves + arg->solution_moves->npremoves;
	if (arg->target_depth != n)
		return false;

	return arg->coord->is_admissible == NULL ||
	    arg->coord->is_admissible(arg->solution_moves);
}

STATIC bool
solve_coord_dfs_stop(const dfsarg_solve_coord_t arg[static 1])
{
	bool hasnissed;
	uint8_t n, pval;
	uint64_t coord;
	const cube_t *c;

	n = arg->solution_moves->nmoves + arg->solution_moves->npremoves;
	if (n >= arg->target_depth)
		return true;

	hasnissed = arg->solution_moves->nmoves > 0 &&
	    arg->solution_moves->npremoves > 0;
	if (!hasnissed && (arg->nissflag & NISSY_NISSFLAG_MIXED))
		return false;

	c = arg->lastisnormal ? &arg->cube : &arg->inverse;

	coord = arg->coord->coord(*c, arg->coord_data);
	pval = get_coord_pval(arg->coord, arg->ptable, coord);

	return n + pval > arg->target_depth;
}

STATIC bool
coord_continue_onnormal(const dfsarg_solve_coord_t arg[static 1])
{
	uint8_t f, nn, ni, th;

	f = arg->nissflag;
	nn = arg->solution_moves->nmoves;
	ni = arg->solution_moves->npremoves;
	th = DIV_ROUND_UP(arg->target_depth, 2);

	if (nn + ni == 0)
		return f & (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_MIXED);

	if (arg->lastisnormal)
		return (f & NISSY_NISSFLAG_NORMAL) ||
		    ((f & NISSY_NISSFLAG_MIXED) && (ni > 0 || nn <= th));

	return (f & NISSY_NISSFLAG_MIXED) && nn == 0 && ni < th &&
	    coord_can_switch(arg->coord, arg->coord_data,
	        ni, arg->solution_moves->premoves);
}

STATIC bool
coord_continue_oninverse(const dfsarg_solve_coord_t arg[static 1])
{
	uint8_t f, nn, ni, th;

	f = arg->nissflag;
	nn = arg->solution_moves->nmoves;
	ni = arg->solution_moves->npremoves;
	th = DIV_ROUND_UP(arg->target_depth, 2);

	if (nn + ni == 0)
		return f & (NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED);

	if (!arg->lastisnormal)
		return (f & NISSY_NISSFLAG_INVERSE) ||
		    ((f & NISSY_NISSFLAG_MIXED) && (nn > 0 || ni < th));

	return (f & NISSY_NISSFLAG_MIXED) && ni == 0 && nn <= th &&
	    coord_can_switch(arg->coord, arg->coord_data,
	        nn, arg->solution_moves->moves);
}

STATIC int64_t
solve_coord_dfs(dfsarg_solve_coord_t arg[static 1])
{
	bool lastbackup;
	uint8_t m, l, nnbackup, nibackup;
	uint32_t mm;
	uint64_t coord;
	int64_t n, ret;
	cube_t backup_cube, backup_inverse;

	coord = arg->coord->coord(arg->cube, arg->coord_data);
	if (coord == 0) {
		if (!coord_solution_admissible(arg))
			return 0;
		return appendsolution(arg->solution_moves,
		    arg->solution_settings, arg->solution_list, true,
		    arg->coord->name);
	}

	if (solve_coord_dfs_stop(arg))
		return 0;

	backup_cube = arg->cube;
	backup_inverse = arg->inverse;
	lastbackup = arg->lastisnormal;
	nnbackup = arg->solution_moves->nmoves;
	nibackup = arg->solution_moves->npremoves;

	ret = 0;
	if (coord_continue_onnormal(arg)) {
		l = arg->solution_moves->nmoves;
		mm = arg->coord->moves_mask;
		if (l != 0) {
			m = arg->solution_moves->moves[l-1];
			mm &= allowedmask[movebase(m)];
		}
		arg->solution_moves->nmoves++;
		arg->lastisnormal = true;

		for (m = 0; m < NMOVES; m++) {
			if (!(mm & (UINT32_C(1) << (uint32_t)m)))
				continue;

			arg->solution_moves->moves[l] = m;
			arg->cube = move(backup_cube, m);
			arg->inverse = premove(backup_inverse, m);
			n = solve_coord_dfs(arg);
			if (n < 0)
				return n;
			ret += n;
			arg->solution_moves->npremoves = nibackup;
		}

		arg->solution_moves->nmoves--;
	}

	arg->lastisnormal = lastbackup;

	if (coord_continue_oninverse(arg)) {
		l = arg->solution_moves->npremoves;
		mm = arg->coord->moves_mask;
		if (l != 0) {
			m = arg->solution_moves->premoves[l-1];
			mm &= allowedmask[movebase(m)];
		}
		arg->solution_moves->npremoves++;
		arg->lastisnormal = false;
		
		for (m = 0; m < NMOVES; m++) {
			if (!(mm & (UINT32_C(1) << (uint32_t)m)))
				continue;

			arg->solution_moves->premoves[l] = m;
			arg->inverse = move(backup_inverse, m);
			arg->cube = premove(backup_cube, m);
			n = solve_coord_dfs(arg);
			if (n < 0)
				return n;
			ret += n;
			arg->solution_moves->nmoves = nnbackup;
		}

		arg->solution_moves->npremoves--;
	}

	arg->cube = backup_cube;
	arg->inverse = backup_inverse;
	arg->lastisnormal = lastbackup;

	return ret;
}

STATIC int64_t
solve_coord_dispatch(
	cube_t cube,
	const char *coord_and_axis,
	uint8_t nissflag,
	uint8_t minmoves,
	uint8_t maxmoves,
	uint64_t maxsolutions,
	uint8_t optimal,
	uint8_t threads,
	uint64_t data_size,
	const unsigned char *data,
	size_t solutions_size,
	char sols[solutions_size]
)
{
	coord_t *coord;
	uint8_t axis;

	parse_coord_and_axis(
	    strlen(coord_and_axis), coord_and_axis, &coord, &axis);

	if (coord == NULL) {
		LOG("Error: could not parse coordinate from '%s'\n",
		    coord_and_axis);
		return NISSY_ERROR_INVALID_SOLVER;
	}

	if (axis == UINT8_ERROR) {
		LOG("Error: could not parse axis from '%s'\n", coord_and_axis);
		return NISSY_ERROR_INVALID_SOLVER;
	}

	return solve_coord(cube, coord, axis, nissflag, minmoves, maxmoves,
	    maxsolutions, optimal, threads, data_size, data,
	    solutions_size, sols);
}

STATIC int64_t
solve_coord(
	cube_t cube,
	coord_t coord [static 1],
	uint8_t axis,
	uint8_t nissflag,
	uint8_t minmoves,
	uint8_t maxmoves,
	uint64_t maxsolutions,
	uint8_t optimal,
	uint8_t threads,
	uint64_t data_size,
	const unsigned char *data,
	size_t solutions_size,
	char sols[solutions_size]
)
{
	int8_t d;
	uint8_t t;
	int64_t ndepth;
	cube_t c;
	const unsigned char *coord_data;
	const unsigned char *ptable;
	dfsarg_solve_coord_t arg;
	tableinfo_t info;
	solution_moves_t solution_moves;
	solution_settings_t solution_settings;
	solution_list_t solution_list;

	t = coord->axistrans[axis];
	c = transform(cube, t);

	if (!coord->is_solvable(c))
		goto solve_coord_error_unsolvable;

	if (!solution_list_init(&solution_list, solutions_size, sols))
		goto solve_coord_error_buffer;

	if (readtableinfo(data_size, data, &info) != NISSY_OK)
		goto solve_coord_error_data;

	if (info.type == TABLETYPE_PRUNING) {
		/* Only the pruning table */
		coord_data = NULL;
		ptable = data + INFOSIZE;
	} else {
		/* Coordinate has extra data */
		coord_data = data + INFOSIZE;
		ptable = data + info.next + INFOSIZE;
	}

	solution_moves_reset(&solution_moves);

	solution_settings = (solution_settings_t) {
		.tmask = TM_SINGLE(inverse_trans(t)),
		.unniss = false,
		.maxmoves = maxmoves,
		.maxsolutions = maxsolutions,
		.optimal = optimal,
	};

	arg = (dfsarg_solve_coord_t) {
		.cube = c,
		.inverse = inverse(c),
		.coord = coord,
		.coord_data = coord_data,
		.ptable = ptable,
		.solution_moves = &solution_moves,
		.solution_settings = &solution_settings,
		.solution_list = &solution_list,
		.nissflag = nissflag,

		/*
		Since no move has been done yet, this field should be
		neither true nor false; using its value now is logically
		undefined behavior.
		TODO: find a more elegant solution
		*/
		.lastisnormal = true,
	};

	if (coord->coord(c, coord_data) == 0) {
		if (minmoves == 0 && !appendsolution(&solution_moves,
		    &solution_settings, &solution_list, true, coord->name))
				goto solve_coord_error_buffer;
		goto solve_coord_done;
	}

	for (
	    d = MAX(minmoves, 1);
	    !solutions_done(&solution_list, &solution_settings, d);
	    d++
	) {
		if (d >= 12)
			LOG("[%s solve] Found %" PRIu64 " solutions, "
			    "searching at depth %" PRId8 "\n",
			    coord->name, solution_list.nsols, d);

		arg.target_depth = d;
		solution_moves_reset(arg.solution_moves);
		ndepth = solve_coord_dfs(&arg);

		if (ndepth < 0)
			return ndepth;

		solution_list.nsols += (uint64_t)ndepth;
	}

solve_coord_done:
	return (int64_t)solution_list.nsols;

solve_coord_error_data:
	LOG("[%s solve] Error reading table\n", coord->name);
	return NISSY_ERROR_DATA;

solve_coord_error_buffer:
	LOG("[%s solve] Error appending solution to buffer: size too small\n",
	    coord->name);
	return NISSY_ERROR_BUFFER_SIZE;

solve_coord_error_unsolvable:
	LOG("[%s solve] Error: cube not ready\n", coord->name);
	return NISSY_ERROR_UNSOLVABLE_CUBE;
}

Generated with cgit - Back to sebastiano.tronto.net