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

Simplify Problem generation commande line API #712

Closed
wants to merge 10 commits into from
22 changes: 3 additions & 19 deletions src/cpp/exe/full_run/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <boost/program_options.hpp>
#include <exception>
#include <iostream>
#include <string>

#include "FullRunOptionsParser.h"
#include "ProblemGeneration.h"
#include "ProblemGenerationLogger.h"
#include "RunProblemGeneration.h"
#include "StudyUpdateRunner.h"
#include "common_mpi.h"
namespace po = boost::program_options;
Expand All @@ -15,26 +14,11 @@ int main(int argc, char** argv) {
mpi::communicator world;
auto options_parser = FullRunOptionsParser();
std::filesystem::path xpansion_output_dir;
std::filesystem::path archive_path;
options_parser.Parse(argc, argv);
if (world.rank() == 0) {
try {
xpansion_output_dir = options_parser.XpansionOutputDir();
archive_path = options_parser.ArchivePath();

const auto log_file_path =
xpansion_output_dir / "lp" / "ProblemGenerationLog.txt";
auto logger = ProblemGenerationLog::BuildLogger(
log_file_path, std::cout, "Full Run - Problem Generation");

auto master_formulation = options_parser.MasterFormulation();
auto additionalConstraintFilename_l =
options_parser.AdditionalConstraintsFilename();
auto weights_file = options_parser.WeightsFile();
const auto unnamed_problems = options_parser.UnnamedProblems();
RunProblemGeneration(xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, archive_path, logger,
log_file_path, weights_file, unnamed_problems);
ProblemGeneration pbg(options_parser);
xpansion_output_dir = pbg.updateProblems();

} catch (std::exception& e) {
std::cerr << "error: " << e.what() << std::endl;
Expand Down
35 changes: 3 additions & 32 deletions src/cpp/exe/lpnamer/main.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@

#include <iostream>

#include "ProblemGeneration.h"
#include "ProblemGenerationExeOptions.h"
#include "ProblemGenerationLogger.h"
#include "RunProblemGeneration.h"

static const std::string LP_DIRNAME = "lp";

void CreateDirectories(const std::filesystem::path& output_path) {
if (!std::filesystem::exists(output_path)) {
std::filesystem::create_directories(output_path);
}
auto lp_path = output_path / LP_DIRNAME;
if (!std::filesystem::exists(lp_path)) {
std::filesystem::create_directories(lp_path);
}
}

int main(int argc, char** argv) {
try {
auto options_parser = ProblemGenerationExeOptions();
options_parser.Parse(argc, argv);

auto xpansion_output_dir = options_parser.XpansionOutputDir();
auto archive_path = options_parser.ArchivePath();
using namespace ProblemGenerationLog;
const auto log_file_path =
xpansion_output_dir / "lp" / "ProblemGenerationLog.txt";

CreateDirectories(xpansion_output_dir);
auto logger = ProblemGenerationLog::BuildLogger(log_file_path, std::cout,
"Problem Generation");

auto master_formulation = options_parser.MasterFormulation();
auto additionalConstraintFilename_l =
options_parser.AdditionalConstraintsFilename();
auto weights_file = options_parser.WeightsFile();
auto unnamed_problems = options_parser.UnnamedProblems();
RunProblemGeneration(xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, archive_path, logger,
log_file_path, weights_file, unnamed_problems);
ProblemGeneration pbg(options_parser);
pbg.updateProblems();

return 0;
} catch (std::exception& e) {
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/full_run/FullRunOptionsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FullRunOptionsParser::FullRunOptionsParser() : ProblemGenerationExeOptions() {
"path to json solution file");
}
void FullRunOptionsParser::Parse(unsigned int argc, const char* const* argv) {
OptionsParser::Parse(argc, argv);
ProblemGenerationExeOptions::Parse(argc, argv);
SetMethod();
}
void FullRunOptionsParser::SetMethod() {
Expand Down
10 changes: 6 additions & 4 deletions src/cpp/full_run/include/FullRunOptionsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class FullRunOptionsParser : public ProblemGenerationExeOptions {
public:
FullRunOptionsParser();
virtual ~FullRunOptionsParser() = default;
~FullRunOptionsParser() override = default;
void Parse(unsigned int argc, const char* const* argv) override;
class FullRunOptionInvalidMethod : public std::runtime_error {
public:
Expand All @@ -18,11 +18,13 @@ class FullRunOptionsParser : public ProblemGenerationExeOptions {
" is not supported! Please choose one of: " +
FullRunOptionsParser::ListAvailalbleMethods()) {}
};
BENDERSMETHOD Method() const { return method_; };
std::filesystem::path BendersOptionsFile() const {
[[nodiscard]] BENDERSMETHOD Method() const { return method_; };
[[nodiscard]] std::filesystem::path BendersOptionsFile() const {
return benders_options_file_;
}
std::filesystem::path SolutionFile() const { return solutionFile_; }
[[nodiscard]] std::filesystem::path SolutionFile() const {
return solutionFile_;
}
static inline std::string ListAvailalbleMethods() {
return "benders,\n benders_by_batch,\n mergeMPS\n";
}
Expand Down
14 changes: 8 additions & 6 deletions src/cpp/lpnamer/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
# ---------------------------------------------------------------------------

add_library (problem_generation_main STATIC
${CMAKE_CURRENT_SOURCE_DIR}/RunProblemGeneration.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ProblemGenerationExeOptions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ProblemGeneration.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/ProblemGeneration.h
${CMAKE_CURRENT_SOURCE_DIR}/include/ProblemGenerationOptions.h
)

target_link_libraries (problem_generation_main
Expand All @@ -26,11 +28,11 @@ target_link_libraries (problem_generation_main

get_target_property(helpers_include helpers INTERFACE_INCLUDE_DIRECTORIES)

target_include_directories (problem_generation_main
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${helpers_include}
)
target_include_directories(problem_generation_main
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${helpers_include}
)


add_library (${PROJECT_NAME}::problem_generation_main ALIAS problem_generation_main)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//
// Created by marechaljas on 27/10/23.
//

#include "RunProblemGeneration.h"
#include "include/ProblemGeneration.h"

#include <execution>
#include <iostream>
Expand All @@ -15,6 +18,7 @@
#include "MasterGeneration.h"
#include "MasterProblemBuilder.h"
#include "MpsTxtWriter.h"
#include "ProblemGenerationLogger.h"
#include "ProblemVariablesFromProblemAdapter.h"
#include "ProblemVariablesZipAdapter.h"
#include "StringManip.h"
Expand All @@ -24,6 +28,54 @@
#include "ZipProblemsProviderAdapter.h"
#include "config.h"

static const std::string LP_DIRNAME = "lp";

void CreateDirectories(const std::filesystem::path& output_path) {
if (!std::filesystem::exists(output_path)) {
std::filesystem::create_directories(output_path);
}
auto lp_path = output_path / LP_DIRNAME;
if (!std::filesystem::exists(lp_path)) {
std::filesystem::create_directories(lp_path);
}
}

ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options)
: options_(options) {}
std::filesystem::path ProblemGeneration::updateProblems() {
const auto xpansion_output_dir = options_.XpansionOutputDir();
const auto archive_path = options_.ArchivePath();

auto deduced_xpansion_output_dir =
options_.deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path);

const auto log_file_path =
xpansion_output_dir / "lp" / "ProblemGenerationLog.txt";

CreateDirectories(deduced_xpansion_output_dir);
auto logger = ProblemGenerationLog::BuildLogger(log_file_path, std::cout,
"Problem Generation");

auto master_formulation = options_.MasterFormulation();
auto additionalConstraintFilename_l =
options_.AdditionalConstraintsFilename();
auto weights_file = options_.WeightsFile();
auto unnamed_problems = options_.UnnamedProblems();
std::filesystem::path deduced_archive_path;
try {
deduced_archive_path = options_.deduceArchivePathIfEmpty(
deduced_xpansion_output_dir, archive_path);
} catch (const ProblemGenerationOptions::MismatchedParameters& m) {
(*logger)(LogUtils::LOGLEVEL::ERR) << m.what();
throw;
}

RunProblemGeneration(deduced_xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, deduced_archive_path,
logger, log_file_path, weights_file, unnamed_problems);
return deduced_xpansion_output_dir;
}

struct Version {
explicit Version(std::string_view version) {
auto split_version = StringManip::split(StringManip::trim(version), '.');
Expand Down Expand Up @@ -58,7 +110,7 @@ struct Version {

std::shared_ptr<ArchiveReader> InstantiateZipReader(
const std::filesystem::path& antares_archive_path);
void ProcessWeights(
void ProblemGeneration::ProcessWeights(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& antares_archive_path,
const std::filesystem::path& weights_file,
Expand All @@ -77,7 +129,7 @@ void ProcessWeights(
yearly_weight_writer.CreateWeightFile();
}

void ExtractUtilsFiles(
void ProblemGeneration::ExtractUtilsFiles(
const std::filesystem::path& antares_archive_path,
const std::filesystem::path& xpansion_output_dir,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger) {
Expand Down Expand Up @@ -126,7 +178,7 @@ std::vector<std::shared_ptr<Problem>> getXpansionProblems(
return adapter->provideProblems(solver_name, solver_log_manager);
}

void RunProblemGeneration(
void ProblemGeneration::RunProblemGeneration(
const std::filesystem::path& xpansion_output_dir,
const std::string& master_formulation,
const std::string& additionalConstraintFilename_l,
Expand Down Expand Up @@ -171,13 +223,12 @@ void RunProblemGeneration(
Couplings couplings;
LinkProblemsGenerator linkProblemsGenerator(
lpDir_, links, solver_name, logger, solver_log_manager, rename_problems);
std::shared_ptr<ArchiveReader> reader =
InstantiateZipReader(antares_archive_path);
std::shared_ptr<ArchiveReader> reader =
InstantiateZipReader(antares_archive_path);

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

std::vector<std::pair<std::shared_ptr<Problem>, ProblemData>>
problems_and_data;
Expand Down
48 changes: 45 additions & 3 deletions src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ namespace po = boost::program_options;
ProblemGenerationExeOptions::ProblemGenerationExeOptions()
: OptionsParser("Problem Generation exe") {
AddOptions()("help,h", "produce help message")(
"output,o",
po::value<std::filesystem::path>(&xpansion_output_dir_)->required(),
"output,o", po::value<std::filesystem::path>(&xpansion_output_dir_),
"antares-xpansion study output")(
"archive,a", po::value<std::filesystem::path>(&archive_path_)->required(),
"archive,a", po::value<std::filesystem::path>(&archive_path_),
"antares-xpansion study zip")(
"formulation,f",
po::value<std::string>(&master_formulation_)->default_value("relaxed"),
Expand All @@ -20,4 +19,47 @@ ProblemGenerationExeOptions::ProblemGenerationExeOptions()
"user weights file")("unnamed-problems,n",
po::bool_switch(&unnamed_problems_),
"use this option if unnamed problems are provided");
}
void ProblemGenerationExeOptions::Parse(unsigned int argc,
const char *const *argv) {
OptionsParser::Parse(argc, argv);
if (XpansionOutputDir().empty() && ArchivePath().empty()) {
auto log_location = LOGLOCATION;
auto msg = "Both output directory and archive path are empty";
throw ProblemGenerationOptions::MissingParameters(msg, log_location);
}
}

std::filesystem::path ProblemGenerationExeOptions::deduceArchivePathIfEmpty(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& archive_path) const {
if (archive_path.empty() && !xpansion_output_dir.empty()) {
if (xpansion_output_dir.string().find("-Xpansion") == std::string::npos) {
auto log_location = LOGLOCATION;
auto msg =
"Archive path is missing and output path does not contains"
" \"-Xpansion\" suffixe. Can't deduce archive file name.";
throw MismatchedParameters(msg, log_location);
}
auto deduced_archive_path = xpansion_output_dir;
auto dir_name = deduced_archive_path.stem().string();
dir_name = dir_name.substr(0, dir_name.find("-Xpansion"));
deduced_archive_path =
deduced_archive_path.replace_filename(dir_name).replace_extension(
".zip");
return deduced_archive_path;
}
return archive_path;
}

std::filesystem::path ProblemGenerationExeOptions::deduceXpansionDirIfEmpty(
std::filesystem::path xpansion_output_dir,
const std::filesystem::path& archive_path) const {
if (xpansion_output_dir.empty() && !archive_path.empty()) {
auto deduced_dir = archive_path;
deduced_dir = deduced_dir.replace_filename(
deduced_dir.stem().replace_extension("").string() + "-Xpansion");
return deduced_dir;
}
return xpansion_output_dir;
}
39 changes: 39 additions & 0 deletions src/cpp/lpnamer/main/include/ProblemGeneration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by marechaljas on 27/10/23.
//

#pragma once

#include <filesystem>
#include <string>

#include "ProblemGenerationExeOptions.h"
#include "ProblemGenerationLogger.h"
#include "ProblemGenerationOptions.h"

class ProblemGeneration {
public:
explicit ProblemGeneration(ProblemGenerationOptions& options);
std::filesystem::path updateProblems();
ProblemGenerationOptions& options_;

virtual void RunProblemGeneration(
const std::filesystem::path& xpansion_output_dir,
const std::string& master_formulation,
const std::string& additionalConstraintFilename_l,
const std::filesystem::path& archive_path,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger,
const std::filesystem::path& log_file_path,
const std::filesystem::path& weights_file, bool unnamed_problems);

private:
void ProcessWeights(
const std::filesystem::path& xpansion_output_dir,
const std::filesystem::path& antares_archive_path,
const std::filesystem::path& weights_file,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
void ExtractUtilsFiles(
const std::filesystem::path& antares_archive_path,
const std::filesystem::path& xpansion_output_dir,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
};
Loading
Loading