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

branch.cpp

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, 2012
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 #include <gecode/int/branch.hh>
00035 
00036 namespace Gecode {
00037 
00038   void
00039   branch(Home home, const IntVarArgs& x,
00040          IntVarBranch vars, IntValBranch vals,
00041          IntBranchFilter bf,
00042          IntVarValPrint vvp) {
00043     using namespace Int;
00044     if (home.failed()) return;
00045     vars.expand(home,x);
00046     ViewArray<IntView> xv(home,x);
00047     ViewSel<IntView>* vs[1] = {
00048       Branch::viewsel(home,vars)
00049     };
00050     switch (vals.select()) {
00051     case IntValBranch::SEL_VALUES_MIN:
00052       Branch::postviewvaluesbrancher<1,true>(home,xv,vs,bf,vvp);
00053       break;
00054     case IntValBranch::SEL_VALUES_MAX:
00055       Branch::postviewvaluesbrancher<1,false>(home,xv,vs,bf,vvp);
00056       break;
00057     default:
00058       postviewvalbrancher<IntView,1,int,2>
00059         (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00060       break;
00061     }
00062   }
00063 
00064   void
00065   branch(Home home, const IntVarArgs& x,
00066          TieBreak<IntVarBranch> vars, IntValBranch vals,
00067          IntBranchFilter bf,
00068          IntVarValPrint vvp) {
00069     using namespace Int;
00070     if (home.failed()) return;
00071     vars.a.expand(home,x);
00072     if ((vars.a.select() == IntVarBranch::SEL_NONE) ||
00073         (vars.a.select() == IntVarBranch::SEL_RND))
00074       vars.b = INT_VAR_NONE();
00075     vars.b.expand(home,x);
00076     if ((vars.b.select() == IntVarBranch::SEL_NONE) ||
00077         (vars.b.select() == IntVarBranch::SEL_RND))
00078       vars.c = INT_VAR_NONE();
00079     vars.c.expand(home,x);
00080     if ((vars.c.select() == IntVarBranch::SEL_NONE) ||
00081         (vars.c.select() == IntVarBranch::SEL_RND))
00082       vars.d = INT_VAR_NONE();
00083     vars.d.expand(home,x);
00084     if (vars.b.select() == IntVarBranch::SEL_NONE) {
00085       branch(home,x,vars.a,vals,bf,vvp);
00086     } else {
00087       ViewArray<IntView> xv(home,x);
00088       if (vars.c.select() == IntVarBranch::SEL_NONE) {
00089         ViewSel<IntView>* vs[2] = {
00090           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b)
00091         };
00092         switch (vals.select()) {
00093         case IntValBranch::SEL_VALUES_MIN:
00094           Branch::postviewvaluesbrancher<2,true>(home,xv,vs,bf,vvp);
00095           break;
00096         case IntValBranch::SEL_VALUES_MAX:
00097           Branch::postviewvaluesbrancher<2,false>(home,xv,vs,bf,vvp);
00098           break;
00099         default:
00100           postviewvalbrancher<IntView,2,int,2>
00101             (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00102         }
00103       } else if (vars.d.select() == IntVarBranch::SEL_NONE) {
00104         ViewSel<IntView>* vs[3] = {
00105           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00106           Branch::viewsel(home,vars.c)
00107         };
00108         switch (vals.select()) {
00109         case IntValBranch::SEL_VALUES_MIN:
00110           Branch::postviewvaluesbrancher<3,true>(home,xv,vs,bf,vvp);
00111           break;
00112         case IntValBranch::SEL_VALUES_MAX:
00113           Branch::postviewvaluesbrancher<3,false>(home,xv,vs,bf,vvp);
00114           break;
00115         default:
00116           postviewvalbrancher<IntView,3,int,2>
00117             (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00118         }
00119       } else {
00120         ViewSel<IntView>* vs[4] = {
00121           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00122           Branch::viewsel(home,vars.c),Branch::viewsel(home,vars.d)
00123         };
00124         switch (vals.select()) {
00125         case IntValBranch::SEL_VALUES_MIN:
00126           Branch::postviewvaluesbrancher<4,true>(home,xv,vs,bf,vvp);
00127           break;
00128         case IntValBranch::SEL_VALUES_MAX:
00129           Branch::postviewvaluesbrancher<4,false>(home,xv,vs,bf,vvp);
00130           break;
00131         default:
00132           postviewvalbrancher<IntView,4,int,2>
00133             (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00134         }
00135       }
00136     }
00137   }
00138 
00139   void
00140   branch(Home home, IntVar x, IntValBranch vals, IntVarValPrint vvp) {
00141     IntVarArgs xv(1); xv[0]=x;
00142     branch(home, xv, INT_VAR_NONE(), vals, nullptr, vvp);
00143   }
00144 
00145 
00146   void
00147   assign(Home home, const IntVarArgs& x,
00148          IntVarBranch vars, IntAssign vals,
00149          IntBranchFilter bf,
00150          IntVarValPrint vvp) {
00151     using namespace Int;
00152     if (home.failed()) return;
00153     ViewArray<IntView> xv(home,x);
00154     ViewSel<IntView>* vs[1] = {
00155       new (home) ViewSelNone<IntView>(home,vars)
00156     };
00157     postviewvalbrancher<IntView,1,int,1>
00158       (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00159   }
00160 
00161   void
00162   branch(Home home, const IntVarArgs& x,
00163          TieBreak<IntVarBranch> vars, IntAssign vals,
00164          IntBranchFilter bf,
00165          IntVarValPrint vvp) {
00166     using namespace Int;
00167     if (home.failed()) return;
00168     vars.a.expand(home,x);
00169     if ((vars.a.select() == IntVarBranch::SEL_NONE) ||
00170         (vars.a.select() == IntVarBranch::SEL_RND))
00171       vars.b = INT_VAR_NONE();
00172     vars.b.expand(home,x);
00173     if ((vars.b.select() == IntVarBranch::SEL_NONE) ||
00174         (vars.b.select() == IntVarBranch::SEL_RND))
00175       vars.c = INT_VAR_NONE();
00176     vars.c.expand(home,x);
00177     if ((vars.c.select() == IntVarBranch::SEL_NONE) ||
00178         (vars.c.select() == IntVarBranch::SEL_RND))
00179       vars.d = INT_VAR_NONE();
00180     vars.d.expand(home,x);
00181     if (vars.b.select() == IntVarBranch::SEL_NONE) {
00182       assign(home,x,vars.a,vals,bf,vvp);
00183     } else {
00184       ViewArray<IntView> xv(home,x);
00185       if (vars.c.select() == IntVarBranch::SEL_NONE) {
00186         ViewSel<IntView>* vs[2] = {
00187           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b)
00188         };
00189         postviewvalbrancher<IntView,2,int,1>
00190           (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00191     } else if (vars.d.select() == IntVarBranch::SEL_NONE) {
00192         ViewSel<IntView>* vs[3] = {
00193           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00194           Branch::viewsel(home,vars.c)
00195         };
00196         postviewvalbrancher<IntView,3,int,1>
00197           (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00198       } else {
00199         ViewSel<IntView>* vs[4] = {
00200           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00201           Branch::viewsel(home,vars.c),Branch::viewsel(home,vars.d)
00202         };
00203         postviewvalbrancher<IntView,4,int,1>
00204           (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00205       }
00206     }
00207   }
00208 
00209   void
00210   assign(Home home, IntVar x, IntAssign ia, IntVarValPrint vvp) {
00211     IntVarArgs xv(1); xv[0]=x;
00212     assign(home, xv, INT_VAR_NONE(), ia, nullptr, vvp);
00213   }
00214 
00215 
00216   void
00217   branch(Home home, const BoolVarArgs& x,
00218          BoolVarBranch vars, BoolValBranch vals,
00219          BoolBranchFilter bf,
00220          BoolVarValPrint vvp) {
00221     using namespace Int;
00222     if (home.failed()) return;
00223     vars.expand(home,x);
00224     ViewArray<BoolView> xv(home,x);
00225     ViewSel<BoolView>* vs[1] = {
00226       Branch::viewsel(home,vars)
00227     };
00228     postviewvalbrancher<BoolView,1,int,2>
00229       (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00230   }
00231 
00232   void
00233   branch(Home home, const BoolVarArgs& x,
00234          TieBreak<BoolVarBranch> vars, BoolValBranch vals,
00235          BoolBranchFilter bf,
00236          BoolVarValPrint vvp) {
00237     using namespace Int;
00238     if (home.failed()) return;
00239     vars.a.expand(home,x);
00240     if ((vars.a.select() == BoolVarBranch::SEL_NONE) ||
00241         (vars.a.select() == BoolVarBranch::SEL_RND))
00242       vars.b = BOOL_VAR_NONE();
00243     vars.b.expand(home,x);
00244     if ((vars.b.select() == BoolVarBranch::SEL_NONE) ||
00245         (vars.b.select() == BoolVarBranch::SEL_RND))
00246       vars.c = BOOL_VAR_NONE();
00247     vars.c.expand(home,x);
00248     if ((vars.c.select() == BoolVarBranch::SEL_NONE) ||
00249         (vars.c.select() == BoolVarBranch::SEL_RND))
00250       vars.d = BOOL_VAR_NONE();
00251     vars.d.expand(home,x);
00252     if (vars.b.select() == BoolVarBranch::SEL_NONE) {
00253       branch(home,x,vars.a,vals,bf,vvp);
00254     } else {
00255       ViewArray<BoolView> xv(home,x);
00256       ValSelCommitBase<BoolView,int>*
00257         vsc = Branch::valselcommit(home,vals);
00258       if (vars.c.select() == BoolVarBranch::SEL_NONE) {
00259         ViewSel<BoolView>* vs[2] = {
00260           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b)
00261         };
00262         postviewvalbrancher<BoolView,2,int,2>(home,xv,vs,vsc,bf,vvp);
00263       } else if (vars.d.select() == BoolVarBranch::SEL_NONE) {
00264         ViewSel<BoolView>* vs[3] = {
00265           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00266           Branch::viewsel(home,vars.c)
00267         };
00268         postviewvalbrancher<BoolView,3,int,2>(home,xv,vs,vsc,bf,vvp);
00269       } else {
00270         ViewSel<BoolView>* vs[4] = {
00271           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00272           Branch::viewsel(home,vars.c),Branch::viewsel(home,vars.d)
00273         };
00274         postviewvalbrancher<BoolView,4,int,2>(home,xv,vs,vsc,bf,vvp);
00275       }
00276     }
00277   }
00278 
00279   void
00280   branch(Home home, BoolVar x, BoolValBranch vals, BoolVarValPrint vvp) {
00281     BoolVarArgs xv(1); xv[0]=x;
00282     branch(home, xv, BOOL_VAR_NONE(), vals, nullptr, vvp);
00283   }
00284 
00285   void
00286   assign(Home home, const BoolVarArgs& x,
00287          BoolVarBranch vars, BoolAssign vals,
00288          BoolBranchFilter bf,
00289          BoolVarValPrint vvp) {
00290     using namespace Int;
00291     if (home.failed()) return;
00292     ViewArray<BoolView> xv(home,x);
00293     ViewSel<BoolView>* vs[1] = {
00294       new (home) ViewSelNone<BoolView>(home,vars)
00295     };
00296     postviewvalbrancher<BoolView,1,int,1>
00297       (home,xv,vs,Branch::valselcommit(home,vals),bf,vvp);
00298   }
00299 
00300   void
00301   assign(Home home, const BoolVarArgs& x,
00302          TieBreak<BoolVarBranch> vars, BoolAssign vals,
00303          BoolBranchFilter bf,
00304          BoolVarValPrint vvp) {
00305     using namespace Int;
00306     if (home.failed()) return;
00307     vars.a.expand(home,x);
00308     if ((vars.a.select() == BoolVarBranch::SEL_NONE) ||
00309         (vars.a.select() == BoolVarBranch::SEL_RND))
00310       vars.b = BOOL_VAR_NONE();
00311     vars.b.expand(home,x);
00312     if ((vars.b.select() == BoolVarBranch::SEL_NONE) ||
00313         (vars.b.select() == BoolVarBranch::SEL_RND))
00314       vars.c = BOOL_VAR_NONE();
00315     vars.c.expand(home,x);
00316     if ((vars.c.select() == BoolVarBranch::SEL_NONE) ||
00317         (vars.c.select() == BoolVarBranch::SEL_RND))
00318       vars.d = BOOL_VAR_NONE();
00319     vars.d.expand(home,x);
00320     if (vars.b.select() == BoolVarBranch::SEL_NONE) {
00321       assign(home,x,vars.a,vals,bf,vvp);
00322     } else {
00323       ViewArray<BoolView> xv(home,x);
00324       ValSelCommitBase<BoolView,int>*
00325         vsc = Branch::valselcommit(home,vals);
00326       if (vars.c.select() == BoolVarBranch::SEL_NONE) {
00327         ViewSel<BoolView>* vs[2] = {
00328           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b)
00329         };
00330         postviewvalbrancher<BoolView,2,int,1>(home,xv,vs,vsc,bf,vvp);
00331       } else if (vars.d.select() == BoolVarBranch::SEL_NONE) {
00332         ViewSel<BoolView>* vs[3] = {
00333           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00334           Branch::viewsel(home,vars.c)
00335         };
00336         postviewvalbrancher<BoolView,3,int,1>(home,xv,vs,vsc,bf,vvp);
00337       } else {
00338         ViewSel<BoolView>* vs[4] = {
00339           Branch::viewsel(home,vars.a),Branch::viewsel(home,vars.b),
00340           Branch::viewsel(home,vars.c),Branch::viewsel(home,vars.d)
00341         };
00342         postviewvalbrancher<BoolView,4,int,1>(home,xv,vs,vsc,bf,vvp);
00343       }
00344     }
00345   }
00346 
00347   void
00348   assign(Home home, BoolVar x, BoolAssign ba, BoolVarValPrint vvp) {
00349     BoolVarArgs xv(1); xv[0]=x;
00350     assign(home, xv, BOOL_VAR_NONE(), ba, nullptr, vvp);
00351   }
00352 
00353 #ifdef GECODE_HAS_CBS
00354 
00355   void
00356   cbsbranch(Home home, const IntVarArgs& x) {
00357     using namespace Int;
00358     if (home.failed()) return;
00359     ViewArray<IntView> y(home,x);
00360     Branch::CBSBrancher<IntView>::post(home,y);
00361   }
00362 
00363   void
00364   cbsbranch(Home home, const BoolVarArgs& x) {
00365     using namespace Int;
00366     if (home.failed()) return;
00367     ViewArray<BoolView> y(home,x);
00368     Branch::CBSBrancher<BoolView>::post(home,y);
00369   }
00370 
00371 #endif
00372 
00373 }
00374 
00375 // STATISTICS: int-post