Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use updated antares-solver API (output path changes) [ANT-2561] #977

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions src/cpp/lpnamer/main/ProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@ ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options)
}
}

std::filesystem::path ProblemGeneration::performAntaresSimulation() {
static std::string lowerCase(std::string data) {
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c) { return std::tolower(c); });
return data;
}
Comment on lines +64 to +68
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simulator only accepts e.g xpress, Xpansion only accepts e.g xpress. We just lowercase the input before giving it to Simulator.


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

optOptions.ortoolsSolver = lowerCase(solver_name_);
auto results =
Antares::API::PerformSimulation(options_.StudyPath(), optOptions);
Antares::API::PerformSimulation(options_.StudyPath(), output, optOptions);
// Add parallel

// Handle errors
Expand All @@ -74,7 +83,32 @@ std::filesystem::path ProblemGeneration::performAntaresSimulation() {
}

lps_ = std::move(results.antares_problems);
return {results.simulationPath};
}

static std::string getCurrentTimestamp() {
// Get the current time point
auto now = std::chrono::system_clock::now();

// Convert to time_t for formatting
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);

// Convert to tm structure
std::tm now_tm;
#ifdef _WIN32
localtime_s(&now_tm, &now_time_t); // Windows-specific
#else
localtime_r(&now_time_t, &now_tm); // POSIX-specific
#endif

// Format the timestamp
std::ostringstream oss;
oss << std::put_time(&now_tm, "%Y%m%d-%H%Meco");
return oss.str();
}

// Useful only for "API" mode
std::filesystem::path generateOutputName(const std::filesystem::path& study) {
return study / "output" / getCurrentTimestamp();
}

std::filesystem::path ProblemGeneration::updateProblems() {
Expand All @@ -92,8 +126,8 @@ std::filesystem::path ProblemGeneration::updateProblems() {
}

if (mode_ == SimulationInputMode::ANTARES_API) {
simulation_dir_ = performAntaresSimulation();
study_dir = options_.StudyPath();
simulation_dir_ = generateOutputName(study_dir);
}

if (mode_ == SimulationInputMode::FILE) {
Expand All @@ -117,14 +151,18 @@ std::filesystem::path ProblemGeneration::updateProblems() {
auto logger = ProblemGenerationLog::BuildLogger(log_file_path, std::cout,
"Problem Generation"s);

set_solver(study_dir, logger.get());

if (mode_ == SimulationInputMode::ANTARES_API) {
performAntaresSimulation(simulation_dir_);
}

auto master_formulation = options_.MasterFormulation();
auto additionalConstraintFilename_l =
options_.AdditionalConstraintsFilename();
auto weights_file = options_.WeightsFile();
auto unnamed_problems = options_.UnnamedProblems();

set_solver(study_dir, logger.get());

RunProblemGeneration(xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, archive_path, logger,
log_file_path, weights_file, unnamed_problems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <antares/solver/lps/LpsFromAntares.h>

#include <filesystem>
#include <string>
#include <optional>
#include <string>

// clang-format off
#include "antares-xpansion/lpnamer/input_reader/MpsTxtWriter.h"
#include "antares-xpansion/lpnamer/model/Problem.h"
#include "antares-xpansion/lpnamer/model/SimulationInputMode.h"
Expand All @@ -18,13 +19,14 @@
#include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h"
#include "ProblemGenerationOptions.h"
#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
// clang-format on

class ProblemGeneration {
public:
explicit ProblemGeneration(ProblemGenerationOptions& options);
virtual ~ProblemGeneration() = default;
std::filesystem::path updateProblems();
ProblemGenerationOptions& options_;
const ProblemGenerationOptions& options_;

private:
virtual void RunProblemGeneration(
Expand All @@ -49,11 +51,13 @@ class ProblemGeneration {
const std::vector<ProblemData>& mpsList, std::filesystem::path& lpDir_,
std::shared_ptr<ArchiveReader> reader,
const Antares::Solver::LpsFromAntares& lps);
virtual void set_solver(std::filesystem::path study_dir, ProblemGenerationLog::ProblemGenerationLogger* logger);
virtual void set_solver(
std::filesystem::path study_dir,
ProblemGenerationLog::ProblemGenerationLogger* logger);

Antares::Solver::LpsFromAntares lps_;
std::optional<SimulationInputMode> mode_;
virtual std::filesystem::path performAntaresSimulation();
virtual void performAntaresSimulation(const std::filesystem::path& output);
std::filesystem::path simulation_dir_;
std::string solver_name_;
};
8 changes: 2 additions & 6 deletions tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@ class ProblemGenerationSpyAndMock : public ProblemGeneration {
}

private:
std::filesystem::path performAntaresSimulation() override {
return options_.StudyPath() / "simulation";
}
void performAntaresSimulation(const std::filesystem::path&) override {}
void set_solver(
std::filesystem::path study_dir,
ProblemGenerationLog::ProblemGenerationLogger* logger) override {

}
ProblemGenerationLog::ProblemGenerationLogger* logger) override {}

public:
std::filesystem::path xpansion_output_dir_;
Expand Down
Loading