Generated on Thu Mar 22 10:39:34 2012 for Gecode by doxygen 1.6.3

lq.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-06-20 11:16:03 +0200 (Mon, 20 Jun 2011) $ by $Author: schulte $
00011  *     $Revision: 12058 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode { namespace Int { namespace Bool {
00039 
00040   /*
00041    * Less or equal propagator
00042    *
00043    */
00044 
00045   template<class BV>
00046   forceinline
00047   Lq<BV>::Lq(Home home, BV b0, BV b1)
00048     : BoolBinary<BV,BV>(home,b0,b1) {}
00049 
00050   template<class BV>
00051   forceinline
00052   Lq<BV>::Lq(Space& home, bool share, Lq<BV>& p)
00053     : BoolBinary<BV,BV>(home,share,p) {}
00054 
00055   template<class BV>
00056   Actor*
00057   Lq<BV>::copy(Space& home, bool share) {
00058     return new (home) Lq<BV>(home,share,*this);
00059   }
00060 
00061   template<class BV>
00062   inline ExecStatus
00063   Lq<BV>::post(Home home, BV b0, BV b1) {
00064     if (b0.zero()) {
00065       return ES_OK;
00066     } else if (b0.one()) {
00067       GECODE_ME_CHECK(b1.one(home));
00068     } else if (b1.zero()) {
00069       GECODE_ME_CHECK(b0.zero(home));
00070     } else if (b1.one()) {
00071       return ES_OK;
00072     } else {
00073       (void) new (home) Lq<BV>(home,b0,b1);
00074     }
00075     return ES_OK;
00076   }
00077 
00078   template<class BV>
00079   ExecStatus
00080   Lq<BV>::propagate(Space& home, const ModEventDelta&) {
00081 #define GECODE_INT_STATUS(S0,S1) \
00082   ((BV::S0<<(1*BV::BITS))|(BV::S1<<(0*BV::BITS)))
00083     switch ((x0.status()<<(1*BV::BITS)) | (x1.status()<<(0*BV::BITS))) {
00084     case GECODE_INT_STATUS(NONE,NONE):
00085       GECODE_NEVER;
00086     case GECODE_INT_STATUS(NONE,ZERO):
00087       GECODE_ME_CHECK(x0.zero_none(home)); break;
00088     case GECODE_INT_STATUS(NONE,ONE):
00089     case GECODE_INT_STATUS(ZERO,NONE):
00090     case GECODE_INT_STATUS(ZERO,ZERO):
00091     case GECODE_INT_STATUS(ZERO,ONE):
00092       break;
00093     case GECODE_INT_STATUS(ONE,NONE):
00094       GECODE_ME_CHECK(x1.one_none(home)); break;
00095     case GECODE_INT_STATUS(ONE,ZERO):
00096       return ES_FAILED;
00097     case GECODE_INT_STATUS(ONE,ONE):
00098       break;
00099     default:
00100       GECODE_NEVER;
00101     }
00102     return home.ES_SUBSUMED(*this);
00103 #undef GECODE_INT_STATUS
00104   }
00105 
00106 
00107   /*
00108    * N-ary Boolean less or equal propagator
00109    *
00110    */
00111 
00112   template<class VX>
00113   forceinline
00114   NaryLq<VX>::NaryLq(Home home, ViewArray<VX>& x)
00115     : NaryPropagator<VX,PC_BOOL_NONE>(home,x),
00116       run(false), n_zero(0), n_one(0), c(home) {
00117     x.subscribe(home,*new (home) Advisor(home,*this,c));
00118   }
00119 
00120   template<class VX>
00121   forceinline
00122   NaryLq<VX>::NaryLq(Space& home, bool share, NaryLq<VX>& p)
00123     : NaryPropagator<VX,PC_BOOL_NONE>(home,share,p),
00124       run(false), n_zero(0), n_one(0) {
00125     c.update(home,share,p.c);
00126   }
00127 
00128   template<class VX>
00129   Actor*
00130   NaryLq<VX>::copy(Space& home, bool share) {
00131     return new (home) NaryLq<VX>(home,share,*this);
00132   }
00133 
00134   template<class VX>
00135   inline ExecStatus
00136   NaryLq<VX>::post(Home home, ViewArray<VX>& x) {
00137     int i = 0;
00138     while (i < x.size())
00139       if (x[i].zero()) {
00140         // All x[j] left of i must be zero as well
00141         for (int j=i; j--; )
00142           GECODE_ME_CHECK(x[j].zero_none(home));
00143         x.drop_fst(i+1); i=0;
00144       } else if (x[i].one()) {
00145         // All x[j] right of i must be one as well
00146         for (int j=i+1; j<x.size(); j++)
00147           GECODE_ME_CHECK(x[j].one(home));
00148         x.drop_lst(i-1); break;
00149       } else {
00150         i++;
00151       }
00152 
00153     if (x.size() == 2)
00154       return Lq<VX>::post(home,x[0],x[1]);
00155     if (x.size() > 2)
00156       (void) new (home) NaryLq(home,x);
00157     return ES_OK;
00158   }
00159 
00160   template<class VX>
00161   PropCost
00162   NaryLq<VX>::cost(const Space&, const ModEventDelta&) const {
00163     return PropCost::binary(PropCost::LO);
00164   }
00165 
00166   template<class VX>
00167   ExecStatus
00168   NaryLq<VX>::advise(Space&, Advisor&, const Delta& d) {
00169     if (VX::zero(d))
00170       n_zero++;
00171     else
00172       n_one++;
00173     return run ? ES_FIX : ES_NOFIX;
00174   }
00175 
00176   template<class VX>
00177   forceinline size_t
00178   NaryLq<VX>::dispose(Space& home) {
00179     Advisors<Advisor> as(c);
00180     x.cancel(home,as.advisor());
00181     c.dispose(home);
00182     (void) NaryPropagator<VX,PC_BOOL_NONE>::dispose(home);
00183     return sizeof(*this);
00184   }
00185 
00186   template<class VX>
00187   ExecStatus
00188   NaryLq<VX>::propagate(Space& home, const ModEventDelta&) {
00189     run = true;
00190     while (n_zero > 0) {
00191       int i = 0;
00192       while (x[i].none())
00193         i++;
00194       if (x[i].one())
00195         return ES_FAILED;
00196       // As the x[j] might be shared, only zero() but not zero_none()
00197       for (int j=i; j--; )
00198         GECODE_ME_CHECK(x[j].zero(home));
00199       n_zero -= i + 1;
00200       assert(n_zero >= 0);
00201       x.drop_fst(i+1);
00202     }
00203 
00204     while (n_one > 0) {
00205       int i = x.size() - 1;
00206       while (x[i].none())
00207         i--;
00208       assert(x[i].one());
00209       // As the x[j] might be shared, only one() but not one_none()
00210       for (int j=i+1; j<x.size(); j++)
00211         GECODE_ME_CHECK(x[j].one(home));
00212       n_one -= x.size() - i;
00213       assert(n_one >= 0);
00214       x.drop_lst(i-1);
00215     }
00216 
00217     if (x.size() < 2)
00218       return home.ES_SUBSUMED(*this);
00219 
00220     run = false;
00221     return ES_FIX;
00222   }
00223 
00224 
00225   /*
00226    * Less posting
00227    *
00228    */
00229 
00230   template<class BV>
00231   forceinline ExecStatus
00232   Le<BV>::post(Home home, BV b0, BV b1) {
00233     GECODE_ME_CHECK(b0.zero(home));
00234     GECODE_ME_CHECK(b1.one(home));
00235     return ES_OK;
00236   }
00237 
00238 }}}
00239 
00240 // STATISTICS: int-prop
00241