lds.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, 2004, 2016
*
* 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.
*
*/
namespace Gecode { namespace Search { namespace Seq {
/*
* Nodes for the probing engine (just remember next alternative
* to try)
*
*/
template<class Tracer>
forceinline
Probe<Tracer>::Node::Node(void) {}
template<class Tracer>
forceinline
Probe<Tracer>::Node::Node(Space* s, const Choice* c, unsigned int a,
unsigned int nid)
: _space(s), _choice(c), _alt(a), _nid(nid) {}
template<class Tracer>
forceinline Space*
Probe<Tracer>::Node::space(void) const {
return _space;
}
template<class Tracer>
forceinline const Choice*
Probe<Tracer>::Node::choice(void) const {
return _choice;
}
template<class Tracer>
forceinline unsigned int
Probe<Tracer>::Node::alt(void) const {
return _alt;
}
template<class Tracer>
forceinline unsigned int
Probe<Tracer>::Node::nid(void) const {
return _nid;
}
template<class Tracer>
forceinline void
Probe<Tracer>::Node::next(void) {
_alt--;
}
/*
* The probing engine: computes all solutions with
* exact number of discrepancies (solutions with
* fewer discrepancies are discarded)
*
*/
template<class Tracer>
forceinline
Probe<Tracer>::Probe(const Options& opt)
: tracer(opt.tracer), ds(heap) {
tracer.engine(SearchTracer::EngineType::LDS, 1U);
tracer.worker();
}
template<class Tracer>
forceinline void
Probe<Tracer>::init(Space* s) {
cur = s; d = 0U; exhausted = true;
if (tracer)
tracer.ei()->invalidate();
}
template<class Tracer>
forceinline void
Probe<Tracer>::reset(Space* s, unsigned int d0) {
tracer.round();
delete cur;
while (!ds.empty())
delete ds.pop().space();
cur = s; d = d0; exhausted = true;
if (tracer)
tracer.ei()->invalidate();
Worker::reset(0);
}
template<class Tracer>
forceinline Statistics
Probe<Tracer>::statistics(void) const {
return *this;
}
template<class Tracer>
forceinline bool
Probe<Tracer>::done(void) const {
return exhausted;
}
template<class Tracer>
forceinline
Probe<Tracer>::~Probe(void) {
tracer.done();
delete cur;
while (!ds.empty())
delete ds.pop().space();
}
template<class Tracer>
forceinline Space*
Probe<Tracer>::next(const Options& opt) {
start();
while (true) {
if (cur == NULL) {
backtrack:
if (ds.empty())
return NULL;
if (stop(opt))
return NULL;
unsigned int a = ds.top().alt();
const Choice* ch = ds.top().choice();
unsigned int nid = ds.top().nid();
if (a == 0) {
cur = ds.pop().space();
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
cur->commit(*ch,0);
delete ch;
} else {
ds.top().next();
cur = ds.top().space()->clone();
if (tracer)
tracer.ei()->init(tracer.wid(), nid, a, *cur, *ch);
cur->commit(*ch,a);
}
node++;
d++;
}
check_discrepancy:
if (d == 0) {
Space* s = cur;
while (s->status(*this) == SS_BRANCH) {
if (stop(opt)) {
cur = s;
return NULL;
}
const Choice* ch = s->choice();
if (ch->alternatives() > 1)
exhausted = false;
if (tracer) {
unsigned int nid = tracer.nid();
SearchTracer::NodeInfo ni(SearchTracer::NodeType::BRANCH,
tracer.wid(), nid, *s, ch);
tracer.node(*tracer.ei(),ni);
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
}
s->commit(*ch,0);
node++;
delete ch;
}
cur = NULL;
if (s->failed()) {
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::FAILED,
tracer.wid(), tracer.nid(), *s);
tracer.node(*tracer.ei(),ni);
}
fail++;
delete s;
goto backtrack;
} else {
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::SOLVED,
tracer.wid(), tracer.nid(), *s);
tracer.node(*tracer.ei(),ni);
}
// Deletes all pending branchings
(void) s->choice();
return s;
}
} else {
node++;
switch (cur->status(*this)) {
case SS_FAILED:
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::FAILED,
tracer.wid(), tracer.nid(), *cur);
tracer.node(*tracer.ei(),ni);
}
fail++;
delete cur;
cur = NULL;
goto backtrack;
case SS_SOLVED:
if (tracer) {
tracer.skip(*tracer.ei());
}
delete cur;
cur = NULL;
goto backtrack;
case SS_BRANCH:
{
const Choice* ch = cur->choice();
unsigned int alt = ch->alternatives();
unsigned int nid = tracer.nid();
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::BRANCH,
tracer.wid(), nid, *cur, ch);
tracer.node(*tracer.ei(),ni);
}
if (alt > 1) {
if (d < alt-1)
exhausted = false;
unsigned int d_a = (d >= alt-1) ? alt-1 : d;
Space* cc = cur->clone();
Node sn(cc,ch,d_a-1,nid);
ds.push(sn);
stack_depth(static_cast<unsigned long int>(ds.entries()));
if (tracer)
tracer.ei()->init(tracer.wid(), nid, d_a, *cur, *ch);
cur->commit(*ch,d_a);
d -= d_a;
} else {
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
cur->commit(*ch,0);
node++;
delete ch;
}
goto check_discrepancy;
}
default: GECODE_NEVER;
}
}
}
}
template<class Tracer>
forceinline
LDS<Tracer>::LDS(Space* s, const Options& o)
: opt(o), e(opt), root(NULL), d(0) {
e.node = 1;
if (s->status(e) == SS_FAILED) {
e.fail++;
e.init(NULL);
} else {
Space* c = snapshot(s,opt);
if (opt.d_l > 0) {
root = c->clone();
}
e.init(c);
}
}
template<class Tracer>
Space*
LDS<Tracer>::next(void) {
while (true) {
Space* s = e.next(opt);
if (s != NULL)
return s;
if (((s == NULL) && e.stopped()) || (++d > opt.d_l) || e.done())
break;
if (d == opt.d_l) {
if (root != NULL)
e.reset(root,d);
root = NULL;
} else if (root != NULL) {
e.reset(root->clone(),d);
}
}
return NULL;
}
template<class Tracer>
bool
LDS<Tracer>::stopped(void) const {
return e.stopped();
}
template<class Tracer>
Statistics
LDS<Tracer>::statistics(void) const {
return e.statistics();
}
template<class Tracer>
forceinline void
LDS<Tracer>::reset(Space* s) {
delete root; root=NULL; d=0;
e.node = 1;
if ((s == NULL) || (s->status(e) == SS_FAILED)) {
delete s;
e.fail++;
e.reset(NULL,0);
} else {
if (opt.d_l > 0) {
root = s->clone();
}
e.reset(s,0);
}
}
template<class Tracer>
forceinline void
LDS<Tracer>::constrain(const Space& b) {
(void) b;
assert(false);
}
template<class Tracer>
LDS<Tracer>::~LDS(void) {
delete root;
}
}}}
// STATISTICS: search-seq
Gecode::Choice
Choice for performing commit
Definition core.hpp:1412
Gecode::Choice::alternatives
unsigned int alternatives(void) const
Return number of alternatives.
Definition core.hpp:3770
Gecode::SearchTracer::NodeInfo
Node information.
Definition search.hh:282
Gecode::SearchTracer::FAILED
@ FAILED
A solution node.
Definition search.hh:278
Gecode::SearchTracer::BRANCH
@ BRANCH
A failed node.
Definition search.hh:279
Gecode::SearchTracer::SOLVED
@ SOLVED
Definition search.hh:277
Gecode::SearchTracer::LDS
@ LDS
Engine is a LDS engine.
Definition search.hh:196
Gecode::Search::Base::stopped
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition base.hpp:56
Gecode::Search::Base::next
virtual T * next(void)
Return next solution (NULL, if none exists or search has been stopped).
Definition base.hpp:46
Gecode::Search::Base::statistics
virtual Statistics statistics(void) const
Return statistics.
Definition base.hpp:51
Gecode::Search::Options
Search engine options
Definition search.hh:746
Gecode::Search::Seq::LDS::opt
Options opt
Search options.
Definition lds.hh:108
Gecode::Search::Seq::LDS::reset
void reset(Space *s)
Reset engine to restart at space s.
Definition lds.hpp:322
Gecode::Search::Seq::LDS::d
unsigned int d
Current discrepancy.
Definition lds.hh:114
Gecode::Search::Seq::LDS::constrain
void constrain(const Space &b)
Constrain future solutions to be better than b (should never be called).
Definition lds.hpp:339
Gecode::Search::Seq::LDS::root
Space * root
Root node for problem.
Definition lds.hh:112
Gecode::Search::Seq::LDS::e
Probe< Tracer > e
The probe engine.
Definition lds.hh:110
Gecode::Search::Seq::LDS::~LDS
virtual ~LDS(void)
Destructor.
Definition lds.hpp:345
Gecode::Search::Seq::LDS::LDS
LDS(Space *s, const Options &o)
Initialize for space s with options o.
Definition lds.hpp:272
Gecode::Search::Seq::Probe::Node
Node in the search tree for LDS
Definition lds.hh:50
Gecode::Search::Seq::Probe::Node::Node
Node(void)
Default constructor.
Definition lds.hpp:44
Gecode::Search::Seq::Probe::Node::nid
unsigned int nid(void) const
Return node identifier.
Definition lds.hpp:72
Gecode::Search::Seq::Probe::Node::next
void next(void)
Set next alternative
Definition lds.hpp:78
Gecode::Search::Seq::Probe::Node::choice
const Choice * choice(void) const
Return choice.
Definition lds.hpp:60
Gecode::Search::Seq::Probe::Node::alt
unsigned int alt(void) const
Return next alternative.
Definition lds.hpp:66
Gecode::Search::Seq::Probe::Node::space
Space * space(void) const
Return space.
Definition lds.hpp:54
Gecode::Search::Seq::Probe< Gecode::Tracer >::d
unsigned int d
Definition lds.hh:83
Gecode::Search::Seq::Probe::tracer
Tracer tracer
Search tracer.
Definition lds.hh:77
Gecode::Search::Seq::Probe::init
void init(Space *s)
Initialize with space s.
Definition lds.hpp:100
Gecode::Search::Seq::Probe< Gecode::Tracer >::cur
Space * cur
Definition lds.hh:81
Gecode::Search::Seq::Probe::done
bool done(void) const
Test whether probing is done.
Definition lds.hpp:127
Gecode::Search::Seq::Probe::~Probe
~Probe(void)
Destructor.
Definition lds.hpp:133
Gecode::Search::Seq::Probe::ds
Support::DynamicStack< Node, Heap > ds
Stack storing current path in search tree
Definition lds.hh:79
Gecode::Search::Seq::Probe::Probe
Probe(const Options &opt)
Initialize.
Definition lds.hpp:92
Gecode::Search::Seq::Probe::next
Space * next(const Options &o)
Search for next solution
Definition lds.hpp:142
Gecode::Search::Seq::Probe::statistics
Statistics statistics(void) const
Return statistics.
Definition lds.hpp:121
Gecode::Search::Seq::Probe< Gecode::Tracer >::exhausted
bool exhausted
Definition lds.hh:85
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::Statistics::node
unsigned long int node
Number of nodes expanded.
Definition search.hh:152
Gecode::Search::Worker::start
void start(void)
Reset stop information.
Definition worker.hh:74
Gecode::Search::Worker::stop
bool stop(const Options &o)
Check whether engine must be stopped.
Definition worker.hh:79
Gecode::Search::Worker::stack_depth
void stack_depth(unsigned long int d)
Record stack depth d.
Definition worker.hh:100
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::StatusStatistics::reset
void reset(void)
Reset information.
Definition core.hpp:4690
Gecode::heap
Heap heap
The single global heap.
Definition heap.cpp:44
Gecode::Space::failed
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4044
Gecode::Space::choice
const Choice * choice(void)
Create new choice for current brancher.
Definition core.cpp:537
Gecode::Space::commit
void commit(const Choice &c, unsigned int a, CommitStatistics &stat=unused_commit)
Commit choice c for alternative a.
Definition core.hpp:3232
Gecode::Space::status
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition core.cpp:252
Gecode::Space::clone
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition core.hpp:3224
Gecode::SS_BRANCH
@ SS_BRANCH
Space must be branched (at least one brancher left)
Definition core.hpp:1684
Gecode::SS_SOLVED
@ SS_SOLVED
Space is solved (no brancher left)
Definition core.hpp:1683
Gecode::SS_FAILED
@ SS_FAILED
Space is failed
Definition core.hpp:1682
Gecode::Search::Seq
Definition pbs.cpp:36
Gecode::Search
Search engines
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56