Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ Date: unreleased
[DESCRIPTION]
This is the development changelog for the next Gecode release.

[ENTRY]
Module: search
What: bug
Rank: major
[DESCRIPTION]
Do not post no-goods from an incomplete slave search in the master of
restart-based search. When the slave configuration function returned
false, for example in large neighbourhood search, the no-goods recorded in
the restricted search space were posted in the master. Search could then
miss better solutions and report a solution as optimal when it was not.

[ENTRY]
Module: search
What: bug
Expand Down
17 changes: 14 additions & 3 deletions gecode/kernel/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,12 @@ namespace Gecode {
unsigned long long int fail(void) const;
/// Return last solution found (possibly nullptr)
const Space* last(void) const;
/// Return no-goods recorded from restart
/**
* \brief Return no-goods recorded from restart
*
* The no-goods are empty if the search on the slave space was
* incomplete, as they only hold in the slave's search space.
*/
const NoGoods& nogoods(void) const;
//@}
/// \name Portfolio-based information
Expand Down Expand Up @@ -2180,15 +2185,21 @@ namespace Gecode {
* portfolio meta search engines.
*
* \li A restart meta search engine calls this function on its
* slave space whenever it finds a solution or exploration
* restarts. \a mi contains information about the current restart.
* slave space when search starts and whenever it finds a solution or
* exploration restarts. \a mi contains information about the
* current restart.
*
* If the function returns true, the search on the slave space is
* considered complete, i.e., if it fails or exhaustively explores the
* entire search space, the meta search engine finishes. If the
* function returns false, the search on the slave space is considered
* incomplete, and the meta engine will restart the search regardless
* of whether the search on the slave space finishes or times out.
* The no-goods recorded by an incomplete search only hold in the
* slave's search space, so the meta engine does not pass them to the
* master configuration function. When search starts, the result only
* determines whether no-goods are passed: the meta engine treats the
* first search on a slave space as complete.
*
* \li A portfolio meta engine calls this function once on each asset
* (that is, on each slave) and passes the number of the asset,
Expand Down
9 changes: 8 additions & 1 deletion gecode/search/rbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ namespace Gecode { namespace Search { namespace Seq {
Engine*
rbsengine(Space* master, Stop* stop, Engine* slave,
const Search::Statistics& stat, const Options& opt, bool best) {
return rbsengine(master,stop,slave,stat,opt,best,true);
}

Engine*
rbsengine(Space* master, Stop* stop, Engine* slave,
const Search::Statistics& stat, const Options& opt, bool best,
bool complete) {
return new RBS(master,static_cast<RestartStop*>(stop), slave,
stat,opt,best);
stat,opt,best,complete);
}


Expand Down
18 changes: 15 additions & 3 deletions gecode/search/rbs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,24 @@ namespace Gecode { namespace Search { namespace Seq {
GECODE_SEARCH_EXPORT Stop*
rbsstop(Stop* so);

/// Create restart engine
/// Create restart engine whose first slave search yields valid no-goods
GECODE_SEARCH_EXPORT Engine*
rbsengine(Space* master, Stop* stop, Engine* slave,
const Search::Statistics& stat, const Options& opt,
bool best);

/**
* \brief Create restart engine
*
* The value \a complete is the result of the slave configuration of the
* space explored by the first engine \a slave. It only determines whether
* the no-goods of that search hold in the master.
*/
GECODE_SEARCH_EXPORT Engine*
rbsengine(Space* master, Stop* stop, Engine* slave,
const Search::Statistics& stat, const Options& opt,
bool best, bool complete);

}}}

namespace Gecode { namespace Search {
Expand Down Expand Up @@ -103,11 +115,11 @@ namespace Gecode {
std::unique_ptr<Space> master(m_opt.clone ? s->clone() : s);
std::unique_ptr<Space> slave(master->clone());
MetaInfo mi(0,MetaInfo::RR_INIT,0,0,nullptr,NoGoods::eng);
slave->slave(mi);
bool complete = slave->slave(mi);
std::unique_ptr<Search::Engine> leaf(
Search::build<T,E>(slave.get(),e_opt));
e = Search::Seq::rbsengine(master.get(),stop.get(),leaf.get(),
stat,m_opt,E<T>::best);
stat,m_opt,E<T>::best,complete);
master.release();
stop.release();
leaf.release();
Expand Down
21 changes: 16 additions & 5 deletions gecode/search/seq/rbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@ namespace Gecode { namespace Search { namespace Seq {
return false;
}

NoGoods&
RBS::master_nogoods(void) {
// The no-goods of an incomplete slave search only hold in its restricted
// search space, not in the master.
if (!nogoods_valid)
return NoGoods::eng;
NoGoods& ng = e->nogoods();
// Reset number of no-goods found
ng.ng(0);
return ng;
}

Space*
RBS::next(void) {
if (restart) {
restart = false;
sslr++;
NoGoods& ng = e->nogoods();
// Reset number of no-goods found
ng.ng(0);
NoGoods& ng = master_nogoods();
MetaInfo mi(stop->restarts(),MetaInfo::RR_SOL,sslr,e->statistics().fail,last,ng);
bool r = master->master(mi);
stop->nogood(ng.ng());
Expand All @@ -80,6 +90,7 @@ namespace Gecode { namespace Search { namespace Seq {
Space* slave = master;
master = master->clone();
complete = slave->slave(mi);
nogoods_valid = complete;
e->reset(slave);
sslr = 0;
stop->restart();
Expand All @@ -100,8 +111,7 @@ namespace Gecode { namespace Search { namespace Seq {
if (!complete && !e->stopped())
stop->restart();
sslr = 0;
NoGoods& ng = e->nogoods();
ng.ng(0);
NoGoods& ng = master_nogoods();
MetaInfo mi(stop->restarts(),e->stopped() ? MetaInfo::RR_LIM : MetaInfo::RR_CMPL,sslr,e->statistics().fail,last,ng);
(void) master->master(mi);
stop->nogood(ng.ng());
Expand All @@ -112,6 +122,7 @@ namespace Gecode { namespace Search { namespace Seq {
Space* slave = master;
master = master->clone();
complete = slave->slave(mi);
nogoods_valid = complete;
e->reset(slave);
} else {
return nullptr;
Expand Down
16 changes: 14 additions & 2 deletions gecode/search/seq/rbs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ namespace Gecode { namespace Search { namespace Seq {
unsigned long int sslr;
/// Whether search for the next solution will be complete
bool complete;
/// Whether the no-goods of the current slave search hold in the master
bool nogoods_valid;
/// Whether a restart must be performed when next is called
bool restart;
/// Whether the engine performs best solution search
bool best;
/// Return the no-goods of the last slave search, valid in the master
NoGoods& master_nogoods(void);
public:
/// Constructor
/**
* \brief Constructor
*
* The value \a complete is the result of the slave configuration of the
* space explored by the first engine \a e0. It only determines whether
* the no-goods of that search hold in the master: the meta engine treats
* the first search as complete.
*/
RBS(Space* s, RestartStop* stop0, Engine* e0,
const Search::Statistics& stat, const Options& o, bool best);
const Search::Statistics& stat, const Options& o, bool best,
bool complete);
/// Return next solution (nullptr, if none exists or search has been stopped)
virtual Space* next(void);
/// Return statistics
Expand Down
5 changes: 3 additions & 2 deletions gecode/search/seq/rbs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ namespace Gecode { namespace Search { namespace Seq {
forceinline
RBS::RBS(Space* s, RestartStop* stop0,
Engine* e0, const Search::Statistics& stat, const Options& opt,
bool best0)
bool best0, bool complete0)
: e(e0), master(s), last(nullptr), co(opt.cutoff), stop(stop0),
sslr(0),
complete(true), restart(false), best(best0) {
complete(true), nogoods_valid(complete0), restart(false),
best(best0) {
stop->limit(stat,(*co)());
}

Expand Down
53 changes: 53 additions & 0 deletions test/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,59 @@ namespace Test {
}
};

/// Space whose first slave search is restricted to x2, x3 in {0,1}
class RestrictedFirstSlave : public Gecode::IntMinimizeSpace {
public:
/// Distinct variables, the cost is x0
Gecode::IntVarArray x;
RestrictedFirstSlave(void) : x(*this,4,0,3) {
Gecode::distinct(*this,x);
Gecode::branch(*this,x,Gecode::INT_VAR_NONE(),Gecode::INT_VAL_MIN());
}
RestrictedFirstSlave(RestrictedFirstSlave& s)
: Gecode::IntMinimizeSpace(s) {
x.update(*this,s.x);
}
Gecode::Space* copy(void) override {
return new RestrictedFirstSlave(*this);
}
Gecode::IntVar cost(void) const override {
return x[0];
}
bool slave(const Gecode::MetaInfo& mi) override {
if ((mi.type() == Gecode::MetaInfo::RESTART) &&
(mi.reason() == Gecode::MetaInfo::RR_INIT)) {
Gecode::dom(*this,x[2],0,1);
Gecode::dom(*this,x[3],0,1);
return false;
}
return true;
}
};

/// No-goods from an incomplete slave search are not posted in the master
class RBSIncompleteNoGoods : public Base {
public:
RBSIncompleteNoGoods(void) : Base("Search::RBS::IncompleteNoGoods") {}
bool run(void) override {
RestrictedFirstSlave* m = new RestrictedFirstSlave;
Gecode::Search::Options o;
o.nogoods_limit = 128U;
o.cutoff = Gecode::Search::Cutoff::constant(100);
Gecode::RBS<RestrictedFirstSlave,Gecode::BAB> rbs(m,o);
delete m;
// The restricted search finds x0 = 2 and rules out x0 in {0,1}
int best = -1;
while (RestrictedFirstSlave* s = rbs.next()) {
best = s->x[0].val();
delete s;
}
return !rbs.stopped() && (best == 0);
}
};

RBSIncompleteNoGoods rbs_incomplete_nogoods;

/// Space for relaxing
class RelaxSpace : public Gecode::Space {
public:
Expand Down
Loading