Skip to content

Commit

Permalink
Modeler 2.5a: Add parameters and YAML parser (#2539)
Browse files Browse the repository at this point in the history
Co-authored-by: payetvin <[email protected]>
Co-authored-by: Vincent Payet <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent a570a0e commit 5d4c168
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ endif()
find_package(Boost REQUIRED)

find_package(antlr4-runtime REQUIRED)
find_package(yaml-cpp REQUIRED)

#Sirius solver
if(POLICY CMP0074)
Expand Down
3 changes: 2 additions & 1 deletion src/libs/antares/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +27,4 @@ target_include_directories(io

install(DIRECTORY include/antares
DESTINATION "include"
)
)
3 changes: 1 addition & 2 deletions src/libs/antares/io/include/antares/io/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#define __LIBS_ANTARES_IO_FILE_H__

#include <filesystem>

#include <yuni/core/string.h>
#include <string>

namespace Antares::IO
{
Expand Down
4 changes: 1 addition & 3 deletions src/solver/modelParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
find_package(yaml-cpp REQUIRED)

set(SOURCES
parser.cpp
encoders.hxx
Expand All @@ -24,4 +22,4 @@ target_link_libraries(modelParser

install(DIRECTORY include/antares
DESTINATION "include"
)
)
1 change: 1 addition & 0 deletions src/solver/modeler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(api)
add_subdirectory(ortoolsImpl)
add_subdirectory(parameters)
14 changes: 14 additions & 0 deletions src/solver/modeler/parameters/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
23 changes: 23 additions & 0 deletions src/solver/modeler/parameters/encoder.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <antares/solver/modeler/parameters/modelerParameters.h>

#include "yaml-cpp/yaml.h"

namespace YAML
{
template<>
struct convert<Antares::Solver::ModelerParameters>
{
static bool decode(const Node& node, Antares::Solver::ModelerParameters& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.solver = node["solver"].as<std::string>();
rhs.solverLogs = node["solver-logs"].as<bool>(false);
rhs.solverParameters = node["solver-parameters"].as<std::string>();
rhs.noOutput = node["no-output"].as<bool>(false);
return true;
}
};
} // namespace YAML
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string>

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <filesystem>

#include <antares/solver/modeler/parameters/modelerParameters.h>

namespace Antares::Solver
{

ModelerParameters parseModelerParameters(const std::filesystem::path& path);
} // namespace Antares::Solver
16 changes: 16 additions & 0 deletions src/solver/modeler/parameters/parseModelerParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <filesystem>
#include <fstream>

#include <antares/io/file.h>

#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<ModelerParameters>();
}
} // namespace Antares::Solver
2 changes: 0 additions & 2 deletions src/solver/systemParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
find_package(yaml-cpp REQUIRED)

set(SOURCES
parser.cpp
converter.cpp
Expand Down
7 changes: 2 additions & 5 deletions src/tests/src/libs/antares/yaml-parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@

find_package(yaml-cpp CONFIG REQUIRED)
Set(SRCS test_yaml_parser.cpp
set(SRCS test_yaml_parser.cpp
)

set(execname "yaml-parser-test")
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})
Expand Down
1 change: 1 addition & 0 deletions src/tests/src/solver/modeler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(api)
add_subdirectory(parameters)
14 changes: 14 additions & 0 deletions src/tests/src/solver/modeler/parameters/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
74 changes: 74 additions & 0 deletions src/tests/src/solver/modeler/parameters/testParametersParsing.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://opensource.org/license/mpl-2-0/>.
*/
#define WIN32_LEAN_AND_MEAN

#include <fstream>
#define BOOST_TEST_MODULE parse modeler parameters

#include <boost/test/unit_test.hpp>

#include <antares/solver/modeler/parameters/parseModelerParameters.h>

#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()

0 comments on commit 5d4c168

Please sign in to comment.