diff --git a/changelog.in b/changelog.in index 1c416a0cc8..32c886749f 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,71 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[ENTRY] +Module: search +What: documentation +Rank: minor +[DESCRIPTION] +Document that LDS does not report reaching its discrepancy limit as a stop. +[MORE] +Finding no solution proves that none exists only if the discrepancy limit +covers every path of the search tree. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Fix a crash when deleting a portfolio of restart-based engines. +[MORE] +Every asset took ownership of the same cutoff and deleted it. The assets now +share one cutoff sequence, which the portfolio deletes once. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Honour the stop object of a portfolio built from engine builders. +[MORE] +The stop object is checked with the statistics of each asset, so a node or +failure limit applies to each asset separately. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Do not report DFS and BAB as stopped when the search tree is exhausted at +the same failure that reaches a limit. +[MORE] +Restart-based search with no-goods then restarted without new no-goods, and +could repeat the same search forever on unsatisfiable models. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Count a failed root as a failure when a search engine is reset. +[MORE] +Restart-based search resets its engine at every restart, so failure limits +did not advance when the slave space failed right away, as it can in large +neighbourhood search. Restart-based search now also checks its stop object +before restarting after an incomplete slave search, so a parallel engine +cannot restart forever. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Do not report restart-based search as stopped when the master fails at a +restart. +[MORE] +Failing the master, for example by posting no-goods, proves that no further +solution exists. FlatZinc reported an unknown result instead. + [ENTRY] Module: search What: bug diff --git a/gecode/search.hh b/gecode/search.hh index 11b40fc458..fffbd436c5 100755 --- a/gecode/search.hh +++ b/gecode/search.hh @@ -863,7 +863,7 @@ namespace Gecode { namespace Search { unsigned int c_d; /// Create a clone during recomputation if distance is greater than \a a_d (adaptive distance) unsigned int a_d; - /// Discrepancy limit (for LDS) + /// Discrepancy limit (for LDS, reaching it does not stop the engine) unsigned int d_l; /// Number of assets (engines) in a portfolio unsigned int assets; @@ -1405,6 +1405,18 @@ namespace Gecode { /** * \brief Limited discrepancy search engine + * + * The engine explores the paths of the search tree with at most + * Search::Options::d_l discrepancies, allowing more discrepancies in each + * iteration, starting from none. + * + * When next() returns nullptr and stopped() returns false, either the + * search tree has been explored completely, or the discrepancy limit has + * been reached while parts of the tree are still unexplored. The engine + * does not report which: a search that finds no (further) solution only + * proves that none exists if no path of the tree has more discrepancies + * than the limit. + * * \ingroup TaskModelSearch */ template @@ -1418,6 +1430,10 @@ namespace Gecode { /** * \brief Invoke limited-discrepancy search for \a s as root node and options\a o + * + * Returning nullptr does not prove that there is no solution when the + * discrepancy limit has been reached, see LDS. + * * \ingroup TaskModelSearch */ template @@ -1529,6 +1545,10 @@ namespace Gecode { * builders, each with its own control and immutable worker capacity. * PBS does not allocate workers or adjust those controls. * + * When the assets are restart-based engines, the portfolio takes + * ownership of the cutoff in \a o and the assets share its sequence: + * each restart of an asset takes the next value of the sequence. + * * The class \a T can implement member functions * \code virtual bool master(const MetaInfo& mi) \endcode * and @@ -1551,6 +1571,9 @@ namespace Gecode { /** * Initialize with engine builders \a sebs * + * The stop object of \a o is checked for each builder's engine with the + * statistics of that engine, as is the stop object of the builder. + * * The outer options must not contain a worker control. Each builder can * instead supply a distinct control for its underlying engine. * In parallel PBS, the outer thread count limits how many builders run, diff --git a/gecode/search/par/bab.hpp b/gecode/search/par/bab.hpp index deb0ae5f9b..e7cf023355 100755 --- a/gecode/search/par/bab.hpp +++ b/gecode/search/par/bab.hpp @@ -62,13 +62,17 @@ namespace Gecode { namespace Search { namespace Par { d = 0; mark = 0; idle = false; - if ((s == nullptr) || (s->status(*this) == SS_FAILED)) { + // Reset the statistics first, so that a failed root is counted + Search::Worker::reset(); + if (s == nullptr) { + cur = nullptr; + } else if (s->status(*this) == SS_FAILED) { + fail++; delete s; cur = nullptr; } else { cur = s; } - Search::Worker::reset(); } template diff --git a/gecode/search/par/dfs.hpp b/gecode/search/par/dfs.hpp index c8f36e10cb..12a94b1cb8 100755 --- a/gecode/search/par/dfs.hpp +++ b/gecode/search/par/dfs.hpp @@ -89,13 +89,17 @@ namespace Gecode { namespace Search { namespace Par { path.reset((s != nullptr) ? ngdl : 0); d = 0; idle = false; - if ((s == nullptr) || (s->status(*this) == SS_FAILED)) { + // Reset the statistics first, so that a failed root is counted + Search::Worker::reset(); + if (s == nullptr) { + cur = nullptr; + } else if (s->status(*this) == SS_FAILED) { + fail++; delete s; cur = nullptr; } else { cur = s; } - Search::Worker::reset(); } template diff --git a/gecode/search/par/pbs.cpp b/gecode/search/par/pbs.cpp index 6d3fc8a5a8..2c8e06f0a8 100644 --- a/gecode/search/par/pbs.cpp +++ b/gecode/search/par/pbs.cpp @@ -43,7 +43,8 @@ namespace Gecode { namespace Search { namespace Par { bool PortfolioStop::stop(const Statistics& s, const Options& o) { return tostop->load(std::memory_order_acquire) || - ((so != nullptr) && so->stop(s,o)); + ((so != nullptr) && so->stop(s,o)) || + ((po != nullptr) && po->stop(s,o)); } void diff --git a/gecode/search/par/pbs.hh b/gecode/search/par/pbs.hh index 40d93e7252..9325ff8271 100644 --- a/gecode/search/par/pbs.hh +++ b/gecode/search/par/pbs.hh @@ -48,13 +48,15 @@ namespace Gecode { namespace Search { namespace Par { private: /// The stop object for the slaves Stop* so; + /// The stop object for the portfolio (possibly nullptr) + Stop* po; /// Worker control for waking a paused asset WorkerControl worker_control; /// Whether search must be stopped std::atomic* tostop; public: - /// Initialize - PortfolioStop(Stop* so, const WorkerControl& control); + /// Initialize with slave stop \a so, portfolio stop \a po, and \a control + PortfolioStop(Stop* so, Stop* po, const WorkerControl& control); /// Set pointer to shared \a tostop variable void share(std::atomic* ts); /// Return true if portfolio engine must be stopped diff --git a/gecode/search/par/pbs.hpp b/gecode/search/par/pbs.hpp index 0f7f03e769..7f6ce1fe30 100755 --- a/gecode/search/par/pbs.hpp +++ b/gecode/search/par/pbs.hpp @@ -139,8 +139,9 @@ namespace Gecode { namespace Search { namespace Par { forceinline - PortfolioStop::PortfolioStop(Stop* so0, const WorkerControl& control) - : so(so0), worker_control(control), tostop(nullptr) {} + PortfolioStop::PortfolioStop(Stop* so0, Stop* po0, + const WorkerControl& control) + : so(so0), po(po0), worker_control(control), tostop(nullptr) {} forceinline void PortfolioStop::share(std::atomic* ts) { diff --git a/gecode/search/pbs.cpp b/gecode/search/pbs.cpp index a910158b30..b52ce8e269 100644 --- a/gecode/search/pbs.cpp +++ b/gecode/search/pbs.cpp @@ -40,6 +40,11 @@ namespace Gecode { namespace Search { namespace Seq { return new PortfolioStop(so); } + Stop* + pbsstop(Stop* so, Stop* po) { + return new PortfolioStop(so,po); + } + Engine* pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, const Statistics& stat, const Search::Options& opt, bool best) { @@ -59,7 +64,12 @@ namespace Gecode { namespace Search { namespace Par { Stop* pbsstop(Stop* so, const WorkerControl& control) { - return new PortfolioStop(so,control); + return new PortfolioStop(so,nullptr,control); + } + + Stop* + pbsstop(Stop* so, Stop* po, const WorkerControl& control) { + return new PortfolioStop(so,po,control); } Engine* diff --git a/gecode/search/pbs.hpp b/gecode/search/pbs.hpp index f042a18f35..720787eac0 100644 --- a/gecode/search/pbs.hpp +++ b/gecode/search/pbs.hpp @@ -101,6 +101,139 @@ namespace Gecode { namespace Search { } } + /** + * \brief Whether engine \a E takes ownership of the cutoff in its options + * \internal + */ + template + struct PbsOwnsCutoff { + static constexpr bool value = false; + }; + + /** + * \brief Restart-based search takes ownership of the cutoff + * \internal + */ + template class E> + struct PbsOwnsCutoff > { + static constexpr bool value = true; + }; + + /** + * \brief A portfolio takes ownership of the cutoff if its assets do + * \internal + */ + template class E> + struct PbsOwnsCutoff > { + static constexpr bool value = PbsOwnsCutoff >::value; + }; + + /** + * \brief Cutoff sequence shared by the assets of a portfolio + * + * A cutoff cannot be copied, so the assets of a portfolio that each take + * ownership of a cutoff get a PbsCutoff each, drawing from one sequence. + * The sequence is disposed with the last PbsCutoff and the reference held + * while the portfolio is built. The original cutoff is disposed as well + * only if building has succeeded, otherwise it stays with the caller. + * + * \internal + */ + class PbsCutoffs : public HeapAllocated { + public: + /// Mutex protecting the sequence, assets can run in parallel + Support::Mutex m; + /// The shared cutoff sequence + Cutoff* c; + /// Number of references + unsigned int n; + /// Whether the cutoff sequence is disposed with the last reference + bool owner; + /// Initialize for cutoff \a c0 with the reference held while building + PbsCutoffs(Cutoff* c0) : c(c0), n(1U), owner(false) {} + /// Release a reference + static void release(PbsCutoffs* s) { + bool last; + { + Support::Lock lock(s->m); + last = (--s->n == 0U); + } + if (last) { + if (s->owner) + delete s->c; + delete s; + } + } + }; + + /** + * \brief Cutoff of a portfolio asset, the assets share the sequence of values + * \internal + */ + class PbsCutoff : public Cutoff { + protected: + /// The shared sequence + PbsCutoffs* s; + /// The current cutoff value of this asset + unsigned long long int v; + public: + /// Initialize as a new reference to \a s0 + PbsCutoff(PbsCutoffs* s0) : s(s0) { + Support::Lock lock(s->m); + v = (*s->c)(); + s->n++; + } + /// Return the current cutoff value + unsigned long long int operator ()(void) const override { + return v; + } + /// Take the next value of the shared sequence and return it + unsigned long long int operator ++(void) override { + Support::Lock lock(s->m); + v = ++(*s->c); + return v; + } + /// Release the reference to the shared sequence + ~PbsCutoff(void) override { + PbsCutoffs::release(s); + } + }; + + /** + * \brief Return the cutoffs shared by the assets of a portfolio + * + * Returns nullptr if the assets of engine \a E do not take ownership of + * the cutoff in \a opt, or if there are fewer than two assets. + * + * \internal + */ + template + forceinline PbsCutoffs* + make_pbs_cutoffs(const Options& opt, unsigned int n_slaves) { + if (!PbsOwnsCutoff::value || (opt.cutoff == nullptr) || + (n_slaves < 2U)) + return nullptr; + return new PbsCutoffs(opt.cutoff); + } + + /** + * \brief Release the cutoffs \a cs held while building + * + * The flag \a built tells whether building has succeeded. + * + * \internal + */ + forceinline void + release_pbs_cutoffs(PbsCutoffs* cs, bool built) { + if (cs == nullptr) + return; + { + Support::Lock lock(cs->m); + cs->owner = built; + } + PbsCutoffs::release(cs); + } + /// A PBS engine builder template class E> class PbsBuilder : public Builder { @@ -133,6 +266,10 @@ namespace Gecode { namespace Search { namespace Seq { GECODE_SEARCH_EXPORT Stop* pbsstop(Stop* so); + /// Create stop object for builder stop \a so and portfolio stop \a po + GECODE_SEARCH_EXPORT Stop* + pbsstop(Stop* so, Stop* po); + /// Create sequential portfolio engine GECODE_SEARCH_EXPORT Engine* pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, @@ -146,6 +283,10 @@ namespace Gecode { namespace Search { namespace Par { GECODE_SEARCH_EXPORT Stop* pbsstop(Stop* so, const WorkerControl& control); + /// Create stop object for builder stop \a so and portfolio stop \a po + GECODE_SEARCH_EXPORT Stop* + pbsstop(Stop* so, Stop* po, const WorkerControl& control); + /// Create parallel portfolio engine GECODE_SEARCH_EXPORT Engine* pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, @@ -179,6 +320,7 @@ namespace Gecode { namespace Search { SearchTracer::EngineType::PBS, n_slaves); Space* unowned_master = master; + PbsCutoffs* cutoffs = make_pbs_cutoffs >(opt,n_slaves); try { for (unsigned int i=0U; islave(i); + std::unique_ptr cutoff; + if (cutoffs != nullptr) { + cutoff.reset(new PbsCutoff(cutoffs)); + opt.cutoff = cutoff.get(); + } slaves[i] = build(slave.get(),opt); + // The asset owns its cutoff + (void) cutoff.release(); } - return Seq::pbsengine( + Engine* e = Seq::pbsengine( slaves,stops,n_slaves,stat,opt,E::best); + release_pbs_cutoffs(cutoffs,true); + return e; } catch (...) { pbscleanup(slaves,stops,n_slaves,unowned_master); + release_pbs_cutoffs(cutoffs,false); throw; } } @@ -219,8 +371,8 @@ namespace Gecode { namespace Search { bool builders_owned = true; try { for (int i=0; ioptions().stop); + // Re-configure slave options, the portfolio stop applies as well + stops[i] = Seq::pbsstop(sebs[i]->options().stop,opt.stop); sebs[i]->options().stop = stops[i]; sebs[i]->options().clone = true; std::unique_ptr slave((i == n_slaves-1) ? @@ -270,6 +422,7 @@ namespace Gecode { namespace Search { } Space* unowned_master = master; + PbsCutoffs* cutoffs = make_pbs_cutoffs >(opt,n_slaves); try { for (unsigned int i=0U; islave(static_cast(i)); + std::unique_ptr cutoff; + if (cutoffs != nullptr) { + cutoff.reset(new PbsCutoff(cutoffs)); + opt.cutoff = cutoff.get(); + } slaves[i] = build(slave.get(),opt); + // The asset owns its cutoff + (void) cutoff.release(); } - return Par::pbsengine(slaves,stops,n_slaves,stat,E::best); + Engine* e = Par::pbsengine(slaves,stops,n_slaves,stat,E::best); + release_pbs_cutoffs(cutoffs,true); + return e; } catch (...) { pbscleanup(slaves,stops,n_slaves,unowned_master); + release_pbs_cutoffs(cutoffs,false); throw; } } @@ -312,8 +475,8 @@ namespace Gecode { namespace Search { bool builders_owned = true; try { for (int i=0; ioptions().stop, + // Re-configure slave options, the portfolio stop applies as well + stops[i] = Par::pbsstop(sebs[i]->options().stop,opt.stop, sebs[i]->options().worker_control); sebs[i]->options().stop = stops[i]; sebs[i]->options().clone = true; @@ -358,6 +521,13 @@ namespace Gecode { if (!opt.clone) delete s; e = Search::Seq::dead(opt,stat); + /* + * The assets would own the cutoff, but a dead engine does not take + * it. This includes the cutoff of an outer portfolio when this is + * one of its assets. + */ + if (Search::PbsOwnsCutoff >::value) + delete opt.cutoff; return; } diff --git a/gecode/search/rbs.hpp b/gecode/search/rbs.hpp index eb925aef9f..a992a3feb7 100644 --- a/gecode/search/rbs.hpp +++ b/gecode/search/rbs.hpp @@ -108,9 +108,9 @@ namespace Gecode { stat.fail++; if (!m_opt.clone) delete s; + e = Search::Seq::dead(e_opt, stat); // The engine owns the cutoff, but a dead engine does not take it delete m_opt.cutoff; - e = Search::Seq::dead(e_opt, stat); } else { std::unique_ptr master(m_opt.clone ? s->clone() : s); std::unique_ptr slave(master->clone()); diff --git a/gecode/search/seq/bab.hpp b/gecode/search/seq/bab.hpp index dade2cb075..45cab15425 100644 --- a/gecode/search/seq/bab.hpp +++ b/gecode/search/seq/bab.hpp @@ -79,6 +79,9 @@ namespace Gecode { namespace Search { namespace Seq { */ start(); while (true) { + // An exhausted search is complete, even when a limit has been reached + if ((cur == nullptr) && path.empty()) + return nullptr; if (stop_seq(opt)) return nullptr; // Recompute and add constraint if necessary @@ -185,13 +188,17 @@ namespace Gecode { namespace Search { namespace Seq { d = 0; mark = 0; delete cur; - if ((s == nullptr) || (s->status(*this) == SS_FAILED)) { + // Reset the statistics first, so that a failed root is counted + Worker::reset(); + if (s == nullptr) { + cur = nullptr; + } else if (s->status(*this) == SS_FAILED) { + fail++; delete s; cur = nullptr; } else { cur = s; } - Worker::reset(); } template diff --git a/gecode/search/seq/dfs.hpp b/gecode/search/seq/dfs.hpp index 0a90f8a53a..cbfadd8d02 100644 --- a/gecode/search/seq/dfs.hpp +++ b/gecode/search/seq/dfs.hpp @@ -58,13 +58,17 @@ namespace Gecode { namespace Search { namespace Seq { delete cur; path.reset(); d = 0; - if ((s == nullptr) || (s->status(*this) == SS_FAILED)) { + // Reset the statistics first, so that a failed root is counted + Worker::reset(); + if (s == nullptr) { + cur = nullptr; + } else if (s->status(*this) == SS_FAILED) { + fail++; delete s; cur = nullptr; } else { cur = s; } - Worker::reset(); } template @@ -89,6 +93,9 @@ namespace Gecode { namespace Search { namespace Seq { */ start(); while (true) { + // An exhausted search is complete, even when a limit has been reached + if ((cur == nullptr) && path.empty()) + return nullptr; if (stop_seq(opt)) return nullptr; while (cur == nullptr) { diff --git a/gecode/search/seq/lds.hpp b/gecode/search/seq/lds.hpp index 297a172b2a..6a19404392 100644 --- a/gecode/search/seq/lds.hpp +++ b/gecode/search/seq/lds.hpp @@ -327,16 +327,19 @@ namespace Gecode { namespace Search { namespace Seq { forceinline void LDS::reset(Space* s) { delete root; root=nullptr; d=0; + // Reset the statistics first, so that a failed root is counted + e.reset(nullptr,0); + if (s == nullptr) + return; e.node = 1; - if ((s == nullptr) || (s->status(e) == SS_FAILED)) { + if (s->status(e) == SS_FAILED) { delete s; e.fail++; - e.reset(nullptr,0); } else { if (opt.d_l > 0) { root = s->clone(); } - e.reset(s,0); + e.init(s); } } diff --git a/gecode/search/seq/pbs.cpp b/gecode/search/seq/pbs.cpp index ec72050c7d..1e79557f76 100644 --- a/gecode/search/seq/pbs.cpp +++ b/gecode/search/seq/pbs.cpp @@ -37,7 +37,8 @@ namespace Gecode { namespace Search { namespace Seq { bool PortfolioStop::stop(const Statistics& s, const Options& o) { - if ((so != nullptr) && so->stop(s,o)) { + if (((so != nullptr) && so->stop(s,o)) || + ((po != nullptr) && po->stop(s,o))) { ssi->done = false; return true; } diff --git a/gecode/search/seq/pbs.hh b/gecode/search/seq/pbs.hh index cd7cf2488f..e755548421 100644 --- a/gecode/search/seq/pbs.hh +++ b/gecode/search/seq/pbs.hh @@ -52,11 +52,13 @@ namespace Gecode { namespace Search { namespace Seq { private: /// The stop object for the slaves Stop* so; + /// The stop object for the portfolio (possibly nullptr) + Stop* po; /// Pointer to shared stop information SharedStopInfo* ssi; public: - /// Initialize - PortfolioStop(Stop* so); + /// Initialize with slave stop \a so and portfolio stop \a po + PortfolioStop(Stop* so, Stop* po=nullptr); /// Initialize shared stop information void share(SharedStopInfo* ssi); /// Return true if portfolio engine must be stopped diff --git a/gecode/search/seq/pbs.hpp b/gecode/search/seq/pbs.hpp index 4dc77efef1..62e95e5c51 100644 --- a/gecode/search/seq/pbs.hpp +++ b/gecode/search/seq/pbs.hpp @@ -37,8 +37,8 @@ namespace Gecode { namespace Search { namespace Seq { forceinline - PortfolioStop::PortfolioStop(Stop* so0) - : so(so0) {} + PortfolioStop::PortfolioStop(Stop* so0, Stop* po0) + : so(so0), po(po0) {} forceinline void PortfolioStop::share(SharedStopInfo* ssi0) { ssi = ssi0; diff --git a/gecode/search/seq/rbs.cpp b/gecode/search/seq/rbs.cpp index c4edfc4106..acb3ebaaba 100755 --- a/gecode/search/seq/rbs.cpp +++ b/gecode/search/seq/rbs.cpp @@ -58,6 +58,12 @@ namespace Gecode { namespace Search { namespace Seq { return false; } + bool + RestartStop::meta_stop(const Statistics& s, const Options& o) { + Support::Lock lock(m); + return (m_stop != nullptr) && m_stop->stop(m_stat+s,o); + } + NoGoods& RBS::master_nogoods(void) { // The no-goods of an incomplete slave search only hold in its restricted @@ -72,6 +78,10 @@ namespace Gecode { namespace Search { namespace Seq { Space* RBS::next(void) { + // A failed master proves that no further solution exists + if (master == nullptr) + return nullptr; + m_stopped = false; if (restart) { restart = false; sslr++; @@ -108,8 +118,18 @@ namespace Gecode { namespace Search { namespace Seq { (e->stopped() && stop->enginestopped()) ) { // The engine must perform a true restart // The number of the restart has been incremented in the stop object - if (!complete && !e->stopped()) + if (!complete && !e->stopped()) { + /* + * The engine might not have checked its stop object, for example + * when the slave failed at the root or a parallel engine had no + * work. Without this check, the restarts might never stop. + */ + if (stop->meta_stop(e->statistics(),m_opt)) { + m_stopped = true; + return nullptr; + } stop->restart(); + } sslr = 0; NoGoods& ng = master_nogoods(); MetaInfo mi(stop->restarts(),e->stopped() ? MetaInfo::RR_LIM : MetaInfo::RR_CMPL,sslr,e->statistics().fail,last,ng); @@ -117,8 +137,13 @@ namespace Gecode { namespace Search { namespace Seq { stop->nogood(ng.ng()); unsigned long long int nl = ++(*co); stop->limit(e->statistics(),nl); - if (stop->status(master) == SS_FAILED) + if (stop->status(master) == SS_FAILED) { + // The statistics of the engine have been added by limit() + delete master; + master = nullptr; + e->reset(nullptr); return nullptr; + } Space* slave = master; master = master->clone(); complete = slave->slave(mi); @@ -141,6 +166,8 @@ namespace Gecode { namespace Search { namespace Seq { RBS::constrain(const Space& b) { if (!best) throw NoBest("RBS::constrain"); + if (master == nullptr) + return; if (last != nullptr) { last->constrain(b); if (last->status() == SS_FAILED) { @@ -162,8 +189,11 @@ namespace Gecode { namespace Search { namespace Seq { * the meta engine does not perform a restart. However the * invocation of next will do so and no restart will be * missed. + * + * The engine may still report a stop when the master failed during a + * restart after a limit. A failed master is a proof, not a stop. */ - return e->stopped(); + return (master != nullptr) && (m_stopped || e->stopped()); } RBS::~RBS(void) { diff --git a/gecode/search/seq/rbs.hh b/gecode/search/seq/rbs.hh index 744e60b4a9..5d7cddf12d 100755 --- a/gecode/search/seq/rbs.hh +++ b/gecode/search/seq/rbs.hh @@ -64,6 +64,8 @@ namespace Gecode { namespace Search { namespace Seq { RestartStop(Stop* s); /// Return true if meta engine must be stopped virtual bool stop(const Statistics& s, const Options& o); + /// Return true if the stop object for the meta engine says so + bool meta_stop(const Statistics& s, const Options& o); /// Return current restart count unsigned long int restarts(void) const; /// Increment current restart count @@ -95,6 +97,8 @@ namespace Gecode { namespace Search { namespace Seq { Cutoff* co; /// The stop control object RestartStop* stop; + /// The options for the stop object of the meta engine + Options m_opt; /// How many solutions since the last restart unsigned long int sslr; /// Whether search for the next solution will be complete @@ -105,6 +109,8 @@ namespace Gecode { namespace Search { namespace Seq { bool restart; /// Whether the engine performs best solution search bool best; + /// Whether the stop object for the meta engine stopped a restart + bool m_stopped; /// Return the no-goods of the last slave search, valid in the master NoGoods& master_nogoods(void); public: diff --git a/gecode/search/seq/rbs.hpp b/gecode/search/seq/rbs.hpp index 8c0081de38..b19d6f1603 100755 --- a/gecode/search/seq/rbs.hpp +++ b/gecode/search/seq/rbs.hpp @@ -98,9 +98,9 @@ namespace Gecode { namespace Search { namespace Seq { Engine* e0, const Search::Statistics& stat, const Options& opt, bool best0, bool complete0) : e(e0), master(s), last(nullptr), co(opt.cutoff), stop(stop0), - sslr(0), + m_opt(opt.expand()), sslr(0), complete(true), nogoods_valid(complete0), restart(false), - best(best0) { + best(best0), m_stopped(false) { stop->limit(stat,(*co)()); } diff --git a/test/search.cpp b/test/search.cpp index 411b5a6dfa..34eb49adae 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1358,6 +1358,201 @@ namespace Test { RBSIncompleteNoGoods rbs_incomplete_nogoods; + /// Space without solutions whose search tree has several failures + class NoSolution : public Gecode::IntMinimizeSpace { + public: + /// Four variables that must be distinct with three values + Gecode::IntVarArray x; + NoSolution(void) : x(*this,4,0,2) { + for (int i=0; i rbs(m,o); + delete m; + if (FailingMaster* s = rbs.next()) { + delete s; + return false; + } + Gecode::Search::Statistics s = rbs.statistics(); + // The failures of the last slave search are counted once + if (rbs.stopped() || (s.restart != 1U) || (s.fail < 2U) || + ((t == 1U) && (s.fail != 2U))) + return false; + if ((rbs.next() != nullptr) || rbs.stopped() || + (rbs.statistics().fail != s.fail) || + (rbs.statistics().node != s.node)) + return false; + } + return true; + } + }; + + RBSFailedMaster rbs_failed_master; + + /// Space without solutions whose restricted slaves fail at the root + class FailingSlave : public NoSolution { + public: + FailingSlave(void) = default; + FailingSlave(FailingSlave& s) : NoSolution(s) {} + Gecode::Space* copy(void) override { + return new FailingSlave(*this); + } + bool slave(const Gecode::MetaInfo& mi) override { + if ((mi.type() == Gecode::MetaInfo::RESTART) && (mi.restart() > 0)) { + fail(); + return false; + } + return true; + } + }; + + /// Stop after five failures, or after many checks as a safeguard + class FiveFailures : public Gecode::Search::Stop { + public: + /// Number of checks + unsigned long int checks = 0; + bool stop(const Gecode::Search::Statistics& s, + const Gecode::Search::Options&) override { + return (s.fail >= 5) || (++checks > 1000); + } + }; + + /// Slave searches failing at the root count as failures + class RBSRootFailure : public Base { + template class Engine> + static bool test(unsigned int threads) { + FailingSlave* m = new FailingSlave; + FiveFailures f; + Gecode::Search::Options o; + o.threads = threads; + o.stop = &f; + o.cutoff = Gecode::Search::Cutoff::constant(1); + Gecode::RBS rbs(m,o); + delete m; + if (FailingSlave* s = rbs.next()) { + delete s; + return false; + } + return rbs.stopped() && (f.checks <= 1000) && + (rbs.statistics().fail >= 5); + } + public: + RBSRootFailure(void) : Base("Search::RBS::RootFailure") {} + bool run(void) override { + return test(1U) && test(2U) && + test(1U) && test(2U) && + test(1U); + } + }; + + RBSRootFailure rbs_root_failure; + + /// Stop after a number of failures, or after many checks as a safeguard + class FailureLimit : public Gecode::Search::Stop { + public: + /// Failure limit + unsigned long long int limit; + /// Number of checks, parallel engines can check concurrently + std::atomic checks{0}; + FailureLimit(unsigned long long int l) : limit(l) {} + bool stop(const Gecode::Search::Statistics& s, + const Gecode::Search::Options&) override { + return (s.fail > limit) || (++checks > 1000); + } + }; + + /// A search exhausted when it reaches a failure limit is not stopped + class ExhaustedAtLimit : public Base { + template class Engine> + static bool test(void) { + NoSolution root; + unsigned long long int f; + { + Engine e(&root); + if (NoSolution* s = e.next()) { + delete s; + return false; + } + f = e.statistics().fail; + } + if (f < 2) + return false; + // The search is exhausted at the failure that exceeds the limit + { + FailureLimit l(f-1); + Gecode::Search::Options o; + o.stop = &l; + Engine e(&root,o); + if ((e.next() != nullptr) || e.stopped()) + return false; + } + // Restarts at that limit post no no-goods and would repeat forever + { + FailureLimit l(1000); + Gecode::Search::Options o; + o.stop = &l; + o.nogoods_limit = 128U; + o.cutoff = Gecode::Search::Cutoff::constant(f-1); + Gecode::RBS rbs(&root,o); + if ((rbs.next() != nullptr) || rbs.stopped()) + return false; + } + return true; + } + public: + ExhaustedAtLimit(void) : Base("Search::ExhaustedAtLimit") {} + bool run(void) override { + return test() && test(); + } + }; + + ExhaustedAtLimit exhausted_at_limit; + /// Space for relaxing class RelaxSpace : public Gecode::Space { public: @@ -1517,6 +1712,134 @@ namespace Test { } }; + /// The stop object of a portfolio built from builders stops the builders + class PBSBuilderStop : public Base { + /// Function returning an engine builder + typedef Gecode::SEB (*Builder)(const Gecode::Search::Options&); + template class Engine> + static bool test(unsigned int threads, Builder builder) { + NoSolution root; + FailureLimit l(1); + Gecode::Search::Options o; + o.threads = threads; + o.stop = &l; + Gecode::SEBs sebs(2); + sebs[0] = builder(Gecode::Search::Options::def); + sebs[1] = builder(Gecode::Search::Options::def); + Gecode::PBS pbs(&root,sebs,o); + return (pbs.next() == nullptr) && pbs.stopped() && (l.checks <= 1000); + } + public: + PBSBuilderStop(void) : Base("Search::PBS::SEB::Stop") {} + bool run(void) override { + for (unsigned int t : {1U,2U}) + if (!test(t,&Gecode::dfs) || + !test(t,&Gecode::bab)) + return false; + return true; + } + }; + + PBSBuilderStop pbs_builder_stop; + + /// Restart-based assets of a portfolio share the cutoff sequence + class PBSRestartCutoff : public Base { + template class Engine> + static bool test(unsigned int threads) { + NoSolution root; + Gecode::Search::Options o; + o.assets = 2; + o.threads = threads; + o.cutoff = Gecode::Search::Cutoff::geometric(1,2); + Gecode::PBS pbs(&root,o); + return (pbs.next() == nullptr) && !pbs.stopped(); + } + public: + PBSRestartCutoff(void) : Base("Search::PBS::RBS::Cutoff") {} + bool run(void) override { + for (unsigned int t : {1U,2U,4U}) + if (!test(t) || !test(t)) + return false; + return true; + } + }; + + PBSRestartCutoff pbs_restart_cutoff; + + /** + * \brief Constant cutoff counting its deletions + * + * The cutoff exceeds the failures of NoSolution, so a restart-based + * asset finishes without restarting. Without no-goods, restarts with a + * smaller constant cutoff would repeat the same search forever. + */ + class CountingCutoff : public Gecode::Search::CutoffConstant { + public: + /// Number of deletions, owned by the test + std::atomic& deleted; + CountingCutoff(std::atomic& d) + : Gecode::Search::CutoffConstant(1000), deleted(d) {} + ~CountingCutoff(void) override { + deleted++; + } + }; + + /// Space without solutions whose first portfolio asset fails + class FailingAsset : public NoSolution { + public: + FailingAsset(void) = default; + FailingAsset(FailingAsset& s) : NoSolution(s) {} + Gecode::Space* copy(void) override { + return new FailingAsset(*this); + } + bool slave(const Gecode::MetaInfo& mi) override { + if ((mi.type() == Gecode::MetaInfo::PORTFOLIO) && (mi.asset() == 0U)) + fail(); + return true; + } + }; + + /// A portfolio of restart-based assets deletes the cutoff exactly once + class PBSRestartCutoffDeleted : public Base { + template class Engine> + static bool test(Root& root, unsigned int threads) { + std::atomic deleted{0U}; + { + Gecode::Search::Options o; + o.assets = 2; + o.threads = threads; + o.cutoff = new CountingCutoff(deleted); + Gecode::PBS pbs(&root,o); + if (Root* s = pbs.next()) { + delete s; + return false; + } + if (pbs.stopped()) + return false; + } + return deleted == 1U; + } + public: + PBSRestartCutoffDeleted(void) + : Base("Search::PBS::RBS::CutoffDeleted") {} + bool run(void) override { + // A failed root, also for a portfolio of portfolios + NoSolution failed; + Gecode::rel(failed,failed.x[0],Gecode::IRT_LE,0); + // An asset of a portfolio of portfolios fails at its root + FailingAsset failing; + for (unsigned int t : {1U,2U,4U}) + if (!test(failed,t) || + !test(failed,t) || + !test(failing,t) || + !test(failing,t)) + return false; + return true; + } + }; + + PBSRestartCutoffDeleted pbs_restart_cutoff_deleted; + /// Iterator for branching types class BranchTypes { private: