Generated on Tue May 22 09:39:44 2018 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(Space& home, const VarBranch<Var>& vb)
00193     : MeritBase<View,double>(home,vb), f(vb.merit()) {
00194     if (!f())
00195       throw InvalidFunction("MeritFunction::MeritFunction");
00196   }
00197   template<class View>
00198   forceinline
00199   MeritFunction<View>::MeritFunction(Space& home, MeritFunction& mf)
00200     : MeritBase<View,double>(home,mf), f(mf.f) {
00201   }
00202   template<class View>
00203   forceinline double
00204   MeritFunction<View>::operator ()(const Space& home, View x, int i) {
00205     typename View::VarType y(x.varimp());
00206     GECODE_VALID_FUNCTION(f());
00207     return f()(home,y,i);
00208   }
00209   template<class View>
00210   forceinline bool
00211   MeritFunction<View>::notice(void) const {
00212     return true;
00213   }
00214   template<class View>
00215   forceinline void
00216   MeritFunction<View>::dispose(Space&) {
00217     f.~SharedData<Function>();
00218   }
00219 
00220 
00221   // Degree merit
00222   template<class View>
00223   forceinline
00224   MeritDegree<View>::MeritDegree(Space& home, const VarBranch<Var>& vb)
00225     : MeritBase<View,unsigned int>(home,vb) {}
00226   template<class View>
00227   forceinline
00228   MeritDegree<View>::MeritDegree(Space& home, MeritDegree& md)
00229     : MeritBase<View,unsigned int>(home,md) {}
00230   template<class View>
00231   forceinline unsigned int
00232   MeritDegree<View>::operator ()(const Space&, View x, int) {
00233     return x.degree();
00234   }
00235 
00236   // AFC merit
00237   template<class View>
00238   forceinline
00239   MeritAFC<View>::MeritAFC(Space& home, const VarBranch<Var>& vb)
00240     : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
00241   template<class View>
00242   forceinline
00243   MeritAFC<View>::MeritAFC(Space& home, MeritAFC& ma)
00244     : MeritBase<View,double>(home,ma), afc(ma.afc) {}
00245   template<class View>
00246   forceinline double
00247   MeritAFC<View>::operator ()(const Space&, View x, int) {
00248     return x.afc();
00249   }
00250   template<class View>
00251   forceinline bool
00252   MeritAFC<View>::notice(void) const {
00253     // Given that AFC is just a fake, this not really necessary
00254     return false;
00255   }
00256   template<class View>
00257   forceinline void
00258   MeritAFC<View>::dispose(Space&) {
00259     // Given that AFC is just a fake, this not really necessary
00260     afc.~AFC();
00261   }
00262 
00263 
00264   // Action merit
00265   template<class View>
00266   forceinline
00267   MeritAction<View>::MeritAction(Space& home, const VarBranch<Var>& vb)
00268     : MeritBase<View,double>(home,vb), action(vb.action()) {}
00269   template<class View>
00270   forceinline
00271   MeritAction<View>::MeritAction(Space& home, MeritAction& ma)
00272     : MeritBase<View,double>(home,ma), action(ma.action) {}
00273   template<class View>
00274   forceinline double
00275   MeritAction<View>::operator ()(const Space&, View, int i) {
00276     return action[i];
00277   }
00278   template<class View>
00279   forceinline bool
00280   MeritAction<View>::notice(void) const {
00281     return true;
00282   }
00283   template<class View>
00284   forceinline void
00285   MeritAction<View>::dispose(Space&) {
00286     action.~Action();
00287   }
00288 
00289   // CHB merit
00290   template<class View>
00291   forceinline
00292   MeritCHB<View>::MeritCHB(Space& home, const VarBranch<Var>& vb)
00293     : MeritBase<View,double>(home,vb), chb(vb.chb()) {}
00294   template<class View>
00295   forceinline
00296   MeritCHB<View>::MeritCHB(Space& home, MeritCHB& ma)
00297     : MeritBase<View,double>(home,ma), chb(ma.chb) {}
00298   template<class View>
00299   forceinline double
00300   MeritCHB<View>::operator ()(const Space&, View, int i) {
00301     return chb[i];
00302   }
00303   template<class View>
00304   forceinline bool
00305   MeritCHB<View>::notice(void) const {
00306     return true;
00307   }
00308   template<class View>
00309   forceinline void
00310   MeritCHB<View>::dispose(Space&) {
00311     chb.~CHB();
00312   }
00313 
00314 }
00315 
00316 // STATISTICS: kernel-branch