From ac94e800a83b1f091711b2265559cb9203e8264c Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Wed, 13 Nov 2024 14:19:17 +0100 Subject: [PATCH] extract structure reader --- src/cpp/benders/benders_core/CMakeLists.txt | 4 +- .../benders_core/CouplingMapGenerator.cpp | 47 +++++++++++++++++++ src/cpp/benders/benders_core/common.cpp | 43 ----------------- .../benders_core/CouplingMapGenerator.h | 6 +++ .../benders/benders_core/common.h | 3 -- src/cpp/benders/factories/BendersFactory.cpp | 1 + src/cpp/benders/merge_mps/MergeMPS.cpp | 3 +- tests/cpp/outer_loop/outer_loop_test.cpp | 1 + 8 files changed, 59 insertions(+), 49 deletions(-) create mode 100644 src/cpp/benders/benders_core/CouplingMapGenerator.cpp create mode 100644 src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CouplingMapGenerator.h diff --git a/src/cpp/benders/benders_core/CMakeLists.txt b/src/cpp/benders/benders_core/CMakeLists.txt index c4bd7ac2c..af3da6c5c 100644 --- a/src/cpp/benders/benders_core/CMakeLists.txt +++ b/src/cpp/benders/benders_core/CMakeLists.txt @@ -25,6 +25,7 @@ target_sources(benders_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CriterionComputation.cpp ${CMAKE_CURRENT_SOURCE_DIR}/CriterionInputDataReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/VariablesGroup.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/CouplingMapGenerator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersBase.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersMathLogger.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h @@ -43,11 +44,12 @@ target_sources(benders_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/CriterionComputation.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/VariablesGroup.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/CouplingMapGenerator.h ) add_library(antaresXpansion::benders_core ALIAS benders_core) - +#set_target_properties(benders_core PROPERTIES UNITY_BUILD ON) target_include_directories(benders_core PUBLIC $ diff --git a/src/cpp/benders/benders_core/CouplingMapGenerator.cpp b/src/cpp/benders/benders_core/CouplingMapGenerator.cpp new file mode 100644 index 000000000..7c33e399a --- /dev/null +++ b/src/cpp/benders/benders_core/CouplingMapGenerator.cpp @@ -0,0 +1,47 @@ +#include +#include + +struct InvalidStructureFile + : public LogUtils::XpansionError { + using LogUtils::XpansionError::XpansionError; +}; + +/*! + * \brief Build the input from the structure file + * + * Function to build the map linking each problem name to its variables and + *their id + * + * \param root : root of the structure file + * + * \param summary_name : name of the structure file + * + * \param coupling_map : empty map to increment + * + * \note The id in the coupling_map is that of the variable in the solver + *responsible for the creation of the structure file. + */ +CouplingMap CouplingMapGenerator::build_input( + const std::filesystem::path &structure_path) { + CouplingMap coupling_map; + std::ifstream summary(structure_path, std::ios::in); + if (!summary) { + std::cout << "Cannot open structure file " << structure_path << std::endl; + return coupling_map; + } + std::string line; + + while (std::getline(summary, line)) { + std::stringstream buffer(line); + std::string problem_name; + std::string variable_name; + int variable_id; + buffer >> problem_name; + buffer >> variable_name; + buffer >> variable_id; + coupling_map[problem_name][variable_name] = variable_id; + } + + summary.close(); + return coupling_map; +} \ No newline at end of file diff --git a/src/cpp/benders/benders_core/common.cpp b/src/cpp/benders/benders_core/common.cpp index b6b9db61d..4c50a8ec5 100644 --- a/src/cpp/benders/benders_core/common.cpp +++ b/src/cpp/benders/benders_core/common.cpp @@ -1,6 +1,5 @@ #include "antares-xpansion/benders/benders_core/common.h" -#include /*! * \brief Return the distance between two point using 2-norm * @@ -30,50 +29,8 @@ void usage(int argc) { } } -struct InvalidStructureFile - : public LogUtils::XpansionError { - using LogUtils::XpansionError::XpansionError; -}; -/*! - * \brief Build the input from the structure file - * - * Function to build the map linking each problem name to its variables and - *their id - * - * \param root : root of the structure file - * - * \param summary_name : name of the structure file - * - * \param coupling_map : empty map to increment - * - * \note The id in the coupling_map is that of the variable in the solver - *responsible for the creation of the structure file. - */ -CouplingMap CouplingMapGenerator::build_input( - const std::filesystem::path &structure_path) { - CouplingMap coupling_map; - std::ifstream summary(structure_path, std::ios::in); - if (!summary) { - std::cout << "Cannot open structure file " << structure_path << std::endl; - return coupling_map; - } - std::string line; - while (std::getline(summary, line)) { - std::stringstream buffer(line); - std::string problem_name; - std::string variable_name; - int variable_id; - buffer >> problem_name; - buffer >> variable_name; - buffer >> variable_id; - coupling_map[problem_name][variable_name] = variable_id; - } - - summary.close(); - return coupling_map; -} Json::Value get_json_file_content(const std::filesystem::path &json_file) { std::ifstream input_file_l(json_file, std::ifstream::binary); diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CouplingMapGenerator.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CouplingMapGenerator.h new file mode 100644 index 000000000..0af84c7a0 --- /dev/null +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CouplingMapGenerator.h @@ -0,0 +1,6 @@ +#pragma once +#include "common.h" + +struct CouplingMapGenerator { + static CouplingMap build_input(const std::filesystem::path &structure_path); +}; diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/common.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/common.h index f8af018f7..795b4f15d 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/common.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/common.h @@ -191,7 +191,4 @@ struct BendersBaseOptions : public BaseOptions { }; void usage(int argc); -struct CouplingMapGenerator { - static CouplingMap build_input(const std::filesystem::path &structure_path); -}; Json::Value get_json_file_content(const std::filesystem::path &json_file); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index b001b6589..524f69816 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -4,6 +4,7 @@ #include #include "antares-xpansion/benders/benders_by_batch/BendersByBatch.h" +#include "antares-xpansion/benders/benders_core/CouplingMapGenerator.h" #include "antares-xpansion/benders/benders_core/MasterUpdate.h" #include "antares-xpansion/benders/benders_core/StartUp.h" #include "antares-xpansion/benders/benders_mpi/BendersMpiOuterLoop.h" diff --git a/src/cpp/benders/merge_mps/MergeMPS.cpp b/src/cpp/benders/merge_mps/MergeMPS.cpp index eedc7b2b7..2eb7c34c7 100644 --- a/src/cpp/benders/merge_mps/MergeMPS.cpp +++ b/src/cpp/benders/merge_mps/MergeMPS.cpp @@ -2,8 +2,7 @@ #include -#include "antares-xpansion/helpers/ArchiveReader.h" -#include "antares-xpansion/xpansion_interfaces/LogUtils.h" +#include "antares-xpansion/benders/benders_core/CouplingMapGenerator.h" #include "antares-xpansion/helpers/Timer.h" MergeMPS::MergeMPS(const MergeMPSOptions &options, Logger &logger, diff --git a/tests/cpp/outer_loop/outer_loop_test.cpp b/tests/cpp/outer_loop/outer_loop_test.cpp index 53f607451..98f2ffc63 100644 --- a/tests/cpp/outer_loop/outer_loop_test.cpp +++ b/tests/cpp/outer_loop/outer_loop_test.cpp @@ -1,4 +1,5 @@ +#include "antares-xpansion/benders/benders_core/CouplingMapGenerator.h" #include "antares-xpansion/benders/benders_core/CriterionInputDataReader.h" #include "antares-xpansion/benders/benders_core/MasterUpdate.h" #include "antares-xpansion/benders/benders_core/VariablesGroup.h"