From 4aabdae61a9ad5f0b43d356493d4997df2c77c96 Mon Sep 17 00:00:00 2001 From: Thomas Bittar Date: Wed, 28 Feb 2024 18:35:54 +0100 Subject: [PATCH] Work in progress --- src/andromede/simulation/decision_tree.py | 19 +++++----- .../test_generate_network_on_tree.py | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 tests/andromede/test_generate_network_on_tree.py diff --git a/src/andromede/simulation/decision_tree.py b/src/andromede/simulation/decision_tree.py index 9a42af66..d2769531 100644 --- a/src/andromede/simulation/decision_tree.py +++ b/src/andromede/simulation/decision_tree.py @@ -61,6 +61,7 @@ def _generate_tree_variables( ) -> Iterable[Variable]: tree_variables = [] for variable in variables.values(): + # Works as we do not allow variables in bounds, hence no problem to copy the corresponding expression nodes as is. If we had variables, we would have to replace the variable names by the ones with tree node information. tree_variables.append( dataclasses.replace(variable, name=f"{tree_node.name}_{variable.name}") ) @@ -86,7 +87,8 @@ def _generate_tree_port_field_definition( def _generate_tree_model( - tree_node: TreeNode, component: Component, network_id: str + tree_node: TreeNode, + component: Component, ) -> Model: variables = _generate_tree_variables( component.model.variables, @@ -106,7 +108,7 @@ def _generate_tree_model( component.model.port_fields_definitions, tree_node ) tree_model = model( - id=f"{network_id}_{component.model.id}", + id=f"{tree_node.name}_{component.model.id}", constraints=constraints, binding_constraints=binding_constraints, parameters=component.model.parameters.values(), @@ -122,23 +124,18 @@ def _generate_tree_model( def _generate_network_on_node(network: Network, tree_node: TreeNode) -> Network: - network_id = tree_node.name - tree_node_network = Network(network_id) + tree_node_network = Network(tree_node.name) for component in network.all_components: - tree_node_model = _generate_tree_model( - tree_node, - component, - network_id, - ) + tree_node_model = _generate_tree_model(tree_node, component) # It would be nice to have the same treatment for nodes and components as they are actually the same thing... if isinstance(component, Node): - network_node = Node(tree_node_model, id=f"{network_id}_{component.id}") + network_node = Node(tree_node_model, id=f"{tree_node.name}_{component.id}") tree_node_network.add_node(network_node) else: tree_node_component = create_component( - tree_node_model, id=f"{network_id}_{component.id}" + tree_node_model, id=f"{tree_node.name}_{component.id}" ) tree_node_network.add_component(tree_node_component) diff --git a/tests/andromede/test_generate_network_on_tree.py b/tests/andromede/test_generate_network_on_tree.py new file mode 100644 index 00000000..b2e516a6 --- /dev/null +++ b/tests/andromede/test_generate_network_on_tree.py @@ -0,0 +1,35 @@ +# Copyright (c) 2024, RTE (https://www.rte-france.com) +# +# See AUTHORS.txt +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 +# +# This file is part of the Antares project. + + +from anytree import Node as TreeNode + +from andromede.libs.standard import THERMAL_CLUSTER_MODEL_HD +from andromede.simulation.decision_tree import _generate_tree_model +from andromede.study.network import create_component + + +def test_generate_model_on_node() -> None: + thermal = create_component(model=THERMAL_CLUSTER_MODEL_HD, id="thermal") + + tree_node_id = "2030" + tree_node_model = _generate_tree_model(TreeNode(tree_node_id), thermal) + + # How to compare model efficiently with only change in name ? + assert tree_node_model.id == f"{tree_node_id}_{thermal.id}" + + for variable in thermal.model.variables.values(): + assert f"{tree_node_id}_{variable.name}" in tree_node_model.variables + + # Create dedicated function + tree_variable = tree_node_model.variables[f"{tree_node_id}_{variable.name}"] + # assert