Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tbittar committed Feb 28, 2024
1 parent 43df341 commit 4aabdae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/andromede/simulation/decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
)
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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)

Expand Down
35 changes: 35 additions & 0 deletions tests/andromede/test_generate_network_on_tree.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4aabdae

Please sign in to comment.