From 5d4c1689631521f39fae5eb59606a5ccd2498032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Omn=C3=A8s?= Date: Thu, 19 Dec 2024 15:29:31 +0100 Subject: [PATCH] Modeler 2.5a: Add parameters and YAML parser (#2539) Co-authored-by: payetvin <113102157+payetvin@users.noreply.github.com> Co-authored-by: Vincent Payet --- src/CMakeLists.txt | 1 + src/libs/antares/io/CMakeLists.txt | 3 +- src/libs/antares/io/include/antares/io/file.h | 3 +- src/solver/modelParser/CMakeLists.txt | 4 +- src/solver/modeler/CMakeLists.txt | 1 + src/solver/modeler/parameters/CMakeLists.txt | 14 ++++ src/solver/modeler/parameters/encoder.hxx | 23 ++++++ .../modeler/parameters/modelerParameters.h | 18 +++++ .../parameters/parseModelerParameters.h | 11 +++ .../parameters/parseModelerParameters.cpp | 16 ++++ src/solver/systemParser/CMakeLists.txt | 2 - .../libs/antares/yaml-parser/CMakeLists.txt | 7 +- src/tests/src/solver/modeler/CMakeLists.txt | 1 + .../solver/modeler/parameters/CMakeLists.txt | 14 ++++ .../parameters/testParametersParsing.cpp | 74 +++++++++++++++++++ 15 files changed, 179 insertions(+), 13 deletions(-) create mode 100644 src/solver/modeler/parameters/CMakeLists.txt create mode 100644 src/solver/modeler/parameters/encoder.hxx create mode 100644 src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h create mode 100644 src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h create mode 100644 src/solver/modeler/parameters/parseModelerParameters.cpp create mode 100644 src/tests/src/solver/modeler/parameters/CMakeLists.txt create mode 100644 src/tests/src/solver/modeler/parameters/testParametersParsing.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 13c67fe7a0..32beee7a3a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -191,6 +191,7 @@ endif() find_package(Boost REQUIRED) find_package(antlr4-runtime REQUIRED) +find_package(yaml-cpp REQUIRED) #Sirius solver if(POLICY CMP0074) diff --git a/src/libs/antares/io/CMakeLists.txt b/src/libs/antares/io/CMakeLists.txt index 9e3211e14b..dc62a95aea 100644 --- a/src/libs/antares/io/CMakeLists.txt +++ b/src/libs/antares/io/CMakeLists.txt @@ -12,6 +12,7 @@ source_group("io" FILES ${SRC_IO}) add_library(io ${SRC_IO} ) +add_library(Antares::io ALIAS io) target_link_libraries(io PRIVATE @@ -26,4 +27,4 @@ target_include_directories(io install(DIRECTORY include/antares DESTINATION "include" -) \ No newline at end of file +) diff --git a/src/libs/antares/io/include/antares/io/file.h b/src/libs/antares/io/include/antares/io/file.h index b401b1cece..a9aed8c850 100644 --- a/src/libs/antares/io/include/antares/io/file.h +++ b/src/libs/antares/io/include/antares/io/file.h @@ -22,8 +22,7 @@ #define __LIBS_ANTARES_IO_FILE_H__ #include - -#include +#include namespace Antares::IO { diff --git a/src/solver/modelParser/CMakeLists.txt b/src/solver/modelParser/CMakeLists.txt index 1881ba78fa..8e5ac13726 100644 --- a/src/solver/modelParser/CMakeLists.txt +++ b/src/solver/modelParser/CMakeLists.txt @@ -1,5 +1,3 @@ -find_package(yaml-cpp REQUIRED) - set(SOURCES parser.cpp encoders.hxx @@ -24,4 +22,4 @@ target_link_libraries(modelParser install(DIRECTORY include/antares DESTINATION "include" -) \ No newline at end of file +) diff --git a/src/solver/modeler/CMakeLists.txt b/src/solver/modeler/CMakeLists.txt index 3c631240ac..74378f6f7f 100644 --- a/src/solver/modeler/CMakeLists.txt +++ b/src/solver/modeler/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(api) add_subdirectory(ortoolsImpl) +add_subdirectory(parameters) diff --git a/src/solver/modeler/parameters/CMakeLists.txt b/src/solver/modeler/parameters/CMakeLists.txt new file mode 100644 index 0000000000..33cfda091e --- /dev/null +++ b/src/solver/modeler/parameters/CMakeLists.txt @@ -0,0 +1,14 @@ +add_library(modeler-parameters + include/antares/solver/modeler/parameters/modelerParameters.h + include/antares/solver/modeler/parameters/parseModelerParameters.h + parseModelerParameters.cpp + encoder.hxx) + +target_link_libraries(modeler-parameters + PRIVATE + yaml-cpp + Antares::io) + +target_include_directories(modeler-parameters + PUBLIC + $) diff --git a/src/solver/modeler/parameters/encoder.hxx b/src/solver/modeler/parameters/encoder.hxx new file mode 100644 index 0000000000..32de3e0d83 --- /dev/null +++ b/src/solver/modeler/parameters/encoder.hxx @@ -0,0 +1,23 @@ +#include + +#include "yaml-cpp/yaml.h" + +namespace YAML +{ +template<> +struct convert +{ + static bool decode(const Node& node, Antares::Solver::ModelerParameters& rhs) + { + if (!node.IsMap()) + { + return false; + } + rhs.solver = node["solver"].as(); + rhs.solverLogs = node["solver-logs"].as(false); + rhs.solverParameters = node["solver-parameters"].as(); + rhs.noOutput = node["no-output"].as(false); + return true; + } +}; +} // namespace YAML diff --git a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h new file mode 100644 index 0000000000..c6977a748b --- /dev/null +++ b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/modelerParameters.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace Antares::Solver +{ +struct ModelerParameters +{ + // OR-Tools solver to be used for the simulation + std::string solver; + // Display solver logs ON/OFF + bool solverLogs = false; + // Specific solver parameters + std::string solverParameters; + // Write output results + bool noOutput = false; +}; +} // namespace Antares::Solver diff --git a/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h new file mode 100644 index 0000000000..2409fcddf1 --- /dev/null +++ b/src/solver/modeler/parameters/include/antares/solver/modeler/parameters/parseModelerParameters.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +#include + +namespace Antares::Solver +{ + +ModelerParameters parseModelerParameters(const std::filesystem::path& path); +} // namespace Antares::Solver diff --git a/src/solver/modeler/parameters/parseModelerParameters.cpp b/src/solver/modeler/parameters/parseModelerParameters.cpp new file mode 100644 index 0000000000..39935db38c --- /dev/null +++ b/src/solver/modeler/parameters/parseModelerParameters.cpp @@ -0,0 +1,16 @@ +#include +#include + +#include + +#include "encoder.hxx" + +namespace Antares::Solver +{ +ModelerParameters parseModelerParameters(const std::filesystem::path& path) +{ + const auto contents = Antares::IO::readFile(path); + YAML::Node root = YAML::Load(contents); + return root.as(); +} +} // namespace Antares::Solver diff --git a/src/solver/systemParser/CMakeLists.txt b/src/solver/systemParser/CMakeLists.txt index 79e1262644..37e09a616a 100644 --- a/src/solver/systemParser/CMakeLists.txt +++ b/src/solver/systemParser/CMakeLists.txt @@ -1,5 +1,3 @@ -find_package(yaml-cpp REQUIRED) - set(SOURCES parser.cpp converter.cpp diff --git a/src/tests/src/libs/antares/yaml-parser/CMakeLists.txt b/src/tests/src/libs/antares/yaml-parser/CMakeLists.txt index 1633966826..dd9ef54891 100644 --- a/src/tests/src/libs/antares/yaml-parser/CMakeLists.txt +++ b/src/tests/src/libs/antares/yaml-parser/CMakeLists.txt @@ -1,6 +1,4 @@ - -find_package(yaml-cpp CONFIG REQUIRED) -Set(SRCS test_yaml_parser.cpp +set(SRCS test_yaml_parser.cpp ) set(execname "yaml-parser-test") @@ -8,8 +6,7 @@ add_executable(${execname} ${SRCS}) target_link_libraries(${execname} PRIVATE yaml-cpp - Boost::unit_test_framework - ) + Boost::unit_test_framework) add_test(NAME yaml-parser COMMAND ${execname}) diff --git a/src/tests/src/solver/modeler/CMakeLists.txt b/src/tests/src/solver/modeler/CMakeLists.txt index 53d257b04e..9a39b3b19b 100644 --- a/src/tests/src/solver/modeler/CMakeLists.txt +++ b/src/tests/src/solver/modeler/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(api) +add_subdirectory(parameters) diff --git a/src/tests/src/solver/modeler/parameters/CMakeLists.txt b/src/tests/src/solver/modeler/parameters/CMakeLists.txt new file mode 100644 index 0000000000..3e989c2b03 --- /dev/null +++ b/src/tests/src/solver/modeler/parameters/CMakeLists.txt @@ -0,0 +1,14 @@ +# Zip writer +add_executable(parse-parameters testParametersParsing.cpp) + +target_link_libraries(parse-parameters + PRIVATE + Boost::unit_test_framework + modeler-parameters + test_utils_unit +) + +set_target_properties(parse-parameters PROPERTIES FOLDER Unit-tests/test-writer) + +add_test(NAME parse-parameters COMMAND parse-parameters) +set_tests_properties(parse-parameters PROPERTIES LABELS unit) diff --git a/src/tests/src/solver/modeler/parameters/testParametersParsing.cpp b/src/tests/src/solver/modeler/parameters/testParametersParsing.cpp new file mode 100644 index 0000000000..aa2b14732b --- /dev/null +++ b/src/tests/src/solver/modeler/parameters/testParametersParsing.cpp @@ -0,0 +1,74 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ +#define WIN32_LEAN_AND_MEAN + +#include +#define BOOST_TEST_MODULE parse modeler parameters + +#include + +#include + +#include "files-system.h" + +BOOST_AUTO_TEST_SUITE(read_modeler_parameters) + +BOOST_AUTO_TEST_CASE(all_properties_set) +{ + const auto working_tmp_dir = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + const auto fileP = working_tmp_dir / "parameters.yaml"; + { + std::ofstream param(fileP); + param << R"( +solver: sirius +solver-logs: false +solver-parameters: PRESOLVE 1 +no-output: true)"; + } + + auto params = Antares::Solver::parseModelerParameters(fileP); + BOOST_CHECK_EQUAL(params.solver, "sirius"); + BOOST_CHECK_EQUAL(params.solverLogs, false); + BOOST_CHECK_EQUAL(params.solverParameters, "PRESOLVE 1"); + BOOST_CHECK_EQUAL(params.noOutput, true); +} + +BOOST_AUTO_TEST_CASE(all_properties_set_out_of_order) +{ + const auto working_tmp_dir = CREATE_TMP_DIR_BASED_ON_TEST_NAME(); + const auto fileP = working_tmp_dir / "parameters.yaml"; + { + std::ofstream param(fileP); + param << R"( +solver-logs: false +solver: sirius +solver-parameters: PRESOLVE 1 +no-output: true)"; + } + + auto params = Antares::Solver::parseModelerParameters(fileP); + BOOST_CHECK_EQUAL(params.solver, "sirius"); + BOOST_CHECK_EQUAL(params.solverLogs, false); + BOOST_CHECK_EQUAL(params.solverParameters, "PRESOLVE 1"); + BOOST_CHECK_EQUAL(params.noOutput, true); +} + +BOOST_AUTO_TEST_SUITE_END()