/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * Tias Guns * * Copyright: * Christian Schulte, 2006 * Tias Guns, 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 #include namespace Gecode { namespace Int { namespace Linear { /* * Baseclass for integer Boolean sum using dependencies * */ template forceinline LinBoolInt::LinBoolInt(Home home, ViewArray& x0, int n_s, int c0) : Propagator(home), co(home), x(x0), n_as(n_s), n_hs(n_s), c(c0) { Advisor* a = new (home) Advisor(home,*this,co); for (int i=0; i forceinline void LinBoolInt::normalize(void) { if (n_as != n_hs) { // Remove views for which no more subscriptions exist int n_x = x.size(); for (int i=n_hs; i--; ) if (!x[i].none()) { x[i]=x[--n_hs]; x[n_hs]=x[--n_x]; } x.size(n_x); } assert(n_as == n_hs); // Remove assigned yet unsubscribed views { int n_x = x.size(); for (int i=n_x-1; i>=n_hs; i--) if (x[i].one()) { c--; x[i]=x[--n_x]; } else if (x[i].zero()) { x[i]=x[--n_x]; } x.size(n_x); } } template forceinline LinBoolInt::LinBoolInt(Space& home, LinBoolInt& p) : Propagator(home,p), n_as(p.n_as), n_hs(n_as) { p.normalize(); c=p.c; co.update(home,p.co); x.update(home,p.x); } template PropCost LinBoolInt::cost(const Space&, const ModEventDelta&) const { return PropCost::unary(PropCost::HI); } template forceinline size_t LinBoolInt::dispose(Space& home) { Advisors as(co); for (int i=0; i forceinline GqBoolInt::GqBoolInt(Home home, ViewArray& x, int c) : LinBoolInt(home,x,c+1,c) {} template forceinline GqBoolInt::GqBoolInt(Space& home, GqBoolInt& p) : LinBoolInt(home,p) {} template Actor* GqBoolInt::copy(Space& home) { return new (home) GqBoolInt(home,*this); } template ExecStatus GqBoolInt::advise(Space& home, Advisor& a, const Delta& d) { // Check whether propagator is running if (n_as == 0) return ES_FIX; if (VX::one(d)) { c--; goto check; } if (c+1 < n_as) goto check; // Find a new subscription for (int i = x.size()-1; i>=n_hs; i--) if (x[i].none()) { std::swap(x[i],x[n_hs]); x[n_hs++].subscribe(home,a); x.size(i+1); return ES_FIX; } else if (x[i].one()) { c--; if (c+1 < n_as) { x.size(i); assert(n_hs <= x.size()); goto check; } } // No view left for subscription x.size(n_hs); check: // Do not update subscription n_as--; int n = x.size()-n_hs+n_as; if ((n < c) && !disabled()) return ES_FAILED; if ((c <= 0) || (c == n)) return ES_NOFIX; else return ES_FIX; } template void GqBoolInt::reschedule(Space& home) { int n = x.size()-n_hs+n_as; if ((c <= 0) || (c >= n)) VX::schedule(home,*this,ME_INT_VAL); } template ExecStatus GqBoolInt::propagate(Space& home, const ModEventDelta&) { // Check for failure due to a disabled propagator if (x.size() - n_hs + n_as < c) return ES_FAILED; if (c > 0) { assert((n_as == c) && (x.size() == n_hs)); // Signal that propagator is running n_as = 0; // All views must be one to satisfy inequality for (int i=0; i ExecStatus GqBoolInt::post(Home home, ViewArray& x, int c) { // Eliminate assigned views int n_x = x.size(); for (int i=n_x; i--; ) if (x[i].zero()) { x[i] = x[--n_x]; } else if (x[i].one()) { x[i] = x[--n_x]; c--; } x.size(n_x); // RHS too large if (n_x < c) return ES_FAILED; // Whatever the x[i] take for values, the inequality is subsumed if (c <= 0) return ES_OK; // Use Boolean disjunction for this special case if (c == 1) return Bool::NaryOrTrue::post(home,x); // All views must be one to satisfy inequality if (c == n_x) { for (int i=0; i c); (void) new (home) GqBoolInt(home,x,c); return ES_OK; } /* * Equal propagator (integer rhs) * */ template forceinline EqBoolInt::EqBoolInt(Home home, ViewArray& x, int c) : LinBoolInt(home,x,std::max(c,x.size()-c)+1,c) {} template forceinline EqBoolInt::EqBoolInt(Space& home, EqBoolInt& p) : LinBoolInt(home,p) {} template Actor* EqBoolInt::copy(Space& home) { return new (home) EqBoolInt(home,*this); } template ExecStatus EqBoolInt::advise(Space& home, Advisor& a, const Delta& d) { // Check whether propagator is running if (n_as == 0) return ES_FIX; if (VX::one(d)) c--; if ((c+1 < n_as) && (x.size()-n_hs < c)) goto check; // Find a new subscription for (int i = x.size()-1; i>=n_hs; i--) if (x[i].none()) { std::swap(x[i],x[n_hs]); x[n_hs++].subscribe(home,a); x.size(i+1); return ES_FIX; } else if (x[i].one()) { c--; } // No view left for subscription x.size(n_hs); check: // Do not update subscription n_as--; int n = x.size()-n_hs+n_as; if (((c < 0) || (c > n)) && !disabled()) return ES_FAILED; if ((c == 0) || (c == n)) return ES_NOFIX; else return ES_FIX; } template void EqBoolInt::reschedule(Space& home) { int n = x.size()-n_hs+n_as; if ((c <= 0) || (c >= n)) VX::schedule(home,*this,ME_INT_VAL); } template ExecStatus EqBoolInt::propagate(Space& home, const ModEventDelta&) { // Check for failure due to being disabled before if ((c < 0) || (c > x.size()-n_hs+n_as)) return ES_FAILED; assert(x.size() == n_hs); // Signal that propagator is running n_as = 0; if (c == 0) { // All views must be zero to satisfy equality for (int i=0; i ExecStatus EqBoolInt::post(Home home, ViewArray& x, int c) { // Eliminate assigned views int n_x = x.size(); for (int i=n_x; i--; ) if (x[i].zero()) { x[i] = x[--n_x]; } else if (x[i].one()) { x[i] = x[--n_x]; c--; } x.size(n_x); // RHS too small or too large if ((c < 0) || (c > n_x)) return ES_FAILED; // All views must be zero to satisfy equality if (c == 0) { for (int i=0; i(home,x,c); return ES_OK; } /* * Integer disequal to Boolean sum * */ template forceinline NqBoolInt::NqBoolInt(Home home, ViewArray& b, int c0) : BinaryPropagator(home, b[b.size()-2], b[b.size()-1]), x(b), c(c0) { assert(x.size() >= 2); x.size(x.size()-2); } template forceinline size_t NqBoolInt::dispose(Space& home) { (void) BinaryPropagator::dispose(home); return sizeof(*this); } template forceinline NqBoolInt::NqBoolInt(Space& home, NqBoolInt& p) : BinaryPropagator(home,p), x(home,p.x.size()) { // Eliminate all zeros and ones in original and update int n = p.x.size(); int p_c = p.c; for (int i=n; i--; ) if (p.x[i].zero()) { n--; p.x[i]=p.x[n]; x[i]=x[n]; } else if (p.x[i].one()) { n--; p_c--; p.x[i]=p.x[n]; x[i]=x[n]; } else { x[i].update(home,p.x[i]); } c = p_c; p.c = p_c; x.size(n); p.x.size(n); } template forceinline ExecStatus NqBoolInt::post(Home home, ViewArray& x, int c) { int n = x.size(); for (int i=n; i--; ) if (x[i].one()) { x[i]=x[--n]; c--; } else if (x[i].zero()) { x[i]=x[--n]; } x.size(n); if ((n < c) || (c < 0)) return ES_OK; if (n == 0) return (c == 0) ? ES_FAILED : ES_OK; if (n == 1) { if (c == 1) { GECODE_ME_CHECK(x[0].zero_none(home)); } else { GECODE_ME_CHECK(x[0].one_none(home)); } return ES_OK; } (void) new (home) NqBoolInt(home,x,c); return ES_OK; } template Actor* NqBoolInt::copy(Space& home) { return new (home) NqBoolInt(home,*this); } template PropCost NqBoolInt::cost(const Space&, const ModEventDelta&) const { return PropCost::linear(PropCost::LO, x.size()); } template forceinline bool NqBoolInt::resubscribe(Space& home, VX& y) { if (y.one()) c--; int n = x.size(); for (int i=n; i--; ) if (x[i].one()) { c--; x[i]=x[--n]; } else if (x[i].zero()) { x[i] = x[--n]; } else { // New unassigned view found assert(!x[i].zero() && !x[i].one()); // Move to y and subscribe y=x[i]; x[i]=x[--n]; x.size(n); y.subscribe(home,*this,PC_INT_VAL,false); return true; } // All views have been assigned! x.size(0); return false; } template ExecStatus NqBoolInt::propagate(Space& home, const ModEventDelta&) { bool s0 = true; if (x0.zero() || x0.one()) s0 = resubscribe(home,x0); bool s1 = true; if (x1.zero() || x1.one()) s1 = resubscribe(home,x1); int n = x.size() + s0 + s1; if ((n < c) || (c < 0)) return home.ES_SUBSUMED(*this); if (n == 0) return (c == 0) ? ES_FAILED : home.ES_SUBSUMED(*this); if (n == 1) { if (s0) { if (c == 1) { GECODE_ME_CHECK(x0.zero_none(home)); } else { GECODE_ME_CHECK(x0.one_none(home)); } } else { assert(s1); if (c == 1) { GECODE_ME_CHECK(x1.zero_none(home)); } else { GECODE_ME_CHECK(x1.one_none(home)); } } return home.ES_SUBSUMED(*this); } return ES_FIX; } /* * Baseclass for reified integer Boolean sum * */ template forceinline ReLinBoolInt::ReLinBoolInt(Home home, ViewArray& x0, int c0, VB b0) : Propagator(home), co(home), x(x0), n_s(x.size()), c(c0), b(b0) { x.subscribe(home,*new (home) Advisor(home,*this,co)); b.subscribe(home,*this,PC_BOOL_VAL); } template forceinline void ReLinBoolInt::normalize(void) { if (n_s != x.size()) { int n_x = x.size(); for (int i=n_x; i--; ) if (!x[i].none()) x[i] = x[--n_x]; x.size(n_x); assert(x.size() == n_s); } } template forceinline ReLinBoolInt::ReLinBoolInt(Space& home, ReLinBoolInt& p) : Propagator(home,p), n_s(p.n_s), c(p.c) { p.normalize(); co.update(home,p.co); x.update(home,p.x); b.update(home,p.b); } template forceinline size_t ReLinBoolInt::dispose(Space& home) { Advisors as(co); x.cancel(home,as.advisor()); co.dispose(home); b.cancel(home,*this,PC_BOOL_VAL); (void) Propagator::dispose(home); return sizeof(*this); } template PropCost ReLinBoolInt::cost(const Space&, const ModEventDelta&) const { return PropCost::unary(PropCost::HI); } template<> /// Traits for Boolean negation view class BoolNegTraits { public: /// The negated view typedef NegBoolView NegView; /// Return negated View static NegBoolView neg(BoolView x) { NegBoolView y(x); return y; } }; template<> /// Traits for Boolean negation view class BoolNegTraits { public: /// The negated view typedef BoolView NegView; /// Return negated View static BoolView neg(NegBoolView x) { return x.base(); } }; /* * Reified greater or equal propagator (integer rhs) * */ template forceinline ReGqBoolInt::ReGqBoolInt(Home home, ViewArray& x, int c, VB b) : ReLinBoolInt(home,x,c,b) {} template forceinline ReGqBoolInt::ReGqBoolInt(Space& home, ReGqBoolInt& p) : ReLinBoolInt(home,p) {} template Actor* ReGqBoolInt::copy(Space& home) { return new (home) ReGqBoolInt(home,*this); } template ExecStatus ReGqBoolInt::advise(Space&, Advisor&, const Delta& d) { if (VX::one(d)) c--; n_s--; if ((n_s < c) || (c <= 0)) return ES_NOFIX; else return ES_FIX; } template void ReGqBoolInt::reschedule(Space& home) { b.reschedule(home,*this,PC_BOOL_VAL); if ((n_s < c) || (c <= 0)) VX::schedule(home,*this,ME_BOOL_VAL); } template ExecStatus ReGqBoolInt::propagate(Space& home, const ModEventDelta&) { if (b.none()) { if (c <= 0) { if (rm != RM_IMP) GECODE_ME_CHECK(b.one_none(home)); } else { if (rm != RM_PMI) GECODE_ME_CHECK(b.zero_none(home)); } } else { normalize(); if (b.one()) { if (rm != RM_PMI) GECODE_REWRITE(*this,(GqBoolInt::post(home(*this),x,c))); } else { if (rm != RM_IMP) { ViewArray::NegView> nx(home,x.size()); for (int i=0; i::neg(x[i]); GECODE_REWRITE(*this,GqBoolInt::NegView> ::post(home(*this),nx,x.size()-c+1)); } } } return home.ES_SUBSUMED(*this); } template ExecStatus ReGqBoolInt::post(Home home, ViewArray& x, int c, VB b) { assert(!b.assigned()); // checked before posting // Eliminate assigned views int n_x = x.size(); for (int i=n_x; i--; ) if (x[i].zero()) { x[i] = x[--n_x]; } else if (x[i].one()) { x[i] = x[--n_x]; c--; } x.size(n_x); if (n_x < c) { // RHS too large if (rm != RM_PMI) GECODE_ME_CHECK(b.zero_none(home)); } else if (c <= 0) { // Whatever the x[i] take for values, the inequality is subsumed if (rm != RM_IMP) GECODE_ME_CHECK(b.one_none(home)); } else if ((c == 1) && (rm == RM_EQV)) { // Equivalent to Boolean disjunction return Bool::NaryOr::post(home,x,b); } else if ((c == n_x) && (rm == RM_EQV)) { // Equivalent to Boolean conjunction, transform to Boolean disjunction ViewArray::NegView> nx(home,n_x); for (int i=0; i::neg(x[i]); return Bool::NaryOr ::NegView, typename BoolNegTraits::NegView> ::post(home,nx,BoolNegTraits::neg(b)); } else { (void) new (home) ReGqBoolInt(home,x,c,b); } return ES_OK; } /* * Reified equal propagator (integer rhs) * */ template forceinline ReEqBoolInt::ReEqBoolInt(Home home, ViewArray& x, int c, VB b) : ReLinBoolInt(home,x,c,b) {} template forceinline ReEqBoolInt::ReEqBoolInt(Space& home, ReEqBoolInt& p) : ReLinBoolInt(home,p) {} template Actor* ReEqBoolInt::copy(Space& home) { return new (home) ReEqBoolInt(home,*this); } template ExecStatus ReEqBoolInt::advise(Space&, Advisor&, const Delta& d) { if (VX::one(d)) c--; n_s--; if ((c < 0) || (c > n_s) || (n_s == 0)) return ES_NOFIX; else return ES_FIX; } template void ReEqBoolInt::reschedule(Space& home) { b.reschedule(home,*this,PC_BOOL_VAL); if ((c < 0) || (c > n_s) || (n_s == 0)) VX::schedule(home,*this,ME_BOOL_VAL); } template ExecStatus ReEqBoolInt::propagate(Space& home, const ModEventDelta&) { if (b.none()) { if ((c == 0) && (n_s == 0)) { if (rm != RM_IMP) GECODE_ME_CHECK(b.one_none(home)); } else { if (rm != RM_PMI) GECODE_ME_CHECK(b.zero_none(home)); } } else { normalize(); if (b.one()) { if (rm != RM_PMI) GECODE_REWRITE(*this,(EqBoolInt::post(home(*this),x,c))); } else { if (rm != RM_IMP) GECODE_REWRITE(*this,(NqBoolInt::post(home(*this),x,c))); } } return home.ES_SUBSUMED(*this); } template ExecStatus ReEqBoolInt::post(Home home, ViewArray& x, int c, VB b) { assert(!b.assigned()); // checked before posting // Eliminate assigned views int n_x = x.size(); for (int i=n_x; i--; ) if (x[i].zero()) { x[i] = x[--n_x]; } else if (x[i].one()) { x[i] = x[--n_x]; c--; } x.size(n_x); if ((n_x < c) || (c < 0)) { // RHS too large if (rm != RM_PMI) GECODE_ME_CHECK(b.zero_none(home)); } else if ((c == 0) && (n_x == 0)) { // all variables set, and c == 0: equality if (rm != RM_IMP) GECODE_ME_CHECK(b.one_none(home)); } else if ((c == 0) && (rm == RM_EQV)) { // Equivalent to Boolean disjunction return Bool::NaryOr::NegView> ::post(home,x,BoolNegTraits::neg(b)); } else if ((c == n_x) && (rm == RM_EQV)) { // Equivalent to Boolean conjunction, transform to Boolean disjunction ViewArray::NegView> nx(home,n_x); for (int i=0; i::neg(x[i]); return Bool::NaryOr ::NegView, typename BoolNegTraits::NegView> ::post(home,nx,BoolNegTraits::neg(b)); } else { (void) new (home) ReEqBoolInt(home,x,c,b); } return ES_OK; } }}} // STATISTICS: int-prop