Generated on Thu Mar 22 10:39:42 2012 for Gecode by doxygen 1.6.3

brancher-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: 2011-05-11 12:44:17 +0200 (Wed, 11 May 2011) $ by $Author: tack $
00011  *     $Revision: 12001 $
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 EmptyViewSelChoice {
00048   public:
00050     size_t size(void) const;
00052     void archive(Archive& e) const;
00053   };
00054 
00058   template<class _View>
00059   class ViewSelBase {
00060   public:
00062     typedef _View View;
00064     typedef EmptyViewSelChoice Choice;
00066     ViewSelBase(void);
00068     ViewSelBase(Space& home, const VarBranchOptions& vbo);
00070     EmptyViewSelChoice choice(Space& home);
00072     EmptyViewSelChoice choice(const Space& home, Archive& e);
00074     void commit(Space& home, const EmptyViewSelChoice& c, unsigned a);
00076     void update(Space& home, bool share, ViewSelBase& vs);
00078     void dispose(Space& home);
00079   };
00080 
00084   template<class View>
00085   class ViewSelNone : public ViewSelBase<View> {
00086   public:
00088     ViewSelNone(void);
00090     ViewSelNone(Space& home, const VarBranchOptions& vbo);
00092     ViewSelStatus init(Space& home, View x);
00094     ViewSelStatus select(Space& home, View x);
00095   };
00096 
00100   template<class View>
00101   class ViewSelDegreeMin : public ViewSelBase<View> {
00102   protected:
00104     unsigned int degree;
00105   public:
00107     ViewSelDegreeMin(void);
00109     ViewSelDegreeMin(Space& home, const VarBranchOptions& vbo);
00111     ViewSelStatus init(Space& home, View x);
00113     ViewSelStatus select(Space& home, View x);
00114   };
00115 
00119   template<class View>
00120   class ViewSelDegreeMax : public ViewSelBase<View> {
00121   protected:
00123     unsigned int degree;
00124   public:
00126     ViewSelDegreeMax(void);
00128     ViewSelDegreeMax(Space& home, const VarBranchOptions& vbo);
00130     ViewSelStatus init(Space& home, View x);
00132     ViewSelStatus select(Space& home, View x);
00133   };
00134 
00138   template<class View>
00139   class ViewSelAfcMin : public ViewSelBase<View> {
00140   protected:
00142     double afc;
00143   public:
00145     ViewSelAfcMin(void);
00147     ViewSelAfcMin(Space& home, const VarBranchOptions& vbo);
00149     ViewSelStatus init(Space& home, View x);
00151     ViewSelStatus select(Space& home, View x);
00152   };
00153 
00157   template<class View>
00158   class ViewSelAfcMax : public ViewSelBase<View> {
00159   protected:
00161     double afc;
00162   public:
00164     ViewSelAfcMax(void);
00166     ViewSelAfcMax(Space& home, const VarBranchOptions& vbo);
00168     ViewSelStatus init(Space& home, View x);
00170     ViewSelStatus select(Space& home, View x);
00171   };
00172 
00174   class ArchivedRandomGenerator : public Support::RandomGenerator {
00175   public:
00177     ArchivedRandomGenerator(void);
00179     ArchivedRandomGenerator(unsigned int seed);
00181     ArchivedRandomGenerator(const Support::RandomGenerator& r);
00183     void archive(Archive& e) const;
00184   };
00185 
00189   template<class _View>
00190   class ViewSelRnd {
00191   protected:
00193     ArchivedRandomGenerator r;
00195     unsigned int n;
00196   public:
00198     typedef _View View;
00200     typedef ArchivedRandomGenerator Choice;
00202     ViewSelRnd(void);
00204     ViewSelRnd(Space& home, const VarBranchOptions& vbo);
00206     ViewSelStatus init(Space& home, _View x);
00208     ViewSelStatus select(Space& home, _View x);
00210     Choice choice(Space& home);
00212     Choice choice(const Space& home, Archive& e);
00214     void commit(Space& home, const Choice& c, unsigned a);
00216     void update(Space& home, bool share, ViewSelRnd& vs);
00218     void dispose(Space& home);
00219   };
00221 
00222   // Empty view selection choice
00223   forceinline size_t
00224   EmptyViewSelChoice::size(void) const {
00225     return sizeof(EmptyViewSelChoice);
00226   }
00227 
00228   forceinline void
00229   EmptyViewSelChoice::archive(Archive& e) const { (void)e; }
00230 
00231   // Selection base class
00232   template<class View>
00233   forceinline
00234   ViewSelBase<View>::ViewSelBase(void) {}
00235   template<class View>
00236   forceinline
00237   ViewSelBase<View>::ViewSelBase(Space&, const VarBranchOptions&) {}
00238   template<class View>
00239   forceinline EmptyViewSelChoice
00240   ViewSelBase<View>::choice(Space&) {
00241     EmptyViewSelChoice c; return c;
00242   }
00243   template<class View>
00244   forceinline EmptyViewSelChoice
00245   ViewSelBase<View>::choice(const Space&, Archive&) {
00246     EmptyViewSelChoice c; return c;
00247   }
00248   template<class View>
00249   forceinline void
00250   ViewSelBase<View>::commit(Space&, const EmptyViewSelChoice&, unsigned int) {}
00251   template<class View>
00252   forceinline void
00253   ViewSelBase<View>::update(Space&, bool, ViewSelBase<View>&) {}
00254   template<class View>
00255   forceinline void
00256   ViewSelBase<View>::dispose(Space&) {}
00257 
00258 
00259   // Select first view
00260   template<class View>
00261   forceinline
00262   ViewSelNone<View>::ViewSelNone(void) {}
00263   template<class View>
00264   forceinline
00265   ViewSelNone<View>::ViewSelNone(Space& home, const VarBranchOptions& vbo)
00266     : ViewSelBase<View>(home,vbo) {}
00267   template<class View>
00268   forceinline ViewSelStatus
00269   ViewSelNone<View>::init(Space&, View) {
00270     return VSS_BEST;
00271   }
00272   template<class View>
00273   forceinline ViewSelStatus
00274   ViewSelNone<View>::select(Space&, View) {
00275     return VSS_BEST;
00276   }
00277 
00278 
00279   // Select variable with smallest degree
00280   template<class View>
00281   forceinline
00282   ViewSelDegreeMin<View>::ViewSelDegreeMin(void) : degree(0U) {}
00283   template<class View>
00284   forceinline
00285   ViewSelDegreeMin<View>::ViewSelDegreeMin(Space& home,
00286                                            const VarBranchOptions& vbo)
00287     : ViewSelBase<View>(home,vbo), degree(0U) {}
00288   template<class View>
00289   forceinline ViewSelStatus
00290   ViewSelDegreeMin<View>::init(Space&, View x) {
00291     degree = x.degree();
00292     return (degree == 0) ? VSS_BEST : VSS_BETTER;
00293   }
00294   template<class View>
00295   forceinline ViewSelStatus
00296   ViewSelDegreeMin<View>::select(Space&, View x) {
00297     if (x.degree() < degree) {
00298       degree = x.degree();
00299       return (degree == 0) ? VSS_BEST : VSS_BETTER;
00300     } else if (x.degree() > degree) {
00301       return VSS_WORSE;
00302     } else {
00303       return VSS_TIE;
00304     }
00305   }
00306 
00307 
00308   // Select variable with largest degree
00309   template<class View>
00310   forceinline
00311   ViewSelDegreeMax<View>::ViewSelDegreeMax(void) : degree(0) {}
00312   template<class View>
00313   forceinline
00314   ViewSelDegreeMax<View>::ViewSelDegreeMax(Space& home,
00315                                            const VarBranchOptions& vbo)
00316     : ViewSelBase<View>(home,vbo), degree(0U) {}
00317   template<class View>
00318   forceinline ViewSelStatus
00319   ViewSelDegreeMax<View>::init(Space&, View x) {
00320     degree = x.degree();
00321     return VSS_BETTER;
00322   }
00323   template<class View>
00324   forceinline ViewSelStatus
00325   ViewSelDegreeMax<View>::select(Space&, View x) {
00326     if (x.degree() > degree) {
00327       degree = x.degree();
00328       return VSS_BETTER;
00329     } else if (x.degree() < degree) {
00330       return VSS_WORSE;
00331     } else {
00332       return VSS_TIE;
00333     }
00334   }
00335 
00336 
00337   // Select variable with smallest afc
00338   template<class View>
00339   forceinline
00340   ViewSelAfcMin<View>::ViewSelAfcMin(void) : afc(0.0) {}
00341   template<class View>
00342   forceinline
00343   ViewSelAfcMin<View>::ViewSelAfcMin(Space& home,
00344                                      const VarBranchOptions& vbo)
00345     : ViewSelBase<View>(home,vbo), afc(0.0) {}
00346   template<class View>
00347   forceinline ViewSelStatus
00348   ViewSelAfcMin<View>::init(Space&, View x) {
00349     afc = x.afc();
00350     return (afc == 0.0) ? VSS_BEST : VSS_BETTER;
00351   }
00352   template<class View>
00353   forceinline ViewSelStatus
00354   ViewSelAfcMin<View>::select(Space&, View x) {
00355     if (x.afc() < afc) {
00356       afc = x.afc();
00357       return (afc == 0.0) ? VSS_BEST : VSS_BETTER;
00358     } else if (x.afc() > afc) {
00359       return VSS_WORSE;
00360     } else {
00361       return VSS_TIE;
00362     }
00363   }
00364 
00365 
00366   // Select variable with largest afc
00367   template<class View>
00368   forceinline
00369   ViewSelAfcMax<View>::ViewSelAfcMax(void) : afc(0.0) {}
00370   template<class View>
00371   forceinline
00372   ViewSelAfcMax<View>::ViewSelAfcMax(Space& home,
00373                                      const VarBranchOptions& vbo)
00374     : ViewSelBase<View>(home,vbo), afc(0.0) {}
00375   template<class View>
00376   forceinline ViewSelStatus
00377   ViewSelAfcMax<View>::init(Space&, View x) {
00378     afc = x.afc();
00379     return VSS_BETTER;
00380   }
00381   template<class View>
00382   forceinline ViewSelStatus
00383   ViewSelAfcMax<View>::select(Space&, View x) {
00384     double xafc = x.afc();
00385     if (xafc > afc) {
00386       afc = xafc;
00387       return VSS_BETTER;
00388     } else if (xafc < afc) {
00389       return VSS_WORSE;
00390     } else {
00391       return VSS_TIE;
00392     }
00393   }
00394 
00395 
00396   // Archived random generator
00397   forceinline
00398   ArchivedRandomGenerator::ArchivedRandomGenerator(void) {}
00399   forceinline
00400   ArchivedRandomGenerator::ArchivedRandomGenerator(unsigned int seed)
00401     : Support::RandomGenerator(seed) {}
00402   forceinline
00403   ArchivedRandomGenerator::ArchivedRandomGenerator
00404     (const Support::RandomGenerator& r) : Support::RandomGenerator(r) {}
00405   forceinline void
00406   ArchivedRandomGenerator::archive(Archive& e) const { e << seed(); }
00407 
00408   // Select variable by random
00409   template<class View>
00410   forceinline
00411   ViewSelRnd<View>::ViewSelRnd(void) : n(0) {}
00412   template<class View>
00413   forceinline
00414   ViewSelRnd<View>::ViewSelRnd(Space&, const VarBranchOptions& vbo)
00415     : r(vbo.seed), n(0) {}
00416   template<class View>
00417   forceinline ViewSelStatus
00418   ViewSelRnd<View>::init(Space&, View) {
00419     n=1;
00420     return VSS_BETTER;
00421   }
00422   template<class View>
00423   forceinline ViewSelStatus
00424   ViewSelRnd<View>::select(Space&, View) {
00425     n++;
00426     return (r(n) == (n-1)) ? VSS_BETTER : VSS_WORSE;
00427   }
00428   template<class View>
00429   forceinline typename ViewSelRnd<View>::Choice
00430   ViewSelRnd<View>::choice(Space&) {
00431     return r;
00432   }
00433   template<class View>
00434   forceinline typename ViewSelRnd<View>::Choice
00435   ViewSelRnd<View>::choice(const Space&, Archive& e) {
00436     return Choice(e.get());
00437   }
00438   template<class View>
00439   forceinline void
00440   ViewSelRnd<View>::commit(Space&, const Choice& c, unsigned int) {
00441     r = c;
00442   }
00443   template<class View>
00444   forceinline void
00445   ViewSelRnd<View>::update(Space&, bool, ViewSelRnd<View>& vs) {
00446     r = vs.r;
00447   }
00448   template<class View>
00449   forceinline void
00450   ViewSelRnd<View>::dispose(Space&) {
00451   }
00452 
00453 }
00454 
00455 // STATISTICS: kernel-branch