Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Oct 27, 2023
1 parent 95265ed commit 225bd24
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 121 deletions.
21 changes: 3 additions & 18 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 @@ -19,22 +18,8 @@ int main(int argc, char** argv) {
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);
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,40 @@
#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) {}
void ProblemGeneration::updateProblems() {
auto xpansion_output_dir = options_.XpansionOutputDir();
auto archive_path = options_.ArchivePath();
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_.MasterFormulation();
auto additionalConstraintFilename_l =
options_.AdditionalConstraintsFilename();
auto weights_file = options_.WeightsFile();
auto unnamed_problems = options_.UnnamedProblems();
RunProblemGeneration(xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, archive_path, logger,
log_file_path, weights_file, unnamed_problems);
}

struct Version {
explicit Version(std::string_view version) {
auto split_version = StringManip::split(StringManip::trim(version), '.');
Expand Down Expand Up @@ -58,7 +96,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 +115,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 +164,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 +209,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
6 changes: 5 additions & 1 deletion src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ 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);
}
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);
void 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);
};
6 changes: 5 additions & 1 deletion src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include <vector>

#include "OptionsParser.h"
#include "ProblemGenerationOptions.h"

class ProblemGenerationExeOptions : public OptionsParser {
class ProblemGenerationExeOptions : public OptionsParser,
public ProblemGenerationOptions {
private:
std::filesystem::path xpansion_output_dir_;
std::string master_formulation_;
Expand All @@ -30,5 +32,7 @@ class ProblemGenerationExeOptions : public OptionsParser {
std::filesystem::path WeightsFile() const { return weights_file_; }
std::vector<int> ActiveYears() const { return active_years_; }
bool UnnamedProblems() const { return unnamed_problems_; }

void Parse(unsigned int argc, const char *const *argv) override;
};
#endif // ANTARES_XPANSION_SRC_CPP_LPNAMER_MAIN_INCLUDE_PROBLEMGENERATIONEXEOPTIONS_H
20 changes: 20 additions & 0 deletions src/cpp/lpnamer/main/include/ProblemGenerationOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by marechaljas on 27/10/23.
//

#pragma once

#include <filesystem>
#include <vector>

class ProblemGenerationOptions {
public:
virtual ~ProblemGenerationOptions() = default;
[[nodiscard]] virtual std::filesystem::path XpansionOutputDir() const = 0;
[[nodiscard]] virtual std::string MasterFormulation() const = 0;
[[nodiscard]] virtual std::string AdditionalConstraintsFilename() const = 0;
[[nodiscard]] virtual std::filesystem::path ArchivePath() const = 0;
[[nodiscard]] virtual std::filesystem::path WeightsFile() const = 0;
[[nodiscard]] virtual std::vector<int> ActiveYears() const = 0;
[[nodiscard]] virtual bool UnnamedProblems() const = 0;
};
27 changes: 0 additions & 27 deletions src/cpp/lpnamer/main/include/RunProblemGeneration.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/cpp/lpnamer/problem_modifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ add_library(lp_namer_problem_modifier STATIC
${CMAKE_CURRENT_SOURCE_DIR}/ZipProblemsProviderAdapter.h
${CMAKE_CURRENT_SOURCE_DIR}/ProblemVariablesFromProblemAdapter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ProblemVariablesFromProblemAdapter.h
)
)

target_include_directories (lp_namer_problem_modifier
PUBLIC
Expand Down
1 change: 1 addition & 0 deletions src/python/antares_xpansion/full_run_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def run(self):

os.chdir(lp_path)
self.logger.info(f"Current directory is now: {os.getcwd()}")
self.logger.info(f"Command is {self.full_command()}")
ret = subprocess.run(
self.full_command(), shell=False, stdout=sys.stdout, stderr=sys.stderr,
encoding='utf-8')
Expand Down
Loading

0 comments on commit 225bd24

Please sign in to comment.