Generated on Tue Apr 18 10:22:04 2017 for Gecode by doxygen 1.6.3

brancher-merit.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main author:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2012
00008  *
00009  *  Last modified:
00010  *     $Date: 2017-03-01 04:28:36 +0100 (Wed, 01 Mar 2017) $ by $Author: schulte $
00011  *     $Revision: 15541 $
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 {
00039 
00049   template<class _View, class _Val>
00050   class MeritBase {
00051   public:
00053     typedef _View View;
00055     typedef typename View::VarType Var;
00057     typedef _Val Val;
00059     MeritBase(Space& home, const VarBranch<Var>& vb);
00061     MeritBase(Space& home, bool share, MeritBase& mb);
00063     bool notice(void) const;
00065     void dispose(Space& home);
00066   };
00067 
00071   template<class View>
00072   class MeritFunction : public MeritBase<View,double> {
00073     using typename MeritBase<View,double>::Var;
00074   public:
00076     typedef typename BranchTraits<Var>::Merit Function;
00077   protected:
00079     SharedData<Function> f;
00080   public:
00082     MeritFunction(Space& home, const VarBranch<Var>& vb);
00084     MeritFunction(Space& home, bool shared, MeritFunction& mf);
00086     double operator ()(const Space& home, View x, int i);
00088     bool notice(void) const;
00090     void dispose(Space& home);
00091   };
00092 
00096   template<class View>
00097   class MeritDegree : public MeritBase<View,unsigned int> {
00098     using typename MeritBase<View,unsigned int>::Var;
00099   public:
00101     MeritDegree(Space& home, const VarBranch<Var>& vb);
00103     MeritDegree(Space& home, bool shared, MeritDegree& md);
00105     unsigned int operator ()(const Space& home, View x, int i);
00106   };
00107 
00111   template<class View>
00112   class MeritAFC : public MeritBase<View,double> {
00113     using typename MeritBase<View,double>::Var;
00114   protected:
00116     AFC afc;
00117   public:
00119     MeritAFC(Space& home, const VarBranch<Var>& vb);
00121     MeritAFC(Space& home, bool shared, MeritAFC& ma);
00123     double operator ()(const Space& home, View x, int i);
00125     bool notice(void) const;
00127     void dispose(Space& home);
00128   };
00129 
00133   template<class View>
00134   class MeritAction : public MeritBase<View,double> {
00135     using typename MeritBase<View,double>::Var;
00136   protected:
00138     Action action;
00139   public:
00141     MeritAction(Space& home, const VarBranch<Var>& vb);
00143     MeritAction(Space& home, bool shared, MeritAction& ma);
00145     double operator ()(const Space& home, View x, int i);
00147     bool notice(void) const;
00149     void dispose(Space& home);
00150   };
00152 
00156   template<class View>
00157   class MeritCHB : public MeritBase<View,double> {
00158     using typename MeritBase<View,double>::Var;
00159   protected:
00161     CHB chb;
00162   public:
00164     MeritCHB(Space& home, const VarBranch<Var>& vb);
00166     MeritCHB(Space& home, bool shared, MeritCHB& ma);
00168     double operator ()(const Space& home, View x, int i);
00170     bool notice(void) const;
00172     void dispose(Space& home);
00173   };
00175 
00176 
00177   // Merit base class
00178   template<class View, class Val>
00179   forceinline
00180   MeritBase<View,Val>::MeritBase(Space&, const VarBranch<Var>&) {}
00181   template<class View, class Val>
00182   forceinline
00183   MeritBase<View,Val>::MeritBase(Space&, bool, MeritBase&) {}
00184   template<class View, class Val>
00185   forceinline bool
00186   MeritBase<View,Val>::notice(void) const {
00187     return false;
00188   }
00189   template<class View, class Val>
00190   forceinline void
00191   MeritBase<View,Val>::dispose(Space&) {}
00192 
00193   // User-defined function merit
00194   template<class View>
00195   forceinline
00196   MeritFunction<View>::MeritFunction(Space& home, const VarBranch<Var>& vb)
00197     : MeritBase<View,double>(home,vb), f(vb.merit()) {
00198     if (!f())
00199       throw InvalidFunction("MeritFunction::MeritFunction");
00200   }
00201   template<class View>
00202   forceinline
00203   MeritFunction<View>::MeritFunction(Space& home, bool shared,
00204                                      MeritFunction& mf)
00205     : MeritBase<View,double>(home,shared,mf) {
00206     f.update(home,shared,mf.f);
00207   }
00208   template<class View>
00209   forceinline double
00210   MeritFunction<View>::operator ()(const Space& home, View x, int i) {
00211     typename View::VarType y(x.varimp());
00212     GECODE_VALID_FUNCTION(f());
00213     return f()(home,y,i);
00214   }
00215   template<class View>
00216   forceinline bool
00217   MeritFunction<View>::notice(void) const {
00218     return true;
00219   }
00220   template<class View>
00221   forceinline void
00222   MeritFunction<View>::dispose(Space&) {
00223     f.~SharedData<Function>();
00224   }
00225 
00226 
00227   // Degree merit
00228   template<class View>
00229   forceinline
00230   MeritDegree<View>::MeritDegree(Space& home, const VarBranch<Var>& vb)
00231     : MeritBase<View,unsigned int>(home,vb) {}
00232   template<class View>
00233   forceinline
00234   MeritDegree<View>::MeritDegree(Space& home, bool shared,
00235                                  MeritDegree& md)
00236     : MeritBase<View,unsigned int>(home,shared,md) {}
00237   template<class View>
00238   forceinline unsigned int
00239   MeritDegree<View>::operator ()(const Space&, View x, int) {
00240     return x.degree();
00241   }
00242 
00243   // AFC merit
00244   template<class View>
00245   forceinline
00246   MeritAFC<View>::MeritAFC(Space& home, const VarBranch<Var>& vb)
00247     : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
00248   template<class View>
00249   forceinline
00250   MeritAFC<View>::MeritAFC(Space& home, bool shared,
00251                            MeritAFC& ma)
00252     : MeritBase<View,double>(home,shared,ma) {
00253     afc.update(home,shared,ma.afc);
00254   }
00255   template<class View>
00256   forceinline double
00257   MeritAFC<View>::operator ()(const Space&, View x, int) {
00258     return x.afc();
00259   }
00260   template<class View>
00261   forceinline bool
00262   MeritAFC<View>::notice(void) const {
00263     return true;
00264   }
00265   template<class View>
00266   forceinline void
00267   MeritAFC<View>::dispose(Space&) {
00268     afc.~AFC();
00269   }
00270 
00271 
00272   // Action merit
00273   template<class View>
00274   forceinline
00275   MeritAction<View>::MeritAction(Space& home, const VarBranch<Var>& vb)
00276     : MeritBase<View,double>(home,vb), action(vb.action()) {}
00277   template<class View>
00278   forceinline
00279   MeritAction<View>::MeritAction(Space& home, bool shared,
00280                                  MeritAction& ma)
00281     : MeritBase<View,double>(home,shared,ma) {
00282     action.update(home, shared, ma.action);
00283   }
00284   template<class View>
00285   forceinline double
00286   MeritAction<View>::operator ()(const Space&, View, int i) {
00287     return action[i];
00288   }
00289   template<class View>
00290   forceinline bool
00291   MeritAction<View>::notice(void) const {
00292     return true;
00293   }
00294   template<class View>
00295   forceinline void
00296   MeritAction<View>::dispose(Space&) {
00297     action.~Action();
00298   }
00299 
00300   // CHB merit
00301   template<class View>
00302   forceinline
00303   MeritCHB<View>::MeritCHB(Space& home, const VarBranch<Var>& vb)
00304     : MeritBase<View,double>(home,vb), chb(vb.chb()) {}
00305   template<class View>
00306   forceinline
00307   MeritCHB<View>::MeritCHB(Space& home, bool shared,
00308                                  MeritCHB& ma)
00309     : MeritBase<View,double>(home,shared,ma) {
00310     chb.update(home, shared, ma.chb);
00311   }
00312   template<class View>
00313   forceinline double
00314   MeritCHB<View>::operator ()(const Space&, View, int i) {
00315     return chb[i];
00316   }
00317   template<class View>
00318   forceinline bool
00319   MeritCHB<View>::notice(void) const {
00320     return true;
00321   }
00322   template<class View>
00323   forceinline void
00324   MeritCHB<View>::dispose(Space&) {
00325     chb.~CHB();
00326   }
00327 
00328 }
00329 
00330 // STATISTICS: kernel-branch