Generated on Sun Feb 17 15:24:06 2019 for Gecode by doxygen 1.6.3

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  *  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 {
00035 
00045   template<class _View, class _Val>
00046   class MeritBase {
00047   public:
00049     typedef _View View;
00051     typedef typename View::VarType Var;
00053     typedef _Val Val;
00055     MeritBase(Space& home, const VarBranch<Var>& vb);
00057     MeritBase(Space& home, MeritBase& mb);
00059     bool notice(void) const;
00061     void dispose(Space& home);
00062   };
00063 
00067   template<class View>
00068   class MeritFunction : public MeritBase<View,double> {
00069     using typename MeritBase<View,double>::Var;
00070   public:
00072     typedef typename BranchTraits<Var>::Merit Function;
00073   protected:
00075     SharedData<Function> f;
00076   public:
00078     MeritFunction(Space& home, const VarBranch<Var>& vb);
00080     MeritFunction(Space& home, MeritFunction& mf);
00082     double operator ()(const Space& home, View x, int i);
00084     bool notice(void) const;
00086     void dispose(Space& home);
00087   };
00088 
00092   template<class View>
00093   class MeritDegree : public MeritBase<View,unsigned int> {
00094     using typename MeritBase<View,unsigned int>::Var;
00095   public:
00097     MeritDegree(Space& home, const VarBranch<Var>& vb);
00099     MeritDegree(Space& home, MeritDegree& md);
00101     unsigned int operator ()(const Space& home, View x, int i);
00102   };
00103 
00107   template<class View>
00108   class MeritAFC : public MeritBase<View,double> {
00109     using typename MeritBase<View,double>::Var;
00110   protected:
00112     AFC afc;
00113   public:
00115     MeritAFC(Space& home, const VarBranch<Var>& vb);
00117     MeritAFC(Space& home, MeritAFC& ma);
00119     double operator ()(const Space& home, View x, int i);
00121     bool notice(void) const;
00123     void dispose(Space& home);
00124   };
00125 
00129   template<class View>
00130   class MeritAction : public MeritBase<View,double> {
00131     using typename MeritBase<View,double>::Var;
00132   protected:
00134     Action action;
00135   public:
00137     MeritAction(Space& home, const VarBranch<Var>& vb);
00139     MeritAction(Space& home, MeritAction& ma);
00141     double operator ()(const Space& home, View x, int i);
00143     bool notice(void) const;
00145     void dispose(Space& home);
00146   };
00148 
00152   template<class View>
00153   class MeritCHB : public MeritBase<View,double> {
00154     using typename MeritBase<View,double>::Var;
00155   protected:
00157     CHB chb;
00158   public:
00160     MeritCHB(Space& home, const VarBranch<Var>& vb);
00162     MeritCHB(Space& home, MeritCHB& ma);
00164     double operator ()(const Space& home, View x, int i);
00166     bool notice(void) const;
00168     void dispose(Space& home);
00169   };
00171 
00172 
00173   // Merit base class
00174   template<class View, class Val>
00175   forceinline
00176   MeritBase<View,Val>::MeritBase(Space&, const VarBranch<Var>&) {}
00177   template<class View, class Val>
00178   forceinline
00179   MeritBase<View,Val>::MeritBase(Space&, MeritBase&) {}
00180   template<class View, class Val>
00181   forceinline bool
00182   MeritBase<View,Val>::notice(void) const {
00183     return false;
00184   }
00185   template<class View, class Val>
00186   forceinline void
00187   MeritBase<View,Val>::dispose(Space&) {}
00188 
00189   // User-defined function merit
00190   template<class View>
00191   forceinline
00192   MeritFunction<View>::MeritFunction
00193     (Space& home, const VarBranch<MeritFunction<View>::Var>& vb)
00194     : MeritBase<View,double>(home,vb), f(vb.merit()) {
00195     if (!f())
00196       throw InvalidFunction("MeritFunction::MeritFunction");
00197   }
00198   template<class View>
00199   forceinline
00200   MeritFunction<View>::MeritFunction(Space& home, MeritFunction& mf)
00201     : MeritBase<View,double>(home,mf), f(mf.f) {
00202   }
00203   template<class View>
00204   forceinline double
00205   MeritFunction<View>::operator ()(const Space& home, View x, int i) {
00206     typename View::VarType y(x.varimp());
00207     GECODE_VALID_FUNCTION(f());
00208     return f()(home,y,i);
00209   }
00210   template<class View>
00211   forceinline bool
00212   MeritFunction<View>::notice(void) const {
00213     return true;
00214   }
00215   template<class View>
00216   forceinline void
00217   MeritFunction<View>::dispose(Space&) {
00218     f.~SharedData<Function>();
00219   }
00220 
00221 
00222   // Degree merit
00223   template<class View>
00224   forceinline
00225   MeritDegree<View>::MeritDegree
00226     (Space& home, const VarBranch<MeritDegree<View>::Var>& vb)
00227     : MeritBase<View,unsigned int>(home,vb) {}
00228   template<class View>
00229   forceinline
00230   MeritDegree<View>::MeritDegree(Space& home, MeritDegree& md)
00231     : MeritBase<View,unsigned int>(home,md) {}
00232   template<class View>
00233   forceinline unsigned int
00234   MeritDegree<View>::operator ()(const Space&, View x, int) {
00235     return x.degree();
00236   }
00237 
00238   // AFC merit
00239   template<class View>
00240   forceinline
00241   MeritAFC<View>::MeritAFC
00242     (Space& home, const VarBranch<MeritAFC<View>::Var>& vb)
00243     : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
00244   template<class View>
00245   forceinline
00246   MeritAFC<View>::MeritAFC(Space& home, MeritAFC& ma)
00247     : MeritBase<View,double>(home,ma), afc(ma.afc) {}
00248   template<class View>
00249   forceinline double
00250   MeritAFC<View>::operator ()(const Space&, View x, int) {
00251     return x.afc();
00252   }
00253   template<class View>
00254   forceinline bool
00255   MeritAFC<View>::notice(void) const {
00256     // Given that AFC is just a fake, this not really necessary
00257     return false;
00258   }
00259   template<class View>
00260   forceinline void
00261   MeritAFC<View>::dispose(Space&) {
00262     // Given that AFC is just a fake, this not really necessary
00263     afc.~AFC();
00264   }
00265 
00266 
00267   // Action merit
00268   template<class View>
00269   forceinline
00270   MeritAction<View>::MeritAction
00271     (Space& home, const VarBranch<MeritAction<View>::Var>& vb)
00272     : MeritBase<View,double>(home,vb), action(vb.action()) {}
00273   template<class View>
00274   forceinline
00275   MeritAction<View>::MeritAction(Space& home, MeritAction& ma)
00276     : MeritBase<View,double>(home,ma), action(ma.action) {}
00277   template<class View>
00278   forceinline double
00279   MeritAction<View>::operator ()(const Space&, View, int i) {
00280     return action[i];
00281   }
00282   template<class View>
00283   forceinline bool
00284   MeritAction<View>::notice(void) const {
00285     return true;
00286   }
00287   template<class View>
00288   forceinline void
00289   MeritAction<View>::dispose(Space&) {
00290     action.~Action();
00291   }
00292 
00293   // CHB merit
00294   template<class View>
00295   forceinline
00296   MeritCHB<View>::MeritCHB
00297     (Space& home, const VarBranch<MeritCHB<View>::Var>& vb)
00298     : MeritBase<View,double>(home,vb), chb(vb.chb()) {}
00299   template<class View>
00300   forceinline
00301   MeritCHB<View>::MeritCHB(Space& home, MeritCHB& ma)
00302     : MeritBase<View,double>(home,ma), chb(ma.chb) {}
00303   template<class View>
00304   forceinline double
00305   MeritCHB<View>::operator ()(const Space&, View, int i) {
00306     return chb[i];
00307   }
00308   template<class View>
00309   forceinline bool
00310   MeritCHB<View>::notice(void) const {
00311     return true;
00312   }
00313   template<class View>
00314   forceinline void
00315   MeritCHB<View>::dispose(Space&) {
00316     chb.~CHB();
00317   }
00318 
00319 }
00320 
00321 // STATISTICS: kernel-branch