aboutsummaryrefslogtreecommitdiff
path: root/src/solve.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-12-16 19:25:58 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-12-16 19:25:58 +0100
commit2f924f942bd6e7126e8f1d8692e475c95bd9fe82 (patch)
treedd5877c2fd836f43523263e48632946423401093 /src/solve.c
parent4e2b4e603c7e84c7556f489d7d8dab06915b3a9b (diff)
downloadnissy-2f924f942bd6e7126e8f1d8692e475c95bd9fe82.tar.gz
nissy-2f924f942bd6e7126e8f1d8692e475c95bd9fe82.zip
Added a new pruning table (equivalent to nxopt31). I have not tested it yet, it takes a while to generate.
Plus I have done a whole lot of refactoring in random places because I cannot focus on one thing at the time.
Diffstat (limited to 'src/solve.c')
-rw-r--r--src/solve.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/src/solve.c b/src/solve.c
index ea3b7ae..a8f9001 100644
--- a/src/solve.c
+++ b/src/solve.c
@@ -8,7 +8,7 @@ static void copy_dfsarg(DfsArg *src, DfsArg *dst);
8static void dfs(DfsArg *arg); 8static void dfs(DfsArg *arg);
9static void dfs_branch(DfsArg *arg); 9static void dfs_branch(DfsArg *arg);
10static bool dfs_check_solved(DfsArg *arg); 10static bool dfs_check_solved(DfsArg *arg);
11static bool dfs_switch_final(DfsArg *arg); 11static bool dfs_switch(DfsArg *arg);
12static void dfs_niss(DfsArg *arg); 12static void dfs_niss(DfsArg *arg);
13static bool dfs_stop(DfsArg *arg); 13static bool dfs_stop(DfsArg *arg);
14static void * instance_thread(void *arg); 14static void * instance_thread(void *arg);
@@ -21,25 +21,43 @@ static bool niss_makes_sense(DfsArg *arg);
21static bool 21static bool
22allowed_next(Move m, DfsArg *arg) 22allowed_next(Move m, DfsArg *arg)
23{ 23{
24 if ((1 << m) & arg->badmoves) 24 bool bad, allowed, order;
25 return false; 25 uint64_t mbit;
26 26
27 if (!possible_next(arg->last2, arg->last1, m)) 27 if (arg->last1 == NULLMOVE)
28 return false; 28 return true;
29 29
30 if (commute(arg->last1, m)) 30 mbit = ((uint64_t)1) << m;
31 return arg->move_position[arg->last1] < arg->move_position[m]; 31 bad = mbit & arg->badmoves;
32 allowed = mbit & arg->step->moveset->mask[arg->last2][arg->last1];
33 order = !commute(arg->last1, m) || arg->last1 < m;
32 34
33 return true; 35 return allowed && !bad && order;
34} 36}
35 37
36static bool 38static bool
37cancel_niss(DfsArg *arg) 39cancel_niss(DfsArg *arg)
38{ 40{
39 return !possible_next(arg->last2, arg->last1, arg->last1inv) && 41 Moveset *ms;
40 !(commute(arg->last1inv, arg->last2inv) && 42 Move i1, i2;
41 arg->last2inv != NULLMOVE && 43 bool p, p1, p2, q, q1, q2;
42 possible_next(arg->last2, arg->last1, arg->last2inv)); 44
45 if (arg->last1inv == NULLMOVE)
46 return false;
47
48 ms = arg->step->moveset;
49 i1 = inverse_move(arg->last1inv);
50 i2 = inverse_move(arg->last2inv);
51
52 p1 = !ms->allowed_next(arg->last2, arg->last1, i1);
53 p2 = !ms->allowed_next(arg->last2, i1, arg->last1);
54 p = p1 || (commute(i1, arg->last1) && p2);
55
56 q1 = !ms->allowed_next(arg->last2, arg->last1, i2);
57 q2 = !ms->allowed_next(arg->last2, i2, arg->last1);
58 q = q1 || (commute(i2, arg->last1) && q2);
59
60 return p || (commute(i1, i2) && q);
43} 61}
44 62
45static void 63static void
@@ -60,8 +78,6 @@ copy_dfsarg(DfsArg *src, DfsArg *dst)
60 dst->sols = src->sols; 78 dst->sols = src->sols;
61 dst->sols_mutex = src->sols_mutex; 79 dst->sols_mutex = src->sols_mutex;
62 dst->current_alg = src->current_alg; 80 dst->current_alg = src->current_alg;
63 dst->sorted_moves = src->sorted_moves;
64 dst->move_position = src->move_position;
65 81
66 copy_estimatedata(src->ed, dst->ed); 82 copy_estimatedata(src->ed, dst->ed);
67} 83}
@@ -77,7 +93,7 @@ dfs(DfsArg *arg)
77 if (dfs_check_solved(arg)) 93 if (dfs_check_solved(arg))
78 return; 94 return;
79 95
80 if (arg->step->final && (sw = dfs_switch_final(arg))) 96 if (arg->step->final && (sw = dfs_switch(arg)))
81 invert_branch(arg); 97 invert_branch(arg);
82 dfs_branch(arg); 98 dfs_branch(arg);
83 99
@@ -98,8 +114,8 @@ dfs_branch(DfsArg *arg)
98 newarg = malloc(sizeof(DfsArg)); 114 newarg = malloc(sizeof(DfsArg));
99 newarg->ed = malloc(sizeof(EstimateData)); 115 newarg->ed = malloc(sizeof(EstimateData));
100 116
101 for (i = 0; arg->sorted_moves[i] != NULLMOVE; i++) { 117 for (i = 0; arg->step->moveset->sorted_moves[i] != NULLMOVE; i++) {
102 m = arg->sorted_moves[i]; 118 m = arg->step->moveset->sorted_moves[i];
103 if (allowed_next(m, arg)) { 119 if (allowed_next(m, arg)) {
104 copy_dfsarg(arg, newarg); 120 copy_dfsarg(arg, newarg);
105 newarg->last2 = arg->last1; 121 newarg->last2 = arg->last1;
@@ -181,20 +197,22 @@ dfs_stop(DfsArg *arg)
181} 197}
182 198
183static bool 199static bool
184dfs_switch_final(DfsArg *arg) 200dfs_switch(DfsArg *arg)
185{ 201{
186 int i, bn, bi; 202 int i, bn, bi;
187 203
188 for (bn = 0, i = 0; arg->sorted_moves[i] != NULLMOVE; i++) 204 bn = 0;
189 if (allowed_next(arg->sorted_moves[i], arg)) 205 for (i = 0; arg->step->moveset->sorted_moves[i] != NULLMOVE; i++)
206 if (allowed_next(arg->step->moveset->sorted_moves[i], arg))
190 bn++; 207 bn++;
191 208
192 swapmove(&(arg->last1), &(arg->last1inv)); 209 swapmove(&(arg->last1), &(arg->last1inv));
193 swapmove(&(arg->last2), &(arg->last2inv)); 210 swapmove(&(arg->last2), &(arg->last2inv));
194 swapu64(&(arg->badmoves), &(arg->badmovesinv)); 211 swapu64(&(arg->badmoves), &(arg->badmovesinv));
195 212
196 for (bi = 0, i = 0; arg->sorted_moves[i] != NULLMOVE; i++) 213 bi = 0;
197 if (allowed_next(arg->sorted_moves[i], arg)) 214 for (i = 0; arg->step->moveset->sorted_moves[i] != NULLMOVE; i++)
215 if (allowed_next(arg->step->moveset->sorted_moves[i], arg))
198 bi++; 216 bi++;
199 217
200 swapmove(&(arg->last1), &(arg->last1inv)); 218 swapmove(&(arg->last1), &(arg->last1inv));
@@ -246,8 +264,6 @@ instance_thread(void *arg)
246 darg.current_alg = new_alg(""); 264 darg.current_alg = new_alg("");
247 append_move(darg.current_alg, node->alg->move[0], 265 append_move(darg.current_alg, node->alg->move[0],
248 node->alg->inv[0]); 266 node->alg->inv[0]);
249 darg.sorted_moves = td->sorted_moves;
250 darg.move_position = td->move_position;
251 darg.ed = new_estimatedata(); 267 darg.ed = new_estimatedata();
252 darg.badmoves = 0; 268 darg.badmoves = 0;
253 darg.badmovesinv = 0; 269 darg.badmovesinv = 0;
@@ -281,8 +297,7 @@ invert_branch(DfsArg *arg)
281static void 297static void
282multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d) 298multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d)
283{ 299{
284 int i, *move_position; 300 int i;
285 Move *sorted_moves;
286 Alg *alg; 301 Alg *alg;
287 AlgList *start; 302 AlgList *start;
288 AlgListNode **node; 303 AlgListNode **node;
@@ -290,8 +305,6 @@ multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d)
290 ThreadDataSolve td[opts->nthreads]; 305 ThreadDataSolve td[opts->nthreads];
291 pthread_mutex_t *start_mutex, *sols_mutex; 306 pthread_mutex_t *start_mutex, *sols_mutex;
292 307
293 move_position = malloc(NMOVES * sizeof(int));
294 sorted_moves = malloc(NMOVES * sizeof(Move));
295 node = malloc(sizeof(AlgListNode *)); 308 node = malloc(sizeof(AlgListNode *));
296 start_mutex = malloc(sizeof(pthread_mutex_t)); 309 start_mutex = malloc(sizeof(pthread_mutex_t));
297 sols_mutex = malloc(sizeof(pthread_mutex_t)); 310 sols_mutex = malloc(sizeof(pthread_mutex_t));
@@ -300,14 +313,11 @@ multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d)
300 pthread_mutex_init(start_mutex, NULL); 313 pthread_mutex_init(start_mutex, NULL);
301 pthread_mutex_init(sols_mutex, NULL); 314 pthread_mutex_init(sols_mutex, NULL);
302 315
303 moveset_to_list(s->moveset, sorted_moves); 316 for (i = 0; s->moveset->sorted_moves[i] != NULLMOVE; i++) {
304 movelist_to_position(sorted_moves, move_position);
305
306 for (i = 0; sorted_moves[i] != NULLMOVE; i++) {
307 alg = new_alg(""); 317 alg = new_alg("");
308 /* TODO: start on inverse also in case of final step 318 /* TODO: start on inverse also in case of final step
309 and ed->sw true */ 319 and ed->sw true */
310 append_move(alg, sorted_moves[i], false); 320 append_move(alg, s->moveset->sorted_moves[i], false);
311 append_alg(start, alg); 321 append_alg(start, alg);
312 if (opts->can_niss) { 322 if (opts->can_niss) {
313 alg->inv[0] = true; 323 alg->inv[0] = true;
@@ -322,8 +332,6 @@ multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d)
322 td[i].cube = c; 332 td[i].cube = c;
323 td[i].step = s; 333 td[i].step = s;
324 td[i].depth = d; 334 td[i].depth = d;
325 td[i].sorted_moves = sorted_moves;
326 td[i].move_position = move_position;
327 td[i].opts = opts; 335 td[i].opts = opts;
328 td[i].start = start; 336 td[i].start = start;
329 td[i].node = node; 337 td[i].node = node;
@@ -340,8 +348,6 @@ multidfs(Cube c, Step *s, SolveOptions *opts, AlgList *sols, int d)
340 free(node); 348 free(node);
341 free(start_mutex); 349 free(start_mutex);
342 free(sols_mutex); 350 free(sols_mutex);
343 free(move_position);
344 free(sorted_moves);
345} 351}
346 352
347static bool 353static bool

Generated with cgit - Back to sebastiano.tronto.net