plasmo-dev / Plasmo.jl

A Platform for Scalable Modeling and Optimization
Other
143 stars 20 forks source link

Adding nodes to a subgraph after the main graph's backend is initialized does not reset graph backend #101

Open dlcole3 opened 8 months ago

dlcole3 commented 8 months ago

I have found that adding to a subgraph does not necessarily update the overall graph's backend. For example, the code below throws an error because the first incident_edges call sets the backend of the graph, and then when the second node is added, the backend on graph g0 is not updated. When Plasmo runs the second call of incident_edges, it does not have the extra_node in its hypermap.

using Plasmo

g0 = OptiGraph()
g1 = OptiGraph()
g2 = OptiGraph()

@optinode(g1, node)
@variable(node, x >= 0)

@optinode(g2, node)
@variable(node, x >= 0)

add_subgraph!(g0, g1)
add_subgraph!(g0, g2)

incident_edges(g0, all_nodes(g1))

@optinode(g1, extra_node)
@variable(extra_node, x >= -5)

incident_edges(g0, all_nodes(g1))

This does make sense since there is no mapping to go from the subgraph to its "owning" graph (only to go from the "owning" graph to the subgraph), so there is no way for the upper graph to know to update its backend. Currently, I get around this in my code by forcing the upper graph to reset the backend by calling Plasmo.set_graph_backend, but if there is an efficient way to fix this in the future, it could be nice.

jalving commented 7 months ago

This may be a good reason to maintain a reference from optinodes to each of their containing optigraphs. The original design of Plasmo.jl actually did this, but we opted to make nodes more modular at the time. This will be added to the roadmap in the re-write.