/usr/include/gecode/int/linear/int-nary.hpp is in libgecode-dev 5.1.0-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2003
*
* Last modified:
* $Date: 2016-06-29 17:28:17 +0200 (Wed, 29 Jun 2016) $ by $Author: schulte $
* $Revision: 15137 $
*
* 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/int/linear/int-noview.hpp>
namespace Gecode { namespace Int { namespace Linear {
/**
* \brief Test if only unit-coefficient arrays used
*
*/
template<class P, class N>
forceinline bool
isunit(ViewArray<P>&, ViewArray<N>&) { return false; }
template<>
forceinline bool
isunit(ViewArray<IntView>&, ViewArray<IntView>&) { return true; }
template<>
forceinline bool
isunit(ViewArray<IntView>&, ViewArray<NoView>&) { return true; }
template<>
forceinline bool
isunit(ViewArray<NoView>&, ViewArray<IntView>&) { return true; }
/*
* Linear propagators
*
*/
template<class Val, class P, class N, PropCond pc>
forceinline
Lin<Val,P,N,pc>::Lin(Home home, ViewArray<P>& x0, ViewArray<N>& y0, Val c0)
: Propagator(home), x(x0), y(y0), c(c0) {
x.subscribe(home,*this,pc);
y.subscribe(home,*this,pc);
}
template<class Val, class P, class N, PropCond pc>
forceinline
Lin<Val,P,N,pc>::Lin(Space& home, bool share, Lin<Val,P,N,pc>& p)
: Propagator(home,share,p), c(p.c) {
x.update(home,share,p.x);
y.update(home,share,p.y);
}
template<class Val, class P, class N, PropCond pc>
PropCost
Lin<Val,P,N,pc>::cost(const Space&, const ModEventDelta&) const {
return PropCost::linear(PropCost::LO, x.size()+y.size());
}
template<class Val, class P, class N, PropCond pc>
void
Lin<Val,P,N,pc>::reschedule(Space& home) {
x.reschedule(home,*this,pc);
y.reschedule(home,*this,pc);
}
template<class Val, class P, class N, PropCond pc>
forceinline size_t
Lin<Val,P,N,pc>::dispose(Space& home) {
x.cancel(home,*this,pc);
y.cancel(home,*this,pc);
(void) Propagator::dispose(home);
return sizeof(*this);
}
/*
* Reified linear propagators
*
*/
template<class Val, class P, class N, PropCond pc, class Ctrl>
forceinline
ReLin<Val,P,N,pc,Ctrl>::ReLin
(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c, Ctrl b0)
: Lin<Val,P,N,pc>(home,x,y,c), b(b0) {
b.subscribe(home,*this,PC_INT_VAL);
}
template<class Val, class P, class N, PropCond pc, class Ctrl>
forceinline
ReLin<Val,P,N,pc,Ctrl>::ReLin
(Space& home, bool share, ReLin<Val,P,N,pc,Ctrl>& p)
: Lin<Val,P,N,pc>(home,share,p) {
b.update(home,share,p.b);
}
template<class Val, class P, class N, PropCond pc, class Ctrl>
void
ReLin<Val,P,N,pc,Ctrl>::reschedule(Space& home) {
x.reschedule(home,*this,pc);
y.reschedule(home,*this,pc);
b.reschedule(home,*this,PC_INT_VAL);
}
template<class Val, class P, class N, PropCond pc, class Ctrl>
forceinline size_t
ReLin<Val,P,N,pc,Ctrl>::dispose(Space& home) {
b.cancel(home,*this,PC_BOOL_VAL);
(void) Lin<Val,P,N,pc>::dispose(home);
return sizeof(*this);
}
/*
* Computing bounds
*
*/
template<class Val, class View>
void
bounds_p(ModEventDelta med, ViewArray<View>& x, Val& c, Val& sl, Val& su) {
int n = x.size();
if (IntView::me(med) == ME_INT_VAL) {
for (int i = n; i--; ) {
Val m = x[i].min();
if (x[i].assigned()) {
c -= m; x[i] = x[--n];
} else {
sl -= m; su -= x[i].max();
}
}
x.size(n);
} else {
for (int i = n; i--; ) {
sl -= x[i].min(); su -= x[i].max();
}
}
}
template<class Val, class View>
void
bounds_n(ModEventDelta med, ViewArray<View>& y, Val& c, Val& sl, Val& su) {
int n = y.size();
if (IntView::me(med) == ME_INT_VAL) {
for (int i = n; i--; ) {
Val m = y[i].max();
if (y[i].assigned()) {
c += m; y[i] = y[--n];
} else {
sl += m; su += y[i].min();
}
}
y.size(n);
} else {
for (int i = n; i--; ) {
sl += y[i].max(); su += y[i].min();
}
}
}
template<class Val, class P, class N>
ExecStatus
prop_bnd(Space& home, ModEventDelta med, Propagator& p,
ViewArray<P>& x, ViewArray<N>& y, Val& c) {
// Eliminate singletons
Val sl = 0;
Val su = 0;
bounds_p<Val,P>(med, x, c, sl, su);
bounds_n<Val,N>(med, y, c, sl, su);
if ((IntView::me(med) == ME_INT_VAL) && ((x.size() + y.size()) <= 1)) {
if (x.size() == 1) {
GECODE_ME_CHECK(x[0].eq(home,c));
return home.ES_SUBSUMED(p);
}
if (y.size() == 1) {
GECODE_ME_CHECK(y[0].eq(home,-c));
return home.ES_SUBSUMED(p);
}
return (c == static_cast<Val>(0)) ?
home.ES_SUBSUMED(p) : ES_FAILED;
}
sl += c; su += c;
const int mod_sl = 1;
const int mod_su = 2;
int mod = mod_sl | mod_su;
do {
if (mod & mod_sl) {
mod -= mod_sl;
// Propagate upper bound for positive variables
for (int i = x.size(); i--; ) {
const Val xi_max = x[i].max();
ModEvent me = x[i].lq(home,sl + x[i].min());
if (me_failed(me))
return ES_FAILED;
if (me_modified(me)) {
su += xi_max - x[i].max();
mod |= mod_su;
}
}
// Propagate lower bound for negative variables
for (int i = y.size(); i--; ) {
const Val yi_min = y[i].min();
ModEvent me = y[i].gq(home,y[i].max() - sl);
if (me_failed(me))
return ES_FAILED;
if (me_modified(me)) {
su += y[i].min() - yi_min;
mod |= mod_su;
}
}
}
if (mod & mod_su) {
mod -= mod_su;
// Propagate lower bound for positive variables
for (int i = x.size(); i--; ) {
const Val xi_min = x[i].min();
ModEvent me = x[i].gq(home,su + x[i].max());
if (me_failed(me))
return ES_FAILED;
if (me_modified(me)) {
sl += xi_min - x[i].min();
mod |= mod_sl;
}
}
// Propagate upper bound for negative variables
for (int i = y.size(); i--; ) {
const Val yi_max = y[i].max();
ModEvent me = y[i].lq(home,y[i].min() - su);
if (me_failed(me))
return ES_FAILED;
if (me_modified(me)) {
sl += y[i].max() - yi_max;
mod |= mod_sl;
}
}
}
} while (mod);
return (sl == su) ? home.ES_SUBSUMED(p) : ES_FIX;
}
/*
* Bound consistent linear equation
*
*/
template<class Val, class P, class N>
forceinline
Eq<Val,P,N>::Eq(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c)
: Lin<Val,P,N,PC_INT_BND>(home,x,y,c) {}
template<class Val, class P, class N>
ExecStatus
Eq<Val,P,N>::post(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c) {
ViewArray<NoView> nva;
if (y.size() == 0) {
(void) new (home) Eq<Val,P,NoView>(home,x,nva,c);
} else if (x.size() == 0) {
(void) new (home) Eq<Val,N,NoView>(home,y,nva,-c);
} else {
(void) new (home) Eq<Val,P,N>(home,x,y,c);
}
return ES_OK;
}
template<class Val, class P, class N>
forceinline
Eq<Val,P,N>::Eq(Space& home, bool share, Eq<Val,P,N>& p)
: Lin<Val,P,N,PC_INT_BND>(home,share,p) {}
/**
* \brief Rewriting of equality to binary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
eqtobin(Space&, bool, Propagator&, ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
eqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 2);
return new (home) EqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
}
template<class Val>
forceinline Actor*
eqtobin(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 2);
return new (home) EqBin<Val,IntView,IntView>
(home,share,p,y[0],y[1],-c);
}
template<class Val>
forceinline Actor*
eqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 2)
return new (home) EqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
if (x.size() == 1)
return new (home) EqBin<Val,IntView,MinusView>
(home,share,p,x[0],MinusView(y[0]),c);
return new (home) EqBin<Val,IntView,IntView>
(home,share,p,y[0],y[1],-c);
}
/**
* \brief Rewriting of equality to ternary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
eqtoter(Space&, bool, Propagator&, ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
eqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 3);
return new (home) EqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
}
template<class Val>
forceinline Actor*
eqtoter(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 3);
return new (home) EqTer<Val,IntView,IntView,IntView>
(home,share,p,y[0],y[1],y[2],-c);
}
template<class Val>
forceinline Actor*
eqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 3)
return new (home) EqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
if (x.size() == 2)
return new (home) EqTer<Val,IntView,IntView,MinusView>
(home,share,p,x[0],x[1],MinusView(y[0]),c);
if (x.size() == 1)
return new (home) EqTer<Val,IntView,IntView,MinusView>
(home,share,p,y[0],y[1],MinusView(x[0]),-c);
return new (home) EqTer<Val,IntView,IntView,IntView>
(home,share,p,y[0],y[1],y[2],-c);
}
template<class Val, class P, class N>
Actor*
Eq<Val,P,N>::copy(Space& home, bool share) {
if (isunit(x,y)) {
// Check whether rewriting is possible
if (x.size() + y.size() == 2)
return eqtobin(home,share,*this,x,y,c);
if (x.size() + y.size() == 3)
return eqtoter(home,share,*this,x,y,c);
}
return new (home) Eq<Val,P,N>(home,share,*this);
}
template<class Val, class P, class N>
ExecStatus
Eq<Val,P,N>::propagate(Space& home, const ModEventDelta& med) {
return prop_bnd<Val,P,N>(home,med,*this,x,y,c);
}
/*
* Reified bound consistent linear equation
*
*/
template<class Val, class P, class N, class Ctrl, ReifyMode rm>
forceinline
ReEq<Val,P,N,Ctrl,rm>::ReEq(Home home,
ViewArray<P>& x, ViewArray<N>& y, Val c, Ctrl b)
: ReLin<Val,P,N,PC_INT_BND,Ctrl>(home,x,y,c,b) {}
template<class Val, class P, class N, class Ctrl, ReifyMode rm>
ExecStatus
ReEq<Val,P,N,Ctrl,rm>::post(Home home,
ViewArray<P>& x, ViewArray<N>& y, Val c, Ctrl b) {
ViewArray<NoView> nva;
if (y.size() == 0) {
(void) new (home) ReEq<Val,P,NoView,Ctrl,rm>(home,x,nva,c,b);
} else if (x.size() == 0) {
(void) new (home) ReEq<Val,N,NoView,Ctrl,rm>(home,y,nva,-c,b);
} else {
(void) new (home) ReEq<Val,P,N,Ctrl,rm>(home,x,y,c,b);
}
return ES_OK;
}
template<class Val, class P, class N, class Ctrl, ReifyMode rm>
forceinline
ReEq<Val,P,N,Ctrl,rm>::ReEq(Space& home, bool share,
ReEq<Val,P,N,Ctrl,rm>& p)
: ReLin<Val,P,N,PC_INT_BND,Ctrl>(home,share,p) {}
template<class Val, class P, class N, class Ctrl, ReifyMode rm>
Actor*
ReEq<Val,P,N,Ctrl,rm>::copy(Space& home, bool share) {
return new (home) ReEq<Val,P,N,Ctrl,rm>(home,share,*this);
}
template<class Val, class P, class N, class Ctrl, ReifyMode rm>
ExecStatus
ReEq<Val,P,N,Ctrl,rm>::propagate(Space& home, const ModEventDelta& med) {
if (b.zero()) {
if (rm == RM_IMP)
return home.ES_SUBSUMED(*this);
GECODE_REWRITE(*this,(Nq<Val,P,N>::post(home(*this),x,y,c)));
}
if (b.one()) {
if (rm == RM_PMI)
return home.ES_SUBSUMED(*this);
GECODE_REWRITE(*this,(Eq<Val,P,N>::post(home(*this),x,y,c)));
}
Val sl = 0;
Val su = 0;
bounds_p<Val,P>(med, x, c, sl, su);
bounds_n<Val,N>(med, y, c, sl, su);
if ((-sl == c) && (-su == c)) {
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
return home.ES_SUBSUMED(*this);
}
if ((-sl > c) || (-su < c)) {
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
return home.ES_SUBSUMED(*this);
}
return ES_FIX;
}
/*
* Domain consistent linear disequation
*
*/
template<class Val, class P, class N>
forceinline
Nq<Val,P,N>::Nq(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c)
: Lin<Val,P,N,PC_INT_VAL>(home,x,y,c) {}
template<class Val, class P, class N>
ExecStatus
Nq<Val,P,N>::post(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c) {
ViewArray<NoView> nva;
if (y.size() == 0) {
(void) new (home) Nq<Val,P,NoView>(home,x,nva,c);
} else if (x.size() == 0) {
(void) new (home) Nq<Val,N,NoView>(home,y,nva,-c);
} else {
(void) new (home) Nq<Val,P,N>(home,x,y,c);
}
return ES_OK;
}
template<class Val, class P, class N>
forceinline
Nq<Val,P,N>::Nq(Space& home, bool share, Nq<Val,P,N>& p)
: Lin<Val,P,N,PC_INT_VAL>(home,share,p) {}
/**
* \brief Rewriting of disequality to binary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
nqtobin(Space&, bool, Propagator&, ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
nqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 2);
return new (home) NqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
}
template<class Val>
forceinline Actor*
nqtobin(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 2);
return new (home) NqBin<Val,IntView,IntView>
(home,share,p,y[0],y[1],-c);
}
template<class Val>
forceinline Actor*
nqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 2)
return new (home) NqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
if (x.size() == 1)
return new (home) NqBin<Val,IntView,MinusView>
(home,share,p,x[0],MinusView(y[0]),c);
return new (home) NqBin<Val,IntView,IntView>
(home,share,p,y[0],y[1],-c);
}
/**
* \brief Rewriting of disequality to ternary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
nqtoter(Space&, bool, Propagator&,ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
nqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 3);
return new (home) NqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
}
template<class Val>
forceinline Actor*
nqtoter(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 3);
return new (home) NqTer<Val,IntView,IntView,IntView>
(home,share,p,y[0],y[1],y[2],-c);
}
template<class Val>
forceinline Actor*
nqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 3)
return new (home) NqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
if (x.size() == 2)
return new (home) NqTer<Val,IntView,IntView,MinusView>
(home,share,p,x[0],x[1],MinusView(y[0]),c);
if (x.size() == 1)
return new (home) NqTer<Val,IntView,IntView,MinusView>
(home,share,p,y[0],y[1],MinusView(x[0]),-c);
return new (home) NqTer<Val,IntView,IntView,IntView>
(home,share,p,y[0],y[1],y[2],-c);
}
template<class Val, class P, class N>
Actor*
Nq<Val,P,N>::copy(Space& home, bool share) {
if (isunit(x,y)) {
// Check whether rewriting is possible
if (x.size() + y.size() == 2)
return nqtobin(home,share,*this,x,y,c);
if (x.size() + y.size() == 3)
return nqtoter(home,share,*this,x,y,c);
}
return new (home) Nq<Val,P,N>(home,share,*this);
}
template<class Val, class P, class N>
ExecStatus
Nq<Val,P,N>::propagate(Space& home, const ModEventDelta&) {
for (int i = x.size(); i--; )
if (x[i].assigned()) {
c -= x[i].val(); x.move_lst(i);
}
for (int i = y.size(); i--; )
if (y[i].assigned()) {
c += y[i].val(); y.move_lst(i);
}
if (x.size() + y.size() <= 1) {
if (x.size() == 1) {
GECODE_ME_CHECK(x[0].nq(home,c)); return home.ES_SUBSUMED(*this);
}
if (y.size() == 1) {
GECODE_ME_CHECK(y[0].nq(home,-c)); return home.ES_SUBSUMED(*this);
}
return (c == static_cast<Val>(0)) ?
ES_FAILED : home.ES_SUBSUMED(*this);
}
return ES_FIX;
}
/*
* Bound consistent linear inequation
*
*/
template<class Val, class P, class N>
forceinline
Lq<Val,P,N>::Lq(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c)
: Lin<Val,P,N,PC_INT_BND>(home,x,y,c) {}
template<class Val, class P, class N>
ExecStatus
Lq<Val,P,N>::post(Home home, ViewArray<P>& x, ViewArray<N>& y, Val c) {
ViewArray<NoView> nva;
if (y.size() == 0) {
(void) new (home) Lq<Val,P,NoView>(home,x,nva,c);
} else if (x.size() == 0) {
(void) new (home) Lq<Val,NoView,N>(home,nva,y,c);
} else {
(void) new (home) Lq<Val,P,N>(home,x,y,c);
}
return ES_OK;
}
template<class Val, class P, class N>
forceinline
Lq<Val,P,N>::Lq(Space& home, bool share, Lq<Val,P,N>& p)
: Lin<Val,P,N,PC_INT_BND>(home,share,p) {}
/**
* \brief Rewriting of inequality to binary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
lqtobin(Space&, bool, Propagator&,ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
lqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 2);
return new (home) LqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
}
template<class Val>
forceinline Actor*
lqtobin(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 2);
return new (home) LqBin<Val,MinusView,MinusView>
(home,share,p,MinusView(y[0]),MinusView(y[1]),c);
}
template<class Val>
forceinline Actor*
lqtobin(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 2)
return new (home) LqBin<Val,IntView,IntView>
(home,share,p,x[0],x[1],c);
if (x.size() == 1)
return new (home) LqBin<Val,IntView,MinusView>
(home,share,p,x[0],MinusView(y[0]),c);
return new (home) LqBin<Val,MinusView,MinusView>
(home,share,p,MinusView(y[0]),MinusView(y[1]),c);
}
/**
* \brief Rewriting of inequality to ternary propagators
*
*/
template<class Val, class P, class N>
forceinline Actor*
lqtoter(Space&, bool, Propagator&, ViewArray<P>&, ViewArray<N>&, Val) {
return NULL;
}
template<class Val>
forceinline Actor*
lqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<NoView>&, Val c) {
assert(x.size() == 3);
return new (home) LqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
}
template<class Val>
forceinline Actor*
lqtoter(Space& home, bool share, Propagator& p,
ViewArray<NoView>&, ViewArray<IntView>& y, Val c) {
assert(y.size() == 3);
return new (home) LqTer<Val,MinusView,MinusView,MinusView>
(home,share,p,MinusView(y[0]),MinusView(y[1]),MinusView(y[2]),c);
}
template<class Val>
forceinline Actor*
lqtoter(Space& home, bool share, Propagator& p,
ViewArray<IntView>& x, ViewArray<IntView>& y, Val c) {
if (x.size() == 3)
return new (home) LqTer<Val,IntView,IntView,IntView>
(home,share,p,x[0],x[1],x[2],c);
if (x.size() == 2)
return new (home) LqTer<Val,IntView,IntView,MinusView>
(home,share,p,x[0],x[1],MinusView(y[0]),c);
if (x.size() == 1)
return new (home) LqTer<Val,IntView,MinusView,MinusView>
(home,share,p,x[0],MinusView(y[0]),MinusView(y[1]),c);
return new (home) LqTer<Val,MinusView,MinusView,MinusView>
(home,share,p,MinusView(y[0]),MinusView(y[1]),MinusView(y[2]),c);
}
template<class Val, class P, class N>
Actor*
Lq<Val,P,N>::copy(Space& home, bool share) {
if (isunit(x,y)) {
// Check whether rewriting is possible
if (x.size() + y.size() == 2)
return lqtobin(home,share,*this,x,y,c);
if (x.size() + y.size() == 3)
return lqtoter(home,share,*this,x,y,c);
}
return new (home) Lq<Val,P,N>(home,share,*this);
}
template<class Val, class P, class N>
ExecStatus
Lq<Val,P,N>::propagate(Space& home, const ModEventDelta& med) {
// Eliminate singletons
Val sl = 0;
if (IntView::me(med) == ME_INT_VAL) {
for (int i = x.size(); i--; ) {
Val m = x[i].min();
if (x[i].assigned()) {
c -= m; x.move_lst(i);
} else {
sl -= m;
}
}
for (int i = y.size(); i--; ) {
Val m = y[i].max();
if (y[i].assigned()) {
c += m; y.move_lst(i);
} else {
sl += m;
}
}
if ((x.size() + y.size()) <= 1) {
if (x.size() == 1) {
GECODE_ME_CHECK(x[0].lq(home,c));
return home.ES_SUBSUMED(*this);
}
if (y.size() == 1) {
GECODE_ME_CHECK(y[0].gq(home,-c));
return home.ES_SUBSUMED(*this);
}
return (c >= static_cast<Val>(0)) ?
home.ES_SUBSUMED(*this) : ES_FAILED;
}
} else {
for (int i = x.size(); i--; )
sl -= x[i].min();
for (int i = y.size(); i--; )
sl += y[i].max();
}
sl += c;
ExecStatus es = ES_FIX;
bool assigned = true;
for (int i = x.size(); i--; ) {
assert(!x[i].assigned());
Val slx = sl + x[i].min();
ModEvent me = x[i].lq(home,slx);
if (me == ME_INT_FAILED)
return ES_FAILED;
if (me != ME_INT_VAL)
assigned = false;
if (me_modified(me) && (slx != x[i].max()))
es = ES_NOFIX;
}
for (int i = y.size(); i--; ) {
assert(!y[i].assigned());
Val sly = y[i].max() - sl;
ModEvent me = y[i].gq(home,sly);
if (me == ME_INT_FAILED)
return ES_FAILED;
if (me != ME_INT_VAL)
assigned = false;
if (me_modified(me) && (sly != y[i].min()))
es = ES_NOFIX;
}
return assigned ? home.ES_SUBSUMED(*this) : es;
}
/*
* Reified bound consistent linear inequation
*
*/
template<class Val, class P, class N, ReifyMode rm>
forceinline
ReLq<Val,P,N,rm>::ReLq(Home home,
ViewArray<P>& x, ViewArray<N>& y, Val c, BoolView b)
: ReLin<Val,P,N,PC_INT_BND,BoolView>(home,x,y,c,b) {}
template<class Val, class P, class N, ReifyMode rm>
ExecStatus
ReLq<Val,P,N,rm>::post(Home home,
ViewArray<P>& x, ViewArray<N>& y, Val c, BoolView b) {
ViewArray<NoView> nva;
if (y.size() == 0) {
(void) new (home) ReLq<Val,P,NoView,rm>(home,x,nva,c,b);
} else if (x.size() == 0) {
(void) new (home) ReLq<Val,NoView,N,rm>(home,nva,y,c,b);
} else {
(void) new (home) ReLq<Val,P,N,rm>(home,x,y,c,b);
}
return ES_OK;
}
template<class Val, class P, class N, ReifyMode rm>
forceinline
ReLq<Val,P,N,rm>::ReLq(Space& home, bool share, ReLq<Val,P,N,rm>& p)
: ReLin<Val,P,N,PC_INT_BND,BoolView>(home,share,p) {}
template<class Val, class P, class N, ReifyMode rm>
Actor*
ReLq<Val,P,N,rm>::copy(Space& home, bool share) {
return new (home) ReLq<Val,P,N,rm>(home,share,*this);
}
template<class Val, class P, class N, ReifyMode rm>
ExecStatus
ReLq<Val,P,N,rm>::propagate(Space& home, const ModEventDelta& med) {
if (b.zero()) {
if (rm == RM_IMP)
return home.ES_SUBSUMED(*this);
GECODE_REWRITE(*this,(Lq<Val,N,P>::post(home(*this),y,x,-c-1)));
}
if (b.one()) {
if (rm == RM_PMI)
return home.ES_SUBSUMED(*this);
GECODE_REWRITE(*this,(Lq<Val,P,N>::post(home(*this),x,y,c)));
}
// Eliminate singletons
Val sl = 0;
Val su = 0;
bounds_p<Val,P>(med,x,c,sl,su);
bounds_n<Val,N>(med,y,c,sl,su);
if (-sl > c) {
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
return home.ES_SUBSUMED(*this);
}
if (-su <= c) {
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
return home.ES_SUBSUMED(*this);
}
return ES_FIX;
}
}}}
// STATISTICS: int-prop
|