/// Print solution virtual void print(std::ostream& os) const { bool assigned = true; for (int i=0; i<succ.size(); i++) { if (!succ[i].assigned()) { assigned = false; break; } } if (assigned) { os << "\tTour: "; int i=0; do { os << i << " -> "; i=succ[i].val(); } while (i != 0); os << 0 << std::endl; os << "\tCost: " << total << std::endl; } else { os << "\tTour: " << std::endl; for (int i=0; i<succ.size(); i++) { os << "\t" << i << " -> " << succ[i] << std::endl; } os << "\tCost: " << total << std::endl; } }
virtual void print(std::ostream& os) const { os << "queens\t"; for (int i = 0; i < q.size(); i++) { os << q[i]; if ((i+1) % (int)sqrt(q.size()) == 0) { os << std::endl << "\t"; } } os << std::endl; }
/// Actual model GolombRuler(const SizeOptions& opt) : IntMinimizeScript(opt), m(*this,opt.size(),0, (opt.size() < 31) ? (1 << (opt.size()-1))-1 : Int::Limits::max) { // Assume first mark to be zero rel(*this, m[0], IRT_EQ, 0); // Order marks rel(*this, m, IRT_LE); // Number of marks and differences const int n = m.size(); const int n_d = (n*n-n)/2; // Array of differences IntVarArgs d(n_d); // Setup difference constraints for (int k=0, i=0; i<n-1; i++) for (int j=i+1; j<n; j++, k++) // d[k] is m[j]-m[i] and must be at least sum of first j-i integers rel(*this, d[k] = expr(*this, m[j]-m[i]), IRT_GQ, (j-i)*(j-i+1)/2); distinct(*this, d, opt.icl()); // Symmetry breaking if (n > 2) rel(*this, d[0], IRT_LE, d[n_d-1]); branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN()); }
/// Print solution virtual void print(std::ostream& os) const { os << "\t"; for (int i=0; i<x.size(); i++) os << "(" << x[i] << "," << y[i] << ") "; os << std::endl; }
/// Print solution virtual void print(std::ostream& os) const { const int n = x.size(); os << "\tx[" << n << "] = {"; for (int i = 0; i < n-1; i++) os << x[i] << "(" << d[i] << "),"; os << x[n-1] << "}" << std::endl; }
void among(Space& space, IntVarArray x, SetVar ss, IntVar a) { int n = x.size(); BoolVarArgs b(space, n, 0, 1); for(int i = 0; i < n; i++) { rel(space, ss, SRT_SUP, x[i], b[i]); } rel(space, sum(b) == a); }
/// Print solution virtual void print(std::ostream& os) const { os << "queens\t"; for (int i = 0; i < q.size(); i++) { os << q[i] << ", "; if ((i+1) % 10 == 0) os << std::endl << "\t"; } os << std::endl; }
/// Print solution virtual void print(std::ostream& os) const { os << "\t"; int a, b; a = b = 0; for (int i = 0; i < x.size(); i++) { a += x[i].val(); b += x[i].val()*x[i].val(); os << x[i] << ", "; } os << " = " << a << ", " << b << std::endl << "\t"; a = b = 0; for (int i = 0; i < y.size(); i++) { a += y[i].val(); b += y[i].val()*y[i].val(); os << y[i] << ", "; } os << " = " << a << ", " << b << std::endl; }
/// Print the solution virtual void print(std::ostream& os) const { os << "\tm = " << m << std::endl << "\tv[] = {"; for (int i = 0; i < v.size(); i++) { os << v[i] << ", "; if ((i+1) % 15 == 0) os << std::endl << "\t "; } os << "};" << std::endl; }
/// Actual model Alpha(const Options& opt) : Script(opt), le(*this,n,1,n) { IntVar a(le[ 0]), b(le[ 1]), c(le[ 2]), e(le[ 4]), f(le[ 5]), g(le[ 6]), h(le[ 7]), i(le[ 8]), j(le[ 9]), k(le[10]), l(le[11]), m(le[12]), n(le[13]), o(le[14]), p(le[15]), q(le[16]), r(le[17]), s(le[18]), t(le[19]), u(le[20]), v(le[21]), w(le[22]), x(le[23]), y(le[24]), z(le[25]); rel(*this, b+a+l+l+e+t == 45, opt.icl()); rel(*this, c+e+l+l+o == 43, opt.icl()); rel(*this, c+o+n+c+e+r+t == 74, opt.icl()); rel(*this, f+l+u+t+e == 30, opt.icl()); rel(*this, f+u+g+u+e == 50, opt.icl()); rel(*this, g+l+e+e == 66, opt.icl()); rel(*this, j+a+z+z == 58, opt.icl()); rel(*this, l+y+r+e == 47, opt.icl()); rel(*this, o+b+o+e == 53, opt.icl()); rel(*this, o+p+e+r+a == 65, opt.icl()); rel(*this, p+o+l+k+a == 59, opt.icl()); rel(*this, q+u+a+r+t+e+t == 50, opt.icl()); rel(*this, s+a+x+o+p+h+o+n+e == 134, opt.icl()); rel(*this, s+c+a+l+e == 51, opt.icl()); rel(*this, s+o+l+o == 37, opt.icl()); rel(*this, s+o+n+g == 61, opt.icl()); rel(*this, s+o+p+r+a+n+o == 82, opt.icl()); rel(*this, t+h+e+m+e == 72, opt.icl()); rel(*this, v+i+o+l+i+n == 100, opt.icl()); rel(*this, w+a+l+t+z == 34, opt.icl()); distinct(*this, le, opt.icl()); switch (opt.branching()) { case BRANCH_NONE: branch(*this, le, INT_VAR_NONE(), INT_VAL_MIN()); break; case BRANCH_INVERSE: branch(*this, le.slice(le.size()-1,-1), INT_VAR_NONE(), INT_VAL_MIN()); break; case BRANCH_SIZE: branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MIN()); break; } }
/// Actual model AllInterval(const SizeOptions& opt) : x(*this, opt.size(), 0, opt.size()-1), d(*this, opt.size()-1, 1, opt.size()-1) { const int n = x.size(); // Set up variables for distance for (int i=0; i<n-1; i++) rel(*this, d[i] == abs(x[i+1]-x[i]), opt.icl()); distinct(*this, x, opt.icl()); distinct(*this, d, opt.icl()); // Break mirror symmetry rel(*this, x[0], IRT_LE, x[1]); // Break symmetry of dual solution rel(*this, d[0], IRT_GR, d[n-2]); branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN()); }
/// Print solution virtual void print(std::ostream& os) const { #if 0 std::ostringstream outputstring1; #endif const int n = x.size(); os << "\tx[" << n << "] = {"; outputstring << "("; for (int i = 0; i < n-1; i++) { os << x[i] << "(" << abs(x[i+1].val()-x[i].val()) << "),"; outputstring << x[i] << " "; // outputstring << x[i] ; } outputstring << x[n-1] << ")"; os << x[n-1] << "}" << std::endl; // strcpy(result,outputstring.str().c_str()); }
/// The actual problem Queens(const SizeOptions& opt) : q(*this,opt.size(),0,opt.size()-1) { const int n = q.size(); switch (opt.propagation()) { case PROP_BINARY: for (int i = 0; i<n; i++) for (int j = i+1; j<n; j++) { rel(*this, q[i] != q[j]); rel(*this, q[i]+i != q[j]+j); rel(*this, q[i]-i != q[j]-j); } break; case PROP_MIXED: for (int i = 0; i<n; i++) for (int j = i+1; j<n; j++) { rel(*this, q[i]+i != q[j]+j); rel(*this, q[i]-i != q[j]-j); } distinct(*this, q, opt.icl()); break; case PROP_DISTINCT: distinct(*this, IntArgs::create(n,0,1), q, opt.icl()); distinct(*this, IntArgs::create(n,0,-1), q, opt.icl()); distinct(*this, q, opt.icl()); break; } switch(opt.branching()) { case BRANCH_MIN: branch(*this, q, INT_VAR_SIZE_MIN(), INT_VAL_MIN()); break; case BRANCH_MID: branch(*this, q, INT_VAR_SIZE_MIN(), INT_VAL_MIN()); break; case BRANCH_MAX_MAX: branch(*this, q, INT_VAR_SIZE_MAX(), INT_VAL_MIN()); break; case BRANCH_KNIGHT_MOVE: branch(*this, q, INT_VAR_MIN_MIN(), INT_VAL_MED()); break; } }
/// Actual model AllInterval(const SizeOptions& opt) : x(*this, opt.size(), 0, opt.size() - 1) { const int n = x.size(); IntVarArgs d(n-1); // Set up variables for distance for (int i=0; i<n-1; i++) d[i] = expr(*this, abs(x[i+1]-x[i]), opt.icl()); // Constrain them to be between 1 and n-1 dom(*this, d, 1, n-1); distinct(*this, x, opt.icl()); distinct(*this, d, opt.icl()); // Break mirror symmetry rel(*this, x[0], IRT_LE, x[1]); // Break symmetry of dual solution rel(*this, d[0], IRT_GR, d[n-2]); branch(*this, x, INT_VAR_SIZE_MIN, INT_VAL_SPLIT_MIN); }
/// Construction of the model. Pentominoes(const SizeOptions& opt) : spec(examples[opt.size()]), width(spec[0].width+1), // Add one for extra row at end. height(spec[0].height), filled(spec[0].amount), nspecs(examples_size[opt.size()]-1), ntiles(compute_number_of_tiles(spec+1, nspecs)), board(*this, width*height, filled,ntiles+1) { spec += 1; // No need for the specification-part any longer // Set end-of-line markers for (int h = 0; h < height; ++h) { for (int w = 0; w < width-1; ++w) rel(*this, board[h*width + w], IRT_NQ, ntiles+1); rel(*this, board[h*width + width - 1], IRT_EQ, ntiles+1); } // Post constraints if (opt.propagation() == PROPAGATION_INT) { int tile = 0; for (int i = 0; i < nspecs; ++i) { for (int j = 0; j < spec[i].amount; ++j) { // Color int col = tile+1; // Expression for color col REG mark(col); // Build expression for complement to color col REG other; bool first = true; for (int j = filled; j <= ntiles; ++j) { if (j == col) continue; if (first) { other = REG(j); first = false; } else { other |= REG(j); } } // End of line marker REG eol(ntiles+1); extensional(*this, board, get_constraint(i, mark, other, eol)); ++tile; } } } else { // opt.propagation() == PROPAGATION_BOOLEAN int ncolors = ntiles + 2; // Boolean variables for channeling BoolVarArgs p(*this,ncolors * board.size(),0,1); // Post channel constraints for (int i=board.size(); i--; ) { BoolVarArgs c(ncolors); for (int j=ncolors; j--; ) c[j]=p[i*ncolors+j]; channel(*this, c, board[i]); } // For placing tile i, we construct the expression over // 0/1-variables and apply it to the projection of // the board on the color for the tile. REG other(0), mark(1); int tile = 0; for (int i = 0; i < nspecs; ++i) { for (int j = 0; j < spec[i].amount; ++j) { int col = tile+1; // Projection for color col BoolVarArgs c(board.size()); for (int k = board.size(); k--; ) { c[k] = p[k*ncolors+col]; } extensional(*this, c, get_constraint(i, mark, other, other)); ++tile; } } } if (opt.symmetry() == SYMMETRY_FULL) { // Remove symmetrical boards IntVarArgs orig(board.size()-height), symm(board.size()-height); int pos = 0; for (int i = 0; i < board.size(); ++i) { if ((i+1)%width==0) continue; orig[pos++] = board[i]; } int w2, h2; bsymmfunc syms[] = {flipx, flipy, flipd1, flipd2, rot90, rot180, rot270}; int symscnt = sizeof(syms)/sizeof(bsymmfunc); for (int i = 0; i < symscnt; ++i) { syms[i](orig, width-1, height, symm, w2, h2); if (width-1 == w2 && height == h2) rel(*this, orig, IRT_LQ, symm); } } // Install branching branch(*this, board, INT_VAR_NONE, INT_VAL_MIN); }
/// Actual model AllInterval(const SizeOptions& opt) : Script(opt), x(*this, opt.size(), 0, 66) { // 66 or opt.size() - 1 const int n = x.size(); IntVarArgs d(n-1); IntVarArgs dd(66); IntVarArgs xx_(n); // pitch class for AllInterval Chords IntVar douze; Rnd r(1U); if ((opt.model() == MODEL_SET) || (opt.model() == MODEL_SET_CHORD) || (opt.model() == MODEL_SSET_CHORD) ||(opt.model() == MODEL_SYMMETRIC_SET)) // Modele original : serie { // Set up variables for distance for (int i=0; i<n-1; i++) d[i] = expr(*this, abs(x[i+1]-x[i]), opt.ipl()); // Constrain them to be between 1 and n-1 dom(*this, d, 1, n-1); dom(*this, x, 0, n-1); if((opt.model() == MODEL_SET_CHORD) || (opt.model() == MODEL_SSET_CHORD)) { /*expr(*this,dd[0]==0); // Set up variables for distance for (int i=0; i<n-1; i++) { expr(*this, dd[i+1] == (dd[i]+d[i])%12, opt.icl()); } // Constrain them to be between 1 and n-1 dom(*this, dd,0, n-1); distinct(*this, dd, opt.icl()); */ rel(*this, abs(x[0]-x[n-1]) == 6, opt.ipl()); } if(opt.symmetry()) { // Break mirror symmetry (renversement) rel(*this, x[0], IRT_LE, x[1]); // Break symmetry of dual solution (retrograde de la serie) -> 1928 solutions pour accords de 12 sons rel(*this, d[0], IRT_GR, d[n-2]); } //series symetriques if ((opt.model() == MODEL_SYMMETRIC_SET)|| (opt.model() == MODEL_SSET_CHORD)) { rel (*this, d[n/2 - 1] == 6); // pivot = triton for (int i=0; i<(n/2)-2; i++) rel(*this,d[i]+d[n-i-2]==12); } } else { for (int j=0; j<n; j++) xx_[j] = expr(*this, x[j] % 12); dom(*this, xx_, 0, 11); distinct(*this, xx_, opt.ipl()); //intervalles for (int i=0; i<n-1; i++) d[i] = expr(*this,x[i+1] - x[i],opt.ipl()); dom(*this, d, 1, n-1); dom(*this, x, 0, n * (n - 1) / 2.); //d'autres choses dont on est certain (contraintes redondantes) : rel(*this, x[0] == 0); rel(*this, x[n-1] == n * (n - 1) / 2.); // break symmetry of dual solution (renversement de l'accord) if(opt.symmetry()) rel(*this, d[0], IRT_GR, d[n-2]); //accords symetriques if (opt.model() == MODEL_SYMMETRIC_CHORD) { rel (*this, d[n/2 - 1] == 6); // pivot = triton for (int i=0; i<(n/2)-2; i++) rel(*this,d[i]+d[n-i-2]==12); } if (opt.model() == MODEL_PARALLEL_CHORD) { rel (*this, d[n/2 - 1] == 6); // pivot = triton for (int i=0; i<(n/2)-2; i++) rel(*this,d[i]+d[n/2 + i]==12); } } distinct(*this, x, opt.ipl()); distinct(*this, d, opt.ipl()); #if 0 //TEST IntVarArray counter(*this,12,0,250); IntVar testcounter(*this,0,250); for (int i=1; i<11; i++) { count(*this, d, i,IRT_EQ,testcounter,opt.icl()); // OK } count(*this, d, counter,opt.icl()); // OK #endif //END TEST if(opt.branching() == 0) branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN()); else branch(*this, x, INT_VAR_RND(r), INT_VAL_RND(r)); }
void print(std::ostream& os) const { for (int index = 0; index < q.size(); ++index) { os << q[index] << " "; } os << endl << endl; }
/// Print solution virtual void print(std::ostream& os) const { os << "\tm[" << m.size() << "] = " << m << std::endl; }
/// Return cost virtual IntVar cost(void) const { return m[m.size()-1]; }
/// The model of the problem CrowdedChess(const SizeOptions& opt) : n(opt.size()), s(*this, n*n, 0, PMAX-1), queens(*this, n, 0, n-1), rooks(*this, n, 0, n-1), knights(*this, n*n, 0, 1) { const int nkval = sizeof(kval)/sizeof(int); const int nn = n*n, q = n, r = n, b = (2*n)-2, k = n <= nkval ? kval[n-1] : kval[nkval-1]; const int e = nn - (q + r + b + k); assert(nn == (e + q + r + b + k)); Matrix<IntVarArray> m(s, n); // *********************** // Basic model // *********************** count(*this, s, E, IRT_EQ, e, opt.icl()); count(*this, s, Q, IRT_EQ, q, opt.icl()); count(*this, s, R, IRT_EQ, r, opt.icl()); count(*this, s, B, IRT_EQ, b, opt.icl()); count(*this, s, K, IRT_EQ, k, opt.icl()); // Collect rows and columns for handling rooks and queens for (int i = 0; i < n; ++i) { IntVarArgs aa = m.row(i), bb = m.col(i); count(*this, aa, Q, IRT_EQ, 1, opt.icl()); count(*this, bb, Q, IRT_EQ, 1, opt.icl()); count(*this, aa, R, IRT_EQ, 1, opt.icl()); count(*this, bb, R, IRT_EQ, 1, opt.icl()); // Connect (queens|rooks)[i] to the row it is in element(*this, aa, queens[i], Q, ICL_DOM); element(*this, aa, rooks[i], R, ICL_DOM); } // N-queens constraints distinct(*this, queens, ICL_DOM); distinct(*this, IntArgs::create(n,0,1), queens, ICL_DOM); distinct(*this, IntArgs::create(n,0,-1), queens, ICL_DOM); // N-rooks constraints distinct(*this, rooks, ICL_DOM); // Collect diagonals for handling queens and bishops for (int l = n; l--; ) { const int il = (n-1) - l; IntVarArgs d1(l+1), d2(l+1), d3(l+1), d4(l+1); for (int i = 0; i <= l; ++i) { d1[i] = m(i+il, i); d2[i] = m(i, i+il); d3[i] = m((n-1)-i-il, i); d4[i] = m((n-1)-i, i+il); } count(*this, d1, Q, IRT_LQ, 1, opt.icl()); count(*this, d2, Q, IRT_LQ, 1, opt.icl()); count(*this, d3, Q, IRT_LQ, 1, opt.icl()); count(*this, d4, Q, IRT_LQ, 1, opt.icl()); if (opt.propagation() == PROP_DECOMPOSE) { count(*this, d1, B, IRT_LQ, 1, opt.icl()); count(*this, d2, B, IRT_LQ, 1, opt.icl()); count(*this, d3, B, IRT_LQ, 1, opt.icl()); count(*this, d4, B, IRT_LQ, 1, opt.icl()); } } if (opt.propagation() == PROP_TUPLE_SET) { IntVarArgs b(s.size()); for (int i = s.size(); i--; ) b[i] = channel(*this, expr(*this, (s[i] == B))); extensional(*this, b, bishops, EPK_DEF, opt.icl()); } // Handle knigths // Connect knigths to board for(int i = n*n; i--; ) knights[i] = expr(*this, (s[i] == K)); knight_constraints(); // *********************** // Redundant constraints // *********************** // Queens and rooks not in the same place // Faster than going through the channelled board-connection for (int i = n; i--; ) rel(*this, queens[i], IRT_NQ, rooks[i]); // Place bishops in two corners (from Schimpf and Hansens solution) // Avoids some symmetries of the problem rel(*this, m(n-1, 0), IRT_EQ, B); rel(*this, m(n-1, n-1), IRT_EQ, B); // *********************** // Branching // *********************** // Place each piece in turn branch(*this, s, INT_VAR_MIN_MIN(), INT_VAL_MIN()); }