Skip to content

Commit

Permalink
extract structure reader
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 13, 2024
1 parent 9549897 commit ac94e80
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 49 deletions.
4 changes: 3 additions & 1 deletion src/cpp/benders/benders_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down
47 changes: 47 additions & 0 deletions src/cpp/benders/benders_core/CouplingMapGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <antares-xpansion/benders/benders_core/CouplingMapGenerator.h>
#include <antares-xpansion/xpansion_interfaces/LogUtils.h>

struct InvalidStructureFile
: public LogUtils::XpansionError<std::runtime_error> {
using LogUtils::XpansionError<std::runtime_error>::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;
}
43 changes: 0 additions & 43 deletions src/cpp/benders/benders_core/common.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "antares-xpansion/benders/benders_core/common.h"

#include <antares-xpansion/xpansion_interfaces/LogUtils.h>
/*!
* \brief Return the distance between two point using 2-norm
*
Expand Down Expand Up @@ -30,50 +29,8 @@ void usage(int argc) {
}
}

struct InvalidStructureFile
: public LogUtils::XpansionError<std::runtime_error> {
using LogUtils::XpansionError<std::runtime_error>::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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include "common.h"

struct CouplingMapGenerator {
static CouplingMap build_input(const std::filesystem::path &structure_path);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <filesystem>

#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"
Expand Down
3 changes: 1 addition & 2 deletions src/cpp/benders/merge_mps/MergeMPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <filesystem>

#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,
Expand Down
1 change: 1 addition & 0 deletions tests/cpp/outer_loop/outer_loop_test.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit ac94e80

Please sign in to comment.