From b7145031e94607355ae30f683f2297806a1f2e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jason=20Mar=C3=A9chal?= <45510813+JasonMarechal25@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:32:16 +0100 Subject: [PATCH] Move json reader where it is used (#948) Json reader is specifically made for and used by StudyUpdater --------- Co-authored-by: Peter Mitri --- src/cpp/helpers/CMakeLists.txt | 1 - src/cpp/study-updater/CMakeLists.txt | 12 +- .../JsonXpansionReader.cpp | 2 +- .../LinkParametersCSVOverwriter.cpp | 3 +- src/cpp/study-updater/StudyUpdater.cpp | 2 +- .../private}/JsonXpansionReader.h | 0 tests/cpp/CMakeLists.txt | 17 +- tests/cpp/helpers/CMakeLists.txt | 2 - tests/cpp/helpers/JsonXpansionReaderTest.cc | 201 ------------------ tests/cpp/study_updater/CMakeLists.txt | 21 ++ .../study_updater/JsonXpansionReaderTest.cc | 200 +++++++++++++++++ 11 files changed, 241 insertions(+), 220 deletions(-) rename src/cpp/{helpers => study-updater}/JsonXpansionReader.cpp (95%) rename src/cpp/{helpers/include/antares-xpansion/helpers => study-updater/private}/JsonXpansionReader.h (100%) delete mode 100644 tests/cpp/helpers/JsonXpansionReaderTest.cc create mode 100644 tests/cpp/study_updater/CMakeLists.txt create mode 100644 tests/cpp/study_updater/JsonXpansionReaderTest.cc diff --git a/src/cpp/helpers/CMakeLists.txt b/src/cpp/helpers/CMakeLists.txt index 72fb82baf..417af2531 100644 --- a/src/cpp/helpers/CMakeLists.txt +++ b/src/cpp/helpers/CMakeLists.txt @@ -22,7 +22,6 @@ target_sources(helpers PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ArchiveReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ArchiveWriter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/FileInBuffer.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/JsonXpansionReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/OptionsParser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/AreaParser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/helpers/AntaresArchiveUpdater.h diff --git a/src/cpp/study-updater/CMakeLists.txt b/src/cpp/study-updater/CMakeLists.txt index 3f729950e..15171a608 100644 --- a/src/cpp/study-updater/CMakeLists.txt +++ b/src/cpp/study-updater/CMakeLists.txt @@ -1,19 +1,21 @@ add_library(xpansion_study_updater_lib) target_sources(xpansion_study_updater_lib PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/JsonXpansionReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/LinkCapacitiesCSVWriter.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdateStrategy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/LinkParametersCSVOverwriter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdateLinkCapacitiesStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdateLinkParameterStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdateRunner.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/LinkParametersCSVOverwriter.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdateStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/StudyUpdater.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/LinkCapacitiesCSVWriter.h - ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdateStrategy.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/LinkParametersCSVOverwriter.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdateLinkCapacitiesStrategy.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdateLinkParameterStrategy.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdateRunner.h - ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/LinkParametersCSVOverwriter.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdateStrategy.h ${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/study-updater/StudyUpdater.h + ${CMAKE_CURRENT_SOURCE_DIR}/private/JsonXpansionReader.h ) target_link_libraries(xpansion_study_updater_lib @@ -29,6 +31,8 @@ target_link_libraries(xpansion_study_updater_lib target_include_directories(xpansion_study_updater_lib PUBLIC $ + PRIVATE + $ ) add_library(${PROJECT_NAME}::xpansion_study_updater_lib ALIAS xpansion_study_updater_lib) diff --git a/src/cpp/helpers/JsonXpansionReader.cpp b/src/cpp/study-updater/JsonXpansionReader.cpp similarity index 95% rename from src/cpp/helpers/JsonXpansionReader.cpp rename to src/cpp/study-updater/JsonXpansionReader.cpp index 6e252ab99..9acd49f0d 100644 --- a/src/cpp/helpers/JsonXpansionReader.cpp +++ b/src/cpp/study-updater/JsonXpansionReader.cpp @@ -1,4 +1,4 @@ -#include "antares-xpansion/helpers/JsonXpansionReader.h" +#include "JsonXpansionReader.h" #include diff --git a/src/cpp/study-updater/LinkParametersCSVOverwriter.cpp b/src/cpp/study-updater/LinkParametersCSVOverwriter.cpp index cb23022e0..da4a66cf7 100644 --- a/src/cpp/study-updater/LinkParametersCSVOverwriter.cpp +++ b/src/cpp/study-updater/LinkParametersCSVOverwriter.cpp @@ -4,9 +4,8 @@ #include "antares-xpansion/study-updater/LinkParametersCSVOverwriter.h" -#include "antares-xpansion/helpers/JsonXpansionReader.h" +#include "JsonXpansionReader.h" #include "antares-xpansion/lpnamer/problem_modifier/LinkdataRecord.h" -#include "antares-xpansion/study-updater/StudyUpdater.h" bool LinkParametersCSVOverWriter::open( const std::filesystem::path& linkdataFilename_l) { diff --git a/src/cpp/study-updater/StudyUpdater.cpp b/src/cpp/study-updater/StudyUpdater.cpp index 9ace90821..dc9ea9180 100644 --- a/src/cpp/study-updater/StudyUpdater.cpp +++ b/src/cpp/study-updater/StudyUpdater.cpp @@ -2,7 +2,7 @@ #include -#include "antares-xpansion/helpers/JsonXpansionReader.h" +#include "JsonXpansionReader.h" #include "antares-xpansion/study-updater/StudyUpdateLinkCapacitiesStrategy.h" #include "antares-xpansion/study-updater/StudyUpdateLinkParameterStrategy.h" #include "antares-xpansion/study-updater/StudyUpdateStrategy.h" diff --git a/src/cpp/helpers/include/antares-xpansion/helpers/JsonXpansionReader.h b/src/cpp/study-updater/private/JsonXpansionReader.h similarity index 100% rename from src/cpp/helpers/include/antares-xpansion/helpers/JsonXpansionReader.h rename to src/cpp/study-updater/private/JsonXpansionReader.h diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index 9f3f7f706..1be9c92fa 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -20,15 +20,16 @@ endif() # -------------------------------------------------------------------------- # unit tests # -------------------------------------------------------------------------- -add_subdirectory(tests_utils) -add_subdirectory(solvers_interface) -add_subdirectory(logger) -add_subdirectory(json_output_writer) +add_subdirectory(benders) +add_subdirectory(full_run) add_subdirectory(helpers) +add_subdirectory(json_output_writer) +add_subdirectory(logger) add_subdirectory(lp_namer) -add_subdirectory(sensitivity) +add_subdirectory(outer_loop) add_subdirectory(restart_benders) +add_subdirectory(sensitivity) +add_subdirectory(solvers_interface) +add_subdirectory(study_updater) +add_subdirectory(tests_utils) add_subdirectory(zip_mps) -add_subdirectory(benders) -add_subdirectory(full_run) -add_subdirectory(outer_loop) diff --git a/tests/cpp/helpers/CMakeLists.txt b/tests/cpp/helpers/CMakeLists.txt index 587dbfa94..6574761c7 100644 --- a/tests/cpp/helpers/CMakeLists.txt +++ b/tests/cpp/helpers/CMakeLists.txt @@ -1,5 +1,4 @@ add_executable (helpers_test - JsonXpansionReaderTest.cc AntaresVersionProviderTest.cpp OptionsParserTest.cpp) @@ -18,7 +17,6 @@ target_link_libraries (helpers_test PRIVATE antaresXpansion::lp_namer_model antaresXpansion::lp_namer_input_reader antaresXpansion::lp_namer_problem_modifier - ${JSONCPP_LIB} ${PROJECT_NAME}::benders_sequential_core ) diff --git a/tests/cpp/helpers/JsonXpansionReaderTest.cc b/tests/cpp/helpers/JsonXpansionReaderTest.cc deleted file mode 100644 index 5408ff851..000000000 --- a/tests/cpp/helpers/JsonXpansionReaderTest.cc +++ /dev/null @@ -1,201 +0,0 @@ -#include -#include - -#include "antares-xpansion/helpers/JsonXpansionReader.h" -#include "gtest/gtest.h" - -class JsonXpansionReaderTest : public ::testing::Test { - protected: - static JsonXpansionReader* jsonXpansionReader_; - - static void SetUpTestCase() { - // called before 1st test - std::string content_l; - std::ofstream file_l; - - // dummy interco tmp file name - file_l.open("temp_out.json"); - - content_l = - "\ -{\n\ - \"antares\" : \n\ - {\n\ - \"name\" : \"unknown\",\n\ - \"version\" : \"unknown\"\n\ - },\n\ - \"antares_xpansion\" : \n\ - {\n\ - \"version\" : \"1.2.0\"\n\ - },\n\ - \"begin\" : \"20-11-2020 17:23:15\",\n\ - \"duration\" : 0.0,\n\ - \"end\" : \"20-11-2020 17:23:15\",\n\ - \"iterations\" : \n\ - {\n\ - \"1\" : \n\ - {\n\ - \"best_ub\" : 4.0,\n\ - \"candidates\" : \n\ - [\n\ - {\n\ - \"invest\" : 0.0,\n\ - \"max\" : 10.0,\n\ - \"min\" : 0.0,\n\ - \"name\" : \"x\"\n\ - }\n\ - ],\n\ - \"duration\" : 0.0074719799999999996,\n\ - \"gap\" : 10000000004.0,\n\ - \"investment_cost\" : 0.0,\n\ - \"lb\" : -10000000000.0,\n\ - \"operational_cost\" : 4.0,\n\ - \"overall_cost\" : 4.0,\n\ - \"relative_gap\" : 2500000001.0,\n\ - \"ub\" : 4.0\n\ - },\n\ - \"2\" : \n\ - {\n\ - \"best_ub\" : 4.0,\n\ - \"candidates\" : \n\ - [\n\ - {\n\ - \"invest\" : 10.0,\n\ - \"max\" : 10.0,\n\ - \"min\" : 0.0,\n\ - \"name\" : \"x\"\n\ - }\n\ - ],\n\ - \"duration\" : 0.0022926299999999999,\n\ - \"gap\" : 5.0,\n\ - \"investment_cost\" : 15.0,\n\ - \"lb\" : -1.0,\n\ - \"operational_cost\" : 0.0,\n\ - \"overall_cost\" : 15.0,\n\ - \"relative_gap\" : 1.0666666666666667,\n\ - \"ub\" : 15.0\n\ - },\n\ - \"3\" : \n\ - {\n\ - \"best_ub\" : 3.25,\n\ - \"candidates\" : \n\ - [\n\ - {\n\ - \"invest\" : 1.5,\n\ - \"max\" : 10.0,\n\ - \"min\" : 0.0,\n\ - \"name\" : \"x\"\n\ - }\n\ - ],\n\ - \"duration\" : 0.001730494,\n\ - \"gap\" : 0.0,\n\ - \"investment_cost\" : 2.25,\n\ - \"lb\" : 3.25,\n\ - \"operational_cost\" : 1.0,\n\ - \"overall_cost\" : 3.25,\n\ - \"relative_gap\" : 0.0,\n\ - \"ub\" : 3.25\n\ - },\n\ - \"4\" : \n\ - {\n\ - \"best_ub\" : 3.25,\n\ - \"candidates\" : \n\ - [\n\ - {\n\ - \"invest\" : 10.0,\n\ - \"max\" : 10.0,\n\ - \"min\" : 0.0,\n\ - \"name\" : \"x\"\n\ - }\n\ - ],\n\ - \"duration\" : 0.0022926299999999999,\n\ - \"gap\" : 5.0,\n\ - \"investment_cost\" : 15.0,\n\ - \"lb\" : -1.0,\n\ - \"operational_cost\" : 0.0,\n\ - \"overall_cost\" : 15.0,\n\ - \"relative_gap\" : 1.0666666666666667,\n\ - \"ub\" : 15.0\n\ - }\n\ - },\n\ - \"nbWeeks\" : 3,\n\ - \"options\" : \n\ - {\n\ - \"ACTIVECUTS\" : false,\n\ - \"AGGREGATION\" : false,\n\ - \"BASIS\" : true,\n\ - \"BOUND_ALPHA\" : true,\n\ - \"CSV_NAME\" : \"benders_output_trace\",\n\ - \"JSON_NAME\" : \"out\",\n\ - \"DELETE_CUT\" : false,\n\ - \"GAP\" : 9.9999999999999995e-07,\n\ - \"INPUTROOT\" : \".\",\n\ - \"LOG_LEVEL\" : 3,\n\ - \"MASTER_NAME\" : \"master\",\n\ - \"MAX_ITERATIONS\" : -1,\n\ - \"OUTPUTROOT\" : \".\",\n\ - \"RAND_AGGREGATION\" : 0,\n\ - \"SLAVE_WEIGHT_VALUE\" : 1.0,\n\ - \"STRUCTURE_FILE\" : \"structure.txt\",\n\ - \"THRESHOLD_AGGREGATION\" : 0,\n\ - \"THRESHOLD_ITERATION\" : 0,\n\ - \"TRACE\" : true\n\ - },\n\ - \"solution\" : \n\ - {\n\ - \"gap\" : 0.0,\n\ - \"investment_cost\" : 2.25,\n\ - \"iteration\" : 3,\n\ - \"operational_cost\" : 1.0,\n\ - \"overall_cost\" : 3.25,\n\ - \"problem_status\" : \"OPTIMAL\",\n\ - \"values\" : \n\ - {\n\ - \"x\" : 1.5\n\ - }\n\ - }\n\ -}\n\ -"; - - file_l << content_l; - file_l.close(); - - jsonXpansionReader_ = new JsonXpansionReader(); - jsonXpansionReader_->read("temp_out.json"); - } - - static void TearDownTestCase() { - // called after last test - - // delete the created tmp file - std::remove("temp_out.json"); - - delete jsonXpansionReader_; - jsonXpansionReader_ = nullptr; - } - - void SetUp() { - // called before each test - } - - void TearDown() { - // called after each test - } -}; - -JsonXpansionReader* JsonXpansionReaderTest::jsonXpansionReader_ = nullptr; - -TEST_F(JsonXpansionReaderTest, getBestIteration) { - ASSERT_EQ(jsonXpansionReader_->getBestIteration(), 3); -} - -TEST_F(JsonXpansionReaderTest, getLastIteration) { - ASSERT_EQ(jsonXpansionReader_->getLastIteration(), 4); -} - -TEST_F(JsonXpansionReaderTest, getSolutionPoint) { - std::map solution_l = - jsonXpansionReader_->getSolutionPoint(); - - ASSERT_EQ(solution_l["x"], 1.5); -} diff --git a/tests/cpp/study_updater/CMakeLists.txt b/tests/cpp/study_updater/CMakeLists.txt new file mode 100644 index 000000000..a1e8a70ff --- /dev/null +++ b/tests/cpp/study_updater/CMakeLists.txt @@ -0,0 +1,21 @@ +add_executable(study_updater_test) +target_sources(study_updater_test PRIVATE + JsonXpansionReaderTest.cc +) + +# Get source dir to include private headers +# Better way would be to rework test and use StudyUpdater::update as entry point +get_target_property(STUDY_UPDATER_SOURCE_DIR ${PROJECT_NAME}::xpansion_study_updater_lib SOURCE_DIR) + +target_include_directories(study_updater_test + PRIVATE + ${STUDY_UPDATER_SOURCE_DIR}/private # for private headers +) + +target_link_libraries(study_updater_test + PRIVATE + GTest::Main + ${PROJECT_NAME}::xpansion_study_updater_lib +) +add_test(NAME study_updater_test COMMAND study_updater_test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) +set_property(TEST study_updater_test PROPERTY LABELS unit) diff --git a/tests/cpp/study_updater/JsonXpansionReaderTest.cc b/tests/cpp/study_updater/JsonXpansionReaderTest.cc new file mode 100644 index 000000000..21cdaac04 --- /dev/null +++ b/tests/cpp/study_updater/JsonXpansionReaderTest.cc @@ -0,0 +1,200 @@ +#include +#include + +#include "JsonXpansionReader.h" +#include "gtest/gtest.h" + +class JsonXpansionReaderTest : public ::testing::Test { + protected: + static JsonXpansionReader* jsonXpansionReader_; + + static void SetUpTestCase() { + // called before 1st test + std::string content_l; + std::ofstream file_l; + + // dummy interco tmp file name + file_l.open("temp_out.json"); + + content_l = R"( +{ + "antares" : + { + "name" : "unknown", + "version" : "unknown" + }, + "antares_xpansion" : + { + "version" : "1.2.0" + }, + "begin" : "20-11-2020 17:23:15", + "duration" : 0.0, + "end" : "20-11-2020 17:23:15", + "iterations" : + { + "1" : + { + "best_ub" : 4.0, + "candidates" : + [ + { + "invest" : 0.0, + "max" : 10.0, + "min" : 0.0, + "name" : "x" + } + ], + "duration" : 0.0074719799999999996, + "gap" : 10000000004.0, + "investment_cost" : 0.0, + "lb" : -10000000000.0, + "operational_cost" : 4.0, + "overall_cost" : 4.0, + "relative_gap" : 2500000001.0, + "ub" : 4.0 + }, + "2" : + { + "best_ub" : 4.0, + "candidates" : + [ + { + "invest" : 10.0, + "max" : 10.0, + "min" : 0.0, + "name" : "x" + } + ], + "duration" : 0.0022926299999999999, + "gap" : 5.0, + "investment_cost" : 15.0, + "lb" : -1.0, + "operational_cost" : 0.0, + "overall_cost" : 15.0, + "relative_gap" : 1.0666666666666667, + "ub" : 15.0 + }, + "3" : + { + "best_ub" : 3.25, + "candidates" : + [ + { + "invest" : 1.5, + "max" : 10.0, + "min" : 0.0, + "name" : "x" + } + ], + "duration" : 0.001730494, + "gap" : 0.0, + "investment_cost" : 2.25, + "lb" : 3.25, + "operational_cost" : 1.0, + "overall_cost" : 3.25, + "relative_gap" : 0.0, + "ub" : 3.25 + }, + "4" : + { + "best_ub" : 3.25, + "candidates" : + [ + { + "invest" : 10.0, + "max" : 10.0, + "min" : 0.0, + "name" : "x" + } + ], + "duration" : 0.0022926299999999999, + "gap" : 5.0, + "investment_cost" : 15.0, + "lb" : -1.0, + "operational_cost" : 0.0, + "overall_cost" : 15.0, + "relative_gap" : 1.0666666666666667, + "ub" : 15.0 + } + }, + "nbWeeks" : 3, + "options" : + { + "ACTIVECUTS" : false, + "AGGREGATION" : false, + "BASIS" : true, + "BOUND_ALPHA" : true, + "CSV_NAME" : "benders_output_trace", + "JSON_NAME" : "out", + "DELETE_CUT" : false, + "GAP" : 9.9999999999999995e-07, + "INPUTROOT" : ".", + "LOG_LEVEL" : 3, + "MASTER_NAME" : "master", + "MAX_ITERATIONS" : -1, + "OUTPUTROOT" : ".", + "RAND_AGGREGATION" : 0, + "SLAVE_WEIGHT_VALUE" : 1.0, + "STRUCTURE_FILE" : "structure.txt", + "THRESHOLD_AGGREGATION" : 0, + "THRESHOLD_ITERATION" : 0, + "TRACE" : true + }, + "solution" : + { + "gap" : 0.0, + "investment_cost" : 2.25, + "iteration" : 3, + "operational_cost" : 1.0, + "overall_cost" : 3.25, + "problem_status" : "OPTIMAL", + "values" : + { + "x" : 1.5 + } + } +} +)"; + + file_l << content_l; + file_l.close(); + + jsonXpansionReader_ = new JsonXpansionReader(); + jsonXpansionReader_->read("temp_out.json"); + } + + static void TearDownTestCase() { + // called after last test + + // delete the created tmp file + std::remove("temp_out.json"); + + delete jsonXpansionReader_; + jsonXpansionReader_ = nullptr; + } + + void SetUp() { + // called before each test + } + + void TearDown() { + // called after each test + } +}; + +JsonXpansionReader* JsonXpansionReaderTest::jsonXpansionReader_ = nullptr; + +TEST_F(JsonXpansionReaderTest, getBestIteration) { + ASSERT_EQ(jsonXpansionReader_->getBestIteration(), 3); +} + +TEST_F(JsonXpansionReaderTest, getLastIteration) { + ASSERT_EQ(jsonXpansionReader_->getLastIteration(), 4); +} + +TEST_F(JsonXpansionReaderTest, getSolutionPoint) { + std::map solution_l = + jsonXpansionReader_->getSolutionPoint(); + + ASSERT_EQ(solution_l["x"], 1.5); +}