Actual model
| 421 | }; |
| 422 | /// Actual model |
| 423 | BinPacking(const InstanceOptions& opt) |
| 424 | : IntMinimizeScript(opt), |
| 425 | spec(opt.instance()), |
| 426 | load(*this, spec.upper(), 0, spec.capacity()), |
| 427 | bin(*this, spec.items(), 0, spec.upper()-1), |
| 428 | bins(*this, spec.lower(), spec.upper()) { |
| 429 | // Number of items |
| 430 | int n = bin.size(); |
| 431 | // Number of bins |
| 432 | int m = load.size(); |
| 433 | |
| 434 | // Size of all items |
| 435 | int s = 0; |
| 436 | for (int i=0; i<n; i++) |
| 437 | s += spec.size(i); |
| 438 | |
| 439 | // Array of sizes |
| 440 | IntArgs sizes(n); |
| 441 | for (int i=0; i<n; i++) |
| 442 | sizes[i] = spec.size(i); |
| 443 | |
| 444 | switch (opt.model()) { |
| 445 | case MODEL_NAIVE: |
| 446 | { |
| 447 | // All loads must add up to all item sizes |
| 448 | linear(*this, load, IRT_EQ, s); |
| 449 | |
| 450 | // Load must be equal to packed items |
| 451 | BoolVarArgs _x(*this, n*m, 0, 1); |
| 452 | Matrix<BoolVarArgs> x(_x, n, m); |
| 453 | |
| 454 | for (int i=0; i<n; i++) |
| 455 | channel(*this, x.col(i), bin[i]); |
| 456 | |
| 457 | for (int j=0; j<m; j++) |
| 458 | linear(*this, sizes, x.row(j), IRT_EQ, load[j]); |
| 459 | } |
| 460 | break; |
| 461 | case MODEL_PACKING: |
| 462 | binpacking(*this, load, bin, sizes); |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | // Break symmetries, only if not using CBDF |
| 467 | if (opt.branching() != BRANCH_CDBF) |
| 468 | for (int i=1; i<n; i++) |
| 469 | if (spec.size(i-1) == spec.size(i)) |
| 470 | rel(*this, bin[i-1] <= bin[i]); |
| 471 | |
| 472 | // Pack items that require a bin for sure! (wlog) |
| 473 | { |
| 474 | int i = 0; |
| 475 | // These items all need a bin due to their own size |
| 476 | for (; (i < n) && (i < m) && (spec.size(i) * 2 > spec.capacity()); i++) |
| 477 | rel(*this, bin[i] == i); |
| 478 | // Check if the next item cannot fit to position i-1 |
| 479 | if ((i < n) && (i < m) && (i > 0) && |
| 480 | (spec.size(i-1) + spec.size(i) > spec.capacity())) |
nothing calls this directly
no test coverage detected