extern "C" value C_create_solver(value unit) { CAMLparam1 (unit); solver_c s = solver_new (); value val = alloc(1, Abstract_tag); Field(val,0) = (value) s; CAMLreturn(val); }
void testit(int* wanted, int Nwanted, int dimquads, int (*compar)(const void *, const void *), anbool cxdx) { int i; solver_t* solver; index_t index; starxy_t* starxy; starxy = field1(); quadlist = bl_new(16, dimquads*sizeof(int)); ninv = 0; solver = solver_new(); memset(&index, 0, sizeof(index_t)); index.index_scale_lower = 1; index.index_scale_upper = 10; index.dimquads = dimquads; index.cx_less_than_dx = index.meanx_less_than_half = cxdx; solver->funits_lower = 0.1; solver->funits_upper = 10; solver_add_index(solver, &index); solver_set_field(solver, starxy); solver_preprocess_field(solver); printf("Found:\n"); solver_run(solver); printf("\n"); fflush(NULL); solver_free_field(solver); solver_free(solver); // bl_sort(quadlist, compar); qsort(wanted, Nwanted, dimquads*sizeof(int), compar); printf("\n\n"); printf("Wanted:\n"); for (i=0; i<Nwanted; i++) { int j; printf("{"); for (j=0; j<dimquads; j++) printf("%s%i", (j?",":""), wanted[i*dimquads+j]); printf("}, "); } printf("\n"); printf("N found: %i; N wanted: %i\n", bl_size(quadlist), Nwanted); printf("N obeying invariants: %i\n", ninv); assert(bl_size(quadlist) == Nwanted); for (i=0; i<bl_size(quadlist); i++) { //int* i1 = bl_access(quadlist, i); //int* i2 = wanted[i]; //printf("[%i, %i, %i] vs [%i, %i, %i]\n", i1[0],i1[1],i1[2], i2[0],i2[1],i2[2]); assert(compar(bl_access(quadlist, i), wanted+i*dimquads) == 0); } bl_free(quadlist); }
void test_solve_multiindex(CuTest* ct) { sl* fns; multiindex_t* mi; int i; solver_t* s = NULL; starxy_t* field = NULL; MatchObj* mo = NULL; xylist_t* xy = NULL; log_init(LOG_VERB); fns = sl_new(4); sl_append(fns, "../util/t10.ind"); sl_append(fns, "../util/t11.ind"); sl_append(fns, "../util/t12.ind"); mi = multiindex_open("../util/t10.skdt", fns, 0); printf("Got %i indices\n", multiindex_n(mi)); for (i=0; i<multiindex_n(mi); i++) { index_t* ind = multiindex_get(mi, i); printf(" %i: %s, %i stars, %i quads (%g to %g arcmin)\n", i, ind->indexname, index_nquads(ind), index_nstars(ind), ind->index_scale_lower/60., ind->index_scale_upper/60.); } s = solver_new(); // 10.8 s->funits_lower = 5.0; s->funits_upper = 15.0; xy = xylist_open("../util/t1.xy"); if (!xy) { ERROR("Failed to open xylist\n"); CuFail(ct, "xylist"); } field = xylist_read_field(xy, NULL); solver_set_field(s, field); solver_set_field_bounds(s, 0, 1000, 0, 1000); for (i=0; i<multiindex_n(mi); i++) { index_t* ind = multiindex_get(mi, i); solver_add_index(s, ind); } solver_run(s); if (solver_did_solve(s)) { mo = solver_get_best_match(s); matchobj_print(mo, LOG_MSG); // HACK -- ugly!! verify_free_matchobj(mo); } xylist_close(xy); solver_cleanup_field(s); solver_free(s); multiindex_free(mi); sl_free2(fns); }
void test2() { int i; solver_t* solver; index_t index; starxy_t* starxy; int wanted[][3] = { { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 3, 4 }, { 0, 5, 4 }, { 0, 6, 4 }, { 0, 6, 5 }, { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 }, { 1, 5, 4 }, { 1, 6, 4 }, { 1, 6, 5 }, { 2, 4, 3 }, { 2, 5, 0 }, { 2, 5, 1 }, { 2, 5, 3 }, { 2, 5, 4 }, { 2, 6, 0 }, { 2, 6, 1 }, { 2, 6, 3 }, { 2, 6, 4 }, { 2, 6, 5 }, { 3, 5, 4 }, { 3, 6, 0 }, { 3, 6, 1 }, { 3, 6, 4 }, { 3, 6, 5 }, { 4, 6, 5 }, }; starxy = field1(); quadlist = bl_new(16, 3*sizeof(uint)); solver = solver_new(); memset(&index, 0, sizeof(index_t)); index.index_scale_lower = 1; index.index_scale_upper = 10; index.dimquads = 3; solver->funits_lower = 0.1; solver->funits_upper = 10; solver_add_index(solver, &index); solver_set_field(solver, starxy); solver_preprocess_field(solver); solver_run(solver); solver_free_field(solver); solver_free(solver); // assert(bl_size(quadlist) == (sizeof(wanted) / (3*sizeof(uint)))); bl_sort(quadlist, compare_tri); for (i=0; i<bl_size(quadlist); i++) { assert(compare_tri(bl_access(quadlist, i), wanted[i]) == 0); } bl_free(quadlist); }
int glp_minisat1(glp_prob *P) { /* solve CNF-SAT problem with MiniSat solver */ solver *s; GLPAIJ *aij; int i, j, len, ret, *ind; double sum; /* check problem object */ if (P == NULL || P->magic != GLP_PROB_MAGIC) xerror("glp_minisat1: P = %p; invalid problem object\n", P); if (P->tree != NULL) xerror("glp_minisat1: operation not allowed\n"); /* integer solution is currently undefined */ P->mip_stat = GLP_UNDEF; P->mip_obj = 0.0; /* check that problem object encodes CNF-SAT instance */ if (glp_check_cnfsat(P) != 0) { xprintf("glp_minisat1: problem object does not encode CNF-SAT " "instance\n"); ret = GLP_EDATA; goto done; } #if 1 /* 07/XI-2015 */ if (sizeof(void *) != sizeof(int)) { xprintf("glp_minisat1: sorry, MiniSat solver is not supported " "on 64-bit platforms\n"); ret = GLP_EFAIL; goto done; } #endif /* solve CNF-SAT problem */ xprintf("Solving CNF-SAT problem...\n"); xprintf("Instance has %d variable%s, %d clause%s, and %d literal%" "s\n", P->n, P->n == 1 ? "" : "s", P->m, P->m == 1 ? "" : "s", P->nnz, P->nnz == 1 ? "" : "s"); /* if CNF-SAT has no clauses, it is satisfiable */ if (P->m == 0) { P->mip_stat = GLP_OPT; for (j = 1; j <= P->n; j++) P->col[j]->mipx = 0.0; goto fini; } /* if CNF-SAT has an empty clause, it is unsatisfiable */ for (i = 1; i <= P->m; i++) { if (P->row[i]->ptr == NULL) { P->mip_stat = GLP_NOFEAS; goto fini; } } /* prepare input data for the solver */ s = solver_new(); solver_setnvars(s, P->n); ind = xcalloc(1+P->n, sizeof(int)); for (i = 1; i <= P->m; i++) { len = 0; for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next) { ind[++len] = toLit(aij->col->j-1); if (aij->val < 0.0) ind[len] = lit_neg(ind[len]); } xassert(len > 0); xassert(solver_addclause(s, &ind[1], &ind[1+len])); } xfree(ind); /* call the solver */ s->verbosity = 1; if (solver_solve(s, 0, 0)) { /* instance is reported as satisfiable */ P->mip_stat = GLP_OPT; /* copy solution to the problem object */ xassert(s->model.size == P->n); for (j = 1; j <= P->n; j++) { P->col[j]->mipx = s->model.ptr[j-1] == l_True ? 1.0 : 0.0; } /* compute row values */ for (i = 1; i <= P->m; i++) { sum = 0; for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next) sum += aij->val * aij->col->mipx; P->row[i]->mipx = sum; } /* check integer feasibility */ for (i = 1; i <= P->m; i++) { if (P->row[i]->mipx < P->row[i]->lb) { /* solution is wrong */ P->mip_stat = GLP_UNDEF; break; } } } else { /* instance is reported as unsatisfiable */ P->mip_stat = GLP_NOFEAS; } solver_delete(s); fini: /* report the instance status */ if (P->mip_stat == GLP_OPT) { xprintf("SATISFIABLE\n"); ret = 0; } else if (P->mip_stat == GLP_NOFEAS) { xprintf("UNSATISFIABLE\n"); ret = 0; } else { xprintf("glp_minisat1: solver failed\n"); ret = GLP_EFAIL; } done: return ret; }