pbs.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2015
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <cmath>
#include <algorithm>
namespace Gecode { namespace Search {
template<class T, template<class> class E>
class PbsBuilder : public Builder {
using Builder::opt;
public:
PbsBuilder(const Options& opt);
virtual Engine* operator() (Space* s) const;
};
template<class T, template<class> class E>
inline
PbsBuilder<T,E>::PbsBuilder(const Options& opt)
: Builder(opt,E<T>::best) {}
template<class T, template<class> class E>
Engine*
PbsBuilder<T,E>::operator() (Space* s) const {
return build<T,PBS<T,E> >(s,opt);
}
}}
#include <gecode/search/seq/dead.hh>
namespace Gecode { namespace Search { namespace Seq {
GECODE_SEARCH_EXPORT Stop*
pbsstop(Stop* so);
GECODE_SEARCH_EXPORT Engine*
pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves,
const Statistics& stat, const Search::Options& opt, bool best);
}}}
namespace Gecode { namespace Search { namespace Par {
GECODE_SEARCH_EXPORT Stop*
pbsstop(Stop* so);
GECODE_SEARCH_EXPORT Engine*
pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves,
const Statistics& stat, bool best);
}}}
namespace Gecode { namespace Search {
template<class T, template<class> class E>
Engine*
pbsseq(T* master, const Search::Statistics& stat, Options& opt) {
Stop* stop = opt.stop;
Region r;
// In case there are more threads than assets requested
opt.threads = std::max(floor(opt.threads /
static_cast<double>(opt.assets)),1.0);
unsigned int n_slaves = opt.assets;
Engine** slaves = r.alloc<Engine*>(n_slaves);
Stop** stops = r.alloc<Stop*>(n_slaves);
WrapTraceRecorder::engine(opt.tracer,
SearchTracer::EngineType::PBS, n_slaves);
for (unsigned int i=0U; i<n_slaves; i++) {
opt.stop = stops[i] = Seq::pbsstop(stop);
Space* slave = (i == n_slaves-1) ?
master : master->clone();
(void) slave->slave(i);
slaves[i] = build<T,E>(slave,opt);
}
return Seq::pbsengine(slaves,stops,n_slaves,stat,opt,E<T>::best);
}
template<class T, template<class> class E>
Engine*
pbsseq(T* master, SEBs& sebs,
const Search::Statistics& stat, Options& opt, bool best) {
Region r;
int n_slaves = sebs.size();
Engine** slaves = r.alloc<Engine*>(n_slaves);
Stop** stops = r.alloc<Stop*>(n_slaves);
WrapTraceRecorder::engine(opt.tracer,
SearchTracer::EngineType::PBS, n_slaves);
for (int i=0; i<n_slaves; i++) {
// Re-configure slave options
stops[i] = Seq::pbsstop(sebs[i]->options().stop);
sebs[i]->options().stop = stops[i];
sebs[i]->options().clone = false;
Space* slave = (i == n_slaves-1) ?
master : master->clone();
(void) slave->slave(i);
slaves[i] = (*sebs[i])(slave);
delete sebs[i];
}
return Seq::pbsengine(slaves,stops,n_slaves,stat,opt,best);
}
#ifdef GECODE_HAS_THREADS
template<class T, template<class> class E>
Engine*
pbspar(T* master, const Search::Statistics& stat, Options& opt) {
Stop* stop = opt.stop;
Region r;
// Limit the number of slaves to the number of threads
unsigned int n_slaves = std::min(static_cast<unsigned int>(opt.threads),
opt.assets);
// Redistribute additional threads to slaves
opt.threads = floor(opt.threads / static_cast<double>(n_slaves));
WrapTraceRecorder::engine(opt.tracer,
SearchTracer::EngineType::PBS, n_slaves);
Engine** slaves = r.alloc<Engine*>(n_slaves);
Stop** stops = r.alloc<Stop*>(n_slaves);
for (unsigned int i=0U; i<n_slaves; i++) {
opt.stop = stops[i] = Par::pbsstop(stop);
Space* slave = (i == n_slaves-1) ?
master : master->clone();
(void) slave->slave(i);
slaves[i] = build<T,E>(slave,opt);
}
return Par::pbsengine(slaves,stops,n_slaves,stat,E<T>::best);
}
template<class T, template<class> class E>
Engine*
pbspar(T* master, SEBs& sebs,
const Search::Statistics& stat, Options& opt, bool best) {
Region r;
// Limit the number of slaves to the number of threads
int n_slaves = std::min(static_cast<int>(opt.threads),
sebs.size());
WrapTraceRecorder::engine(opt.tracer,
SearchTracer::EngineType::PBS, n_slaves);
Engine** slaves = r.alloc<Engine*>(n_slaves);
Stop** stops = r.alloc<Stop*>(n_slaves);
for (int i=0; i<n_slaves; i++) {
// Re-configure slave options
stops[i] = Par::pbsstop(sebs[i]->options().stop);
sebs[i]->options().stop = stops[i];
sebs[i]->options().clone = false;
Space* slave = (i == n_slaves-1) ?
master : master->clone();
(void) slave->slave(i);
slaves[i] = (*sebs[i])(slave);
delete sebs[i];
}
// Delete excess builders
for (int i=n_slaves; i<sebs.size(); i++)
delete sebs[i];
return Par::pbsengine(slaves,stops,n_slaves,stat,best);
}
#endif
}}
namespace Gecode {
template<class T, template<class> class E>
PBS<T,E>::PBS(T* s, const Search::Options& o) {
Search::Options opt(o.expand());
if (opt.assets == 0)
throw Search::NoAssets("PBS::PBS");
Search::Statistics stat;
if (s->status(stat) == SS_FAILED) {
stat.fail++;
if (!opt.clone)
delete s;
e = Search::Seq::dead(opt,stat);
return;
}
// Check whether a clone must be used
T* master = opt.clone ?
dynamic_cast<T*>(s->clone()) : s;
opt.clone = false;
// Always execute master function
(void) master->master(0);
// No need to create a portfolio engine but must run slave function
if (o.assets == 1) {
(void) master->slave(0);
e = Search::build<T,E>(master,opt);
return;
}
#ifdef GECODE_HAS_THREADS
if (opt.threads > 1.0)
e = Search::pbspar<T,E>(master,stat,opt);
else
#endif
e = Search::pbsseq<T,E>(master,stat,opt);
}
template<class T, template<class> class E>
void
PBS<T,E>::build(T* s, SEBs& sebs, const Search::Options& o) {
// Check whether all sebs do either best solution search or not
bool best;
{
int b = 0;
for (int i=0; i<sebs.size(); i++)
b += sebs[i]->best() ? 1 : 0;
if ((b > 0) && (b < sebs.size()))
throw Search::MixedBest("PBS::PBS");
best = (b == sebs.size());
}
Search::Options opt(o.expand());
Search::Statistics stat;
if (s->status(stat) == SS_FAILED) {
stat.fail++;
if (!opt.clone)
delete s;
e = Search::Seq::dead(opt,stat);
return;
}
// Check whether a clone must be used
T* master = opt.clone ?
dynamic_cast<T*>(s->clone()) : s;
opt.clone = false;
// Always execute master function
(void) master->master(0);
#ifdef GECODE_HAS_THREADS
if (opt.threads > 1.0)
e = Search::pbspar<T,E>(master,sebs,stat,opt,best);
else
#endif
e = Search::pbsseq<T,E>(master,sebs,stat,opt,best);
}
template<class T, template<class> class E>
inline
PBS<T,E>::PBS(T* s, SEBs& sebs, const Search::Options& o) {
build(s,sebs,o);
}
template<class T, template<class> class E>
inline T*
pbs(T* s, const Search::Options& o) {
PBS<T,E> r(s,o);
return r.next();
}
template<class T, template<class> class E>
inline SEB
pbs(const Search::Options& o) {
return new Search::PbsBuilder<T,E>(o);
}
}
// STATISTICS: search-other
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::Options::assets
void assets(unsigned int n)
Set default number of assets in a portfolio.
Definition options.hpp:355
Gecode::Options::threads
void threads(double n)
Set number of parallel threads.
Definition options.hpp:292
Gecode::PBS
Meta engine using a portfolio of search engines.
Definition search.hh:1236
Gecode::PBS::build
void build(T *s, SEBs &sebs, const Search::Options &o)
The actual build function.
Definition pbs.hpp:262
Gecode::PBS::PBS
PBS(T *s, const Search::Options &o=Search::Options::def)
Initialize with engines running copies of s with options o.
Definition pbs.hpp:221
Gecode::PBS::best
static const bool best
Whether engine does best solution search.
Definition search.hh:1247
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::SEBs
Passing search engine builder arguments.
Definition search.hh:1002
Gecode::SearchTracer::PBS
@ PBS
Engine is a PBS engine.
Definition search.hh:198
Gecode::Search::Base::e
Engine * e
The actual search engine.
Definition search.hh:932
Gecode::Search::Builder::opt
Options opt
Stored and already expanded options.
Definition search.hh:968
Gecode::Search::Builder::Builder
Builder(const Options &opt, bool best)
Initialize with options opt and best solution search support.
Definition build.hpp:37
Gecode::Search::Builder::best
bool best(void) const
Whether engine is a best solution search engine.
Definition build.hpp:48
Gecode::Search::Engine
Search engine implementation interface
Definition search.hh:899
Gecode::Search::MixedBest
Exception: Mixed non-best and best solution search requested
Definition exception.hpp:54
Gecode::Search::NoAssets
Exception: No assets requested for portfolio-based search
Definition exception.hpp:48
Gecode::Search::Options
Search engine options
Definition search.hh:746
Gecode::Search::Options::expand
Options expand(void) const
Expand with real number of threads.
Definition options.cpp:43
Gecode::Search::Options::stop
Stop * stop
Stop object for stopping search.
Definition search.hh:765
Gecode::Search::Options::tracer
SearchTracer * tracer
Tracer object for tracing search.
Definition search.hh:769
Gecode::Search::Options::assets
unsigned int assets
Number of assets (engines) in a portfolio.
Definition search.hh:759
Gecode::Search::Options::threads
double threads
Number of threads to use.
Definition search.hh:751
Gecode::Search::Par::Engine
Parallel depth-first search engine
Definition engine.hh:46
Gecode::Search::PbsBuilder
A PBS engine builder.
Definition pbs.hpp:41
Gecode::Search::PbsBuilder::PbsBuilder
PbsBuilder(const Options &opt)
The constructor.
Definition pbs.hpp:52
Gecode::Search::PbsBuilder::operator()
virtual Engine * operator()(Space *s) const
The actual build function.
Definition pbs.hpp:57
Gecode::Search::Statistics
Search engine statistics
Definition search.hh:147
Gecode::Search::Statistics::fail
unsigned long int fail
Number of failed nodes in search tree.
Definition search.hh:150
Gecode::Search::Stop
Base-class for Stop-object.
Definition search.hh:799
Gecode::Search::WrapTraceRecorder::engine
static void engine(SearchTracer *tracer, SearchTracer::EngineType t, unsigned int n)
Register engine.
Definition trace-recorder.hpp:152
Gecode::Space
Computation spaces.
Definition core.hpp:1742
dead.hh
Gecode::Space::slave
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
Definition core.cpp:863
Gecode::pbs
T * pbs(T *s, const Search::Options &o=Search::Options::def)
Run a portfolio of search engines.
Definition pbs.hpp:309
Gecode::SS_FAILED
@ SS_FAILED
Space is failed
Definition core.hpp:1682
Gecode::Search::Par
Definition bab.hh:39
Gecode::Search::Par::pbsengine
Engine * pbsengine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, bool best)
Create parallel portfolio engine.
Definition pbs.cpp:66
Gecode::Search::Par::pbsstop
Stop * pbsstop(Stop *so)
Create stop object.
Definition pbs.cpp:61
Gecode::Search::Seq::dead
Engine * dead(const Options &o, const Statistics &stat)
Definition dead.cpp:90
Gecode::Search::Seq::pbsstop
Stop * pbsstop(Stop *so)
Create stop object.
Definition pbs.cpp:39
Gecode::Search::Seq::pbsengine
Engine * pbsengine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, const Search::Options &opt, bool best)
Create sequential portfolio engine.
Definition pbs.cpp:44
Gecode::Search
Search engines
Gecode::Search::build
Engine * build(Space *s, const Options &opt)
Build an engine of type E for a script T.
Definition build.hpp:58
Gecode::Search::pbspar
Engine * pbspar(T *master, const Search::Statistics &stat, Options &opt)
Definition pbs.hpp:153
Gecode::Search::pbsseq
Engine * pbsseq(T *master, const Search::Statistics &stat, Options &opt)
Definition pbs.hpp:95
Gecode
Gecode toplevel namespace
Gecode::SEB
Search::Builder * SEB
Type for a search engine builder.
Definition search.hh:993
GECODE_SEARCH_EXPORT
#define GECODE_SEARCH_EXPORT
Definition search.hh:67