From 92a4d4392492881179e2b9ce91599685f3b4632e Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 11:28:01 +0100 Subject: [PATCH 1/6] Report a failed master after a restart limit as exhausted When the slave search hit its cutoff, RBS restarted by calling the master configuration function. If the master then failed, for example because the posted no-goods proved that no better solution exists, RBS returned no solution but left the stopped slave engine in place, so stopped() kept returning true. FlatZinc then printed an unknown result instead of the proof, and the statistics counted the last slave search twice. Dispose of the failed master and reset the slave engine, as after a master that fails following a solution. RBS reports a stop only while it still has a master, and next() and constrain() do nothing once the master has failed. --- changelog.in | 11 ++++++++ gecode/search/seq/rbs.cpp | 17 ++++++++++-- test/search.cpp | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/changelog.in b/changelog.in index 1c416a0cc8..29f0495eb5 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,17 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[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/seq/rbs.cpp b/gecode/search/seq/rbs.cpp index c4edfc4106..666af04d0e 100755 --- a/gecode/search/seq/rbs.cpp +++ b/gecode/search/seq/rbs.cpp @@ -72,6 +72,9 @@ namespace Gecode { namespace Search { namespace Seq { Space* RBS::next(void) { + // A failed master proves that no further solution exists + if (master == nullptr) + return nullptr; if (restart) { restart = false; sslr++; @@ -117,8 +120,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 +149,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 +172,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) && e->stopped(); } RBS::~RBS(void) { diff --git a/test/search.cpp b/test/search.cpp index 411b5a6dfa..1d315b5397 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1358,6 +1358,63 @@ namespace Test { RBSIncompleteNoGoods rbs_incomplete_nogoods; + /// Space without solutions whose master fails at a restart after a limit + class FailingMaster : public Gecode::Space { + public: + /// Four variables that must be distinct with three values + Gecode::IntVarArray x; + FailingMaster(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 for relaxing class RelaxSpace : public Gecode::Space { public: From 1b588fa288682cab509c595a886a1da8954bbee8 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 11:53:44 +0100 Subject: [PATCH 2/6] Count a failed root as a failure when resetting an engine The constructors of DFS, BAB, and LDS count a root space that fails at status() as a failure, but reset() checked the status before resetting the statistics, so the failure and the root's propagations were lost. RBS resets its engine for every restart, so when the slave space of large neighbourhood search failed right away, neither a FailStop or NodeStop nor the failure slice of a portfolio advanced: RBS with LNS under FailStop(5000) ran 960k episodes with no failure counted. Reset the statistics first and count a failed root, in the sequential and parallel engines. A null space, which RBS passes after its master has failed, is not counted. RBS also relied on the slave engine to check the stop object of the meta engine. A parallel engine without work never checks it, so with a slave failing at the root, restarts continued forever whatever the stop object said. RBS now checks that stop object itself before each restart after an incomplete slave search. --- changelog.in | 13 ++++++++ gecode/search/par/bab.hpp | 8 +++-- gecode/search/par/dfs.hpp | 8 +++-- gecode/search/seq/bab.hpp | 8 +++-- gecode/search/seq/dfs.hpp | 8 +++-- gecode/search/seq/lds.hpp | 9 +++-- gecode/search/seq/rbs.cpp | 21 ++++++++++-- gecode/search/seq/rbs.hh | 6 ++++ gecode/search/seq/rbs.hpp | 4 +-- test/search.cpp | 70 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 140 insertions(+), 15 deletions(-) diff --git a/changelog.in b/changelog.in index 29f0495eb5..2bdecf4c17 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,19 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[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 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/seq/bab.hpp b/gecode/search/seq/bab.hpp index dade2cb075..f67f1c6a22 100644 --- a/gecode/search/seq/bab.hpp +++ b/gecode/search/seq/bab.hpp @@ -185,13 +185,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..7c45905e1e 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 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/rbs.cpp b/gecode/search/seq/rbs.cpp index 666af04d0e..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 @@ -75,6 +81,7 @@ namespace Gecode { namespace Search { namespace Seq { // A failed master proves that no further solution exists if (master == nullptr) return nullptr; + m_stopped = false; if (restart) { restart = false; sslr++; @@ -111,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); @@ -176,7 +193,7 @@ namespace Gecode { namespace Search { namespace Seq { * 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 (master != nullptr) && 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 1d315b5397..0e8e600177 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1415,6 +1415,76 @@ namespace Test { RBSFailedMaster rbs_failed_master; + /// Space without solutions whose restricted slaves fail at the root + class FailingSlave : public Gecode::IntMinimizeSpace { + public: + /// Four variables that must be distinct with three values + Gecode::IntVarArray x; + FailingSlave(void) : x(*this,4,0,2) { + for (int i=0; i 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; + /// Space for relaxing class RelaxSpace : public Gecode::Space { public: From e9e411b32302f0562979a2f053db42ba3557487a Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 11:58:54 +0100 Subject: [PATCH 3/6] Finish an exhausted search before checking the stop object Sequential DFS and BAB checked the stop object before checking whether the search tree was exhausted. When the last failure of the tree exceeded a failure limit, the engine reported a stop although nothing was left to explore. With an empty path there are no no-goods, so RBS restarted from the same master and repeated the same slave search forever. RBS with no-goods on an unsatisfiable pigeonhole model did not terminate for most constant cutoffs between 5 and 17: the search got stuck once the remaining subtree had exactly one failure more than the cutoff. Return as exhausted before checking the stop object, as LDS already does. The parallel engines check the stop object only when they have work, so they were not affected. --- changelog.in | 11 +++++ gecode/search/seq/bab.hpp | 3 ++ gecode/search/seq/dfs.hpp | 3 ++ test/search.cpp | 100 ++++++++++++++++++++++++++++++-------- 4 files changed, 98 insertions(+), 19 deletions(-) diff --git a/changelog.in b/changelog.in index 2bdecf4c17..6563b3839b 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,17 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[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 diff --git a/gecode/search/seq/bab.hpp b/gecode/search/seq/bab.hpp index f67f1c6a22..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 diff --git a/gecode/search/seq/dfs.hpp b/gecode/search/seq/dfs.hpp index 7c45905e1e..cbfadd8d02 100644 --- a/gecode/search/seq/dfs.hpp +++ b/gecode/search/seq/dfs.hpp @@ -93,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/test/search.cpp b/test/search.cpp index 0e8e600177..baaaec0ad9 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1358,20 +1358,33 @@ namespace Test { RBSIncompleteNoGoods rbs_incomplete_nogoods; - /// Space without solutions whose master fails at a restart after a limit - class FailingMaster : public Gecode::Space { + /// 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; - FailingMaster(void) : x(*this,4,0,2) { + NoSolution(void) : x(*this,4,0,2) { for (int i=0; i 0)) { fail(); @@ -1485,6 +1486,67 @@ namespace Test { 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: From 2dddb757f5477f0e1f695494dd51cbef98a51321 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 12:33:28 +0100 Subject: [PATCH 4/6] Check the portfolio stop object for engine builders A portfolio created from engine builders, PBS(s, sebs, o), wrapped the stop object of each builder in its portfolio stop and never looked at o.stop. A portfolio with a 200 ms TimeStop ran for about 3 seconds and then reported that it had not been stopped. The portfolio stop of each asset now checks the stop object of the portfolio as well as that of the builder, with the statistics of the asset, as the homogeneous portfolio already does for its stop object. New overloads of Seq::pbsstop and Par::pbsstop pass both stop objects; the existing ones are kept. --- changelog.in | 10 ++++++++++ gecode/search.hh | 3 +++ gecode/search/par/pbs.cpp | 3 ++- gecode/search/par/pbs.hh | 6 ++++-- gecode/search/par/pbs.hpp | 5 +++-- gecode/search/pbs.cpp | 12 +++++++++++- gecode/search/pbs.hpp | 16 ++++++++++++---- gecode/search/seq/pbs.cpp | 3 ++- gecode/search/seq/pbs.hh | 6 ++++-- gecode/search/seq/pbs.hpp | 4 ++-- test/search.cpp | 36 ++++++++++++++++++++++++++++++++++++ 11 files changed, 89 insertions(+), 15 deletions(-) diff --git a/changelog.in b/changelog.in index 6563b3839b..4a97c3348d 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,16 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[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 diff --git a/gecode/search.hh b/gecode/search.hh index 11b40fc458..844ac6b371 100755 --- a/gecode/search.hh +++ b/gecode/search.hh @@ -1551,6 +1551,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/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..9e5032a2e0 100644 --- a/gecode/search/pbs.hpp +++ b/gecode/search/pbs.hpp @@ -133,6 +133,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 +150,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, @@ -219,8 +227,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) ? @@ -312,8 +320,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; 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/test/search.cpp b/test/search.cpp index baaaec0ad9..fcc6122343 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1378,6 +1378,12 @@ namespace Test { Gecode::IntVar cost(void) const override { return x[0]; } + bool master(const Gecode::MetaInfo& mi) override { + // Keep the branchers for the assets of a portfolio + if (mi.type() == Gecode::MetaInfo::PORTFOLIO) + return true; + return Gecode::IntMinimizeSpace::master(mi); + } }; /// Space without solutions whose master fails at a restart after a limit @@ -1706,6 +1712,36 @@ 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; + /// Iterator for branching types class BranchTypes { private: From 3eef9136a521a67313dfa89b0ce1301fa84083f5 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 12:44:56 +0100 Subject: [PATCH 5/6] Share one cutoff between the restart assets of a portfolio A homogeneous portfolio of restart-based engines, PBS, passed the same options to every asset, so every RBS engine took ownership of the cutoff in o.cutoff. The cutoff was deleted once per asset: a portfolio of two RBS assets crashed when it was deleted, and parallel assets also advanced the same cutoff concurrently. A cutoff cannot be copied without adding a virtual function, so each asset that takes ownership of the cutoff now gets a PbsCutoff, which draws the next value from the shared cutoff under a mutex. The shared cutoff is deleted with the last asset cutoff, and only if the portfolio was built: if building throws, the caller keeps the cutoff, as with RBS. Nested portfolios of restart-based engines are handled the same way; other engines do not take the cutoff and are left unchanged. RBS also deleted the cutoff before creating the engine for a failed root, so a failing allocation there would leave the caller with a deleted cutoff. Delete it once that engine exists. --- changelog.in | 10 +++ gecode/search.hh | 4 + gecode/search/pbs.hpp | 166 +++++++++++++++++++++++++++++++++++++++++- gecode/search/rbs.hpp | 2 +- test/search.cpp | 98 +++++++++++++++++++++++++ 5 files changed, 277 insertions(+), 3 deletions(-) diff --git a/changelog.in b/changelog.in index 4a97c3348d..0116a2d736 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,16 @@ Rank: minor Fix duplicate registration of EFPA's symmetry option, which hid its other command-line options. +[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 diff --git a/gecode/search.hh b/gecode/search.hh index 844ac6b371..2436a5ec44 100755 --- a/gecode/search.hh +++ b/gecode/search.hh @@ -1529,6 +1529,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 diff --git a/gecode/search/pbs.hpp b/gecode/search/pbs.hpp index 9e5032a2e0..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 { @@ -187,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; } } @@ -278,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; } } @@ -366,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/test/search.cpp b/test/search.cpp index fcc6122343..34eb49adae 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -1742,6 +1742,104 @@ namespace Test { 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: From 8eff1d5d3ba4cd6722747a10736749a4c50a8822 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sat, 14 Nov 2026 12:45:34 +0100 Subject: [PATCH 6/6] Document that LDS does not stop at its discrepancy limit When LDS reaches its discrepancy limit with parts of the tree left unexplored, next() returns nullptr and stopped() returns false, exactly as after exhausting the tree. A caller cannot tell an incomplete search from a proof. Reporting the limit through stopped() would change how RBS and PBS treat an LDS asset: RBS would restart it as if a cutoff had been reached, and PBS would stop the whole portfolio. The 6.x engine interface has no other way to report it outside experimental builds, so document the behaviour for LDS, lds(), and the discrepancy limit. --- changelog.in | 10 ++++++++++ gecode/search.hh | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/changelog.in b/changelog.in index 0116a2d736..32c886749f 100755 --- a/changelog.in +++ b/changelog.in @@ -96,6 +96,16 @@ 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 diff --git a/gecode/search.hh b/gecode/search.hh index 2436a5ec44..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