Generated on Tue May 22 09:39:40 2018 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, 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 
00035 #include <gecode/flatzinc/branch.hh>
00036 
00037 namespace Gecode { namespace FlatZinc {
00038 
00039   void
00040   PosIntChoice::archive(Archive& e) const {
00041     Choice::archive(e);
00042     e << _pos;
00043     e << _val;
00044   }
00045 
00046 
00047   bool
00048   IntBoolBrancherBase::status(const Space&) const {
00049     if (start < x.size()) {
00050       for (int i=start; i < x.size(); i++)
00051         if (!x[i].assigned()) {
00052           start = i;
00053           return true;
00054         }
00055       start = x.size();
00056     }
00057     for (int i=start-x.size(); i < y.size(); i++)
00058       if (!y[i].assigned()) {
00059         start = x.size() + i;
00060         return true;
00061       }
00062     return false;
00063   }
00064 
00065   ExecStatus
00066   IntBoolBrancherBase::commit(Space& home, const Choice& _c,
00067                               unsigned int b) {
00068     const PosIntChoice& c
00069       = static_cast<const PosIntChoice&>(_c);
00070     int p=c.pos(); int n=c.val();
00071     if (p < x.size()) {
00072       return me_failed(xvsc->commit(home,b,x[p],p,n)) ?
00073         ES_FAILED : ES_OK;
00074     } else {
00075       p -= x.size();
00076       return me_failed(yvsc->commit(home,b,y[p],p,n)) ?
00077         ES_FAILED : ES_OK;
00078     }
00079   }
00080 
00081   NGL*
00082   IntBoolBrancherBase::ngl(Space& home, const Choice& _c,
00083                            unsigned int b) const {
00084     const PosIntChoice& c
00085       = static_cast<const PosIntChoice&>(_c);
00086     int p=c.pos(); int n=c.val();
00087     if (p < x.size()) {
00088       return xvsc->ngl(home,b,x[p],n);
00089     } else {
00090       p -= x.size();
00091       return yvsc->ngl(home,b,y[p],n);
00092     }
00093   }
00094 
00095   void
00096   IntBoolBrancherBase::print(const Space& home, const Choice& _c,
00097                              unsigned int b,
00098                              std::ostream& o) const {
00099     const PosIntChoice& c
00100       = static_cast<const PosIntChoice&>(_c);
00101     int p=c.pos(); int n=c.val();
00102     if (p < x.size()) {
00103       xvsc->print(home,b,x[p],p,n,o);
00104     } else {
00105       p -= x.size();
00106       yvsc->print(home,b,y[p],p,n,o);
00107     }
00108   }
00109 
00110   const Choice*
00111   IntBoolBrancherBase::choice(const Space& home, Archive& e) {
00112     (void) home;
00113     int p; e >> p;
00114     int v; e >> v;
00115     return new PosIntChoice(*this,2,p,v);
00116   }
00117 
00118 
00119   void
00120   branch(Home home, const IntVarArgs& x, const BoolVarArgs& y,
00121          IntBoolVarBranch vars, IntValBranch vals) {
00122     if (home.failed()) return;
00123     vars.expand(home,x,y);
00124     ViewArray<Int::IntView> xv(home,x);
00125     ViewArray<Int::BoolView> yv(home,y);
00126     ValSelCommitBase<Int::IntView,int>* xvsc =
00127       Int::Branch::valselcommit(home,vals);
00128     ValSelCommitBase<Int::BoolView,int>* yvsc =
00129       Int::Branch::valselcommit(home,i2b(vals));
00130     switch (vars.select()) {
00131     case IntBoolVarBranch::SEL_AFC_MAX:
00132       {
00133         MeritMaxAFC m(home,vars);
00134         IntBoolBrancher<MeritMaxAFC>::post(home,xv,yv,m,xvsc,yvsc);
00135       }
00136       break;
00137     case IntBoolVarBranch::SEL_ACTION_MAX:
00138       {
00139         MeritMaxAction m(home,vars);
00140         IntBoolBrancher<MeritMaxAction>::post(home,xv,yv,m,xvsc,yvsc);
00141       }
00142       break;
00143     case IntBoolVarBranch::SEL_CHB_MAX:
00144       {
00145         MeritMaxCHB m(home,vars);
00146         IntBoolBrancher<MeritMaxCHB>::post(home,xv,yv,m,xvsc,yvsc);
00147       }
00148       break;
00149     case IntBoolVarBranch::SEL_AFC_SIZE_MAX:
00150       {
00151         MeritMaxAFCSize m(home,vars);
00152         IntBoolBrancher<MeritMaxAFCSize>::post(home,xv,yv,m,xvsc,yvsc);
00153       }
00154       break;
00155     case IntBoolVarBranch::SEL_ACTION_SIZE_MAX:
00156       {
00157         MeritMaxActionSize m(home,vars);
00158         IntBoolBrancher<MeritMaxActionSize>::post(home,xv,yv,m,xvsc,yvsc);
00159       }
00160       break;
00161     case IntBoolVarBranch::SEL_CHB_SIZE_MAX:
00162       {
00163         MeritMaxCHBSize m(home,vars);
00164         IntBoolBrancher<MeritMaxCHBSize>::post(home,xv,yv,m,xvsc,yvsc);
00165       }
00166       break;
00167     default:
00168       GECODE_NEVER;
00169     }
00170   }
00171 
00172 }}
00173 
00174 // STATISTICS: flatzinc-branch
00175