Creating nodes with the same variables/constraints in Plasmo

Hi @evrenmturan, welcome to the forum. Thanks for posting over here.

You can’t do what you are trying to do for two reasons:

  1. @optinode(graph, name) will create a node named name, not named what the value of name is.
  2. Even if you could, the optinode would be created in the scope of the CreateNode! function. It wouldn’t be visible to @linkconstraint outside.

Note that 2) isn’t a Plasmo or macro issue, it applies to all Julia code. For example:

julia> function foo(x)
           y = 1
           return x + y
       end
foo (generic function with 1 method)

julia> foo(2)
3

julia> y
ERROR: UndefVarError: `y` not defined

I would do instead something like this:

using Plasmo
function create_node(graph, name::Symbol)
    node = Plasmo.add_node!(graph)
    node.label = string(name)
    @variable(node, y >= 2)
    graph[name] = node
    return node
end
graph = OptiGraph()
trial = create_node(graph, :trial)
@linkconstraint(graph, trial[:y] == 10)

Note that we return trial from create_node so that we can use it in @linkconstraint.

1 Like