From dd28db3ab46471a295546160f25f8a921b940cb1 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Wed, 15 Nov 2023 08:32:47 -0800 Subject: [PATCH] check available Solvers once per program --- .../multisolver_interface/SolverFactory.cpp | 20 +++++++++++------ .../multisolver_interface/SolverFactory.h | 22 +++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/cpp/multisolver_interface/SolverFactory.cpp b/src/cpp/multisolver_interface/SolverFactory.cpp index ef50caa3d..f0b6932ee 100644 --- a/src/cpp/multisolver_interface/SolverFactory.cpp +++ b/src/cpp/multisolver_interface/SolverFactory.cpp @@ -8,18 +8,24 @@ #endif #include "LogUtils.h" #include "multisolver_interface/SolverFactory.h" +std::vector tmp; -SolverFactory::SolverFactory() { - _available_solvers.clear(); - if ((isXpress_available_ = LoadXpress::XpressIsCorrectlyInstalled())) { - _available_solvers.push_back(XPRESS_STR); - } +std::vector SolverLoader::GetAvailableSolvers() { + if (tmp.empty()) { + if (LoadXpress::XpressIsCorrectlyInstalled()) { + tmp.push_back(XPRESS_STR); + } #ifdef COIN_OR - _available_solvers.push_back(CLP_STR); - _available_solvers.push_back(CBC_STR); + tmp.push_back(CLP_STR); + tmp.push_back(CBC_STR); #endif + } + return tmp; } +SolverFactory::SolverFactory() + : _available_solvers(SolverLoader::GetAvailableSolvers()) {} + SolverAbstract::Ptr SolverFactory::create_solver( const std::string &solver_name, const SOLVER_TYPE solver_type) const { if (solver_name == "") { diff --git a/src/cpp/multisolver_interface/include/multisolver_interface/SolverFactory.h b/src/cpp/multisolver_interface/include/multisolver_interface/SolverFactory.h index 36949841d..332eedb71 100644 --- a/src/cpp/multisolver_interface/include/multisolver_interface/SolverFactory.h +++ b/src/cpp/multisolver_interface/include/multisolver_interface/SolverFactory.h @@ -8,6 +8,15 @@ enum class SOLVER_TYPE { INTEGER, CONTINUOUS }; const std::string UNKNOWN_STR("UNKNOWN"), COIN_STR("COIN"), CBC_STR("CBC"), CLP_STR("CLP"), XPRESS_STR("XPRESS"); +/*! + * \class class SolverLoader + * \brief Class to check if supported solvers are available + */ +class SolverLoader { + public: + static std::vector GetAvailableSolvers(); +}; + /*! * \class class SolverFactory * \brief Class to manage the creation of solvers from the different @@ -31,9 +40,8 @@ class SolverFactory { * @param solver_name : Name of the solver to use */ SolverAbstract::Ptr create_solver(const std::string &solver_name) const; - SolverAbstract::Ptr create_solver( - const std::string &solver_name, - SolverLogManager&log_manager) const; + SolverAbstract::Ptr create_solver(const std::string &solver_name, + SolverLogManager &log_manager) const; /** * @brief Creates and returns to an object solver from the wanted @@ -44,9 +52,9 @@ class SolverFactory { */ SolverAbstract::Ptr create_solver(const std::string &solver_name, const SOLVER_TYPE solver_type) const; - SolverAbstract::Ptr create_solver( - const std::string &solver_name, const SOLVER_TYPE solver_type, - SolverLogManager&log_manager) const; + SolverAbstract::Ptr create_solver(const std::string &solver_name, + const SOLVER_TYPE solver_type, + SolverLogManager &log_manager) const; /** * @brief Copy constructor : Creates and returns to an object solver from the @@ -63,5 +71,5 @@ class SolverFactory { */ const std::vector &get_solvers_list() const; - bool isXpress_available_ = false; + bool isXpress_available_ = false; };