diff --git a/changelog.in b/changelog.in index 9d2da9edb8..28b4a63e20 100755 --- a/changelog.in +++ b/changelog.in @@ -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 diff --git a/gecode/kernel/core.hpp b/gecode/kernel/core.hpp index 08423430ee..e365b91d10 100755 --- a/gecode/kernel/core.hpp +++ b/gecode/kernel/core.hpp @@ -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 @@ -2180,8 +2185,9 @@ 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 @@ -2189,6 +2195,11 @@ namespace Gecode { * 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, diff --git a/gecode/search/rbs.cpp b/gecode/search/rbs.cpp index 5eaaae30f0..2069e577d0 100644 --- a/gecode/search/rbs.cpp +++ b/gecode/search/rbs.cpp @@ -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(stop), slave, - stat,opt,best); + stat,opt,best,complete); } diff --git a/gecode/search/rbs.hpp b/gecode/search/rbs.hpp index 846d8dbb77..eb925aef9f 100644 --- a/gecode/search/rbs.hpp +++ b/gecode/search/rbs.hpp @@ -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 { @@ -103,11 +115,11 @@ namespace Gecode { std::unique_ptr master(m_opt.clone ? s->clone() : s); std::unique_ptr 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 leaf( Search::build(slave.get(),e_opt)); e = Search::Seq::rbsengine(master.get(),stop.get(),leaf.get(), - stat,m_opt,E::best); + stat,m_opt,E::best,complete); master.release(); stop.release(); leaf.release(); diff --git a/gecode/search/seq/rbs.cpp b/gecode/search/seq/rbs.cpp index 033c525b1d..c4edfc4106 100755 --- a/gecode/search/seq/rbs.cpp +++ b/gecode/search/seq/rbs.cpp @@ -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()); @@ -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(); @@ -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()); @@ -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; diff --git a/gecode/search/seq/rbs.hh b/gecode/search/seq/rbs.hh index 6b506200de..744e60b4a9 100755 --- a/gecode/search/seq/rbs.hh +++ b/gecode/search/seq/rbs.hh @@ -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 diff --git a/gecode/search/seq/rbs.hpp b/gecode/search/seq/rbs.hpp index c55e4e28b5..8c0081de38 100755 --- a/gecode/search/seq/rbs.hpp +++ b/gecode/search/seq/rbs.hpp @@ -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)()); } diff --git a/test/search.cpp b/test/search.cpp index cb00a26472..411b5a6dfa 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -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 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: