Skip to content

Commit

Permalink
[ANT-2206] Parse yaml (#2433)
Browse files Browse the repository at this point in the history
- Parse user models described as CPP
- Expect to read one library per content
- Only accept yaml content as std::string
- Output object is Antares::Solver::ModelParser::Library
- CMake : remove WITH_YAMLCPP (always ON)

---------

Co-authored-by: Florian Omnès <[email protected]>
  • Loading branch information
JasonMarechal25 and flomnes authored Oct 1, 2024
1 parent ac5218c commit 2d77d87
Show file tree
Hide file tree
Showing 14 changed files with 938 additions and 33 deletions.
3 changes: 0 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ message(STATUS "Build OR-Tools: ${BUILD_ORTOOLS}")
option(WITH_ANTLR4 "With antlr4" OFF)
message(STATUS "With antlr4: ${WITH_ANTLR4}")

option(WITH_YAMLCPP "With yaml-cpp" OFF)
message(STATUS "With yaml-cpp: ${WITH_YAMLCPP}")

option(BUILD_MERSENNE_TWISTER_PYBIND11 "Build pybind11 bindings for Mersenne-Twister" OFF)
if (${BUILD_MERSENNE_TWISTER_PYBIND11})
find_package(pybind11 REQUIRED)
Expand Down
30 changes: 16 additions & 14 deletions src/solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ endif (MSVC)

add_subdirectory(application)
add_subdirectory(constraints-builder)
add_subdirectory(expressions)
add_subdirectory(hydro)
add_subdirectory(infeasible-problem-analysis)
add_subdirectory(libModelObject)
add_subdirectory(lps)
add_subdirectory(misc)
add_subdirectory(modelParser)
add_subdirectory(modeler)
add_subdirectory(optimisation)
add_subdirectory(signal-handling)
add_subdirectory(simulation)
add_subdirectory(ts-generator)
add_subdirectory(utils)
add_subdirectory(variable)
add_subdirectory(modeler)
add_subdirectory(expressions)
add_subdirectory(libModelObject)

#
# Resource file for Windows
Expand Down Expand Up @@ -56,25 +57,26 @@ add_library(solver-lib INTERFACE
add_executable(antares-solver
main.cpp
${SRCS}
modelParser/encoders.hxx
)

set_target_properties(antares-solver PROPERTIES OUTPUT_NAME ${exec_name})

set(ANTARES_SOLVER_LIBS
Antares::args_helper
Antares::date
Antares::benchmarking
Antares::result_writer
Antares::sys
Antares::infoCollection
Antares::checks
Antares::misc
Antares::args_helper
Antares::date
Antares::benchmarking
Antares::result_writer
Antares::sys
Antares::infoCollection
Antares::checks
Antares::misc
Antares::optimization-options
Antares::signal-handling
Antares::locale
yuni-static-uuid
yuni-static-core
${CMAKE_THREADS_LIBS_INIT}
yuni-static-uuid
yuni-static-core
${CMAKE_THREADS_LIBS_INIT}
)

set(ANTARES_SOLVER_LIBS ${ANTARES_SOLVER_LIBS}
Expand Down
11 changes: 6 additions & 5 deletions src/solver/expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,27 @@ set(SRC_Expressions
)

source_group("expressions" FILES ${SRC_Expressions})
add_library(antares-solver-expressions
add_library(solver-expressions
${SRC_Expressions})
add_library(Antares::solver-expressions ALIAS solver-expressions)

target_include_directories(antares-solver-expressions
target_include_directories(solver-expressions
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_link_libraries(antares-solver-expressions
target_link_libraries(solver-expressions
PUBLIC
Antares::logs
Boost::headers
)


add_library(antares-solver-expressions-iterators
add_library(solver-expressions-iterators
iterators/pre-order.cpp
include/antares/solver/expressions/iterators/pre-order.h
)

target_link_libraries(antares-solver-expressions-iterators PRIVATE antares-solver-expressions)
target_link_libraries(solver-expressions-iterators PRIVATE solver-expressions)

install(DIRECTORY include/antares
DESTINATION "include"
Expand Down
27 changes: 27 additions & 0 deletions src/solver/modelParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
find_package(yaml-cpp REQUIRED)

set(SOURCES
parser.cpp
#encoders.hxx
include/antares/solver/modelParser/parser.h
)

# Create the library
add_library(modelParser STATIC ${SOURCES})
add_library(Antares::modelParser ALIAS modelParser)

# Specify include directories
target_include_directories(modelParser
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

# Link dependencies (if any)
target_link_libraries(modelParser
PRIVATE
yaml-cpp
)

install(DIRECTORY include/antares
DESTINATION "include"
)
167 changes: 167 additions & 0 deletions src/solver/modelParser/encoders.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@

/*
* 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/>.
*/

#pragma once

#include "antares/solver/modelParser/model.h"

#include "yaml-cpp/yaml.h"

// Implement convert specializations
namespace YAML
{
template<>
struct convert<Antares::Solver::ModelParser::Parameter>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Parameter& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.name = node["name"].as<std::string>();
rhs.time_dependent = node["time-dependent"].as<bool>();
rhs.scenario_dependent = node["scenario-dependent"].as<bool>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Variable>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Variable& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.name = node["name"].as<std::string>();
rhs.lower_bound = node["lower-bound"].as<double>();
rhs.upper_bound = node["upper-bound"].as<double>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Port>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Port& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.name = node["name"].as<std::string>();
rhs.type = node["type"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::PortFieldDefinition>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::PortFieldDefinition& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.port = node["port"].as<std::string>();
rhs.field = node["field"].as<std::string>();
rhs.definition = node["definition"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Constraint>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Constraint& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.name = node["name"].as<std::string>();
rhs.expression = node["expression"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Model>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Model& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>();
rhs.parameters = node["parameters"]
.as<std::vector<Antares::Solver::ModelParser::Parameter>>();
rhs.variables = node["variables"].as<std::vector<Antares::Solver::ModelParser::Variable>>();
rhs.ports = node["ports"].as<std::vector<Antares::Solver::ModelParser::Port>>();
rhs.port_field_definitions = node["port-field-definitions"]
.as<std::vector<
Antares::Solver::ModelParser::PortFieldDefinition>>();
rhs.constraints = node["constraints"]
.as<std::vector<Antares::Solver::ModelParser::Constraint>>();
rhs.objective = node["objective"].as<std::string>();
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::PortType>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::PortType& rhs)
{
if (!node.IsMap())
{
return false;
}
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>();
for (const auto& field: node["fields"])
{
rhs.fields.push_back(field["name"].as<std::string>());
}
return true;
}
};

template<>
struct convert<Antares::Solver::ModelParser::Library>
{
static bool decode(const Node& node, Antares::Solver::ModelParser::Library& rhs)
{
rhs.id = node["id"].as<std::string>();
rhs.description = node["description"].as<std::string>();
rhs.port_types = node["port-types"]
.as<std::vector<Antares::Solver::ModelParser::PortType>>();
rhs.models = node["models"].as<std::vector<Antares::Solver::ModelParser::Model>>();
return true;
}
};
} // namespace YAML
91 changes: 91 additions & 0 deletions src/solver/modelParser/include/antares/solver/modelParser/model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

/*
* 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/>.
*/

#pragma once
#include <string>
#include <vector>

namespace Antares::Solver::ModelParser
{
// Define structures
struct Parameter
{
std::string name;
bool time_dependent;
bool scenario_dependent;
};

struct Variable
{
std::string name;
double lower_bound;
double upper_bound;
};

struct Port
{
std::string name;
std::string type;
};

struct PortFieldDefinition
{
std::string port;
std::string field;
std::string definition;
};

struct Constraint
{
std::string name;
std::string expression;
};

struct Model
{
std::string id;
std::string description;
std::vector<Parameter> parameters;
std::vector<Variable> variables;
std::vector<Port> ports;
std::vector<PortFieldDefinition> port_field_definitions;
std::vector<Constraint> constraints;
std::string objective;
};

struct PortType
{
std::string id;
std::string description;
// Small optimization: we only need the name of the fields
// No need for an intermediate struct "field" with just a string "name" member
std::vector<std::string> fields;
};

struct Library
{
std::string id;
std::string description;
std::vector<PortType> port_types;
std::vector<Model> models;
};
} // namespace Antares::Solver::ModelParser
Loading

0 comments on commit 2d77d87

Please sign in to comment.