Generated on Mon Jul 6 18:09:15 2009 for Gecode by doxygen 1.5.9

branching-view.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, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2009-02-24 16:44:47 +0100 (Tue, 24 Feb 2009) $ by $Author: schulte $
00011  *     $Revision: 8280 $
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 
00046 
00047   class EmptyViewSelDesc {
00048   public:
00050     size_t size(void) const;
00051   };
00052 
00056   template <class _View>
00057   class ViewSelBase {
00058   public:
00060     typedef _View View;
00062     typedef EmptyViewSelDesc Desc;
00064     ViewSelBase(void);
00066     ViewSelBase(Space& home, const VarBranchOptions& vbo);
00068     EmptyViewSelDesc description(Space& home);
00070     void commit(Space& home, const EmptyViewSelDesc& d, unsigned a);
00072     void update(Space& home, bool share, ViewSelBase& vs);
00074     void dispose(Space& home);
00075   };
00076 
00080   template <class View>
00081   class ViewSelNone : public ViewSelBase<View> {
00082   public:
00084     ViewSelNone(void);
00086     ViewSelNone(Space& home, const VarBranchOptions& vbo);
00088     ViewSelStatus init(Space& home, View x);
00090     ViewSelStatus select(Space& home, View x);
00091   };
00092 
00096   template<class View>
00097   class ViewSelDegreeMin : public ViewSelBase<View> {
00098   protected:
00100     unsigned int degree;
00101   public:
00103     ViewSelDegreeMin(void);
00105     ViewSelDegreeMin(Space& home, const VarBranchOptions& vbo);
00107     ViewSelStatus init(Space& home, View x);
00109     ViewSelStatus select(Space& home, View x);
00110   };
00111 
00115   template<class View>
00116   class ViewSelDegreeMax : public ViewSelBase<View> {
00117   protected:
00119     unsigned int degree;
00120   public:
00122     ViewSelDegreeMax(void);
00124     ViewSelDegreeMax(Space& home, const VarBranchOptions& vbo);
00126     ViewSelStatus init(Space& home, View x);
00128     ViewSelStatus select(Space& home, View x);
00129   };
00130 
00134   template<class _View>
00135   class ViewSelRnd {
00136   protected:
00138     Support::RandomGenerator r;
00140     unsigned int n;
00141   public:
00143     typedef _View View;
00145     typedef Support::RandomGenerator Desc;
00147     ViewSelRnd(void);
00149     ViewSelRnd(Space& home, const VarBranchOptions& vbo);
00151     ViewSelStatus init(Space& home, _View x);
00153     ViewSelStatus select(Space& home, _View x);
00155     Support::RandomGenerator description(Space& home);
00157     void commit(Space& home, const Support::RandomGenerator& d, unsigned a);
00159     void update(Space& home, bool share, ViewSelRnd& vs);
00161     void dispose(Space& home);
00162   };
00164 
00165   // Empty view selection description
00166   forceinline size_t
00167   EmptyViewSelDesc::size(void) const {
00168     return sizeof(EmptyViewSelDesc);
00169   }
00170 
00171   // Selection base class
00172   template<class View>
00173   forceinline
00174   ViewSelBase<View>::ViewSelBase(void) {}
00175   template<class View>
00176   forceinline
00177   ViewSelBase<View>::ViewSelBase(Space&, const VarBranchOptions&) {}
00178   template<class View>
00179   forceinline EmptyViewSelDesc
00180   ViewSelBase<View>::description(Space&) {
00181     EmptyViewSelDesc d; return d;
00182   }
00183   template<class View>
00184   forceinline void
00185   ViewSelBase<View>::commit(Space&, const EmptyViewSelDesc&, unsigned int) {}
00186   template<class View>
00187   forceinline void
00188   ViewSelBase<View>::update(Space&, bool, ViewSelBase<View>&) {}
00189   template<class View>
00190   forceinline void
00191   ViewSelBase<View>::dispose(Space&) {}
00192 
00193 
00194   // Select first view
00195   template<class View>
00196   forceinline
00197   ViewSelNone<View>::ViewSelNone(void) {}
00198   template<class View>
00199   forceinline
00200   ViewSelNone<View>::ViewSelNone(Space& home, const VarBranchOptions& vbo)
00201     : ViewSelBase<View>(home,vbo) {}
00202   template<class View>
00203   forceinline ViewSelStatus
00204   ViewSelNone<View>::init(Space&, View) {
00205     return VSS_BEST;
00206   }
00207   template<class View>
00208   forceinline ViewSelStatus
00209   ViewSelNone<View>::select(Space&, View) {
00210     return VSS_BEST;
00211   }
00212 
00213 
00214   // Select variable with smallest degree
00215   template<class View>
00216   forceinline
00217   ViewSelDegreeMin<View>::ViewSelDegreeMin(void) : degree(0U) {}
00218   template<class View>
00219   forceinline
00220   ViewSelDegreeMin<View>::ViewSelDegreeMin(Space& home,
00221                                            const VarBranchOptions& vbo)
00222     : ViewSelBase<View>(home,vbo), degree(0U) {}
00223   template<class View>
00224   forceinline ViewSelStatus
00225   ViewSelDegreeMin<View>::init(Space&, View x) {
00226     degree = x.degree();
00227     return (degree == 0) ? VSS_BEST : VSS_BETTER;
00228   }
00229   template<class View>
00230   forceinline ViewSelStatus
00231   ViewSelDegreeMin<View>::select(Space&, View x) {
00232     if (x.degree() < degree) {
00233       degree = x.degree();
00234       return (degree == 0) ? VSS_BEST : VSS_BETTER;
00235     } else if (x.degree() > degree) {
00236       return VSS_WORSE;
00237     } else {
00238       return VSS_TIE;
00239     }
00240   }
00241 
00242 
00243   // Select variable with largest degree
00244   template<class View>
00245   forceinline
00246   ViewSelDegreeMax<View>::ViewSelDegreeMax(void) : degree(0) {}
00247   template<class View>
00248   forceinline
00249   ViewSelDegreeMax<View>::ViewSelDegreeMax(Space& home,
00250                                            const VarBranchOptions& vbo)
00251     : ViewSelBase<View>(home,vbo), degree(0U) {}
00252   template<class View>
00253   forceinline ViewSelStatus
00254   ViewSelDegreeMax<View>::init(Space&, View x) {
00255     degree = x.degree();
00256     return VSS_BETTER;
00257   }
00258   template<class View>
00259   forceinline ViewSelStatus
00260   ViewSelDegreeMax<View>::select(Space&, View x) {
00261     if (x.degree() > degree) {
00262       degree = x.degree();
00263       return VSS_BETTER;
00264     } else if (x.degree() < degree) {
00265       return VSS_WORSE;
00266     } else {
00267       return VSS_TIE;
00268     }
00269   }
00270 
00271 
00272   // Select variable by random
00273   template<class View>
00274   forceinline
00275   ViewSelRnd<View>::ViewSelRnd(void) : n(0) {}
00276   template<class View>
00277   forceinline
00278   ViewSelRnd<View>::ViewSelRnd(Space&, const VarBranchOptions& vbo)
00279     : r(vbo.seed), n(0) {}
00280   template<class View>
00281   forceinline ViewSelStatus
00282   ViewSelRnd<View>::init(Space&, View) {
00283     n=1;
00284     return VSS_BETTER;
00285   }
00286   template<class View>
00287   forceinline ViewSelStatus
00288   ViewSelRnd<View>::select(Space&, View) {
00289     n++;
00290     return (r(n) == (n-1)) ? VSS_BETTER : VSS_WORSE;
00291   }
00292   template<class View>
00293   forceinline Support::RandomGenerator
00294   ViewSelRnd<View>::description(Space&) {
00295     return r;
00296   }
00297   template<class View>
00298   forceinline void
00299   ViewSelRnd<View>::commit(Space&, const Support::RandomGenerator& d,
00300                            unsigned int) {
00301     r = d;
00302   }
00303   template<class View>
00304   forceinline void
00305   ViewSelRnd<View>::update(Space&, bool, ViewSelRnd<View>& vs) {
00306     r = vs.r;
00307   }
00308   template<class View>
00309   forceinline void
00310   ViewSelRnd<View>::dispose(Space&) {
00311   }
00312 
00313 }
00314 
00315 // STATISTICS: kernel-branch