nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

solve.h (9182B)


      1 typedef struct {
      2 	cube_t cube;
      3 	cube_t inverse;
      4 	uint8_t target_depth;
      5 	solution_moves_t *solution_moves;
      6 	solution_settings_t *solution_settings;
      7 	solution_list_t *solution_list;
      8 	uint8_t nissflag;
      9 	bool lastisnormal;
     10 	coord_t *coord;
     11 	const unsigned char *coord_data;
     12 	const unsigned char *ptable;
     13 } dfsarg_solve_coord_t;
     14 
     15 STATIC int64_t solve_coord(oriented_cube_t, coord_t [static 1], uint8_t,
     16     uint8_t, uint8_t, uint8_t, uint64_t, uint8_t, uint8_t, uint64_t,
     17     const unsigned char *, size_t n, char [n]);
     18 STATIC int64_t solve_coord_dispatch(oriented_cube_t, const char *, uint8_t,
     19     uint8_t, uint8_t, uint64_t, uint8_t, uint8_t, uint64_t,
     20     const unsigned char *, size_t n, char [n]);
     21 STATIC bool coord_solution_admissible(const dfsarg_solve_coord_t [static 1]);
     22 STATIC bool solve_coord_dfs_stop(const dfsarg_solve_coord_t [static 1]);
     23 STATIC bool coord_continue_onnormal(const dfsarg_solve_coord_t [static 1]);
     24 STATIC bool coord_continue_oninverse(const dfsarg_solve_coord_t [static 1]);
     25 STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t [static 1]);
     26 
     27 STATIC bool
     28 coord_solution_admissible(const dfsarg_solve_coord_t arg[static 1])
     29 {
     30 	uint8_t n;
     31 
     32 	n = arg->solution_moves->nmoves + arg->solution_moves->npremoves;
     33 	if (arg->target_depth != n)
     34 		return false;
     35 
     36 	return arg->coord->is_admissible == NULL ||
     37 	    arg->coord->is_admissible(arg->solution_moves);
     38 }
     39 
     40 STATIC bool
     41 solve_coord_dfs_stop(const dfsarg_solve_coord_t arg[static 1])
     42 {
     43 	bool hasnissed;
     44 	uint8_t n, pval;
     45 	uint64_t coord;
     46 	const cube_t *c;
     47 
     48 	n = arg->solution_moves->nmoves + arg->solution_moves->npremoves;
     49 	if (n >= arg->target_depth)
     50 		return true;
     51 
     52 	hasnissed = arg->solution_moves->nmoves > 0 &&
     53 	    arg->solution_moves->npremoves > 0;
     54 	if (!hasnissed && (arg->nissflag & NISSY_NISSFLAG_MIXED))
     55 		return false;
     56 
     57 	c = arg->lastisnormal ? &arg->cube : &arg->inverse;
     58 
     59 	coord = arg->coord->coord(*c, arg->coord_data);
     60 	pval = get_coord_pval(arg->coord, arg->ptable, coord);
     61 
     62 	return n + pval > arg->target_depth;
     63 }
     64 
     65 STATIC bool
     66 coord_continue_onnormal(const dfsarg_solve_coord_t arg[static 1])
     67 {
     68 	uint8_t f, nn, ni, th;
     69 
     70 	f = arg->nissflag;
     71 	nn = arg->solution_moves->nmoves;
     72 	ni = arg->solution_moves->npremoves;
     73 	th = DIV_ROUND_UP(arg->target_depth, 2);
     74 
     75 	if (nn + ni == 0)
     76 		return f & (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_MIXED);
     77 
     78 	if (arg->lastisnormal)
     79 		return (f & NISSY_NISSFLAG_NORMAL) ||
     80 		    ((f & NISSY_NISSFLAG_MIXED) && (ni > 0 || nn <= th));
     81 
     82 	return (f & NISSY_NISSFLAG_MIXED) && nn == 0 && ni < th &&
     83 	    coord_can_switch(arg->coord, arg->coord_data,
     84 	        ni, arg->solution_moves->premoves);
     85 }
     86 
     87 STATIC bool
     88 coord_continue_oninverse(const dfsarg_solve_coord_t arg[static 1])
     89 {
     90 	uint8_t f, nn, ni, th;
     91 
     92 	f = arg->nissflag;
     93 	nn = arg->solution_moves->nmoves;
     94 	ni = arg->solution_moves->npremoves;
     95 	th = DIV_ROUND_UP(arg->target_depth, 2);
     96 
     97 	if (nn + ni == 0)
     98 		return f & (NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED);
     99 
    100 	if (!arg->lastisnormal)
    101 		return (f & NISSY_NISSFLAG_INVERSE) ||
    102 		    ((f & NISSY_NISSFLAG_MIXED) && (nn > 0 || ni < th));
    103 
    104 	return (f & NISSY_NISSFLAG_MIXED) && ni == 0 && nn <= th &&
    105 	    coord_can_switch(arg->coord, arg->coord_data,
    106 	        nn, arg->solution_moves->moves);
    107 }
    108 
    109 STATIC int64_t
    110 solve_coord_dfs(dfsarg_solve_coord_t arg[static 1])
    111 {
    112 	bool lastbackup;
    113 	uint8_t m, l, nnbackup, nibackup;
    114 	uint32_t mm;
    115 	uint64_t coord;
    116 	int64_t n, ret;
    117 	cube_t backup_cube, backup_inverse;
    118 
    119 	coord = arg->coord->coord(arg->cube, arg->coord_data);
    120 	if (coord == 0) {
    121 		if (!coord_solution_admissible(arg))
    122 			return 0;
    123 		return appendsolution(arg->solution_moves,
    124 		    arg->solution_settings, arg->solution_list, true,
    125 		    arg->coord->name);
    126 	}
    127 
    128 	if (solve_coord_dfs_stop(arg))
    129 		return 0;
    130 
    131 	backup_cube = arg->cube;
    132 	backup_inverse = arg->inverse;
    133 	lastbackup = arg->lastisnormal;
    134 	nnbackup = arg->solution_moves->nmoves;
    135 	nibackup = arg->solution_moves->npremoves;
    136 
    137 	ret = 0;
    138 	if (coord_continue_onnormal(arg)) {
    139 		l = arg->solution_moves->nmoves;
    140 		mm = arg->coord->moves_mask;
    141 		if (l != 0) {
    142 			m = arg->solution_moves->moves[l-1];
    143 			mm &= allowedmask[movebase(m)];
    144 		}
    145 		arg->solution_moves->nmoves++;
    146 		arg->lastisnormal = true;
    147 
    148 		for (m = 0; m < NMOVES; m++) {
    149 			if (!(mm & (UINT32_C(1) << (uint32_t)m)))
    150 				continue;
    151 
    152 			arg->solution_moves->moves[l] = m;
    153 			arg->cube = move(backup_cube, m);
    154 			arg->inverse = premove(backup_inverse, m);
    155 			n = solve_coord_dfs(arg);
    156 			if (n < 0)
    157 				return n;
    158 			ret += n;
    159 			arg->solution_moves->npremoves = nibackup;
    160 		}
    161 
    162 		arg->solution_moves->nmoves--;
    163 	}
    164 
    165 	arg->lastisnormal = lastbackup;
    166 
    167 	if (coord_continue_oninverse(arg)) {
    168 		l = arg->solution_moves->npremoves;
    169 		mm = arg->coord->moves_mask;
    170 		if (l != 0) {
    171 			m = arg->solution_moves->premoves[l-1];
    172 			mm &= allowedmask[movebase(m)];
    173 		}
    174 		arg->solution_moves->npremoves++;
    175 		arg->lastisnormal = false;
    176 		
    177 		for (m = 0; m < NMOVES; m++) {
    178 			if (!(mm & (UINT32_C(1) << (uint32_t)m)))
    179 				continue;
    180 
    181 			arg->solution_moves->premoves[l] = m;
    182 			arg->inverse = move(backup_inverse, m);
    183 			arg->cube = premove(backup_cube, m);
    184 			n = solve_coord_dfs(arg);
    185 			if (n < 0)
    186 				return n;
    187 			ret += n;
    188 			arg->solution_moves->nmoves = nnbackup;
    189 		}
    190 
    191 		arg->solution_moves->npremoves--;
    192 	}
    193 
    194 	arg->cube = backup_cube;
    195 	arg->inverse = backup_inverse;
    196 	arg->lastisnormal = lastbackup;
    197 
    198 	return ret;
    199 }
    200 
    201 STATIC int64_t
    202 solve_coord_dispatch(
    203 	oriented_cube_t oc,
    204 	const char *coord_and_axis,
    205 	uint8_t nissflag,
    206 	uint8_t minmoves,
    207 	uint8_t maxmoves,
    208 	uint64_t maxsolutions,
    209 	uint8_t optimal,
    210 	uint8_t threads,
    211 	uint64_t data_size,
    212 	const unsigned char *data,
    213 	size_t solutions_size,
    214 	char sols[solutions_size]
    215 )
    216 {
    217 	coord_t *coord;
    218 	uint8_t axis;
    219 
    220 	parse_coord_and_axis(
    221 	    strlen(coord_and_axis), coord_and_axis, &coord, &axis);
    222 
    223 	if (coord == NULL) {
    224 		LOG("Error: could not parse coordinate from '%s'\n",
    225 		    coord_and_axis);
    226 		return NISSY_ERROR_INVALID_SOLVER;
    227 	}
    228 
    229 	if (axis == UINT8_ERROR) {
    230 		LOG("Error: could not parse axis from '%s'\n", coord_and_axis);
    231 		return NISSY_ERROR_INVALID_SOLVER;
    232 	}
    233 
    234 	return solve_coord(oc, coord, axis, nissflag, minmoves, maxmoves,
    235 	    maxsolutions, optimal, threads, data_size, data,
    236 	    solutions_size, sols);
    237 }
    238 
    239 STATIC int64_t
    240 solve_coord(
    241 	oriented_cube_t oc,
    242 	coord_t coord [static 1],
    243 	uint8_t axis,
    244 	uint8_t nissflag,
    245 	uint8_t minmoves,
    246 	uint8_t maxmoves,
    247 	uint64_t maxsolutions,
    248 	uint8_t optimal,
    249 	uint8_t threads,
    250 	uint64_t data_size,
    251 	const unsigned char *data,
    252 	size_t solutions_size,
    253 	char sols[solutions_size]
    254 )
    255 {
    256 	int8_t d;
    257 	uint8_t t;
    258 	int64_t ndepth;
    259 	cube_t c;
    260 	const unsigned char *coord_data;
    261 	const unsigned char *ptable;
    262 	dfsarg_solve_coord_t arg;
    263 	tableinfo_t info;
    264 	solution_moves_t solution_moves;
    265 	solution_settings_t solution_settings;
    266 	solution_list_t solution_list;
    267 
    268 	t = coord->axistrans[axis];
    269 	c = transform(oc.cube, t);
    270 
    271 	if (!coord->is_solvable(c))
    272 		goto solve_coord_error_unsolvable;
    273 
    274 	if (!solution_list_init(&solution_list, solutions_size, sols))
    275 		goto solve_coord_error_buffer;
    276 
    277 	if (readtableinfo(data_size, data, &info) != NISSY_OK)
    278 		goto solve_coord_error_data;
    279 
    280 	if (info.type == TABLETYPE_PRUNING) {
    281 		/* Only the pruning table */
    282 		coord_data = NULL;
    283 		ptable = data + INFOSIZE;
    284 	} else {
    285 		/* Coordinate has extra data */
    286 		coord_data = data + INFOSIZE;
    287 		ptable = data + info.next + INFOSIZE;
    288 	}
    289 
    290 	solution_moves_reset(&solution_moves);
    291 
    292 	solution_settings = (solution_settings_t) {
    293 		.tmask = TM_SINGLE(inverse_trans(t)),
    294 		.unniss = false,
    295 		.maxmoves = maxmoves,
    296 		.maxsolutions = maxsolutions,
    297 		.optimal = optimal,
    298 		.orientation = oc.orientation,
    299 	};
    300 
    301 	arg = (dfsarg_solve_coord_t) {
    302 		.cube = c,
    303 		.inverse = inverse(c),
    304 		.coord = coord,
    305 		.coord_data = coord_data,
    306 		.ptable = ptable,
    307 		.solution_moves = &solution_moves,
    308 		.solution_settings = &solution_settings,
    309 		.solution_list = &solution_list,
    310 		.nissflag = nissflag,
    311 
    312 		/*
    313 		Since no move has been done yet, this field should be
    314 		neither true nor false; using its value now is logically
    315 		undefined behavior.
    316 		TODO: find a more elegant solution
    317 		*/
    318 		.lastisnormal = true,
    319 	};
    320 
    321 	if (coord->coord(c, coord_data) == 0) {
    322 		if (minmoves == 0 && !appendsolution(&solution_moves,
    323 		    &solution_settings, &solution_list, true, coord->name))
    324 				goto solve_coord_error_buffer;
    325 		goto solve_coord_done;
    326 	}
    327 
    328 	for (
    329 	    d = MAX(minmoves, 1);
    330 	    !solutions_done(&solution_list, &solution_settings, d);
    331 	    d++
    332 	) {
    333 		if (d >= 12)
    334 			LOG("[%s solve] Found %" PRIu64 " solutions, "
    335 			    "searching at depth %" PRId8 "\n",
    336 			    coord->name, solution_list.nsols, d);
    337 
    338 		arg.target_depth = d;
    339 		solution_moves_reset(arg.solution_moves);
    340 		ndepth = solve_coord_dfs(&arg);
    341 
    342 		if (ndepth < 0)
    343 			return ndepth;
    344 
    345 		solution_list.nsols += (uint64_t)ndepth;
    346 	}
    347 
    348 solve_coord_done:
    349 	return (int64_t)solution_list.nsols;
    350 
    351 solve_coord_error_data:
    352 	LOG("[%s solve] Error reading table\n", coord->name);
    353 	return NISSY_ERROR_DATA;
    354 
    355 solve_coord_error_buffer:
    356 	LOG("[%s solve] Error appending solution to buffer: size too small\n",
    357 	    coord->name);
    358 	return NISSY_ERROR_BUFFER_SIZE;
    359 
    360 solve_coord_error_unsolvable:
    361 	LOG("[%s solve] Error: cube not ready\n", coord->name);
    362 	return NISSY_ERROR_UNSOLVABLE_CUBE;
    363 }