Skip to content

Commit

Permalink
Solver config
Browse files Browse the repository at this point in the history
Helper to handle solver_names casse
  • Loading branch information
JasonMarechal25 committed Jan 13, 2025
1 parent fa9c8fa commit 10634f2
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 75 deletions.
35 changes: 17 additions & 18 deletions src/cpp/lpnamer/main/ProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,26 @@ ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options)
}
}

static std::string lowerCase(std::string data) {
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c) { return std::tolower(c); });
return data;
namespace {
bool islower(std::string_view str) {
return std::ranges::all_of(str, [](char c) { return std::islower(c); });
}
}

static std::string solverXpansionToSimulator(const std::string& in) {
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
std::string lower_case_in = lowerCase(in);
if (lower_case_in == "xpress") return "xpress";
if (lower_case_in == "cbc" || lower_case_in == "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");
}

void ProblemGeneration::performAntaresSimulation(
const std::filesystem::path& output) {
Antares::Solver::Optimization::OptimizationOptions optOptions;

optOptions.ortoolsSolver = solverXpansionToSimulator(solver_name_);
optOptions.ortoolsSolver = solverXpansionToSimulator(solver_config_);
auto results =
Antares::API::PerformSimulation(options_.StudyPath(), output, optOptions);
// Add parallel
Expand Down Expand Up @@ -249,7 +248,7 @@ void validateMasterFormulation(
* @return
*/
std::vector<std::shared_ptr<Problem>> ProblemGeneration::getXpansionProblems(
SolverLogManager& solver_log_manager, const std::string& solver_name,
SolverLogManager& solver_log_manager, SolverConfig solver_name,
const std::vector<ProblemData>& mpsList, std::filesystem::path& lpDir_,
std::shared_ptr<ArchiveReader> reader,
const Antares::Solver::LpsFromAntares& lps = {}) {
Expand All @@ -260,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_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_name, solver_log_manager);
return adapter.provideProblems(solver_config_.name, solver_log_manager);
}
case SimulationInputMode::ANTARES_API: {
XpansionProblemsFromAntaresProvider adapter(lps);
return adapter.provideProblems(solver_name, solver_log_manager);
return adapter.provideProblems(solver_config_.name, solver_log_manager);
}
default:
throw LogUtils::XpansionError<std::runtime_error>(
Expand Down Expand Up @@ -319,14 +318,14 @@ void ProblemGeneration::RunProblemGeneration(
auto solver_log_manager = SolverLogManager(log_file_path);
Couplings couplings;
LinkProblemsGenerator linkProblemsGenerator(
lpDir_, links, solver_name_, logger, solver_log_manager, rename_problems);
lpDir_, links, solver_config_, logger, solver_log_manager, rename_problems);
std::shared_ptr<ArchiveReader> reader =
antares_archive_path.empty() ? std::make_shared<ArchiveReader>()
: InstantiateZipReader(antares_archive_path);

/* Main stuff */
std::vector<std::shared_ptr<Problem>> xpansion_problems = getXpansionProblems(
solver_log_manager, solver_name_, mpsList, lpDir_, reader, lps_);
solver_log_manager, solver_config_, mpsList, lpDir_, reader, lps_);

std::vector<std::pair<std::shared_ptr<Problem>, ProblemData>>
problems_and_data;
Expand Down Expand Up @@ -384,7 +383,7 @@ void ProblemGeneration::RunProblemGeneration(
}
MasterGeneration master_generation(
xpansion_output_dir, links, additionalConstraints, couplings,
master_formulation, solver_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 All @@ -394,7 +393,7 @@ void ProblemGeneration::set_solver(
ProblemGenerationLog::ProblemGenerationLogger* logger) {
SettingsReader settingsReader(
study_dir / "user" / "expansion" / "settings.ini", logger);
solver_name_ = settingsReader.Solver();
solver_config_ = settingsReader.Solver();
}

std::shared_ptr<ArchiveReader> InstantiateZipReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h"
#include "ProblemGenerationOptions.h"
#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
#include "antares-xpansion/multisolver_interface/SolverConfig.h"
// clang-format on

class ProblemGeneration {
Expand Down Expand Up @@ -47,7 +48,7 @@ class ProblemGeneration {
const std::filesystem::path& xpansion_output_dir,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
std::vector<std::shared_ptr<Problem>> getXpansionProblems(
SolverLogManager& solver_log_manager, const std::string& solver_name,
SolverLogManager& solver_log_manager, SolverConfig solver_name,
const std::vector<ProblemData>& mpsList, std::filesystem::path& lpDir_,
std::shared_ptr<ArchiveReader> reader,
const Antares::Solver::LpsFromAntares& lps);
Expand All @@ -59,5 +60,5 @@ class ProblemGeneration {
std::optional<SimulationInputMode> mode_;
virtual void performAntaresSimulation(const std::filesystem::path& output);
std::filesystem::path simulation_dir_;
std::string solver_name_;
SolverConfig solver_config_{"Coin"};
};
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_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_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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <utility>

#include "antares-xpansion/multisolver_interface/SolverConfig.h"
#include "antares-xpansion/multisolver_interface/SolverFactory.h"
std::shared_ptr<Problem> MPSFileProblemProviderAdapter::provide_problem(
const std::string& solver_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

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

#include <filesystem>
#include <memory>
#include <mutex>
Expand All @@ -10,14 +8,16 @@
#include "antares-xpansion/helpers/ArchiveReader.h"
#include "antares-xpansion/helpers/ArchiveWriter.h"
#include "antares-xpansion/helpers/FileInBuffer.h"
#include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h"
#include "antares-xpansion/lpnamer/input_reader/MpsTxtWriter.h"
#include "antares-xpansion/lpnamer/input_reader/VariableFileReader.h"
#include "antares-xpansion/lpnamer/problem_modifier/IProblemProviderPort.h"
#include "antares-xpansion/lpnamer/problem_modifier/IProblemVariablesProviderPort.h"
#include "antares-xpansion/lpnamer/problem_modifier/IProblemWriter.h"
#include "antares-xpansion/lpnamer/input_reader/MpsTxtWriter.h"
#include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h"
#include "antares-xpansion/lpnamer/problem_modifier/ProblemModifier.h"
#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
#include "antares-xpansion/multisolver_interface/SolverConfig.h"
#include "antares-xpansion/xpansion_interfaces/StringManip.h"
#include "antares-xpansion/lpnamer/input_reader/VariableFileReader.h"

const std::string CANDIDATES_INI{"candidates.ini"};
const std::string STRUCTURE_FILE{"structure.txt"};
Expand All @@ -30,11 +30,11 @@ class LinkProblemsGenerator {
public:
LinkProblemsGenerator(
std::filesystem::path& lpDir, const std::vector<ActiveLink>& links,
std::string solver_name,
SolverConfig solver_config,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger,
SolverLogManager& solver_log_manager, bool rename_problems)
: _links(links),
_solver_name(std::move(solver_name)),
solver_config_(std::move(solver_config)),
lpDir_(lpDir),
logger_(std::move(logger)),
rename_problems_(rename_problems),
Expand All @@ -53,7 +53,7 @@ class LinkProblemsGenerator {

private:
const std::vector<ActiveLink>& _links;
std::string _solver_name;
SolverConfig solver_config_;
std::filesystem::path lpDir_ = "";
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger_;
mutable std::mutex coupling_mutex_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "antares-xpansion/lpnamer/problem_modifier/IProblemProviderPort.h"
#include "antares-xpansion/multisolver_interface/SolverConfig.h"
class MPSFileProblemProviderAdapter : public IProblemProviderPort {
public:
MPSFileProblemProviderAdapter(std::filesystem::path root,
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/multisolver_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
# ---------------------------------------------------------------------------
list(APPEND Solver_sources
${CMAKE_CURRENT_LIST_DIR}/SolverFactory.cpp
${CMAKE_CURRENT_LIST_DIR}/SolverConfig.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/Solver.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/SolverAbstract.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/SolverConfig.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/SolverFactory.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/dynamic_library.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/multisolver_interface/environment.h
Expand Down
23 changes: 23 additions & 0 deletions src/cpp/multisolver_interface/SolverConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#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}};
SolverConfig::SolverConfig(std::string solver_name) {
init(std::move(solver_name));
}
bool SolverConfig::operator==(const std::string& rhs) const {
return std::ranges::equal(
name, rhs, [](char a, char b) { return ::tolower(a) == ::tolower(b); });
}
SolverConfig& SolverConfig::operator=(const std::string& rhs) {
init(rhs);
return *this;
}
void SolverConfig::init(std::string solver_name) {
std::ranges::transform(solver_name, std::back_inserter(name), ::tolower);
save_restore_supported = save_restore_support.at(name);
}
Loading

0 comments on commit 10634f2

Please sign in to comment.