bool UCamRad::setCameraParameters(float hx, float hy, float k1, float k2, float focalLng) { bool result; double pixSize = dev->getPixelSize(); // result = par.setCameraParameters(hx, hy, k1, k2, focalLng, pixSize); if (varIntrinsic != NULL) createVars(); if (varDistortion != NULL) { // then the rest is probably OK too varDistortion->setDouble(k1, 0); varDistortion->setDouble(k2, 1); varIntrinsic->setDouble(focalLng / pixSize, 0); varIntrinsic->setDouble(focalLng / pixSize, 4); varIntrinsic->setDouble(hx / pixSize, 2); varIntrinsic->setDouble(hy / pixSize, 5); } if (result) { updateLensParams(0, 0); result = setRadialErrorMatrix(); } return result; }
/** Constructor for Solver Set all to Zero execpt the game * @brief Solver::SatBaseClass::SatBaseClass * @param _game: The game, that will be solved */ Solver::SatBaseClass::SatBaseClass(const Game::Game& _game, const std::string& _satSolver): Solver::SolverBaseClass(_game), gameField(_game), trueVar(0), falseVar(0), countVars(0), countClausel(0), neededTimeForCreateDimacs(0), neededTimeForSatsolving(0) { satSolver = _satSolver; createVars(); }
Photo() { srand(so.rnd_seed); // n_names = 12 + (int) floor((double) rand()/RAND_MAX*4); // n_prefs = 24 + (int) floor((double) rand()/RAND_MAX*12); n_names = 10 + (int) floor((double) rand()/RAND_MAX*3); n_prefs = 20 + (int) floor((double) rand()/RAND_MAX*10); prefs = (int*) malloc(2*n_prefs*sizeof(int)); for (int i = 0; i < 2*n_prefs; i++) prefs[i] = rand()%n_names; printf("%d preferences\n", n_prefs); // Create vars createVars(x, n_names, 1, n_names); createVars(f, 2*n_prefs); createVar(sat, 0, n_prefs); // Post some constraints // Map preferences to fulfilment for (int i = 0; i < n_prefs; i++) { int_rel_reif(x[prefs[2*i+0]], IRT_EQ, x[prefs[2*i+1]], f[2*i], 1); int_rel_reif(x[prefs[2*i+0]], IRT_EQ, x[prefs[2*i+1]], f[2*i+1], -1); } // Sum of fulfilment bool_linear(f, IRT_GE, sat); all_different(x); // Break some symmetries int_rel(x[0], IRT_LT, x[1]); // Post some branchings // branch(x, VAR_INORDER, VAL_MIN); branch(x, VAR_DEGREE_MAX, VAL_MIN); // Declare output variables (optional) optimize(sat, OPT_MAX); }
int createModel(const Problem<double>& P, IloEnv& env, IloModel& m, IloNumVarArray& t, IloNumVarMatrix& x, IloNumVarMatrix& y, IloNumVarMatrix& b, IloNumVarMatrix& w,const std::vector<int>& config) { try { return createVars(P,env,x,y,b,w) || createConstraints(P,env,m,t,x,y,b,w,config); } catch (IloException& e){ std::cout << "iloexception in createmodel" << e <<std::endl; e.end(); return 1; } }
void val_sym_break(vec<IntVar*>& x, int l, int u) { vec<IntVar*> y; createVars(y, u-l+1, 0, x.size(), true); for (int i = 0; i < x.size(); i++) { x[i]->specialiseToEL(); } for (int i = l; i <= u; i++) { for (int j = 0; j < x.size(); j++) { bool_rel(y[i-l]->getLit(j,1), BRT_R_IMPL, x[j]->getLit(i,1)); bool_rel(x[j]->getLit(i,1), BRT_R_IMPL, y[i-l]->getLit(j,3)); } } for (int i = 0; i < u-l; i++) { new BinLTInf(y[i], y[i+1], x.size()); } }
static int compute_prob(void) /* this is the function that implements the compute_prob predicate used in pp.pl */ { YAP_Term out,arg1,arg2,arg3,arg4; variables vars; expr expression; DdNode * function; DdManager * mgr; int nBVar,i,intBits,create_dot; FILE * file; DdNode * array[1]; double prob; char * onames[1]; char inames[1000][20]; char * names[1000]; tablerow * nodes; arg1=YAP_ARG1; arg2=YAP_ARG2; arg3=YAP_ARG3; arg4=YAP_ARG4; mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); create_dot=YAP_IntOfTerm(arg4); vars=createVars(arg1,mgr,create_dot,inames); //Cudd_PrintInfo(mgr,stderr); /* automatic variable reordering, default method CUDD_REORDER_SIFT used */ //printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); //printf("order %d\n",order); Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME); /* Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT); printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); printf("order %d\n",order); printf("%d",CUDD_REORDER_RANDOM_PIVOT); */ expression=createExpression(arg2); function=retFunction(mgr,expression,vars); /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram) because it is easier to interpret and to print */ //add=Cudd_BddToAdd(mgr,function); //Cudd_PrintInfo(mgr,stderr); if (create_dot) /* if specified by the user, a dot file for the BDD is written to cpl.dot */ { nBVar=vars.nBVar; for(i=0; i<nBVar; i++) names[i]=inames[i]; array[0]=function; onames[0]="Out"; file = open_file("cpl.dot", "w"); Cudd_DumpDot(mgr,1,array,names,onames,file); fclose(file); } nodes=init_table(vars.nBVar); intBits=sizeof(unsigned int)*8; prob=Prob(function,vars,nodes); out=YAP_MkFloatTerm(prob); destroy_table(nodes,vars.nBVar); Cudd_Quit(mgr); for(i=0; i<vars.nVar; i++) { free(vars.varar[i].probabilities); free(vars.varar[i].booleanVars); } free(vars.varar); free(vars.bVar2mVar); for(i=0; i<expression.nTerms; i++) { free(expression.terms[i].factors); } free(expression.terms); return(YAP_Unify(out,arg3)); }
void main() { createVars(); CoachAttendant(); }/*end simulation*/
void main(){ createVars(); Waiter(); }/*end simulation*/
GridColouring(int _n, int _m, int _c) : n(_n), m(_m), c(_c) { // Create vars createVars(x, n, m, 1, c, true); // Constraints // For each rectangle, corners cannot all be the same colour for (int r1 = 0; r1 < n; r1++) { for (int r2 = r1+1; r2 < n; r2++) { for (int c1 = 0; c1 < m; c1++) { for (int c2 = c1+1; c2 < m; c2++) { for (int z = 1; z <= c; z++) { vec<BoolView> a; a.push(BoolView(x[r1][c1]->getLit(z,0))); a.push(BoolView(x[r2][c1]->getLit(z,0))); a.push(BoolView(x[r1][c2]->getLit(z,0))); a.push(BoolView(x[r2][c2]->getLit(z,0))); bool_clause(a); } } } } } // Balance constraints if (BALANCE) { IntVar *llimit = getConstant(m/c); IntVar *ulimit = getConstant(m/c+1); for (int i = 0; i < n; i++) { for (int z = 1; z <= c; z++) { vec<BoolView> a; for (int j = 0; j < m; j++) { a.push(BoolView(x[i][j]->getLit(z,1))); } bool_linear(a, IRT_LE, ulimit); bool_linear(a, IRT_GE, llimit); } } } // Mathamatical global constraint if (USE_GLOBAL) addGlobalProp(); // Test if (TEST) { assert(n == 16); assert(m == 16); assert(c == 4); for (int i = 0; i < 4; i++) { for (int j = 0; j < 16; j++) { x[i][j]->setVal((i+j/4)%4+1); } } } // Post some branchings vec<IntVar*> s; flatten(x, s); branch(s, VAR_INORDER, VAL_MIN); }
MOSP(int _n, int _m) : n(_n), m(_m) { generateInstance(); createVars(s, n, 0, n-1); createVars(e, n, 0, n-1); createVars(sb, n, n); createVars(eb, n, n); createVars(o, n, n); createVar(stacks, 0, n); // create customer graph bool g[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i][j] = false; for (int k = 0; k < m; k++) { if (a[i][k] && a[j][k]) { g[i][j] = true; break; } } } } // AllDiff constraint on e all_different(e); // Min constraints on s[i] for (int i = 0; i < n; i++) { vec<IntVar*> x; for (int j = 0; j < n; j++) if (g[i][j]) x.push(e[j]); minimum(x, s[i]); } // open constraints for (int i = 0; i < n; i++) { for (int t = 0; t < n; t++) { int_rel_reif(s[i], IRT_LE, t, sb[i][t]); int_rel_reif(e[i], IRT_GE, t, eb[i][t]); vec<BoolView> x; x.push(sb[i][t]); x.push(eb[i][t]); array_bool_and(x, o[t][i]); } } // stack constraints for (int i = 0; i < n; i++) { bool_linear(o[i], IRT_LE, stacks); } // dominance breaking constraints vec<vec<BoolView> > r; createVars(r, n, n); // Find out which stacks can immediately close for (int t = 0; t < n; t++) { for (int i = 0; i < n; i++) { vec<BoolView> a; for (int j = 0; j < n; j++) if (g[i][j]) a.push(sb[j][t]); a.push(eb[i][t]); array_bool_and(a, r[i][t]); } } // If none of the lex better stacks can instantly close // and this one can, close it now for (int t = 0; t < n-1; t++) { for (int i = 0; i < n; i++) { vec<BoolView> a, b; for (int j = 0; j < i; j++) a.push(r[j][t]); b.push(r[i][t]); b.push(eb[i][t+1]); bool_clause(a, b); } } // branch on end times branch(e, VAR_MIN_MIN, VAL_MIN); // set optimization target optimize(stacks, OPT_MIN); }
static int compute_prob(void) /* this is the function that implements the compute_prob predicate used in pp.pl */ { YAP_Term out,arg1,arg2,arg3,arg4; array_t * variables,* expression, * bVar2mVar; DdNode * function, * add; DdManager * mgr; int nBVar,i,j,intBits,create_dot; FILE * file; DdNode * array[1]; char * onames[1]; char inames[1000][20]; char * names[1000]; GHashTable * nodes; /* hash table that associates nodes with their probability if already computed, it is defined in glib */ Cudd_ReorderingType order; arg1=YAP_ARG1; arg2=YAP_ARG2; arg3=YAP_ARG3; arg4=YAP_ARG4; mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); variables=array_alloc(variable,0); bVar2mVar=array_alloc(int,0); create_dot=YAP_IntOfTerm(arg4); createVars(variables,arg1,mgr,bVar2mVar,create_dot,inames); //Cudd_PrintInfo(mgr,stderr); /* automatic variable reordering, default method CUDD_REORDER_SIFT used */ //printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); //printf("order %d\n",order); Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME); /* Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT); printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); printf("order %d\n",order); printf("%d",CUDD_REORDER_RANDOM_PIVOT); */ expression=array_alloc(array_t *,0); createExpression(expression,arg2); function=retFunction(mgr,expression,variables); /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram) because it is easier to interpret and to print */ add=Cudd_BddToAdd(mgr,function); //Cudd_PrintInfo(mgr,stderr); if (create_dot) /* if specified by the user, a dot file for the BDD is written to cpl.dot */ { nBVar=array_n(bVar2mVar); for(i=0;i<nBVar;i++) names[i]=inames[i]; array[0]=add; onames[0]="Out"; file = open_file("cpl.dot", "w"); Cudd_DumpDot(mgr,1,array,names,onames,file); fclose(file); } nodes=g_hash_table_new(my_hash,my_equal); intBits=sizeof(unsigned int)*8; /* dividend is a global variable used by my_hash it is equal to an unsigned int with binary representation 11..1 */ dividend=1; for(j=1;j<intBits;j++) { dividend=(dividend<<1)+1; } out=YAP_MkFloatTerm(Prob(add,variables,bVar2mVar,nodes)); g_hash_table_foreach (nodes,dealloc,NULL); g_hash_table_destroy(nodes); Cudd_Quit(mgr); array_free(variables); array_free(bVar2mVar); array_free(expression); return(YAP_Unify(out,arg3)); }
NNQueens(int _n) : n(_n) { createVars(x, n, n, 1, n); vec<vec<IntVar*> > xt; transpose(x, xt); for (int i = 0; i < n; i++) { all_different(x[i]); all_different(xt[i]); } for (int d = 1; d < 2*n-2; d++) { vec<IntVar*> t; for (int i = 0; i < n; i++) { if (d-i < 0 || d-i >= n) continue; t.push(x[i][d-i]); } all_different(t); } for (int d = -(n-2); d <= n-2; d++) { vec<IntVar*> t; for (int i = 0; i < n; i++) { if (i-d < 0 || i-d >= n) continue; t.push(x[i][i-d]); } all_different(t); } vec<IntVar*> s; flatten(x, s); branch(s, VAR_INORDER, VAL_MIN); // branch(s, VAR_SIZE_MIN, VAL_MIN); output_vars(s); if (so.ldsb) { val_sym_ldsb(s, 1, n); // horizontal flip vec<IntVar*> sym1; for (int i = 0; i < n; i++) { for (int j = 0; j < n/2; j++) { sym1.push(x[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n/2; j++) { sym1.push(x[i][n-j-1]); } } var_seq_sym_ldsb(2, n*(n/2), sym1); // diagonal sym vec<IntVar*> sym2; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { sym2.push(x[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { sym2.push(x[j][i]); } } var_seq_sym_ldsb(2, n*(n-1)/2, sym2); } else if (so.sym_static) { for (int i = 0; i < n; i++) { int_rel(x[0][i], IRT_EQ, i+1); } } }