Generated on Fri Mar 20 15:56:15 2015 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: 2013-02-18 21:53:58 +0100 (Mon, 18 Feb 2013) $ by $Author: schulte $
00011  *     $Revision: 13310 $
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 _Val Val;
00057     MeritBase(Space& home, const VarBranch& vb);
00059     MeritBase(Space& home, bool share, MeritBase& mb);
00061     bool notice(void) const;
00063     void dispose(Space& home);
00064   };
00065 
00069   template<class View>
00070   class MeritFunction : public MeritBase<View,double> {
00071   public:
00073     typedef typename View::VarType Var;
00075     typedef typename BranchTraits<Var>::Merit Function;
00076   protected:
00078     Function f;
00079   public:
00081     MeritFunction(Space& home, const VarBranch& vb);
00083     MeritFunction(Space& home, bool shared, MeritFunction& mf);
00085     double operator ()(const Space& home, View x, int i);
00086   };
00087 
00091   template<class View>
00092   class MeritDegree : public MeritBase<View,unsigned int> {
00093   public:
00095     MeritDegree(Space& home, const VarBranch& vb);
00097     MeritDegree(Space& home, bool shared, MeritDegree& md);
00099     unsigned int operator ()(const Space& home, View x, int i);
00100   };
00101 
00105   template<class View>
00106   class MeritAFC : public MeritBase<View,double> {
00107   protected:
00109     AFC afc;    
00110   public:
00112     MeritAFC(Space& home, const VarBranch& vb);
00114     MeritAFC(Space& home, bool shared, MeritAFC& ma);
00116     double operator ()(const Space& home, View x, int i);
00118     bool notice(void) const;
00120     void dispose(Space& home);
00121   };
00122 
00126   template<class View>
00127   class MeritActivity : public MeritBase<View,double> {
00128   protected:
00130     Activity activity;
00131   public:
00133     MeritActivity(Space& home, const VarBranch& vb);
00135     MeritActivity(Space& home, bool shared, MeritActivity& ma);
00137     double operator ()(const Space& home, View x, int i);
00139     bool notice(void) const;
00141     void dispose(Space& home);
00142   };
00144 
00145 
00146   // Merit base class
00147   template<class View, class Val>
00148   forceinline
00149   MeritBase<View,Val>::MeritBase(Space&, const VarBranch&) {}
00150   template<class View, class Val>
00151   forceinline
00152   MeritBase<View,Val>::MeritBase(Space&, bool, MeritBase&) {}
00153   template<class View, class Val>
00154   forceinline bool
00155   MeritBase<View,Val>::notice(void) const {
00156     return false;
00157   }
00158   template<class View, class Val>
00159   forceinline void
00160   MeritBase<View,Val>::dispose(Space&) {}
00161 
00162   // User-defined function merit
00163   template<class View>
00164   forceinline
00165   MeritFunction<View>::MeritFunction(Space& home, const VarBranch& vb)
00166     : MeritBase<View,double>(home,vb),
00167       f(function_cast<Function>(vb.merit())) {}
00168   template<class View>
00169   forceinline
00170   MeritFunction<View>::MeritFunction(Space& home, bool shared, 
00171                                      MeritFunction& mf) 
00172     : MeritBase<View,double>(home,shared,mf), f(mf.f) {}
00173   template<class View>
00174   forceinline double
00175   MeritFunction<View>::operator ()(const Space& home, View x, int i) {
00176     typename View::VarType y(x.varimp());
00177     return f(home,y,i);
00178   }
00179 
00180   // Degree merit
00181   template<class View>
00182   forceinline
00183   MeritDegree<View>::MeritDegree(Space& home, const VarBranch& vb)
00184     : MeritBase<View,unsigned int>(home,vb) {}
00185   template<class View>
00186   forceinline
00187   MeritDegree<View>::MeritDegree(Space& home, bool shared, 
00188                                  MeritDegree& md)
00189     : MeritBase<View,unsigned int>(home,shared,md) {}
00190   template<class View>
00191   forceinline unsigned int
00192   MeritDegree<View>::operator ()(const Space&, View x, int) {
00193     return x.degree();
00194   }
00195 
00196   // AFC merit
00197   template<class View>
00198   forceinline
00199   MeritAFC<View>::MeritAFC(Space& home, const VarBranch& vb)
00200     : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
00201   template<class View>
00202   forceinline
00203   MeritAFC<View>::MeritAFC(Space& home, bool shared, 
00204                            MeritAFC& ma)
00205     : MeritBase<View,double>(home,shared,ma) {
00206     afc.update(home,shared,ma.afc);
00207   }
00208   template<class View>
00209   forceinline double
00210   MeritAFC<View>::operator ()(const Space& home, View x, int) {
00211     return x.afc(home);
00212   }
00213   template<class View>
00214   forceinline bool
00215   MeritAFC<View>::notice(void) const {
00216     return true;
00217   }
00218   template<class View>
00219   forceinline void
00220   MeritAFC<View>::dispose(Space&) {
00221     afc.~AFC();
00222   }
00223 
00224 
00225   // Acitivity merit
00226   template<class View>
00227   forceinline
00228   MeritActivity<View>::MeritActivity(Space& home, const VarBranch& vb)
00229     : MeritBase<View,double>(home,vb), activity(vb.activity()) {}
00230   template<class View>
00231   forceinline
00232   MeritActivity<View>::MeritActivity(Space& home, bool shared, 
00233                                      MeritActivity& ma)
00234     : MeritBase<View,double>(home,shared,ma) {
00235     activity.update(home, shared, ma.activity);
00236   }
00237   template<class View>
00238   forceinline double
00239   MeritActivity<View>::operator ()(const Space&, View, int i) {
00240     return activity[i];
00241   }
00242   template<class View>
00243   forceinline bool
00244   MeritActivity<View>::notice(void) const {
00245     return true;
00246   }
00247   template<class View>
00248   forceinline void
00249   MeritActivity<View>::dispose(Space&) {
00250     activity.~Activity();
00251   }
00252 
00253 }
00254 
00255 // STATISTICS: kernel-branch