diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-03-05 15:17:07 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-03-05 15:17:07 +0100 |
| commit | 1a5bfe9b08707b0aef748d7921a419ba4a046fba (patch) | |
| tree | fc23d1fce16a5a7dd822274adab309dbbb3663e7 | |
| parent | 87a15e960e31365698df7e06cd3e6b851e17c1a5 (diff) | |
| download | nissy-1a5bfe9b08707b0aef748d7921a419ba4a046fba.tar.gz nissy-1a5bfe9b08707b0aef748d7921a419ba4a046fba.zip | |
Some fixes
| -rw-r--r-- | src/solve.c | 3 | ||||
| -rw-r--r-- | src/solve.h | 3 | ||||
| -rw-r--r-- | src/solver_step.c | 26 | ||||
| -rw-r--r-- | src/threader_eager.c | 7 |
4 files changed, 27 insertions, 12 deletions
diff --git a/src/solve.c b/src/solve.c index 41f99a2..2d0c94d 100644 --- a/src/solve.c +++ b/src/solve.c | |||
| @@ -115,5 +115,8 @@ solve(Cube *cube, SolveOptions *opts, Solver **solver, Threader *threader) | |||
| 115 | (opts->optimal != -1 && d >= opts->optimal + optimal); | 115 | (opts->optimal != -1 && d >= opts->optimal + optimal); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | /* TODO: some cleanup (free cubedata) */ | ||
| 119 | /* TODO: actually, preparation should be done somewhere else */ | ||
| 120 | |||
| 118 | return sols; | 121 | return sols; |
| 119 | } | 122 | } |
diff --git a/src/solve.h b/src/solve.h index b09c81b..8182887 100644 --- a/src/solve.h +++ b/src/solve.h | |||
| @@ -36,12 +36,13 @@ struct solver { | |||
| 36 | void * param; | 36 | void * param; |
| 37 | /* TODO: the following should be part of a generic cube description */ | 37 | /* TODO: the following should be part of a generic cube description */ |
| 38 | /* TODO: remove alloc? */ | 38 | /* TODO: remove alloc? */ |
| 39 | /* TODO: revisit apply_move, maybe apply_alg? or both? */ | ||
| 39 | void * (*alloc_cubedata)(void *); | 40 | void * (*alloc_cubedata)(void *); |
| 40 | void (*copy_cubedata)(void *, void *, void *); | 41 | void (*copy_cubedata)(void *, void *, void *); |
| 41 | void (*free_cubedata)(void *, void *); | 42 | void (*free_cubedata)(void *, void *); |
| 42 | void (*invert_cube)(void *, void *); | 43 | void (*invert_cube)(void *, void *); |
| 43 | bool (*is_solved)(void *, void *); | 44 | bool (*is_solved)(void *, void *); |
| 44 | void (*apply_alg)(void *, void *, Alg *); | 45 | void (*apply_move)(void *, void *, Move); |
| 45 | /* TODO: remove dependence on Cube, preparation should be done before */ | 46 | /* TODO: remove dependence on Cube, preparation should be done before */ |
| 46 | void * (*prepare_cube)(void *, Cube *); | 47 | void * (*prepare_cube)(void *, Cube *); |
| 47 | }; | 48 | }; |
diff --git a/src/solver_step.c b/src/solver_step.c index 5c9d149..52fa347 100644 --- a/src/solver_step.c +++ b/src/solver_step.c | |||
| @@ -6,7 +6,7 @@ typedef struct { | |||
| 6 | Trans * t; | 6 | Trans * t; |
| 7 | } CubeData; | 7 | } CubeData; |
| 8 | 8 | ||
| 9 | static void apply_alg_cubedata(void *, void *, Alg *); | 9 | static void apply_move_cubedata(void *, void *, Move); |
| 10 | static void init_indexes(Step *, CubeData *); | 10 | static void init_indexes(Step *, CubeData *); |
| 11 | static void * prepare_cube(void *, Cube *); | 11 | static void * prepare_cube(void *, Cube *); |
| 12 | static bool move_check_stop_eager(void *, DfsArg *, Threader *); | 12 | static bool move_check_stop_eager(void *, DfsArg *, Threader *); |
| @@ -22,13 +22,17 @@ static bool niss_makes_sense(void *, void *, Alg *); | |||
| 22 | static Solver * new_stepsolver_nocheckstop(Step *step); | 22 | static Solver * new_stepsolver_nocheckstop(Step *step); |
| 23 | 23 | ||
| 24 | static void | 24 | static void |
| 25 | apply_alg_cubedata(void *param, void *cubedata, Alg *alg) | 25 | apply_move_cubedata(void *param, void *cubedata, Move m) |
| 26 | { | 26 | { |
| 27 | Step *s = (Step *)param; | 27 | Step *s = (Step *)param; |
| 28 | CubeData *data = (CubeData *)cubedata; | 28 | CubeData *data = (CubeData *)cubedata; |
| 29 | 29 | ||
| 30 | apply_alg(alg, data->cube); | 30 | Trans tt; |
| 31 | init_indexes(s, data); | 31 | for (int i = 0; i < s->n_coord; i++) { |
| 32 | Move mm = transform_move(data->t[i], m); | ||
| 33 | data->val[i] = move_coord(s->coord[i], mm, data->val[i], &tt); | ||
| 34 | data->t[i] = transform_trans(tt, data->t[i]); | ||
| 35 | } | ||
| 32 | } | 36 | } |
| 33 | 37 | ||
| 34 | static void | 38 | static void |
| @@ -101,6 +105,8 @@ move_check_stop_lazy(void *param, DfsArg *arg, Threader *threader) | |||
| 101 | return move_check_stop_nonsol(param, arg, threader); | 105 | return move_check_stop_nonsol(param, arg, threader); |
| 102 | } | 106 | } |
| 103 | 107 | ||
| 108 | /* TODO: split in 2 (nissable / non-nissable) and only move cube | ||
| 109 | when nissable */ | ||
| 104 | static bool | 110 | static bool |
| 105 | move_check_stop_nonsol(void *param, DfsArg *arg, Threader *threader) | 111 | move_check_stop_nonsol(void *param, DfsArg *arg, Threader *threader) |
| 106 | { | 112 | { |
| @@ -113,6 +119,7 @@ move_check_stop_nonsol(void *param, DfsArg *arg, Threader *threader) | |||
| 113 | s = (Step *)param; | 119 | s = (Step *)param; |
| 114 | data = (CubeData *)arg->cubedata; | 120 | data = (CubeData *)arg->cubedata; |
| 115 | 121 | ||
| 122 | |||
| 116 | bound = 0; | 123 | bound = 0; |
| 117 | goal = arg->d - arg->current_alg->len; | 124 | goal = arg->d - arg->current_alg->len; |
| 118 | /* TODO: check if len is 0 */ | 125 | /* TODO: check if len is 0 */ |
| @@ -130,6 +137,8 @@ move_check_stop_nonsol(void *param, DfsArg *arg, Threader *threader) | |||
| 130 | return true; | 137 | return true; |
| 131 | } | 138 | } |
| 132 | } | 139 | } |
| 140 | if (arg->opts->can_niss && !arg->niss) | ||
| 141 | apply_move(lastmove, data->cube); | ||
| 133 | 142 | ||
| 134 | return false; | 143 | return false; |
| 135 | } | 144 | } |
| @@ -184,8 +193,9 @@ copy_cubedata(void *param, void *src, void *dst) | |||
| 184 | olddata = (CubeData *)src; | 193 | olddata = (CubeData *)src; |
| 185 | newdata = (CubeData *)dst; | 194 | newdata = (CubeData *)dst; |
| 186 | 195 | ||
| 187 | /* Copy reference: we never move the cube */ | 196 | /* TODO: do not copy if not nissable */ |
| 188 | newdata->cube = olddata->cube; | 197 | newdata->cube = malloc(sizeof(Cube)); |
| 198 | copy_cube(olddata->cube, newdata->cube); | ||
| 189 | for (i = 0; i < s->n_coord; i++) { | 199 | for (i = 0; i < s->n_coord; i++) { |
| 190 | newdata->val[i] = olddata->val[i]; | 200 | newdata->val[i] = olddata->val[i]; |
| 191 | newdata->t[i] = olddata->t[i]; | 201 | newdata->t[i] = olddata->t[i]; |
| @@ -201,7 +211,7 @@ free_cubedata(void *param, void *cubedata) | |||
| 201 | 211 | ||
| 202 | free(data->t); | 212 | free(data->t); |
| 203 | free(data->val); | 213 | free(data->val); |
| 204 | /* We do not free the cube */ | 214 | free(data->cube); |
| 205 | free(data); | 215 | free(data); |
| 206 | } | 216 | } |
| 207 | 217 | ||
| @@ -254,7 +264,7 @@ new_stepsolver_nocheckstop(Step *step) | |||
| 254 | solver->moveset = step->moveset; | 264 | solver->moveset = step->moveset; |
| 255 | solver->param = step; | 265 | solver->param = step; |
| 256 | 266 | ||
| 257 | solver->apply_alg = apply_alg_cubedata; | 267 | solver->apply_move = apply_move_cubedata; |
| 258 | solver->prepare_cube = prepare_cube; | 268 | solver->prepare_cube = prepare_cube; |
| 259 | solver->is_solved = is_solved_step; | 269 | solver->is_solved = is_solved_step; |
| 260 | solver->validate_solution = validate_solution; | 270 | solver->validate_solution = validate_solution; |
diff --git a/src/threader_eager.c b/src/threader_eager.c index 7013518..261b995 100644 --- a/src/threader_eager.c +++ b/src/threader_eager.c | |||
| @@ -43,7 +43,7 @@ possible_starts(DfsArg *arg, Solver *solver) | |||
| 43 | AlgList *ret = new_alglist(); | 43 | AlgList *ret = new_alglist(); |
| 44 | 44 | ||
| 45 | if (solver->is_solved(solver->param, arg->cubedata)) { | 45 | if (solver->is_solved(solver->param, arg->cubedata)) { |
| 46 | if (arg->opts->min_moves == 0) | 46 | if (arg->opts->min_moves == 0 && arg->d == 0) |
| 47 | append_sol(new_alg(""), arg->threaddata); | 47 | append_sol(new_alg(""), arg->threaddata); |
| 48 | return ret; | 48 | return ret; |
| 49 | } | 49 | } |
| @@ -82,15 +82,16 @@ instance_thread(void *arg) | |||
| 82 | *(tid->node) = (*(tid->node))->next; | 82 | *(tid->node) = (*(tid->node))->next; |
| 83 | pthread_mutex_unlock(tid->start_mutex); | 83 | pthread_mutex_unlock(tid->start_mutex); |
| 84 | 84 | ||
| 85 | /* TODO: adjust for longer (arbitrarily long?) starting sequences */ | ||
| 85 | void *data = tid->solver->alloc_cubedata(tid->solver->param); | 86 | void *data = tid->solver->alloc_cubedata(tid->solver->param); |
| 86 | tid->solver->copy_cubedata( | 87 | tid->solver->copy_cubedata( |
| 87 | tid->solver->param, tid->arg->cubedata, data); | 88 | tid->solver->param, tid->arg->cubedata, data); |
| 88 | tid->solver->apply_alg( | ||
| 89 | tid->solver->param, data, node->alg); | ||
| 90 | bool inv = node->alg->inv[node->alg->len-1]; | 89 | bool inv = node->alg->inv[node->alg->len-1]; |
| 91 | if (inv) | 90 | if (inv) |
| 92 | tid->solver->invert_cube( | 91 | tid->solver->invert_cube( |
| 93 | tid->solver->param, data); | 92 | tid->solver->param, data); |
| 93 | tid->solver->apply_move( | ||
| 94 | tid->solver->param, data, node->alg->move[0]); | ||
| 94 | 95 | ||
| 95 | DfsArg newarg; | 96 | DfsArg newarg; |
| 96 | newarg.cubedata = data; | 97 | newarg.cubedata = data; |
