diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e136eefe47..92ed3278c0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -260,7 +260,7 @@ jobs: shell: bash run: > ctest --test-dir "$GITHUB_WORKSPACE/build-ninja" - --output-on-failure -R '^(build-gecode-test|test)$' + --output-on-failure -R '^(build-gecode-test|test|hub-spoke-api-.*)$' - name: Configure CMake Debug audit smoke shell: bash diff --git a/CMakeLists.txt b/CMakeLists.txt index d293267aec..1a66c12d92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1476,6 +1476,35 @@ if(BUILD_TESTING) message(WARNING "Skipping public test harness targets because required modules are disabled") endif() + # Overrides of the hub()/spoke() Space hooks must compile, and a Gecode 6 + # master() or slave() override must not, even without override. Each fail + # case overrides one old hook and must fail with the deleted-function + # diagnostic, not for another reason. MSVC words it differently (C2282), + # so there the test only requires the build to fail. + foreach(api pass fail-master fail-slave) + add_library(hub-spoke-api-${api} OBJECT EXCLUDE_FROM_ALL test/search/hub-spoke-api-${api}.cpp) + target_link_libraries(hub-spoke-api-${api} PRIVATE gecodekernel) + add_test(NAME hub-spoke-api-${api} + COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" + --target hub-spoke-api-${api} --config $) + endforeach() + set_tests_properties(hub-spoke-api-pass PROPERTIES + FIXTURES_SETUP hub-spoke-api + RESOURCE_LOCK gecode-test-build) + foreach(hook master slave) + # The failing fixtures stay out of the compilation database used for lint. + set_target_properties(hub-spoke-api-fail-${hook} PROPERTIES EXPORT_COMPILE_COMMANDS OFF) + set_tests_properties(hub-spoke-api-fail-${hook} PROPERTIES + FIXTURES_REQUIRED hub-spoke-api + RESOURCE_LOCK gecode-test-build) + if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + set_tests_properties(hub-spoke-api-fail-${hook} PROPERTIES WILL_FAIL TRUE) + else() + set_tests_properties(hub-spoke-api-fail-${hook} PROPERTIES + PASS_REGULAR_EXPRESSION "cannot override a deleted function;overriding deleted function") + endif() + endforeach() + set(GECODE_CAN_BUILD_TESTS ${GECODE_CAN_BUILD_TEST_HARNESS}) foreach(required minimodel driver) if(NOT TARGET gecode${required}) diff --git a/README.md b/README.md index 3fb11c2e01..6e76ddbf62 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ The tutorial *Modeling and Programming with Gecode* explains how to model and solve problems with Gecode, and the reference documentation describes the complete API. +For source-level breaking changes, see the +[Gecode 7 migration notes](docs/gecode-7-migration.md). + ## Download Gecode Gecode 6.5.0 is distributed as source code. Source archives can be downloaded from diff --git a/changelog.in b/changelog.in index 0823b65937..99dea0fe2f 100755 --- a/changelog.in +++ b/changelog.in @@ -75,6 +75,18 @@ Date: unreleased [DESCRIPTION] This is the development changelog for the next Gecode release. +[ENTRY] +Module: search +What: change +Rank: major +[DESCRIPTION] +Rename the meta-search Space hooks master() and slave() to hub() and spoke(). +[MORE] +There are no compatibility aliases. Space declares master() and slave() as +deleted, so an old override fails to compile with or without override. This +includes the FlatZincSpace override of slave(). See +docs/gecode-7-migration.md. + [ENTRY] Module: search What: change diff --git a/docs/gecode-7-migration.md b/docs/gecode-7-migration.md new file mode 100644 index 0000000000..26156e5728 --- /dev/null +++ b/docs/gecode-7-migration.md @@ -0,0 +1,42 @@ +# Gecode 7 migration notes + +## Meta-search hooks: master() and slave() are now hub() and spoke() + +Rename the overrides. Their signatures and return values are unchanged: + +```cpp +// Gecode 6 +bool master(const Gecode::MetaInfo& mi) override; +bool slave(const Gecode::MetaInfo& mi) override; + +// Gecode 7 +bool hub(const Gecode::MetaInfo& mi) override; +bool spoke(const Gecode::MetaInfo& mi) override; +``` + +There are no compatibility aliases. `Space` declares `master()` and +`slave()` as deleted, so an old override fails to compile whether or not it +is marked `override`. Restart-based search (RBS) and portfolio-based search +(PBS) call the renamed hooks at the same points and with the same defaults +as before. + +The hooks run on these spaces: + +- RBS calls `hub()` after each solution and at each restart. The hub + carries the last solution and the no-goods from one restart to the next. + RBS calls `spoke()` on each new spoke, a clone of the hub, including the + first. The default `hub()` constrains the hub with the last solution, if + any, and posts the no-goods. +- PBS calls `hub()` once, then calls `spoke()` with the asset number on the + spoke of each asset, the hub or a clone of it. The default `hub()` + removes all branchers, so `spoke()` must post the branching of each + asset. + +The examples show typical overrides: + +- `examples/crossword.cpp`: a `hub()` that posts no-goods and continues + after a solution instead of restarting. +- `examples/photo.cpp`: an RBS `spoke()` that relaxes the last solution for + large neighbourhood search. +- `examples/qcp.cpp`: a PBS `spoke()` that posts a different branching for + each asset. diff --git a/examples/crossword.cpp b/examples/crossword.cpp index d3c6075047..729d3bb116 100755 --- a/examples/crossword.cpp +++ b/examples/crossword.cpp @@ -282,7 +282,7 @@ class Crossword : public Script { << n; } /// Do not perform a restart when a solution is found - bool master(const MetaInfo& mi) { + bool hub(const MetaInfo& mi) { if (mi.type() == MetaInfo::RESTART) // Post no-goods mi.nogoods().post(*this); diff --git a/examples/job-shop.cpp b/examples/job-shop.cpp index fcfe95f182..d497ded4c3 100755 --- a/examples/job-shop.cpp +++ b/examples/job-shop.cpp @@ -700,16 +700,16 @@ solve(const JobShopOptions& opt) { { Support::Timer t; t.start(); Search::Statistics stat; - JobShopProbe* master = new JobShopProbe(opt); + JobShopProbe* root = new JobShopProbe(opt); - if (master->status() != SS_SOLVED) { - delete master; + if (root->status() != SS_SOLVED) { + delete root; std::cerr << "Error: has no solution..." << std::endl; return; } - l = master->cost().min(); - u = master->cost().max(); + l = root->cost().min(); + u = root->cost().max(); FailTimeStop fts(opt.fail_probe(),opt.time_probe()); CommonOptions so(opt); @@ -722,7 +722,7 @@ solve(const JobShopOptions& opt) { << std::endl; for (unsigned int p=0; p(master->clone()); + JobShopProbe* jsp = static_cast(root->clone()); jsp->branch(p,rnd); DFS dfs(jsp,so); JobShopProbe* s = dfs.next(); @@ -740,7 +740,7 @@ solve(const JobShopOptions& opt) { } stat += statj; } - delete master; + delete root; print(stat,false); std::cout << "\t\t\truntime: "; @@ -756,11 +756,11 @@ solve(const JobShopOptions& opt) { // Dichotomic search { - JobShopSolve* master = new JobShopSolve(opt); + JobShopSolve* model = new JobShopSolve(opt); - if (master->status() == SS_FAILED) { - delete master; + if (model->status() == SS_FAILED) { + delete model; std::cerr << "Error: has no solution..." << std::endl; return; } @@ -775,7 +775,7 @@ solve(const JobShopOptions& opt) { while (l < u) { std::cout << "\t\tBounds: [" << l << "," << u << "]" << std::endl; - JobShopSolve* jss = static_cast(master->clone()); + JobShopSolve* jss = static_cast(model->clone()); int m = (l + u) / 2; rel(*jss, jss->cost() >= l); rel(*jss, jss->cost() <= m); @@ -810,7 +810,7 @@ solve(const JobShopOptions& opt) { } if (l == u) { - delete master; + delete model; std::cout << std::endl << "\tFound best solution and proved optimality." << std::endl; @@ -822,15 +822,15 @@ solve(const JobShopOptions& opt) { Support::Timer t; t.start(); std::cout << std::endl << "\tSolving..." << std::endl; - rel(*master, master->cost() >= l); - rel(*master, master->cost() < u); + rel(*model, model->cost() >= l); + rel(*model, model->cost() < u); CommonOptions so(opt); if (opt.time_solve() > 0U) so.stop = Search::Stop::time(opt.time_solve()); so.cutoff = Search::Cutoff::geometric(JobShopConfig::restart_scale, JobShopConfig::restart_base); - RBS rbs(master,so); + RBS rbs(model,so); while (JobShopSolve* s = rbs.next()) { s->print(std::cout); u = s->cost().val(); diff --git a/examples/photo.cpp b/examples/photo.cpp index ed264158c5..15a19e3dd1 100644 --- a/examples/photo.cpp +++ b/examples/photo.cpp @@ -128,8 +128,8 @@ class Photo : public IntMinimizeScript { break; } } - /// Slave function for restarts - bool slave(const MetaInfo& mi) { + /// Spoke function for restarts + bool spoke(const MetaInfo& mi) { if ((mi.type() == MetaInfo::RESTART) && (mi.restart() > 0) && (p > 0.0)) { const Photo& l = static_cast(*mi.last()); diff --git a/examples/qcp.cpp b/examples/qcp.cpp index 49a75d5f92..b74d144b3f 100644 --- a/examples/qcp.cpp +++ b/examples/qcp.cpp @@ -211,8 +211,8 @@ class QCP : public Script { } } } - /// Slave function for portfolio search - virtual bool slave(const MetaInfo& mi) { + /// Spoke function for portfolio search + virtual bool spoke(const MetaInfo& mi) { if (mi.type() == MetaInfo::PORTFOLIO) { double tbf = opt.tbf(); Rnd r(seeds[mi.asset() % n_seeds]); diff --git a/gecode/flatzinc.hh b/gecode/flatzinc.hh index 1d46712726..78b2039c63 100755 --- a/gecode/flatzinc.hh +++ b/gecode/flatzinc.hh @@ -682,8 +682,8 @@ namespace Gecode { namespace FlatZinc { virtual void constrain(const Space& s); /// Copy function virtual Gecode::Space* copy(void); - /// Slave function for restarts - virtual bool slave(const MetaInfo& mi); + /// Spoke function for restarts + virtual bool spoke(const MetaInfo& mi); /// \name AST to variable and value conversion //@{ diff --git a/gecode/flatzinc/flatzinc.cpp b/gecode/flatzinc/flatzinc.cpp index cbe0a360a8..c7cffdb5c7 100644 --- a/gecode/flatzinc/flatzinc.cpp +++ b/gecode/flatzinc/flatzinc.cpp @@ -2127,7 +2127,7 @@ namespace Gecode { namespace FlatZinc { } bool - FlatZincSpace::slave(const MetaInfo& mi) { + FlatZincSpace::spoke(const MetaInfo& mi) { if (mi.type() == MetaInfo::RESTART) { if (restart_data.initialized() && restart_data().mark_complete) { // Fail the space diff --git a/gecode/kernel/core.cpp b/gecode/kernel/core.cpp index 21b8584bfb..e5407336c0 100644 --- a/gecode/kernel/core.cpp +++ b/gecode/kernel/core.cpp @@ -902,7 +902,7 @@ namespace Gecode { } bool - Space::master(const MetaInfo& mi) { + Space::hub(const MetaInfo& mi) { switch (mi.type()) { case MetaInfo::RESTART: if (mi.last() != nullptr) @@ -920,7 +920,7 @@ namespace Gecode { } bool - Space::slave(const MetaInfo&) { + Space::spoke(const MetaInfo&) { return true; } diff --git a/gecode/kernel/core.hpp b/gecode/kernel/core.hpp index 00ceb07ec1..548efb571c 100755 --- a/gecode/kernel/core.hpp +++ b/gecode/kernel/core.hpp @@ -1730,8 +1730,8 @@ namespace Gecode { /** * \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. + * The no-goods are empty if the search on the spoke space was + * incomplete, as they only hold in the spoke's search space. */ const NoGoods& nogoods(void) const; //@} @@ -2192,62 +2192,70 @@ namespace Gecode { GECODE_KERNEL_EXPORT virtual SpaceComparison compare(const Space& other) const; /** - * \brief Master configuration function for meta search engines + * \brief Hub configuration function for meta search engines * - * This configuration function is used by both restart and - * portfolio meta search engines. + * \li Restart-based search (RBS) calls this function on its hub + * after each solution and before each restart. \a mi + * describes the restart. After a solution, returning true + * restarts search from a new spoke, and returning false + * continues the current search without calling spoke(). At a + * restart caused by the cutoff or by an incomplete spoke being + * exhausted, the result is ignored and search restarts. + * Whatever the result, if the hub fails after the call, RBS + * ends. * - * \li A restart meta search engine calls this function on its - * master space whenever it finds a solution or exploration - * restarts. \a mi contains information about the current restart. + * The default function constrains the hub with the last + * solution, if any, posts the no-goods from \a mi, and + * returns true. * - * If a solution has been found, then search will continue with - * a restart id the function returns true, otherwise search - * will continue. + * \li Portfolio-based search (PBS) calls this function once on + * its hub before creating the assets. The result is ignored. * - * The default function posts no-goods obtained from \a mi. - * - * \li A portfolio meta engine calls this function once on - * the master space. The return value is ignored. - * - * The default function does nothing. + * The default function removes all branchers, so the spoke() + * function must post the branching for each asset. * * \ingroup TaskModelScript */ GECODE_KERNEL_EXPORT - virtual bool master(const MetaInfo& mi); + virtual bool hub(const MetaInfo& mi); /** - * \brief Slave configuration function for meta search engines - * - * This configuration function is used by both restart and - * portfolio meta search engines. - * - * \li A restart meta search engine calls this function on its - * 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. This also holds for the first - * search when search starts. - * - * \li A portfolio meta engine calls this function once on each asset - * (that is, on each slave) and passes the number of the asset, - * starting from zero. + * \brief Spoke configuration function for meta search engines + * + * \li RBS calls this function on each spoke, a clone of the hub, + * before searching it: once when search starts and once at + * each restart. \a mi describes the restart. Returning true + * declares the search of the spoke complete: when it is + * exhausted, RBS finishes. Returning false declares it + * incomplete: when it is exhausted, RBS restarts. The no-goods + * recorded by an incomplete search only hold in the spoke's + * search space, so RBS does not pass them to hub(). This also + * holds for the first search when search starts. + * + * \li PBS calls this function once on the spoke of each asset, + * the hub or a clone of it, and passes the asset number, + * starting from zero. The result is ignored. * * The default function does nothing and returns true. * * \ingroup TaskModelScript */ GECODE_KERNEL_EXPORT - virtual bool slave(const MetaInfo& mi); + virtual bool spoke(const MetaInfo& mi); + + /** + * \brief Renamed to hub() in Gecode 7 + * + * Deleted so that a Gecode 6 override, with or without + * \c override, fails to compile instead of never being called. + */ + virtual bool master(const MetaInfo& mi) = delete; + /** + * \brief Renamed to spoke() in Gecode 7 + * + * Deleted so that a Gecode 6 override, with or without + * \c override, fails to compile instead of never being called. + */ + virtual bool slave(const MetaInfo& mi) = delete; /* * Member functions for search engines diff --git a/gecode/search.hh b/gecode/search.hh index f4467897ab..05d2a8057c 100755 --- a/gecode/search.hh +++ b/gecode/search.hh @@ -1456,14 +1456,13 @@ namespace Gecode { * periodically restart the search of engine \a E. * * The class \a T can implement member functions - * \code virtual bool master(const MetaInfo& mi) \endcode + * \code virtual bool hub(const MetaInfo& mi) \endcode * and - * \code virtual bool slave(const MetaInfo& mi) \endcode + * \code virtual bool spoke(const MetaInfo& mi) \endcode * - * Whenever exploration restarts or a solution is found, the - * engine executes the functions on the master and slave - * space. For more details, consult "Modeling and Programming - * with Gecode". + * The engine calls hub() on its hub after solutions and at restarts, + * and spoke() on each spoke it clones from the hub. See Space::hub() + * and Space::spoke() for details. * * \ingroup TaskModelSearch */ @@ -1484,14 +1483,13 @@ namespace Gecode { * periodically restart the search of engine \a E. * * The class \a T can implement member functions - * \code virtual bool master(const MetaInfo& mi) \endcode + * \code virtual bool hub(const MetaInfo& mi) \endcode * and - * \code virtual bool slave(const MetaInfo& mi) \endcode + * \code virtual bool spoke(const MetaInfo& mi) \endcode * - * Whenever exploration restarts or a solution is found, the - * engine executes the functions on the master and slave - * space. For more details, consult "Modeling and Programming - * with Gecode". + * The engine calls hub() on its hub after solutions and at restarts, + * and spoke() on each spoke it clones from the hub. See Space::hub() + * and Space::spoke() for details. * * \ingroup TaskModelSearch */ @@ -1510,22 +1508,22 @@ namespace Gecode { namespace Search { namespace Meta { /// Build a sequential engine template class E> - Engine* sequential(T* master, const Search::Statistics& stat, Options& opt); + Engine* sequential(T* hub, const Search::Statistics& stat, Options& opt); /// Build a sequential engine template class E> - Engine* sequential(T* master, SEBs& sebs, + Engine* sequential(T* hub, SEBs& sebs, const Search::Statistics& stat, Options& opt, bool best); #ifdef GECODE_HAS_THREADS /// Build a parallel engine template class E> - Engine* parallel(T* master, const Search::Statistics& stat, Options& opt); + Engine* parallel(T* hub, const Search::Statistics& stat, Options& opt); /// Build a parallel engine template class E> - Engine* parallel(T* master, SEBs& sebs, + Engine* parallel(T* hub, SEBs& sebs, const Search::Statistics& stat, Options& opt, bool best); #endif @@ -1552,12 +1550,13 @@ namespace Gecode { * 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 + * \code virtual bool hub(const MetaInfo& mi) \endcode * and - * \code virtual bool slave(const MetaInfo& mi) \endcode + * \code virtual bool spoke(const MetaInfo& mi) \endcode * - * When the assets are created, these functions are executed. - * For more details, consult "Modeling and Programming with Gecode". + * The engine calls hub() once on its hub and then spoke() on the + * spoke of each asset. See Space::hub() and Space::spoke() for + * details. * * \ingroup TaskModelSearch */ @@ -1601,12 +1600,13 @@ namespace Gecode { * asset. PBS does not implement worker-allocation policy. * * The class \a T can implement member functions - * \code virtual bool master(const MetaInfo& mi) \endcode + * \code virtual bool hub(const MetaInfo& mi) \endcode * and - * \code virtual bool slave(const MetaInfo& mi) \endcode + * \code virtual bool spoke(const MetaInfo& mi) \endcode * - * When the assets are created, these functions are executed. - * For more details, consult "Modeling and Programming with Gecode". + * The engine calls hub() once on its hub and then spoke() on the + * spoke of each asset. See Space::hub() and Space::spoke() for + * details. * * \ingroup TaskModelSearch */ diff --git a/gecode/search/par/bab.hpp b/gecode/search/par/bab.hpp index 0f658098b1..e048391bf0 100755 --- a/gecode/search/par/bab.hpp +++ b/gecode/search/par/bab.hpp @@ -488,7 +488,7 @@ namespace Gecode { namespace Search { namespace Par { // Wait for reset cycle stopped e_reset_ack_stop.wait(); // A worker can fail after next() returns. Report that failure before - // a restart engine invokes its master callback and resets this engine. + // a restart engine invokes its hub callback and resets this engine. Support::Lock lock(m_search); if (failure != nullptr) std::rethrow_exception(failure); diff --git a/gecode/search/par/pbs.hh b/gecode/search/par/pbs.hh index cf73f5c839..fb180a69f0 100644 --- a/gecode/search/par/pbs.hh +++ b/gecode/search/par/pbs.hh @@ -44,10 +44,10 @@ namespace Gecode { namespace Search { namespace Par { - /// Stop object used for controlling slaves in a portfolio + /// Stop object used for controlling assets in a portfolio class GECODE_SEARCH_EXPORT PortfolioStop : public Stop { private: - /// The stop object for the slaves + /// The stop object for the assets Stop* so; /// The stop object for the portfolio (possibly nullptr) Stop* po; @@ -56,7 +56,7 @@ namespace Gecode { namespace Search { namespace Par { /// Whether search must be stopped std::atomic* tostop; public: - /// Initialize with slave stop \a so, portfolio stop \a po, and \a control + /// Initialize with asset 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); @@ -74,9 +74,9 @@ namespace Gecode { namespace Search { namespace Par { template class PBS; - /// Runnable slave of a portfolio master + /// Runnable asset in a portfolio template - class GECODE_SEARCH_EXPORT Slave : public Support::Runnable { + class GECODE_SEARCH_EXPORT Asset : public Support::Runnable { protected: /// Completion event for the current run class Completion : public Support::Terminator { @@ -91,33 +91,33 @@ namespace Gecode { namespace Search { namespace Par { /// Wait for completion and consume the signal void wait(void); } completion; - /// The master engine - PBS* master; - /// The slave engine - Engine* slave; + /// The portfolio engine + PBS* portfolio; + /// The wrapped search engine + Engine* engine; /// Stop object Stop* stop; public: - /// Initialize with master \a m, slave \a s, and its stop object \a so - Slave(PBS* m, Engine* s, Stop* so); - /// Return statistics of slave + /// Initialize with portfolio \a p, engine \a e, and its stop object \a so + Asset(PBS* p, Engine* e, Stop* so); + /// Return statistics of asset Statistics statistics(void) const; - /// Check whether slave has been stopped + /// Check whether asset has been stopped bool stopped(void) const; - /// Check whether slave stopped at a local limit + /// Check whether asset stopped at a local limit bool limited(void) const; /// Return the completion terminator virtual Support::Terminator* terminator(void) const; - /// Wait for the slave to complete its current run + /// Wait for the asset to complete its current run void wait(void); - /// Wake a paused slave so that it can observe the portfolio stop + /// Wake a paused asset so that it can observe the portfolio stop void wake(void); /// Constrain with better solution \a b void constrain(const Space& b); /// Perform one run virtual void run(void); - /// Delete slave - virtual ~Slave(void); + /// Delete asset + virtual ~Asset(void); }; /// Collect all solutions @@ -131,13 +131,13 @@ namespace Gecode { namespace Search { namespace Par { /// Initialize CollectAll(void); /// Add a solution \a a reported by \a r and always return true - bool add(Space* s, Slave* r); + bool add(Space* s, Asset* r); /// Dummy function bool constrain(const Space& b); /// Check whether there is any solution left bool empty(void) const; /// Return solution reported by \a r - Space* get(Slave*& r); + Space* get(Asset*& r); /// Destructor ~CollectAll(void); }; @@ -148,20 +148,20 @@ namespace Gecode { namespace Search { namespace Par { /// Currently best solution Space* b; /// Who has reported the best solution (nullptr if solution has already been reported) - Slave* reporter; + Asset* reporter; public: /// Whether it collects best solutions static const bool best = true; /// Initialize CollectBest(void); /// Add a solution \a s by \a r and return whether is was better - bool add(Space* s, Slave* r); + bool add(Space* s, Asset* r); /// Check whether \a b better and update accordingly bool constrain(const Space& b); /// Check whether there is any solution left bool empty(void) const; /// Return solution reported by \a r (only if a better one was found) - Space* get(Slave*& r); + Space* get(Asset*& r); /// Destructor ~CollectBest(void); }; @@ -169,19 +169,19 @@ namespace Gecode { namespace Search { namespace Par { /// Parallel portfolio engine implementation template class GECODE_SEARCH_EXPORT PBS : public Engine { - friend class Slave; + friend class Asset; protected: - /// Master statistics + /// Statistics of propagating the model Statistics stat; - /// Slave engines - Slave** slaves; - /// Number of slave engines - unsigned int n_slaves; - /// Number of active slave engines + /// Asset engines + Asset** assets; + /// Number of asset engines + unsigned int n_assets; + /// Number of active asset engines unsigned int n_active; - /// Whether a slave has been stopped - std::atomic slave_stop; - /// Whether a slave stopped at a local limit + /// Whether an asset has been stopped + std::atomic asset_stop; + /// Whether an asset stopped at a local limit bool incomplete; /// Shared stop flag std::atomic tostop; @@ -189,23 +189,23 @@ namespace Gecode { namespace Search { namespace Par { Collect solutions; /// Mutex for synchronization Support::Mutex m; - /// Number of busy slaves + /// Number of busy assets unsigned int n_busy; - /// Signal that number of busy slaves becomes zero + /// Signal that number of busy assets becomes zero Support::Event idle; /// Failure raised while running or updating a portfolio asset std::exception_ptr failure; - /// Process report from slave, return false if solution was ignored - bool report(Slave* slave, Space* s); - /// Process failure from slave + /// Process report from asset, return false if solution was ignored + bool report(Asset* asset, Space* s); + /// Process failure from asset void fail(void); /** * The key invariant of the engine is as follows: * - n_busy is always zero outside the next() function. * - that entails, that locking is only needed inside next(). - * - the slaves 0..n_active-1 still might not have exhausted their + * - the assets 0..n_active-1 still might not have exhausted their * search space. - * - the slaves n_active..n_slaves-1 have exhausted their search space. + * - the assets n_active..n_assets-1 have exhausted their search space. */ public: /// Initialize diff --git a/gecode/search/par/pbs.hpp b/gecode/search/par/pbs.hpp index c9d97fc779..fde4156058 100755 --- a/gecode/search/par/pbs.hpp +++ b/gecode/search/par/pbs.hpp @@ -41,8 +41,8 @@ namespace Gecode { namespace Search { namespace Par { template forceinline - Slave::Completion::Completion(void) { - // A slave has not been submitted yet. This initial signal lets the + Asset::Completion::Completion(void) { + // An asset has not been submitted yet. This initial signal lets the // first submission use the same consume-before-reuse handshake as all // subsequent submissions. done.signal(); @@ -50,13 +50,13 @@ namespace Gecode { namespace Search { namespace Par { template forceinline void - Slave::Completion::terminated(void) { + Asset::Completion::terminated(void) { done.signal(); } template forceinline void - Slave::Completion::wait(void) { + Asset::Completion::wait(void) { done.wait(); } @@ -65,7 +65,7 @@ namespace Gecode { namespace Search { namespace Par { CollectAll::CollectAll(void) : solutions(heap) {} forceinline bool - CollectAll::add(Space* s, Slave*) { + CollectAll::add(Space* s, Asset*) { solutions.push(s); return true; } @@ -79,7 +79,7 @@ namespace Gecode { namespace Search { namespace Par { return solutions.empty(); } forceinline Space* - CollectAll::get(Slave*&) { + CollectAll::get(Asset*&) { return solutions.pop(); } forceinline @@ -93,7 +93,7 @@ namespace Gecode { namespace Search { namespace Par { CollectBest::CollectBest(void) : b(nullptr), reporter(nullptr) {} forceinline bool - CollectBest::add(Space* s, Slave* r) { + CollectBest::add(Space* s, Asset* r) { if ((b != nullptr) && !Search::better(*s,*b,"PBS::CollectBest::add")) { delete s; @@ -119,7 +119,7 @@ namespace Gecode { namespace Search { namespace Par { return reporter == nullptr; } forceinline Space* - CollectBest::get(Slave*& r) { + CollectBest::get(Asset*& r) { assert(!empty()); r = reporter; reporter = nullptr; @@ -144,50 +144,50 @@ namespace Gecode { namespace Search { namespace Par { template forceinline - Slave::Slave(PBS* m, Engine* s, Stop* so) - : Support::Runnable(false), master(m), slave(s), stop(so) {} + Asset::Asset(PBS* p, Engine* e, Stop* so) + : Support::Runnable(false), portfolio(p), engine(e), stop(so) {} template forceinline Statistics - Slave::statistics(void) const { - return slave->statistics(); + Asset::statistics(void) const { + return engine->statistics(); } template forceinline bool - Slave::stopped(void) const { - return slave->stopped(); + Asset::stopped(void) const { + return engine->stopped(); } template forceinline bool - Slave::limited(void) const { + Asset::limited(void) const { #ifdef GECODE_HAS_EXPERIMENTAL - return slave->limited(); + return engine->limited(); #else return false; #endif } template forceinline Support::Terminator* - Slave::terminator(void) const { + Asset::terminator(void) const { return const_cast(&completion); } template forceinline void - Slave::wait(void) { + Asset::wait(void) { completion.wait(); } template forceinline void - Slave::wake(void) { + Asset::wake(void) { static_cast(stop)->wake(); } template forceinline void - Slave::constrain(const Space& b) { - slave->constrain(b); + Asset::constrain(const Space& b) { + engine->constrain(b); } template - Slave::~Slave(void) { - delete slave; + Asset::~Asset(void) { + delete engine; delete stop; } @@ -197,12 +197,12 @@ namespace Gecode { namespace Search { namespace Par { forceinline PBS::PBS(Engine** engines, Stop** stops, unsigned int n, const Statistics& stat0) - : stat(stat0), slaves(heap.alloc*>(n)), - n_slaves(n), n_active(n), - slave_stop(false), incomplete(false), tostop(false), n_busy(0) { - // Initialize slaves - for (unsigned int i=0U; i(this,engines[i],stops[i]); + : stat(stat0), assets(heap.alloc*>(n)), + n_assets(n), n_active(n), + asset_stop(false), incomplete(false), tostop(false), n_busy(0) { + // Initialize assets + for (unsigned int i=0U; i(this,engines[i],stops[i]); static_cast(stops[i])->share(&tostop); } } @@ -210,14 +210,14 @@ namespace Gecode { namespace Search { namespace Par { template forceinline bool - PBS::report(Slave* slave, Space* s) { + PBS::report(Asset* asset, Space* s) { // If b is false the report should be repeated (solution was worse) bool b = true; bool wake = false; m.acquire(); if (s != nullptr) { try { - b = solutions.add(s,slave); + b = solutions.add(s,asset); if (b) { tostop.store(true, std::memory_order_release); wake = true; @@ -229,29 +229,29 @@ namespace Gecode { namespace Search { namespace Par { tostop.store(true, std::memory_order_release); wake = true; } - } else if (slave->stopped() && !slave->limited()) { + } else if (asset->stopped() && !asset->limited()) { if (!tostop.load(std::memory_order_acquire)) { - slave_stop.store(true, std::memory_order_release); + asset_stop.store(true, std::memory_order_release); tostop.store(true, std::memory_order_release); wake = true; } } else { - // The slave is done or stopped at a local limit (then search is incomplete) - if (slave->limited()) + // The asset is done or stopped at a local limit (then search is incomplete) + if (asset->limited()) incomplete = true; - // Move slave to inactive + // Move asset to inactive unsigned int i=0; - while (slaves[i] != slave) + while (assets[i] != asset) i++; assert(i < n_active); assert(n_active > 0); - std::swap(slaves[i],slaves[--n_active]); + std::swap(assets[i],assets[--n_active]); tostop.store(true, std::memory_order_release); wake = true; } if (wake) for (unsigned int i=0U; iwake(); + assets[i]->wake(); if (b) { if (--n_busy == 0) idle.signal(); @@ -268,7 +268,7 @@ namespace Gecode { namespace Search { namespace Par { failure = std::current_exception(); tostop.store(true, std::memory_order_release); for (unsigned int i=0U; iwake(); + assets[i]->wake(); if (--n_busy == 0) idle.signal(); m.release(); @@ -276,14 +276,14 @@ namespace Gecode { namespace Search { namespace Par { template void - Slave::run(void) { + Asset::run(void) { try { Space* s; do { - s = slave->next(); - } while (!master->report(this,s)); + s = engine->next(); + } while (!portfolio->report(this,s)); } catch (...) { - master->fail(); + portfolio->fail(); } } @@ -297,35 +297,35 @@ namespace Gecode { namespace Search { namespace Par { std::rethrow_exception(f); } if (solutions.empty()) - slave_stop.store(false, std::memory_order_release); + asset_stop.store(false, std::memory_order_release); while (solutions.empty() && (n_active > 0) && (failure == nullptr) && - !slave_stop.load(std::memory_order_acquire)) { - // Clear the internal stop used to interrupt sibling slaves + !asset_stop.load(std::memory_order_acquire)) { + // Clear the internal stop used to interrupt sibling assets tostop.store(false, std::memory_order_release); - // Invariant: all slaves are idle! + // Invariant: all assets are idle! assert(n_busy == 0); assert(!tostop.load(std::memory_order_acquire)); - // Run all active slaves + // Run all active assets n_busy = n_active; for (unsigned int i=0U; iwait(); - Support::Thread::run(slaves[i]); + assets[i]->wait(); + Support::Thread::run(assets[i]); } m.release(); - // Wait for all slaves to become idle + // Wait for all assets to become idle idle.wait(); m.acquire(); } - // Invariant all slaves are idle! + // Invariant all assets are idle! assert(n_busy == 0); if (solutions.empty() && (n_active == 0) && incomplete) - slave_stop.store(true, std::memory_order_release); + asset_stop.store(true, std::memory_order_release); // Report a stored failure before broadcasting a solution, which could // call into the failed asset and replace the original exception. @@ -341,13 +341,13 @@ namespace Gecode { namespace Search { namespace Par { if (solutions.empty()) { s = nullptr; } else { - Slave* r; + Asset* r; s = solutions.get(r); if (Collect::best) { try { for (unsigned int i=0U; iconstrain(*s); + if (assets[i] != r) + assets[i]->constrain(*s); } catch (...) { delete s; failure = std::current_exception(); @@ -365,7 +365,7 @@ namespace Gecode { namespace Search { namespace Par { template bool PBS::stopped(void) const { - return slave_stop.load(std::memory_order_acquire); + return asset_stop.load(std::memory_order_acquire); } template @@ -373,8 +373,8 @@ namespace Gecode { namespace Search { namespace Par { PBS::statistics(void) const { assert(n_busy == 0); Statistics s(stat); - for (unsigned int i=0U; istatistics(); + for (unsigned int i=0U; istatistics(); return s; } @@ -388,7 +388,7 @@ namespace Gecode { namespace Search { namespace Par { if (solutions.constrain(b)) { // The solution is better for (unsigned int i=0U; iconstrain(b); + assets[i]->constrain(b); } } catch (...) { failure = std::current_exception(); @@ -398,15 +398,15 @@ namespace Gecode { namespace Search { namespace Par { template PBS::~PBS(void) { - // A report can make n_busy zero before Slave::run and Thread::Run::exec + // A report can make n_busy zero before Asset::run and Thread::Run::exec // have returned. Wait for the completion handshake before deleting the - // PBS-owned slaves. - for (unsigned int i=0U; iwait(); + // PBS-owned assets. + for (unsigned int i=0U; iwait(); assert(n_busy == 0); - for (unsigned int i=0U; i*>(slaves,n_slaves); + for (unsigned int i=0U; i*>(assets,n_assets); } }}} diff --git a/gecode/search/pbs.cpp b/gecode/search/pbs.cpp index b52ce8e269..70116536ac 100644 --- a/gecode/search/pbs.cpp +++ b/gecode/search/pbs.cpp @@ -46,12 +46,12 @@ namespace Gecode { namespace Search { namespace Seq { } Engine* - pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, + pbsengine(Engine** assets, Stop** stops, unsigned int n_assets, const Statistics& stat, const Search::Options& opt, bool best) { if (best) - return new PBS(slaves,stops,n_slaves,stat,opt); + return new PBS(assets,stops,n_assets,stat,opt); else - return new PBS(slaves,stops,n_slaves,stat,opt); + return new PBS(assets,stops,n_assets,stat,opt); } }}} @@ -73,12 +73,12 @@ namespace Gecode { namespace Search { namespace Par { } Engine* - pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, + pbsengine(Engine** assets, Stop** stops, unsigned int n_assets, const Statistics& stat, bool best) { if (best) - return new PBS(slaves,stops,n_slaves,stat); + return new PBS(assets,stops,n_assets,stat); else - return new PBS(slaves,stops,n_slaves,stat); + return new PBS(assets,stops,n_assets,stat); } }}} diff --git a/gecode/search/pbs.hpp b/gecode/search/pbs.hpp index 720787eac0..b5d2a67c9e 100644 --- a/gecode/search/pbs.hpp +++ b/gecode/search/pbs.hpp @@ -40,13 +40,13 @@ namespace Gecode { namespace Search { /// Dispose partially constructed portfolio assets forceinline void - pbscleanup(Engine** slaves, Stop** stops, unsigned int n_slaves, - Space* master) { - for (unsigned int i=0U; i forceinline PbsCutoffs* - make_pbs_cutoffs(const Options& opt, unsigned int n_slaves) { + make_pbs_cutoffs(const Options& opt, unsigned int n_assets) { if (!PbsOwnsCutoff::value || (opt.cutoff == nullptr) || - (n_slaves < 2U)) + (n_assets < 2U)) return nullptr; return new PbsCutoffs(opt.cutoff); } @@ -272,7 +272,7 @@ namespace Gecode { namespace Search { namespace Seq { /// Create sequential portfolio engine GECODE_SEARCH_EXPORT Engine* - pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, + pbsengine(Engine** assets, Stop** stops, unsigned int n_assets, const Statistics& stat, const Search::Options& opt, bool best); }}} @@ -289,7 +289,7 @@ namespace Gecode { namespace Search { namespace Par { /// Create parallel portfolio engine GECODE_SEARCH_EXPORT Engine* - pbsengine(Engine** slaves, Stop** stops, unsigned int n_slaves, + pbsengine(Engine** assets, Stop** stops, unsigned int n_assets, const Statistics& stat, bool best); }}} @@ -298,7 +298,7 @@ namespace Gecode { namespace Search { template class E> Engine* - pbsseq(T* master, const Search::Statistics& stat, Options& opt) { + pbsseq(T* hub, const Search::Statistics& stat, Options& opt) { Stop* stop = opt.stop; // Retain each seed: a throwing nested builder may consume its own clone. opt.clone = true; @@ -308,42 +308,42 @@ namespace Gecode { namespace Search { opt.threads = std::max(floor(opt.threads / static_cast(opt.assets)),1.0); - unsigned int n_slaves = opt.assets; - Engine** slaves = r.alloc(n_slaves); - Stop** stops = r.alloc(n_slaves); - for (unsigned int i=0U; i(n_assets); + Stop** stops = r.alloc(n_assets); + for (unsigned int i=0U; i >(opt,n_slaves); + Space* unowned_hub = hub; + PbsCutoffs* cutoffs = make_pbs_cutoffs >(opt,n_assets); try { - for (unsigned int i=0U; i slave((i == n_slaves-1) ? - master : master->clone()); - if (slave.get() == master) - unowned_master = nullptr; - (void) slave->slave(i); + std::unique_ptr spoke((i == n_assets-1) ? + hub : hub->clone()); + if (spoke.get() == hub) + unowned_hub = nullptr; + (void) spoke->spoke(i); std::unique_ptr cutoff; if (cutoffs != nullptr) { cutoff.reset(new PbsCutoff(cutoffs)); opt.cutoff = cutoff.get(); } - slaves[i] = build(slave.get(),opt); + assets[i] = build(spoke.get(),opt); // The asset owns its cutoff (void) cutoff.release(); } Engine* e = Seq::pbsengine( - slaves,stops,n_slaves,stat,opt,E::best); + assets,stops,n_assets,stat,opt,E::best); release_pbs_cutoffs(cutoffs,true); return e; } catch (...) { - pbscleanup(slaves,stops,n_slaves,unowned_master); + pbscleanup(assets,stops,n_assets,unowned_hub); release_pbs_cutoffs(cutoffs,false); throw; } @@ -351,46 +351,46 @@ namespace Gecode { namespace Search { template class E> Engine* - pbsseq(T* master, SEBs& sebs, + pbsseq(T* hub, SEBs& sebs, const Search::Statistics& stat, Options& opt, bool best) { Region r; - int n_slaves = sebs.size(); - Engine** slaves = r.alloc(n_slaves); - Stop** stops = r.alloc(n_slaves); - for (int i=0; i(n_assets); + Stop** stops = r.alloc(n_assets); + for (int i=0; i(n_slaves)); + static_cast(n_assets)); - Space* unowned_master = master; + Space* unowned_hub = hub; bool builders_owned = true; try { - for (int i=0; ioptions().stop,opt.stop); sebs[i]->options().stop = stops[i]; sebs[i]->options().clone = true; - std::unique_ptr slave((i == n_slaves-1) ? - master : master->clone()); - if (slave.get() == master) - unowned_master = nullptr; - (void) slave->slave(static_cast(i)); - slaves[i] = (*sebs[i])(slave.get()); + std::unique_ptr spoke((i == n_assets-1) ? + hub : hub->clone()); + if (spoke.get() == hub) + unowned_hub = nullptr; + (void) spoke->spoke(static_cast(i)); + assets[i] = (*sebs[i])(spoke.get()); } pbscleanup(sebs); builders_owned = false; return Seq::pbsengine( - slaves,stops,static_cast(n_slaves),stat,opt,best); + assets,stops,static_cast(n_assets),stat,opt,best); } catch (...) { if (builders_owned) pbscleanup(sebs); - pbscleanup(slaves,stops,static_cast(n_slaves), - unowned_master); + pbscleanup(assets,stops,static_cast(n_assets), + unowned_hub); throw; } } @@ -399,52 +399,52 @@ namespace Gecode { namespace Search { template class E> Engine* - pbspar(T* master, const Search::Statistics& stat, Options& opt) { + pbspar(T* hub, const Search::Statistics& stat, Options& opt) { Stop* stop = opt.stop; // Retain each seed: a throwing nested builder may consume its own clone. opt.clone = true; Region r; - // Limit the number of slaves to the number of threads - unsigned int n_slaves = std::min(static_cast(opt.threads), + // Limit the number of assets to the number of threads + unsigned int n_assets = std::min(static_cast(opt.threads), opt.assets); - // Redistribute additional threads to slaves - opt.threads = floor(opt.threads / static_cast(n_slaves)); + // Redistribute additional threads to assets + opt.threads = floor(opt.threads / static_cast(n_assets)); WrapTraceRecorder::engine(opt.tracer, - SearchTracer::EngineType::PBS, n_slaves); + SearchTracer::EngineType::PBS, n_assets); - Engine** slaves = r.alloc(n_slaves); - Stop** stops = r.alloc(n_slaves); - for (unsigned int i=0U; i(n_assets); + Stop** stops = r.alloc(n_assets); + for (unsigned int i=0U; i >(opt,n_slaves); + Space* unowned_hub = hub; + PbsCutoffs* cutoffs = make_pbs_cutoffs >(opt,n_assets); try { - for (unsigned int i=0U; i slave((i == n_slaves-1) ? - master : master->clone()); - if (slave.get() == master) - unowned_master = nullptr; - (void) slave->slave(static_cast(i)); + std::unique_ptr spoke((i == n_assets-1) ? + hub : hub->clone()); + if (spoke.get() == hub) + unowned_hub = nullptr; + (void) spoke->spoke(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); + assets[i] = build(spoke.get(),opt); // The asset owns its cutoff (void) cutoff.release(); } - Engine* e = Par::pbsengine(slaves,stops,n_slaves,stat,E::best); + Engine* e = Par::pbsengine(assets,stops,n_assets,stat,E::best); release_pbs_cutoffs(cutoffs,true); return e; } catch (...) { - pbscleanup(slaves,stops,n_slaves,unowned_master); + pbscleanup(assets,stops,n_assets,unowned_hub); release_pbs_cutoffs(cutoffs,false); throw; } @@ -452,50 +452,50 @@ namespace Gecode { namespace Search { template class E> Engine* - pbspar(T* master, SEBs& sebs, + pbspar(T* hub, SEBs& sebs, const Search::Statistics& stat, Options& opt, bool best) { Region r; - // Limit the number of slaves to the number of threads - int n_slaves = std::min(static_cast(opt.threads), + // Limit the number of assets to the number of threads + int n_assets = std::min(static_cast(opt.threads), sebs.size()); WrapTraceRecorder::engine(opt.tracer, SearchTracer::EngineType::PBS, - static_cast(n_slaves)); + static_cast(n_assets)); - Engine** slaves = r.alloc(n_slaves); - Stop** stops = r.alloc(n_slaves); - for (int i=0; i(n_assets); + Stop** stops = r.alloc(n_assets); + for (int i=0; ioptions().stop,opt.stop, sebs[i]->options().worker_control); sebs[i]->options().stop = stops[i]; sebs[i]->options().clone = true; - std::unique_ptr slave((i == n_slaves-1) ? - master : master->clone()); - if (slave.get() == master) - unowned_master = nullptr; - (void) slave->slave(static_cast(i)); - slaves[i] = (*sebs[i])(slave.get()); + std::unique_ptr spoke((i == n_assets-1) ? + hub : hub->clone()); + if (spoke.get() == hub) + unowned_hub = nullptr; + (void) spoke->spoke(static_cast(i)); + assets[i] = (*sebs[i])(spoke.get()); } pbscleanup(sebs); builders_owned = false; return Par::pbsengine( - slaves,stops,static_cast(n_slaves),stat,best); + assets,stops,static_cast(n_assets),stat,best); } catch (...) { if (builders_owned) pbscleanup(sebs); - pbscleanup(slaves,stops,static_cast(n_slaves), - unowned_master); + pbscleanup(assets,stops,static_cast(n_assets), + unowned_hub); throw; } } @@ -532,17 +532,17 @@ namespace Gecode { } // Check whether a clone must be used - T* master = opt.clone ? + T* hub = opt.clone ? dynamic_cast(s->clone()) : s; opt.clone = false; - // Always execute master function - (void) master->master(0); + // Always execute hub function + (void) hub->hub(0); - // No need to create a portfolio engine but must run slave function + // No need to create a portfolio engine but must run spoke function if (o.assets == 1) { - std::unique_ptr seed(master); - (void) master->slave(0); + std::unique_ptr seed(hub); + (void) hub->spoke(0); // Retain the seed if a nested builder rejects its worker control. opt.clone = true; e = Search::build(seed.get(),opt); @@ -551,10 +551,10 @@ namespace Gecode { #ifdef GECODE_HAS_THREADS if (opt.threads > 1.0) - e = Search::pbspar(master,stat,opt); + e = Search::pbspar(hub,stat,opt); else #endif - e = Search::pbsseq(master,stat,opt); + e = Search::pbsseq(hub,stat,opt); } template class E> @@ -586,19 +586,19 @@ namespace Gecode { } // Check whether a clone must be used - T* master = opt.clone ? + T* hub = opt.clone ? dynamic_cast(s->clone()) : s; opt.clone = false; - // Always execute master function - (void) master->master(0); + // Always execute hub function + (void) hub->hub(0); #ifdef GECODE_HAS_THREADS if (opt.threads > 1.0) - e = Search::pbspar(master,sebs,stat,opt,best); + e = Search::pbspar(hub,sebs,stat,opt,best); else #endif - e = Search::pbsseq(master,sebs,stat,opt,best); + e = Search::pbsseq(hub,sebs,stat,opt,best); } template class E> diff --git a/gecode/search/rbs.cpp b/gecode/search/rbs.cpp index 2069e577d0..1b78157317 100644 --- a/gecode/search/rbs.cpp +++ b/gecode/search/rbs.cpp @@ -41,16 +41,16 @@ namespace Gecode { namespace Search { namespace Seq { } Engine* - rbsengine(Space* master, Stop* stop, Engine* slave, + rbsengine(Space* hub, Stop* stop, Engine* engine, const Search::Statistics& stat, const Options& opt, bool best) { - return rbsengine(master,stop,slave,stat,opt,best,true); + return rbsengine(hub,stop,engine,stat,opt,best,true); } Engine* - rbsengine(Space* master, Stop* stop, Engine* slave, + rbsengine(Space* hub, Stop* stop, Engine* engine, const Search::Statistics& stat, const Options& opt, bool best, bool complete) { - return new RBS(master,static_cast(stop), slave, + return new RBS(hub,static_cast(stop), engine, stat,opt,best,complete); } diff --git a/gecode/search/rbs.hpp b/gecode/search/rbs.hpp index a992a3feb7..89ffef358a 100644 --- a/gecode/search/rbs.hpp +++ b/gecode/search/rbs.hpp @@ -43,21 +43,21 @@ namespace Gecode { namespace Search { namespace Seq { GECODE_SEARCH_EXPORT Stop* rbsstop(Stop* so); - /// Create restart engine whose first slave search yields valid no-goods + /// Create restart engine whose first spoke search yields valid no-goods GECODE_SEARCH_EXPORT Engine* - rbsengine(Space* master, Stop* stop, Engine* slave, + rbsengine(Space* hub, Stop* stop, Engine* engine, 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. + * The value \a complete is the result of the spoke configuration of the + * space explored by the first \a engine. It only determines whether + * the no-goods of that search hold in the hub. */ GECODE_SEARCH_EXPORT Engine* - rbsengine(Space* master, Stop* stop, Engine* slave, + rbsengine(Space* hub, Stop* stop, Engine* engine, const Search::Statistics& stat, const Options& opt, bool best, bool complete); @@ -112,15 +112,15 @@ namespace Gecode { // The engine owns the cutoff, but a dead engine does not take it delete m_opt.cutoff; } else { - std::unique_ptr master(m_opt.clone ? s->clone() : s); - std::unique_ptr slave(master->clone()); + std::unique_ptr hub(m_opt.clone ? s->clone() : s); + std::unique_ptr spoke(hub->clone()); MetaInfo mi(0,MetaInfo::RR_INIT,0,0,nullptr,NoGoods::eng); - bool complete = slave->slave(mi); + bool complete = spoke->spoke(mi); std::unique_ptr leaf( - Search::build(slave.get(),e_opt)); - e = Search::Seq::rbsengine(master.get(),stop.get(),leaf.get(), + Search::build(spoke.get(),e_opt)); + e = Search::Seq::rbsengine(hub.get(),stop.get(),leaf.get(), stat,m_opt,E::best,complete); - master.release(); + hub.release(); stop.release(); leaf.release(); } diff --git a/gecode/search/seq/pbs.hh b/gecode/search/seq/pbs.hh index e755548421..ef6cf4537b 100644 --- a/gecode/search/seq/pbs.hh +++ b/gecode/search/seq/pbs.hh @@ -47,17 +47,17 @@ namespace Gecode { namespace Search { namespace Seq { unsigned long long int l; }; - /// Stop object used for controlling slaves in a portfolio + /// Stop object used for controlling assets in a portfolio class GECODE_SEARCH_EXPORT PortfolioStop : public Stop { private: - /// The stop object for the slaves + /// The stop object for the assets Stop* so; /// The stop object for the portfolio (possibly nullptr) Stop* po; /// Pointer to shared stop information SharedStopInfo* ssi; public: - /// Initialize with slave stop \a so and portfolio stop \a po + /// Initialize with asset stop \a so and portfolio stop \a po PortfolioStop(Stop* so, Stop* po=nullptr); /// Initialize shared stop information void share(SharedStopInfo* ssi); @@ -65,61 +65,61 @@ namespace Gecode { namespace Search { namespace Seq { virtual bool stop(const Statistics& s, const Options& o); }; - /// Runnable slave of a portfolio master - class Slave { + /// Asset engine in a sequential portfolio + class Asset { protected: - /// The slave engine - Engine* slave; + /// The wrapped search engine + Engine* engine; /// Stop object Stop* stop; public: /// Default constructor - Slave(void); + Asset(void); /// Copy constructor - Slave(const Slave& s) = default; + Asset(const Asset& s) = default; /// Assignment operator - Slave& operator =(const Slave& s) = default; - /// Initialize with slave \a s and its stop object \a so - void init(Engine* s, Stop* so); + Asset& operator =(const Asset& s) = default; + /// Initialize with engine \a e and its stop object \a so + void init(Engine* e, Stop* so); /// Return next solution Space* next(void); - /// Return statistics of slave + /// Return statistics of asset Statistics statistics(void) const; - /// Check whether slave has been stopped + /// Check whether asset has been stopped bool stopped(void) const; - /// Check whether slave stopped at a local limit + /// Check whether asset stopped at a local limit bool limited(void) const; /// Constrain with better solution \a b void constrain(const Space& b); /// Perform one run void run(void); - /// Delete slave - ~Slave(void); + /// Delete asset + ~Asset(void); }; /// Sequential portfolio engine implementation template class GECODE_SEARCH_EXPORT PBS : public Engine { protected: - /// Master statistics + /// Statistics of propagating the model and of removed assets Statistics stat; - /// Shared slave information + /// Stop information shared by the assets SharedStopInfo ssi; /// Size of a slice unsigned int slice; - /// Slaves - Slave* slaves; - /// Number of slave engines - unsigned int n_slaves; - /// Current slave to run + /// Assets + Asset* assets; + /// Number of asset engines + unsigned int n_assets; + /// Current asset to run unsigned int cur; - /// Whether a slave has been stopped - bool slave_stop; - /// Whether a slave stopped at a local limit + /// Whether an asset has been stopped + bool asset_stop; + /// Whether an asset stopped at a local limit bool incomplete; public: /// Initialize - PBS(Engine** slaves, Stop** stops, unsigned int n, + PBS(Engine** assets, Stop** stops, unsigned int n, const Statistics& stat, const Search::Options& opt); /// Return next solution (nullptr, if none exists or search has been stopped) virtual Space* next(void); diff --git a/gecode/search/seq/pbs.hpp b/gecode/search/seq/pbs.hpp index e126eb2577..4164993c59 100644 --- a/gecode/search/seq/pbs.hpp +++ b/gecode/search/seq/pbs.hpp @@ -46,39 +46,39 @@ namespace Gecode { namespace Search { namespace Seq { forceinline - Slave::Slave(void) - : slave(nullptr), stop(nullptr) {} + Asset::Asset(void) + : engine(nullptr), stop(nullptr) {} forceinline void - Slave::init(Engine* e, Stop* s) { - slave = e; stop = s; + Asset::init(Engine* e, Stop* so) { + engine = e; stop = so; } forceinline Space* - Slave::next(void) { - return slave->next(); + Asset::next(void) { + return engine->next(); } forceinline Statistics - Slave::statistics(void) const { - return slave->statistics(); + Asset::statistics(void) const { + return engine->statistics(); } forceinline bool - Slave::stopped(void) const { - return slave->stopped(); + Asset::stopped(void) const { + return engine->stopped(); } forceinline bool - Slave::limited(void) const { + Asset::limited(void) const { #ifdef GECODE_HAS_EXPERIMENTAL - return slave->limited(); + return engine->limited(); #else return false; #endif } forceinline void - Slave::constrain(const Space& b) { - slave->constrain(b); + Asset::constrain(const Space& b) { + engine->constrain(b); } forceinline - Slave::~Slave(void) { - delete slave; + Asset::~Asset(void) { + delete engine; delete stop; } @@ -89,13 +89,13 @@ namespace Gecode { namespace Search { namespace Seq { const Statistics& stat0, const Search::Options& opt) : stat(stat0), slice(opt.slice), - slaves(heap.alloc(n)), n_slaves(n), cur(0), - slave_stop(false), incomplete(false) { + assets(heap.alloc(n)), n_assets(n), cur(0), + asset_stop(false), incomplete(false) { ssi.done = false; ssi.l = opt.slice; for (unsigned int i=0U; i(s[i])); + assets[i].init(e[i],static_cast(s[i])); static_cast(s[i])->share(&ssi); } } @@ -103,18 +103,18 @@ namespace Gecode { namespace Search { namespace Seq { template Space* PBS::next(void) { - slave_stop = false; + asset_stop = false; unsigned int n_exhausted = 0; - while (n_slaves > 0) { + while (n_assets > 0) { ssi.done = false; - if (Space* s = slaves[cur].next()) { - // Constrain other slaves + if (Space* s = assets[cur].next()) { + // Constrain other assets if (best) { try { for (unsigned int i=0U; i bool PBS::stopped(void) const { - return slave_stop; + return asset_stop; } template Statistics PBS::statistics(void) const { Statistics s(stat); - for (unsigned int i=0U; i::constrain(const Space& b) { if (!best) throw NoBest("PBS::constrain"); - for (unsigned int i=0U; i PBS::~PBS(void) { - for (unsigned int i=0U; inogoods(); @@ -78,30 +78,30 @@ namespace Gecode { namespace Search { namespace Seq { Space* RBS::next(void) { - // A failed master proves that no further solution exists - if (master == nullptr) + // A failed hub proves that no further solution exists + if (hub == nullptr) return nullptr; m_stopped = false; if (restart) { restart = false; sslr++; - NoGoods& ng = master_nogoods(); + NoGoods& ng = hub_nogoods(); MetaInfo mi(stop->restarts(),MetaInfo::RR_SOL,sslr,e->statistics().fail,last,ng); - bool r = master->master(mi); + bool r = hub->hub(mi); stop->nogood(ng.ng()); - if (stop->status(master) == SS_FAILED) { + if (stop->status(hub) == SS_FAILED) { stop->update(e->statistics()); - delete master; - master = nullptr; + delete hub; + hub = nullptr; e->reset(nullptr); return nullptr; } else if (r) { stop->update(e->statistics()); - Space* slave = master; - master = master->clone(); - complete = slave->slave(mi); + Space* spoke = hub; + hub = hub->clone(); + complete = spoke->spoke(mi); nogoods_valid = complete; - e->reset(slave); + e->reset(spoke); sslr = 0; stop->restart(); } @@ -121,7 +121,7 @@ namespace Gecode { namespace Search { namespace Seq { 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 + * when the spoke 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)) { @@ -131,24 +131,24 @@ namespace Gecode { namespace Search { namespace Seq { stop->restart(); } sslr = 0; - NoGoods& ng = master_nogoods(); + NoGoods& ng = hub_nogoods(); MetaInfo mi(stop->restarts(),e->stopped() ? MetaInfo::RR_LIM : MetaInfo::RR_CMPL,sslr,e->statistics().fail,last,ng); - (void) master->master(mi); + (void) hub->hub(mi); stop->nogood(ng.ng()); unsigned long long int nl = ++(*co); stop->limit(e->statistics(),nl); - if (stop->status(master) == SS_FAILED) { + if (stop->status(hub) == SS_FAILED) { // The statistics of the engine have been added by limit() - delete master; - master = nullptr; + delete hub; + hub = nullptr; e->reset(nullptr); return nullptr; } - Space* slave = master; - master = master->clone(); - complete = slave->slave(mi); + Space* spoke = hub; + hub = hub->clone(); + complete = spoke->spoke(mi); nogoods_valid = complete; - e->reset(slave); + e->reset(spoke); } else { return nullptr; } @@ -166,13 +166,13 @@ namespace Gecode { namespace Search { namespace Seq { RBS::constrain(const Space& b) { if (!best) throw NoBest("RBS::constrain"); - if (master == nullptr) + if (hub == nullptr) return; if ((last != nullptr) && !Search::better(b,*last,"RBS::constrain")) return; delete last; last = b.clone(); - master->constrain(b); + hub->constrain(b); e->constrain(b); } @@ -185,15 +185,15 @@ namespace Gecode { namespace Search { namespace Seq { * 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. + * The engine may still report a stop when the hub failed during a + * restart after a limit. A failed hub is a proof, not a stop. */ - return (master != nullptr) && (m_stopped || e->stopped()); + return (hub != nullptr) && (m_stopped || e->stopped()); } RBS::~RBS(void) { delete e; - delete master; + delete hub; delete last; delete co; delete stop; diff --git a/gecode/search/seq/rbs.hh b/gecode/search/seq/rbs.hh index 10627f45b1..2c57744a59 100755 --- a/gecode/search/seq/rbs.hh +++ b/gecode/search/seq/rbs.hh @@ -72,7 +72,7 @@ namespace Gecode { namespace Search { namespace Seq { void restart(void); /// Add no-goods to meta statistics void nogood(unsigned long int n); - /// Test master status with meta statistics + /// Test hub status with meta statistics SpaceStatus status(Space* s); /// Set current limit for the engine to \a l fails void limit(const Statistics& s, unsigned long long int l); @@ -89,8 +89,8 @@ namespace Gecode { namespace Search { namespace Seq { protected: /// The actual engine Engine* e; - /// The master space to restart from - Space* master; + /// The hub space to restart from + Space* hub; /// The last solution space (possibly nullptr) Space* last; /// The cutoff object @@ -103,7 +103,7 @@ 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 + /// Whether the no-goods of the current spoke search hold in the hub bool nogoods_valid; /// Whether a restart must be performed when next is called bool restart; @@ -111,13 +111,13 @@ namespace Gecode { namespace Search { namespace Seq { 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); + /// Return the no-goods of the last spoke search, valid in the hub + NoGoods& hub_nogoods(void); public: /** * \brief Constructor * - * The value \a complete is the result of the slave configuration of the + * The value \a complete is the result of the spoke configuration of the * space explored by the first engine \a e0, and is used as for the * searches after later restarts. */ diff --git a/gecode/search/seq/rbs.hpp b/gecode/search/seq/rbs.hpp index ff022f47de..a4bacb56e5 100755 --- a/gecode/search/seq/rbs.hpp +++ b/gecode/search/seq/rbs.hpp @@ -97,7 +97,7 @@ namespace Gecode { namespace Search { namespace Seq { RBS::RBS(Space* s, RestartStop* stop0, Engine* e0, const Search::Statistics& stat, const Options& opt, bool best0, bool complete0) - : e(e0), master(s), last(nullptr), co(opt.cutoff), stop(stop0), + : e(e0), hub(s), last(nullptr), co(opt.cutoff), stop(stop0), m_opt(opt.expand()), sslr(0), complete(complete0), nogoods_valid(complete0), restart(false), best(best0), m_stopped(false) { diff --git a/test/search.cpp b/test/search.cpp index fea6cf8d8c..e1cddede19 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -150,7 +150,7 @@ namespace Test { : Space(s), graph(s.graph), maximize(s.maximize), vertex(s.vertex), ceiling(s.ceiling) {} Space* copy(void) override { return new TreeSpace(*this); } - bool master(const MetaInfo&) override { return true; } + bool hub(const MetaInfo&) override { return true; } const Node& node(void) const { return (*graph)[vertex]; } void constrain(const Space& s) override { ceiling = static_cast(s).node().cost; @@ -403,8 +403,8 @@ namespace Test { virtual int solutions(void) const = 0; /// Verify that this is best solution virtual bool best(void) const = 0; - /// Master configuration function that does not restart - virtual bool master(const MetaInfo& mi) { + /// Hub configuration function that does not restart + virtual bool hub(const MetaInfo& mi) { if (mi.type() == MetaInfo::RESTART) { if (mi.last() != nullptr) constrain(*mi.last()); @@ -649,12 +649,12 @@ namespace Test { return "Sol"; } /// Rule out that solution is found more than once during restarts - virtual bool master(const MetaInfo& mi) { + virtual bool hub(const MetaInfo& mi) { switch (mi.type()) { case MetaInfo::RESTART: // With an objective, restart from a bound improving on the last solution if (htc != HTC_NONE) - return Space::master(mi); + return Space::hub(mi); if (mi.last() != nullptr) { const HasSolutions* s = static_cast(mi.last()); @@ -674,6 +674,577 @@ namespace Test { } }; + /// Return a bit for restart reason \a r + static unsigned int + reason_bit(MetaInfo::RestartReason r) { + return 1U << static_cast(r); + } + + /// Observations shared by meta-search dispatch test spaces + struct MetaDispatchState { + std::atomic hub_calls{0}; + std::atomic spoke_calls{0}; + std::atomic hub_reasons{0}; + std::atomic spoke_reasons{0}; + std::atomic assets{0}; + std::atomic live_spaces{0}; + std::atomic copies{0}; + std::atomic constrain_calls{0}; + std::atomic constrain_hub_calls{0}; + std::atomic constrain_spoke_calls{0}; + std::atomic constrain_asset_mask{0}; + std::atomic constrain_hub_asset_mask{0}; + std::atomic constrain_spoke_asset_mask{0}; + std::atomic nogood_handoffs{0}; + bool restart_after_solution = true; + bool solution_spoke_complete = true; + bool fail_solution_spoke = false; + bool nogood_model = false; + /// Whether a PBS spoke() posts the branching for its asset + bool spoke_branching = true; + /// Whether the first RBS spoke reaches the cutoff without a solution + bool limit_first_spoke = false; + }; + + /// Common implementation for meta-search dispatch test spaces + class MetaDispatchSpace : public Space { + protected: + /// RBS role of a space + enum class RestartRole { Hub, Spoke }; + /// Variable used to provide two solutions + IntVar x; + /// Optional queens model used to force restart no-good extraction + IntVarArray q; + /// Optional variables without a solution, used to reach the cutoff + IntVarArray y; + /// Shared hook observations and configuration + MetaDispatchState* state; + /// RBS role retained across cloning + RestartRole restart_role; + /// PBS asset identity, or -1 before portfolio dispatch + int portfolio_asset; + + /// Record and implement a Hub callback + bool + observe_hub(const MetaInfo& mi) { + state->hub_calls.fetch_add(1, std::memory_order_relaxed); + if (mi.type() == MetaInfo::PORTFOLIO) + // PBS must ignore the callback result. + return false; + restart_role = RestartRole::Hub; + state->hub_reasons.fetch_or(reason_bit(mi.reason()), std::memory_order_relaxed); + bool default_result = Space::hub(mi); + if ((mi.type() == MetaInfo::RESTART) && (mi.nogoods().ng() > 0)) + state->nogood_handoffs.fetch_add(1, std::memory_order_relaxed); + return (mi.reason() == MetaInfo::RR_SOL) ? state->restart_after_solution : default_result; + } + /// Record and implement a Spoke callback + bool + observe_spoke(const MetaInfo& mi) { + state->spoke_calls.fetch_add(1, std::memory_order_relaxed); + if (mi.type() == MetaInfo::PORTFOLIO) { + portfolio_asset = static_cast(mi.asset()); + state->assets.fetch_or(1U << mi.asset(), std::memory_order_relaxed); + // The default hub() removes the model's branching. A hub() + // override keeps it, and then this branching is redundant. + if (state->spoke_branching) + Gecode::branch(*this, x, INT_VAL_MIN()); + // PBS must ignore the callback result. + return false; + } + state->spoke_reasons.fetch_or(reason_bit(mi.reason()), std::memory_order_relaxed); + restart_role = RestartRole::Spoke; + if ((mi.reason() == MetaInfo::RR_INIT) && state->limit_first_spoke) { + // Four distinct values from three fail only during search. + distinct(*this, y, IPL_VAL); + Gecode::branch(*this, y, INT_VAR_NONE(), INT_VAL_MIN()); + } + if ((mi.reason() == MetaInfo::RR_SOL) && state->fail_solution_spoke) + fail(); + return (mi.reason() == MetaInfo::RR_SOL) ? state->solution_spoke_complete : true; + } + + public: + /// Initialize with shared observations \a s + MetaDispatchSpace(MetaDispatchState& s) + : x(*this, 0, 1), q(*this, s.nogood_model ? 8 : 0, 0, 7), y(*this, s.limit_first_spoke ? 4 : 0, 0, 2), + state(&s), restart_role(RestartRole::Hub), portfolio_asset(-1) { + state->live_spaces.fetch_add(1, std::memory_order_relaxed); + if (state->nogood_model) { + distinct(*this, IntArgs::create(q.size(), 0, 1), q, IPL_VAL); + distinct(*this, IntArgs::create(q.size(), 0, -1), q, IPL_VAL); + distinct(*this, q, IPL_VAL); + Gecode::branch(*this, q, INT_VAR_NONE(), INT_VAL_MIN()); + } + Gecode::branch(*this, x, INT_VAL_MIN()); + } + /// Clone \a s + MetaDispatchSpace(MetaDispatchSpace& s) + : Space(s), state(s.state), restart_role(s.restart_role), portfolio_asset(s.portfolio_asset) { + state->live_spaces.fetch_add(1, std::memory_order_relaxed); + state->copies.fetch_add(1, std::memory_order_relaxed); + x.update(*this, s.x); + q.update(*this, s.q); + y.update(*this, s.y); + } + /// Record destruction for clone-ownership checks + ~MetaDispatchSpace(void) override { + state->live_spaces.fetch_sub(1, std::memory_order_relaxed); + } + /// Require a larger value than solution \a s + void + constrain(const Space& s) override { + state->constrain_calls.fetch_add(1, std::memory_order_relaxed); + if (restart_role == RestartRole::Hub) + state->constrain_hub_calls.fetch_add(1, std::memory_order_relaxed); + else + state->constrain_spoke_calls.fetch_add(1, std::memory_order_relaxed); + if (portfolio_asset >= 0) { + const unsigned int asset = 1U << static_cast(portfolio_asset); + state->constrain_asset_mask.fetch_or(asset, std::memory_order_relaxed); + if (restart_role == RestartRole::Hub) + state->constrain_hub_asset_mask.fetch_or(asset, std::memory_order_relaxed); + else + state->constrain_spoke_asset_mask.fetch_or(asset, std::memory_order_relaxed); + } + rel(*this, x, IRT_GR, static_cast(s).x.val()); + } + /// Compare with solution \a s, larger values are better + SpaceComparison + compare(const Space& s) const override { + return compare_int(x.val(), static_cast(s).x.val(), Sense::Maximize); + } + /// Return the solution value + int + value(void) const { + return x.val(); + } + /// Return whether the solution variable is assigned + bool + assigned(void) const { + return x.assigned(); + } + }; + + /// Dispatch test space overriding only Hub + class HubDispatchSpace : public MetaDispatchSpace { + public: + /// Initialize with shared observations \a s + HubDispatchSpace(MetaDispatchState& s) : MetaDispatchSpace(s) {} + /// Clone \a s + HubDispatchSpace(HubDispatchSpace& s) : MetaDispatchSpace(s) {} + /// Copy during cloning + Space* + copy(void) override { + return new HubDispatchSpace(*this); + } + /// Observe Hub dispatch + bool + hub(const MetaInfo& mi) override { + return observe_hub(mi); + } + }; + + /// Dispatch test space overriding only Spoke + class SpokeDispatchSpace : public MetaDispatchSpace { + public: + /// Initialize with shared observations \a s + SpokeDispatchSpace(MetaDispatchState& s) : MetaDispatchSpace(s) {} + /// Clone \a s + SpokeDispatchSpace(SpokeDispatchSpace& s) : MetaDispatchSpace(s) {} + /// Copy during cloning + Space* + copy(void) override { + return new SpokeDispatchSpace(*this); + } + /// Observe Spoke dispatch + bool + spoke(const MetaInfo& mi) override { + return observe_spoke(mi); + } + }; + + /// Dispatch test space overriding both Hub and Spoke + class HubSpokeDispatchSpace : public MetaDispatchSpace { + public: + /// Initialize with shared observations \a s + HubSpokeDispatchSpace(MetaDispatchState& s) : MetaDispatchSpace(s) {} + /// Clone \a s + HubSpokeDispatchSpace(HubSpokeDispatchSpace& s) : MetaDispatchSpace(s) {} + /// Copy during cloning + Space* + copy(void) override { + return new HubSpokeDispatchSpace(*this); + } + /// Observe Hub dispatch + bool + hub(const MetaInfo& mi) override { + return observe_hub(mi); + } + /// Observe Spoke dispatch + bool + spoke(const MetaInfo& mi) override { + return observe_spoke(mi); + } + }; + + /// Hub and Spoke hook dispatch through RBS and PBS + class MetaDispatch : public Base { + public: + /// Dispatch scenario + enum class Scenario { + RbsHubOnly, + RbsSpokeOnly, + RbsHubContinue, + RbsHubRestart, + RbsSpokeComplete, + RbsSpokeIncomplete, + RbsLimit, + PbsHubOnly, + PbsSpokeOnly, + PbsSingle, + PbsSequentialHubOnly, + PbsSequentialSpokeOnly, + PbsSequential, + PbsParallelHubOnly, + PbsParallelSpokeOnly, + PbsParallel, + PbsParallelThreadLimit, + PbsDefaultHubRemovesBranchers, + RbsNoGoodsResetOwnership, + MetaStopOwnership, + BabIncumbent + }; + + protected: + /// Scenario under test + Scenario scenario; + + /// Return whether \a counter holds \a expected + static bool + holds(const std::atomic& counter, unsigned int expected) { + return counter.load(std::memory_order_relaxed) == expected; + } + /// Check independent callback dispatch for one RBS model + template + static bool + rbs_dispatch(unsigned int hub_calls, unsigned int spoke_calls) { + MetaDispatchState state; + Model* m = new Model(state); + Gecode::Search::Options o; + o.threads = 1; + o.cutoff = Gecode::Search::Cutoff::constant(100); + Gecode::RBS search(m, o); + delete m; + + Model* first = search.next(); + Model* second = search.next(); + bool ok = (first != nullptr) && (second != nullptr) && holds(state.hub_calls, hub_calls) && + holds(state.spoke_calls, spoke_calls); + delete first; + delete second; + return ok; + } + /// Run one RBS dispatch scenario + static bool + rbs(bool restart_after_solution, bool fail_solution_spoke, bool solution_spoke_complete) { + MetaDispatchState state; + state.restart_after_solution = restart_after_solution; + state.fail_solution_spoke = fail_solution_spoke; + state.solution_spoke_complete = solution_spoke_complete; + + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(state); + Gecode::Search::Options o; + o.threads = 1; + o.cutoff = Gecode::Search::Cutoff::constant(100); + Gecode::RBS search(m, o); + delete m; + + HubSpokeDispatchSpace* first = search.next(); + bool ok = first != nullptr; + delete first; + + HubSpokeDispatchSpace* second = search.next(); + if (!fail_solution_spoke) { + ok = ok && (second != nullptr) && holds(state.hub_calls, 1U) && + holds(state.spoke_calls, restart_after_solution ? 2U : 1U) && + holds(state.hub_reasons, reason_bit(MetaInfo::RR_SOL)) && + holds(state.spoke_reasons, reason_bit(MetaInfo::RR_INIT) | + (restart_after_solution ? reason_bit(MetaInfo::RR_SOL) : 0U)); + } else if (solution_spoke_complete) { + ok = ok && (second == nullptr) && holds(state.hub_calls, 1U) && holds(state.spoke_calls, 2U) && + holds(state.hub_reasons, reason_bit(MetaInfo::RR_SOL)) && + holds(state.spoke_reasons, reason_bit(MetaInfo::RR_INIT) | reason_bit(MetaInfo::RR_SOL)); + } else { + ok = ok && (second != nullptr) && holds(state.hub_calls, 2U) && holds(state.spoke_calls, 3U) && + holds(state.hub_reasons, reason_bit(MetaInfo::RR_SOL) | reason_bit(MetaInfo::RR_CMPL)) && + holds(state.spoke_reasons, reason_bit(MetaInfo::RR_INIT) | reason_bit(MetaInfo::RR_SOL) | + reason_bit(MetaInfo::RR_CMPL)); + } + delete second; + return ok; + } + /// Run one PBS dispatch scenario + template + static bool + pbs(unsigned int assets, double threads, unsigned int hub_calls, unsigned int spoke_calls, + unsigned int asset_mask) { + MetaDispatchState state; + Model* m = new Model(state); + Gecode::Search::Options o; + o.assets = assets; + o.threads = threads; + Gecode::PBS search(m, o); + delete m; + + Model* solution = search.next(); + bool ok = (solution != nullptr) && solution->assigned() && holds(state.hub_calls, hub_calls) && + holds(state.spoke_calls, spoke_calls) && holds(state.assets, asset_mask); + delete solution; + return ok; + } + /// Check that the default PBS hub() removes the model's branching + static bool + pbs_default_hub_removes_branchers(void) { + MetaDispatchState state; + state.spoke_branching = false; + SpokeDispatchSpace* m = new SpokeDispatchSpace(state); + Gecode::Search::Options o; + o.assets = 3; + o.threads = 1; + Gecode::PBS search(m, o); + delete m; + + // A spoke without branching is a solution as soon as it is stable. + SpokeDispatchSpace* solution = search.next(); + bool ok = (solution != nullptr) && !solution->assigned() && holds(state.spoke_calls, 3U); + delete solution; + return ok; + } + /// Check RBS dispatch when the first spoke reaches the cutoff + static bool + rbs_limit(void) { + MetaDispatchState state; + state.limit_first_spoke = true; + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(state); + Gecode::Search::Options o; + o.threads = 1; + // No-goods from the first spoke would refer to its own brancher. + o.nogoods_limit = 0; + o.cutoff = Gecode::Search::Cutoff::constant(1); + Gecode::RBS search(m, o); + delete m; + + HubSpokeDispatchSpace* solution = search.next(); + bool ok = (solution != nullptr) && solution->assigned() && holds(state.hub_calls, 1U) && + holds(state.spoke_calls, 2U) && holds(state.hub_reasons, reason_bit(MetaInfo::RR_LIM)) && + holds(state.spoke_reasons, reason_bit(MetaInfo::RR_INIT) | reason_bit(MetaInfo::RR_LIM)); + delete solution; + return ok; + } + /// Exercise RBS no-good posting, reset, and clone ownership + static bool + rbs_nogoods_reset_ownership(void) { + MetaDispatchState state; + state.nogood_model = true; + bool ok = true; + { + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(state); + Gecode::Search::Options o; + o.threads = 1; + o.nogoods_limit = 16; + o.cutoff = Gecode::Search::Cutoff::constant(1); + Gecode::RBS search(m, o); + delete m; + + unsigned int solutions = 0; + while (HubSpokeDispatchSpace* solution = search.next()) { + solutions++; + delete solution; + } + ok = (solutions == 2U) && (state.spoke_calls.load(std::memory_order_relaxed) >= 2U) && + (state.constrain_calls.load(std::memory_order_relaxed) >= 1U) && + (state.nogood_handoffs.load(std::memory_order_relaxed) >= 1U); + } + return ok && holds(state.live_spaces, 0U) && (state.copies.load(std::memory_order_relaxed) > 0U); + } + /// Stop RBS and PBS, then verify orderly clone destruction + static bool + meta_stop_ownership(void) { + MetaDispatchState rbs_state; + // Continue after a solution, so that the spoke engine prunes with it + rbs_state.restart_after_solution = false; + bool ok = true; + { + Gecode::Search::NodeStop stop(0); + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(rbs_state); + Gecode::Search::Options o; + o.threads = 1; + o.stop = &stop; + o.cutoff = Gecode::Search::Cutoff::constant(1); + Gecode::RBS search(m, o); + delete m; + HubSpokeDispatchSpace* solution = search.next(); + ok = (solution == nullptr) && search.stopped(); + delete solution; + } + ok = ok && holds(rbs_state.live_spaces, 0U); + + MetaDispatchState pbs_state; + { + Gecode::Search::NodeStop stop(0); + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(pbs_state); + Gecode::Search::Options o; + o.assets = 3; +#ifdef GECODE_HAS_THREADS + o.threads = 3; +#else + o.threads = 1; +#endif + o.stop = &stop; + Gecode::PBS search(m, o); + delete m; + HubSpokeDispatchSpace* solution = search.next(); + ok = ok && (solution == nullptr) && search.stopped(); + delete solution; + } + return ok && holds(pbs_state.live_spaces, 0U); + } + /// Exercise incumbent propagation through RBS and PBS wrappers + static bool + bab_incumbent(void) { + MetaDispatchState rbs_state; + // Continue after a solution, so that the spoke engine prunes with it + rbs_state.restart_after_solution = false; + bool ok = true; + { + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(rbs_state); + Gecode::Search::Options o; + o.threads = 1; + o.cutoff = Gecode::Search::Cutoff::constant(100); + SEB builder = Gecode::rbs(o); + Gecode::Search::Engine* search = (*builder)(m); + delete builder; + delete m; + HubSpokeDispatchSpace* best = nullptr; + while (HubSpokeDispatchSpace* solution = static_cast(search->next())) { + search->constrain(*solution); + delete best; + best = solution; + } + ok = (best != nullptr) && (best->value() == 1) && + (rbs_state.constrain_hub_calls.load(std::memory_order_relaxed) > 0U) && + (rbs_state.constrain_spoke_calls.load(std::memory_order_relaxed) > 0U); + delete best; + delete search; + } + ok = ok && holds(rbs_state.live_spaces, 0U); + + MetaDispatchState pbs_state; + { + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(pbs_state); + Gecode::Search::Options o; + o.assets = 3; + o.threads = 1; + Gecode::PBS search(m, o); + delete m; + HubSpokeDispatchSpace* best = nullptr; + while (HubSpokeDispatchSpace* solution = search.next()) { + delete best; + best = solution; + } + ok = ok && (best != nullptr) && (best->value() == 1) && holds(pbs_state.constrain_asset_mask, 7U); + delete best; + } + ok = ok && holds(pbs_state.live_spaces, 0U); + + MetaDispatchState nested_state; + nested_state.restart_after_solution = false; + { + HubSpokeDispatchSpace* m = new HubSpokeDispatchSpace(nested_state); + Gecode::Search::Options outer; + outer.assets = 3; + outer.threads = 1; + Gecode::Search::Options inner[3]; + SEBs assets(3); + for (int i = 0; i < 3; i++) { + inner[i].threads = 1; + // Each RBS engine owns and deletes its cutoff. + inner[i].cutoff = Gecode::Search::Cutoff::constant(100); + assets[i] = Gecode::rbs(inner[i]); + } + Gecode::PBS search(m, assets, outer); + delete m; + HubSpokeDispatchSpace* best = nullptr; + while (HubSpokeDispatchSpace* solution = search.next()) { + delete best; + best = solution; + } + ok = ok && (best != nullptr) && (best->value() == 1) && + holds(nested_state.constrain_hub_asset_mask, 7U) && + holds(nested_state.constrain_spoke_asset_mask, 7U); + delete best; + } + return ok && holds(nested_state.live_spaces, 0U); + } + + public: + /// Initialize scenario \a s with name \a n + MetaDispatch(const std::string& n, Scenario s) : Base("Search::MetaDispatch::" + n), scenario(s) {} + /// Run test + bool + run(void) override { + switch (scenario) { + case Scenario::RbsHubOnly: + return rbs_dispatch(1, 0); + case Scenario::RbsSpokeOnly: + return rbs_dispatch(0, 2); + case Scenario::RbsHubContinue: + return rbs(false, false, true); + case Scenario::RbsHubRestart: + return rbs(true, false, true); + case Scenario::RbsSpokeComplete: + return rbs(true, true, true); + case Scenario::RbsSpokeIncomplete: + return rbs(true, true, false); + case Scenario::RbsLimit: + return rbs_limit(); + case Scenario::PbsHubOnly: + return pbs(1, 1, 1, 0, 0); + case Scenario::PbsSpokeOnly: + return pbs(1, 1, 0, 1, 1); + case Scenario::PbsSingle: + return pbs(1, 1, 1, 1, 1); + case Scenario::PbsSequentialHubOnly: + return pbs(3, 1, 1, 0, 0); + case Scenario::PbsSequentialSpokeOnly: + return pbs(3, 1, 0, 3, 7); + case Scenario::PbsSequential: + return pbs(3, 1, 1, 3, 7); + case Scenario::PbsParallelHubOnly: + return pbs(3, 3, 1, 0, 0); + case Scenario::PbsParallelSpokeOnly: + return pbs(3, 3, 0, 3, 7); + case Scenario::PbsParallel: + return pbs(3, 3, 1, 3, 7); + case Scenario::PbsParallelThreadLimit: + // Parallel PBS runs only as many assets as it has threads. + return pbs(3, 2, 1, 2, 3); + case Scenario::PbsDefaultHubRemovesBranchers: + return pbs_default_hub_removes_branchers(); + case Scenario::RbsNoGoodsResetOwnership: + return rbs_nogoods_reset_ownership(); + case Scenario::MetaStopOwnership: + return meta_stop_ownership(); + case Scenario::BabIncumbent: + return bab_incumbent(); + default: + GECODE_NEVER; + } + return false; + } + }; + /// %Base class for search tests class Test : public Base { public: @@ -1255,10 +1826,10 @@ namespace Test { } virtual Space* copy(void) { return new ParallelObjective(*this); } virtual IntVar cost(void) const { return x; } - virtual bool master(const MetaInfo& mi) { + virtual bool hub(const MetaInfo& mi) { if (mi.type() == MetaInfo::PORTFOLIO) return false; - return Space::master(mi); + return Space::hub(mi); } }; @@ -1298,10 +1869,10 @@ namespace Test { throw ComparisonError(); return SpaceComparison::Incomparable; } - virtual bool master(const MetaInfo& mi) { + virtual bool hub(const MetaInfo& mi) { if (mi.type() == MetaInfo::PORTFOLIO) return false; - return Space::master(mi); + return Space::hub(mi); } }; @@ -2142,26 +2713,26 @@ namespace Test { } }; - /// Space whose first slave search is restricted to x2, x3 in {0,1} - class RestrictedFirstSlave : public Gecode::IntMinimizeSpace { + /// Space whose first spoke search is restricted to x2, x3 in {0,1} + class RestrictedFirstSpoke : public Gecode::IntMinimizeSpace { public: /// Distinct variables, the cost is x0 Gecode::IntVarArray x; - RestrictedFirstSlave(void) : x(*this,4,0,3) { + RestrictedFirstSpoke(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) + RestrictedFirstSpoke(RestrictedFirstSpoke& s) : Gecode::IntMinimizeSpace(s) { x.update(*this,s.x); } Gecode::Space* copy(void) override { - return new RestrictedFirstSlave(*this); + return new RestrictedFirstSpoke(*this); } Gecode::IntVar cost(void) const override { return x[0]; } - bool slave(const Gecode::MetaInfo& mi) override { + bool spoke(const Gecode::MetaInfo& mi) override { if ((mi.type() == Gecode::MetaInfo::RESTART) && (mi.reason() == Gecode::MetaInfo::RR_INIT)) { Gecode::dom(*this,x[2],0,1); @@ -2172,20 +2743,20 @@ namespace Test { } }; - /// No-goods from an incomplete slave search are not posted in the master + /// No-goods from an incomplete spoke search are not posted in the hub class RBSIncompleteNoGoods : public Base { public: RBSIncompleteNoGoods(void) : Base("Search::RBS::IncompleteNoGoods") {} bool run(void) override { - RestrictedFirstSlave* m = new RestrictedFirstSlave; + RestrictedFirstSpoke* m = new RestrictedFirstSpoke; Gecode::Search::Options o; o.nogoods_limit = 128U; o.cutoff = Gecode::Search::Cutoff::constant(100); - Gecode::RBS rbs(m,o); + 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()) { + while (RestrictedFirstSpoke* s = rbs.next()) { best = s->x[0].val(); delete s; } @@ -2215,23 +2786,23 @@ namespace Test { Gecode::IntVar cost(void) const override { return x[0]; } - bool master(const Gecode::MetaInfo& mi) override { + bool hub(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); + return Gecode::IntMinimizeSpace::hub(mi); } }; - /// Space without solutions whose master fails at a restart after a limit - class FailingMaster : public NoSolution { + /// Space without solutions whose hub fails at a restart after a limit + class FailingHub : public NoSolution { public: - FailingMaster(void) = default; - FailingMaster(FailingMaster& s) : NoSolution(s) {} + FailingHub(void) = default; + FailingHub(FailingHub& s) : NoSolution(s) {} Gecode::Space* copy(void) override { - return new FailingMaster(*this); + return new FailingHub(*this); } - bool master(const Gecode::MetaInfo& mi) override { + bool hub(const Gecode::MetaInfo& mi) override { if ((mi.type() == Gecode::MetaInfo::RESTART) && (mi.reason() == Gecode::MetaInfo::RR_LIM)) fail(); @@ -2239,24 +2810,24 @@ namespace Test { } }; - /// A master failing at a restart after a limit proves exhaustion - class RBSFailedMaster : public Base { + /// A hub failing at a restart after a limit proves exhaustion + class RBSFailedHub : public Base { public: - RBSFailedMaster(void) : Base("Search::RBS::FailedMaster") {} + RBSFailedHub(void) : Base("Search::RBS::FailedHub") {} bool run(void) override { for (unsigned int t : {1U,2U}) { - FailingMaster* m = new FailingMaster; + FailingHub* m = new FailingHub; Gecode::Search::Options o; o.threads = t; o.cutoff = Gecode::Search::Cutoff::constant(1); - Gecode::RBS rbs(m,o); + Gecode::RBS rbs(m,o); delete m; - if (FailingMaster* s = rbs.next()) { + if (FailingHub* s = rbs.next()) { delete s; return false; } Gecode::Search::Statistics s = rbs.statistics(); - // The failures of the last slave search are counted once + // The failures of the last spoke search are counted once if (rbs.stopped() || (s.restart != 1U) || (s.fail < 2U) || ((t == 1U) && (s.fail != 2U))) return false; @@ -2269,17 +2840,17 @@ namespace Test { } }; - RBSFailedMaster rbs_failed_master; + RBSFailedHub rbs_failed_hub; - /// Space without solutions whose restricted slaves fail at the root - class FailingSlave : public NoSolution { + /// Space without solutions whose restricted spokes fail at the root + class FailingSpoke : public NoSolution { public: - FailingSlave(void) = default; - FailingSlave(FailingSlave& s) : NoSolution(s) {} + FailingSpoke(void) = default; + FailingSpoke(FailingSpoke& s) : NoSolution(s) {} Gecode::Space* copy(void) override { - return new FailingSlave(*this); + return new FailingSpoke(*this); } - bool slave(const Gecode::MetaInfo& mi) override { + bool spoke(const Gecode::MetaInfo& mi) override { if ((mi.type() == Gecode::MetaInfo::RESTART) && (mi.restart() > 0)) { fail(); return false; @@ -2299,19 +2870,19 @@ namespace Test { } }; - /// Slave searches failing at the root count as failures + /// Spoke 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; + FailingSpoke* m = new FailingSpoke; FiveFailures f; Gecode::Search::Options o; o.threads = threads; o.stop = &f; o.cutoff = Gecode::Search::Cutoff::constant(1); - Gecode::RBS rbs(m,o); + Gecode::RBS rbs(m,o); delete m; - if (FailingSlave* s = rbs.next()) { + if (FailingSpoke* s = rbs.next()) { delete s; return false; } @@ -2329,15 +2900,15 @@ namespace Test { RBSRootFailure rbs_root_failure; - /// Space with solutions whose first, incomplete slave fails at the root - class FailingFirstSlave : public RestrictedFirstSlave { + /// Space with solutions whose first, incomplete spoke fails at the root + class FailingFirstSpoke : public RestrictedFirstSpoke { public: - FailingFirstSlave(void) = default; - FailingFirstSlave(FailingFirstSlave& s) : RestrictedFirstSlave(s) {} + FailingFirstSpoke(void) = default; + FailingFirstSpoke(FailingFirstSpoke& s) : RestrictedFirstSpoke(s) {} Gecode::Space* copy(void) override { - return new FailingFirstSlave(*this); + return new FailingFirstSpoke(*this); } - bool slave(const Gecode::MetaInfo& mi) override { + bool spoke(const Gecode::MetaInfo& mi) override { if ((mi.type() == Gecode::MetaInfo::RESTART) && (mi.reason() == Gecode::MetaInfo::RR_INIT)) { fail(); @@ -2347,24 +2918,24 @@ namespace Test { } }; - /// A failed first slave search that is incomplete is not a proof - class RBSFailedFirstSlave : public Base { + /// A failed first spoke search that is incomplete is not a proof + class RBSFailedFirstSpoke : public Base { template class Engine> static bool test(unsigned int threads) { - FailingFirstSlave* m = new FailingFirstSlave; + FailingFirstSpoke* m = new FailingFirstSpoke; Gecode::Search::Options o; o.threads = threads; o.cutoff = Gecode::Search::Cutoff::constant(1); - Gecode::RBS rbs(m,o); + Gecode::RBS rbs(m,o); delete m; - FailingFirstSlave* s = rbs.next(); + FailingFirstSpoke* s = rbs.next(); if (s == nullptr) return false; delete s; return rbs.statistics().restart >= 1U; } public: - RBSFailedFirstSlave(void) : Base("Search::RBS::FailedFirstSlave") {} + RBSFailedFirstSpoke(void) : Base("Search::RBS::FailedFirstSpoke") {} bool run(void) override { return test(1U) && test(2U) && test(1U) && test(2U) && @@ -2372,7 +2943,7 @@ namespace Test { } }; - RBSFailedFirstSlave rbs_failed_first_slave; + RBSFailedFirstSpoke rbs_failed_first_spoke; /// Stop after a number of failures, or after many checks as a safeguard class FailureLimit : public Gecode::Search::Stop { @@ -2525,36 +3096,37 @@ namespace Test { private: /// Whether best solution search is used bool best; - /// Number of master threads - unsigned int mt; - /// Number of slave threads - unsigned int st; + /// Number of portfolio threads + unsigned int portfolio_threads; + /// Number of threads of each asset + unsigned int asset_threads; public: /// Initialize test - SEBPBS(const std::string& e, bool b, unsigned int mt0, unsigned int st0) - : Test("PBS::SEB::"+e+"::"+Model::name()+"::"+str(mt0)+"::"+str(st0), - HTB_BINARY,HTB_BINARY,HTB_BINARY), best(b), mt(mt0), st(st0) {} + SEBPBS(const std::string& e, bool b, unsigned int portfolio_threads0, unsigned int asset_threads0) + : Test("PBS::SEB::"+e+"::"+Model::name()+"::"+str(portfolio_threads0)+"::"+str(asset_threads0), + HTB_BINARY,HTB_BINARY,HTB_BINARY), best(b), + portfolio_threads(portfolio_threads0), asset_threads(asset_threads0) {} /// Run test virtual bool run(void) { using namespace Gecode; Model* m = new Model(htb1,htb2,htb3); Gecode::Search::FailStop f(2); - Gecode::Search::Options mo; - mo.threads = mt; - mo.d_l = 100; - mo.stop = &f; + Gecode::Search::Options portfolio_options; + portfolio_options.threads = portfolio_threads; + portfolio_options.d_l = 100; + portfolio_options.stop = &f; - Gecode::Search::Options so; - so.threads = st; - so.d_l = 100; - so.cutoff = Gecode::Search::Cutoff::constant(1000000); + Gecode::Search::Options asset_options; + asset_options.threads = asset_threads; + asset_options.d_l = 100; + asset_options.cutoff = Gecode::Search::Cutoff::constant(1000000); if (best) { SEBs sebs(3); - sebs[0] = bab(so); - sebs[1] = bab(so); - sebs[2] = rbs(so); - Gecode::PBS pbs(m, sebs, mo); + sebs[0] = bab(asset_options); + sebs[1] = bab(asset_options); + sebs[2] = rbs(asset_options); + Gecode::PBS pbs(m, sebs, portfolio_options); delete m; Model* b = nullptr; @@ -2572,10 +3144,10 @@ namespace Test { return ok; } else { SEBs sebs(3); - sebs[0] = dfs(so); - sebs[1] = lds(so); - sebs[2] = rbs(so); - Gecode::PBS pbs(m, sebs, mo); + sebs[0] = dfs(asset_options); + sebs[1] = lds(asset_options); + sebs[2] = rbs(asset_options); + Gecode::PBS pbs(m, sebs, portfolio_options); int n = 3 * m->solutions(); delete m; @@ -2674,7 +3246,7 @@ namespace Test { Gecode::Space* copy(void) override { return new FailingAsset(*this); } - bool slave(const Gecode::MetaInfo& mi) override { + bool spoke(const Gecode::MetaInfo& mi) override { if ((mi.type() == Gecode::MetaInfo::PORTFOLIO) && (mi.asset() == 0U)) fail(); return true; @@ -2849,6 +3421,17 @@ namespace Test { c_d,a_d,t); } // Restart-based search + (void)new MetaDispatch("RBS::HubOnly", MetaDispatch::Scenario::RbsHubOnly); + (void)new MetaDispatch("RBS::SpokeOnly", MetaDispatch::Scenario::RbsSpokeOnly); + (void)new MetaDispatch("RBS::HubContinue", MetaDispatch::Scenario::RbsHubContinue); + (void)new MetaDispatch("RBS::HubRestart", MetaDispatch::Scenario::RbsHubRestart); + (void)new MetaDispatch("RBS::SpokeComplete", MetaDispatch::Scenario::RbsSpokeComplete); + (void)new MetaDispatch("RBS::SpokeIncomplete", MetaDispatch::Scenario::RbsSpokeIncomplete); + (void)new MetaDispatch("RBS::Limit", MetaDispatch::Scenario::RbsLimit); + (void)new MetaDispatch("RBS::NoGoodsResetOwnership", + MetaDispatch::Scenario::RbsNoGoodsResetOwnership); + (void)new MetaDispatch("Lifecycle::StopOwnership", MetaDispatch::Scenario::MetaStopOwnership); + (void)new MetaDispatch("BAB::Incumbent", MetaDispatch::Scenario::BabIncumbent); for (unsigned int t=1; t<=4; t++) { (void) new RBS("DFS",t); (void) new RBS("LDS",t); @@ -2861,6 +3444,20 @@ namespace Test { (void) new RBS("BAB",t); } // Portfolio-based search + (void)new MetaDispatch("PBS::HubOnly", MetaDispatch::Scenario::PbsHubOnly); + (void)new MetaDispatch("PBS::SpokeOnly", MetaDispatch::Scenario::PbsSpokeOnly); + (void)new MetaDispatch("PBS::Single", MetaDispatch::Scenario::PbsSingle); + (void)new MetaDispatch("PBS::SequentialHubOnly", MetaDispatch::Scenario::PbsSequentialHubOnly); + (void)new MetaDispatch("PBS::SequentialSpokeOnly", MetaDispatch::Scenario::PbsSequentialSpokeOnly); + (void)new MetaDispatch("PBS::Sequential", MetaDispatch::Scenario::PbsSequential); + (void)new MetaDispatch("PBS::DefaultHubRemovesBranchers", + MetaDispatch::Scenario::PbsDefaultHubRemovesBranchers); +#ifdef GECODE_HAS_THREADS + (void)new MetaDispatch("PBS::ParallelHubOnly", MetaDispatch::Scenario::PbsParallelHubOnly); + (void)new MetaDispatch("PBS::ParallelSpokeOnly", MetaDispatch::Scenario::PbsParallelSpokeOnly); + (void)new MetaDispatch("PBS::Parallel", MetaDispatch::Scenario::PbsParallel); + (void)new MetaDispatch("PBS::ParallelThreadLimit", MetaDispatch::Scenario::PbsParallelThreadLimit); +#endif for (unsigned int a=1; a<=4; a++) for (unsigned int t=1; t<=2*a; t++) { (void) new PBS("DFS",false,a,t); @@ -2874,14 +3471,14 @@ namespace Test { (void) new PBS("BAB",true,a,t); } // Portfolio-based search using SEBs - for (unsigned int mt=1; mt<=3; mt += 2) - for (unsigned int st=1; st<=8; st++) { - (void) new SEBPBS("BAB",true,mt,st); - (void) new SEBPBS("BAB",true,mt,st); - (void) new SEBPBS("BAB",true,mt,st); - (void) new SEBPBS("DFS+LDS",false,mt,st); - (void) new SEBPBS("DFS+LDS",false,mt,st); - (void) new SEBPBS("DFS+LDS",false,mt,st); + for (unsigned int pt=1; pt<=3; pt += 2) + for (unsigned int at=1; at<=8; at++) { + (void) new SEBPBS("BAB",true,pt,at); + (void) new SEBPBS("BAB",true,pt,at); + (void) new SEBPBS("BAB",true,pt,at); + (void) new SEBPBS("DFS+LDS",false,pt,at); + (void) new SEBPBS("DFS+LDS",false,pt,at); + (void) new SEBPBS("DFS+LDS",false,pt,at); } } }; diff --git a/test/search/hub-spoke-api-fail-master.cpp b/test/search/hub-spoke-api-fail-master.cpp new file mode 100644 index 0000000000..f1e08c09d8 --- /dev/null +++ b/test/search/hub-spoke-api-fail-master.cpp @@ -0,0 +1,64 @@ +/* + * Main authors: + * Mikael Zayenz Lagerkvist + * + * Copyright: + * Mikael Zayenz Lagerkvist, 2026 + * + * This file is part of Gecode, the generic constraint + * development environment: + * http://www.gecode.dev + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * This file must not compile: it is hub-spoke-api-pass.cpp with the + * hub() override replaced by a Gecode 6 master() override without + * override. Space deletes master(), and a non-deleted function cannot + * override a deleted one. Successful compilation means that an old + * override would again compile and silently never be called. + * + */ + +#include + +namespace Test { namespace Search { + + /// Space overriding the meta-search hooks + class HookSpace : public Gecode::Space { + public: + HookSpace(void) {} + HookSpace(HookSpace& s) : Gecode::Space(s) {} + Gecode::Space* + copy(void) override { + return new HookSpace(*this); + } + bool + master(const Gecode::MetaInfo&) { + return true; + } + bool + spoke(const Gecode::MetaInfo&) override { + return true; + } + }; + +}} // namespace Test::Search + +// STATISTICS: test-search diff --git a/test/search/hub-spoke-api-fail-slave.cpp b/test/search/hub-spoke-api-fail-slave.cpp new file mode 100644 index 0000000000..f8b3070fb2 --- /dev/null +++ b/test/search/hub-spoke-api-fail-slave.cpp @@ -0,0 +1,64 @@ +/* + * Main authors: + * Mikael Zayenz Lagerkvist + * + * Copyright: + * Mikael Zayenz Lagerkvist, 2026 + * + * This file is part of Gecode, the generic constraint + * development environment: + * http://www.gecode.dev + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * This file must not compile: it is hub-spoke-api-pass.cpp with the + * spoke() override replaced by a Gecode 6 slave() override without + * override. Space deletes slave(), and a non-deleted function cannot + * override a deleted one. Successful compilation means that an old + * override would again compile and silently never be called. + * + */ + +#include + +namespace Test { namespace Search { + + /// Space overriding the meta-search hooks + class HookSpace : public Gecode::Space { + public: + HookSpace(void) {} + HookSpace(HookSpace& s) : Gecode::Space(s) {} + Gecode::Space* + copy(void) override { + return new HookSpace(*this); + } + bool + hub(const Gecode::MetaInfo&) override { + return true; + } + bool + slave(const Gecode::MetaInfo&) { + return true; + } + }; + +}} // namespace Test::Search + +// STATISTICS: test-search diff --git a/test/search/hub-spoke-api-pass.cpp b/test/search/hub-spoke-api-pass.cpp new file mode 100644 index 0000000000..9c684f6ac8 --- /dev/null +++ b/test/search/hub-spoke-api-pass.cpp @@ -0,0 +1,63 @@ +/* + * Main authors: + * Mikael Zayenz Lagerkvist + * + * Copyright: + * Mikael Zayenz Lagerkvist, 2026 + * + * This file is part of Gecode, the generic constraint + * development environment: + * http://www.gecode.dev + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * This file must compile: it overrides the Gecode 7 meta-search hooks + * hub() and spoke(). The files hub-spoke-api-fail-master.cpp and + * hub-spoke-api-fail-slave.cpp differ from it only in this comment and + * in one hook. + * + */ + +#include + +namespace Test { namespace Search { + + /// Space overriding the meta-search hooks + class HookSpace : public Gecode::Space { + public: + HookSpace(void) {} + HookSpace(HookSpace& s) : Gecode::Space(s) {} + Gecode::Space* + copy(void) override { + return new HookSpace(*this); + } + bool + hub(const Gecode::MetaInfo&) override { + return true; + } + bool + spoke(const Gecode::MetaInfo&) override { + return true; + } + }; + +}} // namespace Test::Search + +// STATISTICS: test-search