Skip to content

Commit

Permalink
Harden interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Jan 15, 2025
1 parent 10634f2 commit 462e534
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/cpp/lpnamer/main/ProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ static std::string solverXpansionToSimulator(const SolverConfig& in) {
// in could be Cbc or CBC depending on whether it is defined or not in the
// settings file
// Use lowerCase in any case to be robust to these subtleties
assert(islower(in.name));
if (in.name == "xpress") return "xpress";
if (in.name == "cbc" || in.name == "coin") return "coin";
assert(islower(in.Name()));
if (in.Name() == "xpress") return "xpress";
if (in.Name() == "cbc" || in.Name() == "coin") return "coin";
throw std::invalid_argument("Invalid solver");
}

Expand Down Expand Up @@ -259,16 +259,16 @@ std::vector<std::shared_ptr<Problem>> ProblemGeneration::getXpansionProblems(
switch (mode_.value()) {
case SimulationInputMode::FILE: {
FileProblemsProviderAdapter adapter(lpDir_, problem_names);
return adapter.provideProblems(solver_config_.name, solver_log_manager);
return adapter.provideProblems(solver_config_.Name(), solver_log_manager);
}
case SimulationInputMode::ARCHIVE: {
ZipProblemsProviderAdapter adapter(lpDir_, std::move(reader),
problem_names);
return adapter.provideProblems(solver_config_.name, solver_log_manager);
return adapter.provideProblems(solver_config_.Name(), solver_log_manager);
}
case SimulationInputMode::ANTARES_API: {
XpansionProblemsFromAntaresProvider adapter(lps);
return adapter.provideProblems(solver_config_.name, solver_log_manager);
return adapter.provideProblems(solver_config_.Name(), solver_log_manager);
}
default:
throw LogUtils::XpansionError<std::runtime_error>(
Expand Down Expand Up @@ -383,7 +383,7 @@ void ProblemGeneration::RunProblemGeneration(
}
MasterGeneration master_generation(
xpansion_output_dir, links, additionalConstraints, couplings,
master_formulation, solver_config_.name, logger, solver_log_manager);
master_formulation, solver_config_.Name(), logger, solver_log_manager);
(*logger)(LogUtils::LOGLEVEL::INFO)
<< "Problem Generation ran in: "
<< format_time_str(problem_generation_timer.elapsed()) << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void LinkProblemsGenerator::treat(
IProblemProviderPort *problem_provider,
IProblemVariablesProviderPort *variable_provider, IProblemWriter *writer) {
auto in_prblm =
problem_provider->provide_problem(solver_config_.name, solver_log_manager_);
problem_provider->provide_problem(solver_config_.Name(), solver_log_manager_);

treat(problem_name, couplings, in_prblm.get(), variable_provider, writer);
}
Expand Down Expand Up @@ -76,7 +76,7 @@ void LinkProblemsGenerator::treatloop(const std::filesystem::path &root,
auto adapter = std::make_unique<MPSFileProblemProviderAdapter>(
root, mps._problem_mps);
auto problem =
adapter->provide_problem(solver_config_.name, solver_log_manager_);
adapter->provide_problem(solver_config_.Name(), solver_log_manager_);
std::unique_ptr<IProblemVariablesProviderPort> variables_provider;
if (rename_problems_) {
variables_provider = std::make_unique<ProblemVariablesFileAdapter>(
Expand Down
30 changes: 26 additions & 4 deletions src/cpp/multisolver_interface/SolverConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

#include "antares-xpansion/multisolver_interface/SolverConfig.h"

#include <algorithm>
#include <utility>

const std::map<std::string, bool> SolverConfig::save_restore_support = {
{"clp", false}, {"cbc", false}, {"coin", false}, {"xpress", true}};
#include "antares-xpansion/xpansion_interfaces/LogUtils.h"

SolverConfig::SolverConfig(std::string solver_name) {
init(std::move(solver_name));
}
Expand All @@ -18,6 +17,29 @@ SolverConfig& SolverConfig::operator=(const std::string& rhs) {
return *this;
}
void SolverConfig::init(std::string solver_name) {
std::ranges::transform(solver_name, std::back_inserter(name), ::tolower);
//Locally defined constant to prevent static initialisation error
const std::map<std::string, bool> save_restore_support = {
{"clp", false}, {"cbc", false}, {"coin", false}, {"xpress", true}};
name = std::move(solver_name);
std::ranges::transform(name, name.begin(), ::tolower);
if (save_restore_support.find(name) == save_restore_support.end()) {
throw LogUtils::XpansionError<std::invalid_argument>("Invalid solver name",
LOGLOCATION);
}
save_restore_supported = save_restore_support.at(name);
use_save_restore = save_restore_supported;
}
std::filesystem::path SolverConfig::FileName(const std::string& problemName,
std::string solverName) {
const std::string save_ext = ".svf";
const std::string default_ext = ".mps";
if (problemName == "master") return {"master"};
SolverConfig solverConfig(std::move(solverName));
std::filesystem::path path{problemName};
if (solverConfig.use_save_restore) {
path.replace_extension(save_ext);
} else {
path.replace_extension(default_ext);
}
return path;
}
18 changes: 9 additions & 9 deletions src/cpp/multisolver_interface/SolverFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ SolverAbstract::Ptr SolverFactory::create_solver(
SolverAbstract::Ptr SolverFactory::create_solver(
const SolverConfig &solver_config) const {
SolverAbstract::Ptr ret;
if (solver_config.name.empty()) {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
if (solver_config.Name().empty()) {
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
} else if (isXpress_available_ && solver_config == XPRESS_STR) {
ret = std::make_shared<SolverXpress>();
}
Expand All @@ -71,7 +71,7 @@ SolverAbstract::Ptr SolverFactory::create_solver(
}
#endif
else {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
}
ret->init();
return ret;
Expand All @@ -80,8 +80,8 @@ SolverAbstract::Ptr SolverFactory::create_solver(
SolverAbstract::Ptr SolverFactory::create_solver(
const SolverConfig &solver_config, const SOLVER_TYPE solver_type) const {
SolverAbstract::Ptr ret;
if (solver_config.name.empty()) {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
if (solver_config.Name().empty()) {
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
} else if (isXpress_available_ && solver_config == XPRESS_STR) {
ret = std::make_shared<SolverXpress>();
}
Expand All @@ -93,16 +93,16 @@ SolverAbstract::Ptr SolverFactory::create_solver(
}
#endif
else {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
}
ret->init();
return ret;
}

SolverAbstract::Ptr SolverFactory::create_solver(
const SolverConfig &solver_config, SolverLogManager &log_manager) const {
if (solver_config.name.empty()) {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
if (solver_config.Name().empty()) {
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
}
SolverAbstract::Ptr ret;
if (isXpress_available_ && solver_config == XPRESS_STR) {
Expand All @@ -116,7 +116,7 @@ SolverAbstract::Ptr SolverFactory::create_solver(
}
#endif
else {
throw InvalidSolverNameException(solver_config.name, LOGLOCATION);
throw InvalidSolverNameException(solver_config.Name(), LOGLOCATION);
}
ret->init();
return ret;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <filesystem>
#include <map>
#include <string>

Expand All @@ -8,19 +9,22 @@
* Invariant: name is lowercase
*/
class SolverConfig {
static const std::map<std::string, bool> save_restore_support;
void init(std::string solver_name);
std::string name;
bool save_restore_supported{false};
bool use_save_restore{false};

public:

static std::filesystem::path FileName(const std::string& problemName, std::string solverName);
explicit SolverConfig(std::string name);
SolverConfig(SolverConfig&&) = default;
SolverConfig(const SolverConfig&) = default;
SolverConfig& operator=(const SolverConfig&) = default;
SolverConfig& operator=(SolverConfig&&) = default;
~SolverConfig() = default;

std::string name;
bool save_restore_supported{false};
[[nodiscard]] std::string Name() const { return name; }
~SolverConfig() = default;
bool operator==(const std::string& rhs) const;
SolverConfig& operator=(const std::string& rhs);
};
};

0 comments on commit 462e534

Please sign in to comment.