Skip to content

Commit

Permalink
Fix interaction resolve ports / add component context
Browse files Browse the repository at this point in the history
  • Loading branch information
tbittar committed Aug 21, 2024
1 parent b0197da commit 1911e29
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
13 changes: 1 addition & 12 deletions src/andromede/expression/context_adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,10 @@ class ContextAdder(CopyVisitor):

component_id: str

# def variable(self, node: VariableNode) -> ExpressionNodeEfficient:
# return ComponentVariableNode(self.component_id, node.name)

def parameter(self, node: ParameterNode) -> ExpressionNodeEfficient:
return ComponentParameterNode(self.component_id, node.name)

# def comp_variable(self, node: ComponentVariableNode) -> ExpressionNodeEfficient:
# raise ValueError(
# "This expression has already been associated to another component."
# )

def comp_parameter(self, node: ComponentParameterNode) -> ExpressionNodeEfficient:
raise ValueError(
"This expression has already been associated to another component."
)
# Nothing is done is a component parameter node is encountered as it may have been generated from port resolution


def add_component_context(
Expand Down
11 changes: 4 additions & 7 deletions src/andromede/expression/linear_expression_efficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,19 +953,16 @@ def resolve_port(
port_expr += sum_expressions(
[port_term.coefficient * expression for expression in expressions]
)
return self + port_expr
self_without_ports = LinearExpressionEfficient(self.terms, self.constant)
return self_without_ports + port_expr

def add_component_context(self, component_id: str) -> "LinearExpressionEfficient":
result_terms = {}
for term in self.terms.values():
if term.component_id:
raise ValueError(
"This expression has already been associated to another component."
)

# Some terms may involve variable from other component if they arise from previous port resolution
result_term = dataclasses.replace(
term,
component_id=component_id,
component_id=term.component_id if term.component_id else component_id,
coefficient=add_component_context(component_id, term.coefficient),
time_operator=(
dataclasses.replace(
Expand Down
5 changes: 3 additions & 2 deletions src/andromede/simulation/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ def _instantiate_model_expression(
1. add component ID for variables and parameters of THIS component
2. replace port fields by their definition
"""
with_component = model_expression.add_component_context(component_id)
with_component_and_ports = with_component.resolve_port(
# We need to resolve ports before adding component context as binding constraints with ports may involve parameters from the current component
with_ports = model_expression.resolve_port(
component_id, optimization_context.connection_fields_expressions
)
with_component_and_ports = with_ports.add_component_context(component_id)
return with_component_and_ports


Expand Down

0 comments on commit 1911e29

Please sign in to comment.