Generated on Tue Apr 18 10:22:10 2017 for Gecode by doxygen 1.6.3

pbs.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2015
00008  *
00009  *  Last modified:
00010  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
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 #include <climits>
00039 
00040 namespace Gecode { namespace Search { namespace Meta { namespace Sequential {
00041 
00042 
00043   forceinline
00044   PortfolioStop::PortfolioStop(Stop* so0)
00045     : so(so0) {}
00046   forceinline void
00047   PortfolioStop::share(SharedStopInfo* ssi0) {
00048     ssi = ssi0;
00049   }
00050 
00051 
00052   forceinline void
00053   Slave::init(Engine* e, Stop* s) {
00054     slave = e; stop = s;
00055   }
00056   forceinline Space*
00057   Slave::next(void) {
00058     return slave->next();
00059   }
00060   forceinline Statistics
00061   Slave::statistics(void) const {
00062     return slave->statistics();
00063   }
00064   forceinline bool
00065   Slave::stopped(void) const {
00066     return slave->stopped();
00067   }
00068   forceinline void
00069   Slave::constrain(const Space& b) {
00070     slave->constrain(b);
00071   }
00072   forceinline
00073   Slave::~Slave(void) {
00074     delete slave;
00075     delete stop;
00076   }
00077 
00078 
00079   template<bool best>
00080   forceinline
00081   PBS<best>::PBS(Engine** e, Stop** s, unsigned int n,
00082                  const Statistics& stat0,
00083                  const Search::Options& opt)
00084     : stat(stat0), slice(opt.slice),
00085       slaves(heap.alloc<Slave>(n)), n_slaves(n), cur(0),
00086       slave_stop(false) {
00087     ssi.done = false;
00088     ssi.l = opt.slice;
00089     for (unsigned int i=n; i--; ) {
00090       slaves[i].init(e[i],static_cast<PortfolioStop*>(s[i]));
00091       static_cast<PortfolioStop*>(s[i])->share(&ssi);
00092     }
00093   }
00094 
00095   template<bool best>
00096   Space*
00097   PBS<best>::next(void) {
00098     slave_stop = false;
00099     unsigned int n_exhausted = 0;
00100     while (n_slaves > 0) {
00101       if (Space* s = slaves[cur].next()) {
00102         // Constrain other slaves
00103         if (best) {
00104           for (unsigned int i=0; i<cur; i++)
00105             slaves[i].constrain(*s);
00106           for (unsigned int i=cur+1; i<n_slaves; i++)
00107             slaves[i].constrain(*s);
00108         }
00109         return s;
00110       }
00111       if (slaves[cur].stopped()) {
00112         if (ssi.done) {
00113           cur++; n_exhausted++;
00114         } else {
00115           slave_stop = true;
00116           return NULL;
00117         }
00118       } else {
00119         // This slave is done, kill it after saving the statistics
00120         stat += slaves[cur].statistics();
00121         slaves[cur].~Slave();
00122         slaves[cur] = slaves[--n_slaves];
00123         if (n_slaves == 1)
00124           // Disable stoping by seeting a high limit
00125           ssi.l = ULONG_MAX;
00126       }
00127       if (n_exhausted == n_slaves) {
00128         n_exhausted = 0;
00129         // Increment by one slice
00130         ssi.l += slice;
00131       }
00132       if (cur == n_slaves)
00133         cur = 0;
00134     }
00135     return NULL;
00136   }
00137 
00138   template<bool best>
00139   bool
00140   PBS<best>::stopped(void) const {
00141     return slave_stop;
00142   }
00143 
00144   template<bool best>
00145   Statistics
00146   PBS<best>::statistics(void) const {
00147     Statistics s(stat);
00148     for (unsigned int i=n_slaves; i--; )
00149       s += slaves[i].statistics();
00150     return s;
00151   }
00152 
00153   template<bool best>
00154   void
00155   PBS<best>::constrain(const Space& b) {
00156     if (!best)
00157       throw NoBest("PBS::constrain");
00158     for (unsigned int i=0; i<n_slaves; i++)
00159       slaves[i].constrain(b);
00160   }
00161 
00162   template<bool best>
00163   PBS<best>::~PBS(void) {
00164     for (unsigned int i=n_slaves; i--; )
00165       slaves[i].~Slave();
00166     // Note that n_slaves might be different now!
00167     heap.rfree(slaves);
00168   }
00169 
00170 }}}}
00171 
00172 // STATISTICS: search-meta