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); }
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); }
constexpr auto all_different(const T1 &a, const T2 &b, const T2s &... bs) { return all(a != b, a != bs...) && all_different(b, bs...); }
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); } } }