Generated on Thu Apr 11 13:58:54 2019 for Gecode by doxygen 1.6.3

branch.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, 2017
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode { namespace FlatZinc {
00035 
00036   forceinline
00037   IntBoolVarBranch::IntBoolVarBranch(Select s0, double d)
00038     : VarBranch<IntVar>(d), s(s0) {}
00039 
00040   forceinline
00041   IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAFC i, BoolAFC b)
00042     : s(s0), iafc(i), bafc(b) {}
00043 
00044   forceinline
00045   IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAction i, BoolAction b)
00046     : s(s0), iaction(i), baction(b) {}
00047 
00048   forceinline
00049   IntBoolVarBranch::IntBoolVarBranch(Select s0, IntCHB i, BoolCHB b)
00050     : s(s0), ichb(i), bchb(b) {}
00051 
00052   forceinline IntBoolVarBranch::Select
00053   IntBoolVarBranch::select(void) const {
00054     return s;
00055   }
00056 
00057   forceinline IntAFC
00058   IntBoolVarBranch::intafc(void) const {
00059     return iafc;
00060   }
00061   forceinline BoolAFC
00062   IntBoolVarBranch::boolafc(void) const {
00063     return bafc;
00064   }
00065 
00066   forceinline IntAction
00067   IntBoolVarBranch::intaction(void) const {
00068     return iaction;
00069   }
00070   forceinline BoolAction
00071   IntBoolVarBranch::boolaction(void) const {
00072     return baction;
00073   }
00074 
00075   forceinline IntCHB
00076   IntBoolVarBranch::intchb(void) const {
00077     return ichb;
00078   }
00079   forceinline BoolCHB
00080   IntBoolVarBranch::boolchb(void) const {
00081     return bchb;
00082   }
00083   forceinline void
00084   IntBoolVarBranch::expand(Home home, const IntVarArgs& x, const BoolVarArgs& y) {
00085     switch (select()) {
00086     case IntBoolVarBranch::SEL_AFC_MAX:
00087     case IntBoolVarBranch::SEL_AFC_SIZE_MAX:
00088       if (!iafc)
00089         iafc = IntAFC(home,x,decay());
00090       if (!bafc)
00091         bafc = BoolAFC(home,y,decay());
00092       break;
00093     case IntBoolVarBranch::SEL_ACTION_MAX:
00094     case IntBoolVarBranch::SEL_ACTION_SIZE_MAX:
00095       if (!iaction)
00096         iaction = IntAction(home,x,decay());
00097       if (!baction)
00098         baction = BoolAction(home,y,decay());
00099       break;
00100     case IntBoolVarBranch::SEL_CHB_MAX:
00101     case IntBoolVarBranch::SEL_CHB_SIZE_MAX:
00102       if (!ichb)
00103         ichb = IntCHB(home,x);
00104       if (!bchb)
00105         bchb = BoolCHB(home,y);
00106       break;
00107     default: ;
00108     }
00109   }
00110 
00111 
00112 
00113   inline IntBoolVarBranch
00114   INTBOOL_VAR_AFC_MAX(double d) {
00115     return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX,d);
00116   }
00117   inline IntBoolVarBranch
00118   INTBOOL_VAR_AFC_MAX(IntAFC ia, BoolAFC ba) {
00119     return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX,ia,ba);
00120   }
00121   inline IntBoolVarBranch
00122   INTBOOL_VAR_ACTION_MAX(double d) {
00123     return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX,d);
00124   }
00125   inline IntBoolVarBranch
00126   INTBOOL_VAR_ACTION_MAX(IntAction ia, BoolAction ba) {
00127     return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX,ia,ba);
00128   }
00129   inline IntBoolVarBranch
00130   INTBOOL_VAR_CHB_MAX(double d) {
00131     return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX,d);
00132   }
00133   inline IntBoolVarBranch
00134   INTBOOL_VAR_CHB_MAX(IntCHB ic, BoolCHB bc) {
00135     return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX,ic,bc);
00136   }
00137   inline IntBoolVarBranch
00138   INTBOOL_VAR_AFC_SIZE_MAX(double d) {
00139     return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX,d);
00140   }
00141   inline IntBoolVarBranch
00142   INTBOOL_VAR_AFC_SIZE_MAX(IntAFC ia, BoolAFC ba) {
00143     return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX,ia,ba);
00144   }
00145   inline IntBoolVarBranch
00146   INTBOOL_VAR_ACTION_SIZE_MAX(double d) {
00147     return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX,d);
00148   }
00149   inline IntBoolVarBranch
00150   INTBOOL_VAR_ACTION_SIZE_MAX(IntAction ia, BoolAction ba) {
00151     return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX,ia,ba);
00152   }
00153   inline IntBoolVarBranch
00154   INTBOOL_VAR_CHB_SIZE_MAX(double d) {
00155     return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX,d);
00156   }
00157   inline IntBoolVarBranch
00158   INTBOOL_VAR_CHB_SIZE_MAX(IntCHB ic, BoolCHB bc) {
00159     return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX,ic,bc);
00160   }
00161 
00162 
00163 
00164   forceinline
00165   MeritMaxAFC::MeritMaxAFC(Space&, const IntBoolVarBranch& ibvb)
00166     : iafc(ibvb.intafc()), bafc(ibvb.boolafc()) {}
00167   forceinline
00168   MeritMaxAFC::MeritMaxAFC(Space&, MeritMaxAFC& m)
00169     : iafc(m.iafc), bafc(m.bafc) {}
00170   forceinline double
00171   MeritMaxAFC::operator ()(Int::IntView x, int) const {
00172     return x.afc();
00173   }
00174   forceinline double
00175   MeritMaxAFC::operator ()(Int::BoolView x, int) const {
00176     return x.afc();
00177   }
00178   forceinline void
00179   MeritMaxAFC::dispose(void) {
00180     iafc.~IntAFC();
00181     bafc.~BoolAFC();
00182   }
00183 
00184 
00185   forceinline
00186   MeritMaxAFCSize::MeritMaxAFCSize(Space&, const IntBoolVarBranch& ibvb)
00187     : iafc(ibvb.intafc()), bafc(ibvb.boolafc()) {}
00188   forceinline
00189   MeritMaxAFCSize::MeritMaxAFCSize(Space&, MeritMaxAFCSize& m)
00190     : iafc(m.iafc), bafc(m.bafc) {}
00191   forceinline double
00192   MeritMaxAFCSize::operator ()(Int::IntView x, int) const {
00193     return x.afc() / x.size();
00194   }
00195   forceinline double
00196   MeritMaxAFCSize::operator ()(Int::BoolView x, int) const {
00197     return x.afc() / 2.0;
00198   }
00199   forceinline void
00200   MeritMaxAFCSize::dispose(void) {
00201     iafc.~IntAFC();
00202     bafc.~BoolAFC();
00203   }
00204 
00205 
00206   forceinline
00207   MeritMaxAction::MeritMaxAction(Space&, const IntBoolVarBranch& ibvb)
00208     : iaction(ibvb.intaction()), baction(ibvb.boolaction()) {}
00209   forceinline
00210   MeritMaxAction::MeritMaxAction(Space&, MeritMaxAction& m)
00211     : iaction(m.iaction), baction(m.baction) {}
00212   forceinline double
00213   MeritMaxAction::operator ()(Int::IntView, int i) const {
00214     return iaction[i];
00215   }
00216   forceinline double
00217   MeritMaxAction::operator ()(Int::BoolView, int i) const {
00218     return baction[i];
00219   }
00220   forceinline void
00221   MeritMaxAction::dispose(void) {
00222     iaction.~IntAction();
00223     baction.~BoolAction();
00224   }
00225 
00226 
00227   forceinline
00228   MeritMaxActionSize::MeritMaxActionSize(Space&, const IntBoolVarBranch& ibvb)
00229     : iaction(ibvb.intaction()), baction(ibvb.boolaction()) {}
00230   forceinline
00231   MeritMaxActionSize::MeritMaxActionSize(Space&, MeritMaxActionSize& m)
00232     : iaction(m.iaction), baction(m.baction) {}
00233   forceinline double
00234   MeritMaxActionSize::operator ()(Int::IntView x, int i) const {
00235     return iaction[i] / x.size();
00236   }
00237   forceinline double
00238   MeritMaxActionSize::operator ()(Int::BoolView, int i) const {
00239     return baction[i] / 2.0;
00240   }
00241   forceinline void
00242   MeritMaxActionSize::dispose(void) {
00243     iaction.~IntAction();
00244     baction.~BoolAction();
00245   }
00246 
00247 
00248   forceinline
00249   MeritMaxCHB::MeritMaxCHB(Space&, const IntBoolVarBranch& ibvb)
00250     : ichb(ibvb.intchb()), bchb(ibvb.boolchb()) {}
00251   forceinline
00252   MeritMaxCHB::MeritMaxCHB(Space&, MeritMaxCHB& m)
00253     : ichb(m.ichb), bchb(m.bchb) {}
00254   forceinline double
00255   MeritMaxCHB::operator ()(Int::IntView, int i) const {
00256     return ichb[i];
00257   }
00258   forceinline double
00259   MeritMaxCHB::operator ()(Int::BoolView, int i) const {
00260     return bchb[i];
00261   }
00262   forceinline void
00263   MeritMaxCHB::dispose(void) {
00264     ichb.~IntCHB();
00265     bchb.~BoolCHB();
00266   }
00267 
00268 
00269   forceinline
00270   MeritMaxCHBSize::MeritMaxCHBSize(Space&, const IntBoolVarBranch& ibvb)
00271     : ichb(ibvb.intchb()), bchb(ibvb.boolchb()) {}
00272   forceinline
00273   MeritMaxCHBSize::MeritMaxCHBSize(Space&, MeritMaxCHBSize& m)
00274     : ichb(m.ichb), bchb(m.bchb) {}
00275   forceinline double
00276   MeritMaxCHBSize::operator ()(Int::IntView x, int i) const {
00277     return ichb[i] / x.size();
00278   }
00279   forceinline double
00280   MeritMaxCHBSize::operator ()(Int::BoolView, int i) const {
00281     return bchb[i] / 2.0;
00282   }
00283   forceinline void
00284   MeritMaxCHBSize::dispose(void) {
00285     ichb.~IntCHB();
00286     bchb.~BoolCHB();
00287   }
00288 
00289 
00290   forceinline
00291   PosIntChoice::PosIntChoice(const Brancher& b, unsigned int a, int p, int n)
00292     : Choice(b,a), _pos(p), _val(n) {}
00293   forceinline int
00294   PosIntChoice::pos(void) const {
00295     return _pos;
00296   }
00297   forceinline int
00298   PosIntChoice::val(void) const {
00299     return _val;
00300   }
00301 
00302 
00303   forceinline
00304   IntBoolBrancherBase::
00305   IntBoolBrancherBase(Home home,
00306                   ViewArray<Int::IntView> x0,
00307                   ViewArray<Int::BoolView> y0,
00308                   ValSelCommitBase<Int::IntView,int>* xvsc0,
00309                   ValSelCommitBase<Int::BoolView,int>* yvsc0)
00310     : Brancher(home), x(x0), y(y0), start(0), xvsc(xvsc0), yvsc(yvsc0) {
00311     home.notice(*this,AP_DISPOSE,true);
00312   }
00313 
00314   forceinline
00315   IntBoolBrancherBase::
00316   IntBoolBrancherBase(Space& home, IntBoolBrancherBase& b)
00317     : Brancher(home,b), start(b.start),
00318       xvsc(b.xvsc->copy(home)), yvsc(b.yvsc->copy(home)) {
00319     x.update(home,b.x);
00320     y.update(home,b.y);
00321   }
00322 
00323   forceinline size_t
00324   IntBoolBrancherBase::dispose(Space& home) {
00325     home.ignore(*this,AP_DISPOSE,true);
00326     xvsc->dispose(home);
00327     yvsc->dispose(home);
00328     (void) Brancher::dispose(home);
00329     return sizeof(IntBoolBrancherBase);
00330   }
00331 
00332 
00333   template<class Merit>
00334   forceinline
00335   IntBoolBrancher<Merit>::
00336   IntBoolBrancher(Home home,
00337                   ViewArray<Int::IntView> x,
00338                   ViewArray<Int::BoolView> y,
00339                   Merit& m,
00340                   ValSelCommitBase<Int::IntView,int>* xvsc,
00341                   ValSelCommitBase<Int::BoolView,int>* yvsc)
00342     : IntBoolBrancherBase(home,x,y,xvsc,yvsc), merit(m) {}
00343 
00344   template<class Merit>
00345   forceinline void
00346   IntBoolBrancher<Merit>::
00347   post(Home home,
00348        ViewArray<Int::IntView> x,
00349        ViewArray<Int::BoolView> y,
00350        Merit& m,
00351        ValSelCommitBase<Int::IntView,int>* xvsc,
00352        ValSelCommitBase<Int::BoolView,int>* yvsc) {
00353     (void) new (home) IntBoolBrancher<Merit>(home, x, y, m, xvsc, yvsc);
00354   }
00355 
00356   template<class Merit>
00357   forceinline
00358   IntBoolBrancher<Merit>::
00359   IntBoolBrancher(Space& home, IntBoolBrancher<Merit>& b)
00360     : IntBoolBrancherBase(home,b), merit(home, b.merit) {
00361   }
00362 
00363   template<class Merit>
00364   Actor*
00365   IntBoolBrancher<Merit>::copy(Space& home) {
00366     return new (home) IntBoolBrancher<Merit>(home,*this);
00367   }
00368 
00369   template<class Merit>
00370   const Choice*
00371   IntBoolBrancher<Merit>::choice(Space& home) {
00372     int p = start;
00373     double m;
00374     if (p < x.size()) {
00375       assert(!x[p].assigned());
00376       m=merit(x[p],p);
00377       for (int i=p+1; i<x.size(); i++)
00378         if (!x[i].assigned()) {
00379           double mi = merit(x[i],i);
00380           if (mi > m) {
00381             m=mi; p=i;
00382           }
00383         }
00384       for (int i=0; i<y.size(); i++)
00385         if (!y[i].assigned()) {
00386           double mi = merit(y[i],i);
00387           if (mi > m) {
00388             m=mi; p=i+x.size();
00389           }
00390         }
00391     } else {
00392       assert(!y[p-x.size()].assigned());
00393       m=merit(y[p-x.size()],p-x.size());
00394       for (int i=p-x.size()+1; i<y.size(); i++)
00395         if (!y[i].assigned()) {
00396           double mi = merit(y[i],i);
00397           if (mi > m) {
00398             m=mi; p=i+x.size();
00399           }
00400         }
00401     }
00402     int v;
00403     if (p < x.size()) {
00404       v=xvsc->val(home,x[p],p);
00405     } else {
00406       v=yvsc->val(home,y[p-x.size()],p-x.size());
00407     }
00408     return new PosIntChoice(*this,2,p,v);
00409   }
00410 
00411   template<class Merit>
00412   size_t
00413   IntBoolBrancher<Merit>::dispose(Space& home) {
00414     merit.dispose();
00415     (void) IntBoolBrancherBase::dispose(home);
00416     return sizeof(IntBoolBrancher<Merit>);
00417   }
00418 
00419 
00420   forceinline BoolValBranch
00421   i2b(const IntValBranch& ivb) {
00422     switch (ivb.select()) {
00423     case IntValBranch::SEL_MIN:
00424     case IntValBranch::SEL_MED:
00425     case IntValBranch::SEL_SPLIT_MIN:
00426     case IntValBranch::SEL_RANGE_MIN:
00427       return BOOL_VAL_MIN();
00428     case IntValBranch::SEL_MAX:
00429     case IntValBranch::SEL_SPLIT_MAX:
00430     case IntValBranch::SEL_RANGE_MAX:
00431       return BOOL_VAL_MAX();
00432     case IntValBranch::SEL_RND:
00433       return BOOL_VAL_RND(ivb.rnd());
00434     case IntValBranch::SEL_VAL_COMMIT:
00435     default:
00436     GECODE_NEVER;
00437     }
00438     GECODE_NEVER;
00439     return BOOL_VAL_MIN();
00440   }
00441 
00442 }}
00443 
00444 // STATISTICS: flatzinc-branch
00445