Hi @evrenmturan, welcome to the forum. Thanks for posting over here.
You can’t do what you are trying to do for two reasons:
@optinode(graph, name)will create a node namedname, not named what the value ofnameis.- Even if you could, the
optinodewould be created in the scope of theCreateNode!function. It wouldn’t be visible to@linkconstraintoutside.
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.