1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Copyright:
* Mikael Lagerkvist, 2009
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <iomanip>
using namespace Gecode;
// Problems
namespace {
// Problem data
extern const int* problems[];
// Number of problems
extern const unsigned int n_problems;
}
namespace {
/**
* \brief %Options for car sequencing problems
*
* \relates CarSequence
*/
class CarOptions : public SizeOptions {
private:
/// Max slack parameter
Driver::UnsignedIntOption _maxstall;
public:
/// Initialize options for example with name \a s
CarOptions(const char* s)
: SizeOptions(s),
_maxstall("maxstall", "Maximum numbere of stalls", 30)
{
// Add options
add(_maxstall);
}
/// Parse options from arguments \a argv (number is \a argc)
void parse(int& argc, char* argv[]) {
SizeOptions::parse(argc,argv);
}
/// Get max stalls
int maxstall(void) const { return _maxstall.value(); }
};
/**
* \brief Propagator that pushes all occurrences of a value to the end.
*
* This propagator uses a variable array \f$x=\langle
* x_1,x_2,\ldots,x_n\rangle\f$, a variable \f$y\f$, and a value
* \f$val\f$. It It makes sure that the last \f$y\f$ variables of
* \f$x\f$ are assigned the value, and that the value does not
* appear in the rest of the array. Furthermore, the constraint
* ensure that \$fval\$f isnot adjacent to \$fval-1\$f.
*
* Since the propagator is custom-made for the car sequencing
* example, it relies on the fact that the value will be equal to
* the upper bound to speed up computation. For example, it can
* safely rely on only subscribing to bound events.
*
* \relates CarSequence
*/
template <class View>
class PushToEnd : public NaryOnePropagator<View,Int::PC_INT_BND> {
protected:
using NaryOnePropagator<View,Int::PC_INT_BND>::x;
using NaryOnePropagator<View,Int::PC_INT_BND>::y;
int val;
/// Constructor for cloning \a p
PushToEnd(Space& home, PushToEnd& p);
/// Constructor for posting
PushToEnd(Space& home, ViewArray<View>& x0, View y0, int val0);
public:
/// Constructor for rewriting \a p during cloning
PushToEnd(Space& home, Propagator& p,
ViewArray<View>& x0, View y0, int val0);
/// Copy propagator during cloning
virtual Actor* copy(Space& home);
/// Perform propagation
virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
/// Post propagator
static ExecStatus post(Space& home,
ViewArray<View>& x0, View y0, int val0);
};
template <class View>
inline
PushToEnd<View>::PushToEnd(Space& home,
ViewArray<View>& x0, View y0, int val0)
: NaryOnePropagator<View,Int::PC_INT_BND>(home,x0,y0), val(val0) {}
template <class View>
ExecStatus
PushToEnd<View>::post(Space& home,
ViewArray<View>& x0, View y0, int val0) {
(void) new (home) PushToEnd<View>(home,x0,y0,val0);
return ES_OK;
}
template <class View>
inline
PushToEnd<View>::PushToEnd(Space& home, PushToEnd<View>& p)
: NaryOnePropagator<View,Int::PC_INT_BND>(home,p), val(p.val) {}
template <class View>
inline
PushToEnd<View>::PushToEnd(Space& home, Propagator& p,
ViewArray<View>& x0, View y0, int val0)
: NaryOnePropagator<View,Int::PC_INT_BND>(home,p,x0,y0), val(val0) {}
template <class View>
Actor*
PushToEnd<View>::copy(Space& home) {
return new (home) PushToEnd<View>(home,*this);
}
template <class View>
ExecStatus
PushToEnd<View>::propagate(Space& home, const ModEventDelta&) {
// Find number of required positions
int min = 0;
for (int i = x.size(); i-- && x[i].min() >= val-1; ) {
++min;
}
// Find number of possible positions
int max = 0;
{
int i = x.size();
while (i--) {
if (x[i].max() != val) break;
++max;
if (max >= y.max()) break;
}
// No variables later than max can have value val
while (i--) {
GECODE_ME_CHECK(x[i].le(home, val));
}
}
// Constrain y to be in {min..max}
GECODE_ME_CHECK(y.gq(home, min));
GECODE_ME_CHECK(y.lq(home, max));
// At least the y.min() last values have value val
for (int i = 0, pos = x.size()-1; i < y.min(); ++i, --pos) {
GECODE_ME_CHECK(x[pos].eq(home, val));
}
return y.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
}
/** \brief Post PushToEnd propagator.
*/
void pushtoend(Space& home, IntVarArgs x, IntVar y, int val) {
ViewArray<Int::IntView> vx(home, x);
Int::IntView vy(y);
GECODE_ES_FAIL(PushToEnd<Int::IntView>::post(home, vx, vy, val));
}
}
/**
* \brief %Example: Car sequencing
*
* See problem 1 at http://www.csplib.org/.
*
* This model uses extra stall-slots instead of violations, as proposed
* in "Combining Forces to Solve the Car Sequencing Problem", Perron
* and Shaw, CPAIOR 2004.
*
* \ingroup Example
*/
class CarSequencing : public Script {
public:
/// Branching variants
enum {
BRANCH_INORDER, ///< Branch from left to right
BRANCH_MIDDLE ///< Branch from middle out
};
/// Propagation variants
enum {
PROP_REGULAR, ///< Use regular constraints
PROP_CUSTOM ///< Use custom constraint
};
protected:
/// Problem number
const int problem;
/// Number of cars
const int ncars;
/// Number of options
const int noptions;
/// Number of classes
const int nclasses;
/// Maximum number of stalls
const int maxstall;
/// Stall number
const int stallval;
/// End number
const int endval;
/// Number of stalls (cost to minimize)
IntVar nstall;
/// Number of end markers
IntVar nend;
/// Sequence of cars produced
IntVarArray s;
public:
/// Initial model
CarSequencing(const CarOptions& opt)
: Script(opt),
problem(opt.size()),
ncars(problems[problem][0]),
noptions(problems[problem][1]),
nclasses(problems[problem][2]),
maxstall(opt.maxstall()),
stallval(nclasses),
endval(nclasses+1),
nstall(*this, 0, maxstall),
nend(*this, 0, maxstall),
s(*this, ncars+maxstall, 0, nclasses+1)
{
// Read problem
const int* probit = problems[problem] + 3;
// Sequence requirements for the options.
IntArgs max(noptions), block(noptions);
for (int i = 0; i < noptions; ++i ) {
max[i] = *probit++;
}
for (int i = 0; i < noptions; ++i ) {
block[i] = *probit++;
}
// Number of cars per class
IntArgs ncc(nclasses);
// What classes require an option
IntSetArgs classes(noptions);
int** cdata = new int*[noptions];
for (int i = noptions; i--; ) cdata[i] = new int[nclasses];
int* n = new int[noptions];
for (int i = noptions; i--; ) n[i] = 0;
// Read data
for (int c = 0; c < nclasses; ++c) {
probit++;
ncc[c] = *probit++;
for (int o = 0; o < noptions; ++o) {
if (*probit++) {
cdata[o][n[o]++] = c;
}
}
}
// Transfer specification data to int-sets
for (int o = noptions; o--; ) {
classes[o] = IntSet(cdata[o], n[o]);
delete [] cdata[o];
}
delete [] cdata;
delete [] n;
// Count the cars
{
IntSetArgs c(nclasses+2);
for (int i = nclasses; i--; ) {
c[i] = IntSet(ncc[i], ncc[i]);
}
c[stallval] = IntSet(0, maxstall);
c[ endval] = IntSet(0, maxstall);
count(*this, s, c);
}
// Count number of end and stalls
count(*this, s, stallval, IRT_EQ, nstall);
count(*this, s, endval, IRT_EQ, nend);
rel(*this, nstall+nend == maxstall);
// Make sure nothing is overloaded
IntSet one(1, 1);
for (int o = noptions; o--; ) {
// sb[i] reflects if car s[i] has option o
BoolVarArgs sb(s.size());
for (int i = s.size(); i--; ) {
BoolVar b(*this, 0, 1);
dom(*this, s[i], classes[o], b);
sb[i] = b;
}
sequence(*this, sb, one, block[o], 0, max[o]);
}
// End-markers located at end only
switch (opt.propagation()) {
case PROP_REGULAR: {
IntArgs notend(nclasses), notstallend(nclasses+1);
for (int i = nclasses; i--; ) {
notend[i] = i;
notstallend[i] = i;
}
notstallend[nclasses] = stallval;
REG r = *REG(notend) + REG(notstallend) + *REG(endval);
extensional(*this, s, r);
for (int pos = s.size()-1, i = 0; i < maxstall; ++i, --pos) {
rel(*this, (nend > i) >> (s[pos]==endval));
}
} break;
case PROP_CUSTOM: {
pushtoend(*this, s, nend, endval);
} break;
}
// Branching
switch (opt.branching()) {
case BRANCH_INORDER:
branch(*this, s, INT_VAR_NONE(), INT_VAL_MIN());
break;
case BRANCH_MIDDLE: {
IntVarArgs m(s.size());
int mid = s.size() / 2;
int pos = 0;
m[pos++] = s[mid];
for (int i = 1; i <= m.size()/2; ++i) {
if (mid-i >= 0)
m[pos++] = s[mid-i];
if (mid+i < s.size())
m[pos++] = s[mid+i];
}
assert(pos == m.size());
branch(*this, m, INT_VAR_NONE(), INT_VAL_MIN());
break;
}
}
}
/// Return cost
virtual void constrain(const Space& _best) {
const CarSequencing& best = static_cast<const CarSequencing&>(_best);
rel(*this, nstall, IRT_LE, best.nstall.val());
}
/// Print solution
virtual void
print(std::ostream& os) const {
int width = nclasses > 9 ? 2 : 1;
const char* space = nclasses > 9 ? " " : "";
os << "Stall slots=" << nstall
<< ", End slots=" << nend << std::endl;
int i = 0;
for (; i < s.size(); ++i) {
if (s[i].assigned()) {
int v = s[i].val();
if (v == endval) break;
if (v == stallval) os << space << "_ ";
else os << std::setw(width) << v << " ";
} else {
os << space << "? ";
}
if ((i+1)%20 == 0) os << std::endl;
}
if (i%20)
os << std::endl;
os << std::endl;
}
/// Constructor for cloning \a s
CarSequencing(CarSequencing& cs)
: Script(cs),
problem(cs.problem),
ncars(cs.ncars),
noptions(cs.noptions),
nclasses(cs.nclasses),
maxstall(cs.maxstall),
stallval(cs.stallval),
endval(cs.endval)
{
nstall.update(*this, cs.nstall);
nend.update(*this, cs.nend);
s.update(*this, cs.s);
}
/// Copy during cloning
virtual Space*
copy(void) {
return new CarSequencing(*this);
}
};
/** \brief Main-function
* \relates CarSequencing
*/
int
main(int argc, char* argv[]) {
CarOptions opt("CarSequencing");
opt.solutions(0);
opt.size(0);
opt.branching(CarSequencing::BRANCH_INORDER);
opt.branching(CarSequencing::BRANCH_INORDER, "inorder");
opt.branching(CarSequencing::BRANCH_MIDDLE, "middle");
opt.propagation(CarSequencing::PROP_CUSTOM);
opt.propagation(CarSequencing::PROP_REGULAR, "regular");
opt.propagation(CarSequencing::PROP_CUSTOM, "custom");
opt.parse(argc,argv);
if (opt.size() >= n_problems) {
std::cerr << "Error: size must be between 0 and "
<< n_problems-1 << std::endl;
return 1;
}
Script::run<CarSequencing,BAB,CarOptions>(opt);
return 0;
}
namespace {
/// Problems from CSPLib
/// Simple initial example
const int p0[] = {
10, 5, 6,
1, 2, 1, 2, 1,
2, 3, 3, 5, 5,
0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 0, 1, 0,
2, 2, 0, 1, 0, 0, 1,
3, 2, 0, 1, 0, 1, 0,
4, 2, 1, 0, 1, 0, 0,
5, 2, 1, 1, 0, 0, 0
};
// ---------------------------------
// Problem 4/72 (Regin & Puget // 1)
// ---------------------------------
const int p1[] = {
100, 5, 22,
1, 2, 1, 2, 1,
2, 3, 3, 5, 5,
0, 6, 1, 0, 0, 1, 0,
1, 10, 1, 1, 1, 0, 0,
2, 2, 1, 1, 0, 0, 1,
3, 2, 0, 1, 1, 0, 0,
4, 8, 0, 0, 0, 1, 0,
5, 15, 0, 1, 0, 0, 0,
6, 1, 0, 1, 1, 1, 0,
7, 5, 0, 0, 1, 1, 0,
8, 2, 1, 0, 1, 1, 0,
9, 3, 0, 0, 1, 0, 0,
10, 2, 1, 0, 1, 0, 0,
11, 1, 1, 1, 1, 0, 1,
12, 8, 0, 1, 0, 1, 0,
13, 3, 1, 0, 0, 1, 1,
14, 10, 1, 0, 0, 0, 0,
15, 4, 0, 1, 0, 0, 1,
16, 4, 0, 0, 0, 0, 1,
17, 2, 1, 0, 0, 0, 1,
18, 4, 1, 1, 0, 0, 0,
19, 6, 1, 1, 0, 1, 0,
20, 1, 1, 0, 1, 0, 1,
21, 1, 1, 1, 1, 1, 1,
};
// --------------------------------
// Problem 6/76, (Regin & Puget // 2)
// --------------------------------
const int p2[] = {
100, 5, 22,
1, 2, 1, 2, 1,
2, 3, 3, 5, 5,
0, 13, 1, 0, 0, 0, 0,
1, 8, 0, 0, 0, 1, 0,
2, 7, 0, 1, 0, 0, 0,
3, 1, 1, 0, 0, 1, 0,
4, 12, 0, 0, 1, 0, 0,
5, 5, 0, 1, 0, 1, 0,
6, 5, 0, 0, 1, 1, 0,
7, 6, 0, 1, 1, 0, 0,
8, 3, 1, 0, 0, 0, 1,
9, 12, 1, 1, 0, 0, 0,
10, 8, 1, 1, 0, 1, 0,
11, 2, 1, 0, 0, 1, 1,
12, 2, 1, 1, 1, 0, 0,
13, 1, 0, 1, 0, 1, 1,
14, 4, 1, 0, 1, 0, 0,
15, 4, 0, 1, 0, 0, 1,
16, 1, 1, 1, 0, 1, 1,
17, 2, 1, 0, 1, 1, 0,
18, 1, 0, 0, 0, 0, 1,
19, 1, 1, 1, 1, 1, 0,
20, 1, 1, 1, 0, 0, 1,
21, 1, 0, 1, 1, 1, 0,
};
// ---------------------------------
// Problem 10/93, (Regin & Puget // 3)
// ---------------------------------
const int p3[] = {
100, 5, 25,
1, 2, 1, 2, 1,
2, 3, 3, 5, 5,
0, 7, 1, 0, 0, 1, 0,
1, 11, 1, 1, 0, 0, 0,
2, 1, 0, 1, 1, 1, 1,
3, 3, 1, 0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,
24, 1, 1, 1, 1, 1, 1,
25, 1, 1, 1, 1, 1, 0,
};
// ----------------------------------
// Problem 19/71, (Regin & Puget // 4)
// ----------------------------------
const int p5[] = {
100, 5, 23,
1, 2, 1, 2, 1,
2, 3, 3, 5, 5,
0, 2, 0, 0, 0, 1, 1,
1, 2, 0, 0, 1, 0, 1,
2, 5, 0, 1, 1, 1, 0,
3, 4, 0, 0, 0, 1, 0,
4, 4, 0, 1, 0, 1, 0,
5, 1, 1, 1, 0, 0, 1,
6, 3, 1, 1, 1, 0, 1,
7, 4, 0, 0, 1, 0, 0,
8, 19, 0, 1, 0, 0, 0,
9, 7, 1, 1, 0, 1, 0,
10, 10, 1, 0, 0, 0, 0,
11, 1, 0, 0, 1, 1, 0,
12, 5, 1, 1, 1, 1, 0,
13, 2, 1, 0, 1, 1, 0,
14, 6, 1, 1, 0, 0, 0,
15, 4, 1, 1, 1, 0, 0,
16, 8, 1, 0, 0, 1, 0,
17, 1, 1, 0, 0, 0, 1,
18, 4, 0, 1, 1, 0, 0,
19, 2, 0, 0, 0, 0, 1,
20, 4, 0, 1, 0, 0, 1,
21, 1, 1, 1, 0, 1, 1,
22, 1, 0, 1, 1, 0, 1,
};
const int* problems[] = {
&p0[0],
&p1[0],
&p2[0],
&p3[0],
&p4[0],
&p5[0],
};
/// The number of instances
const unsigned int n_problems = sizeof(problems)/sizeof(int*);
};
// STATISTICS: example-any
|