# Creating nodes with the same variables/constraints in Plasmo

**URL:** <https://discourse.julialang.org/t/creating-nodes-with-the-same-variables-constraints-in-plasmo/111651>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization\
**Created:** [March 15, 2024, 9:17am UTC](https://discourse.julialang.org/t/creating-nodes-with-the-same-variables-constraints-in-plasmo/111651 "2024-03-15T09:17:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![evrenmturan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evrenmturan/32/207773_2.png) [@evrenmturan](https://discourse.julialang.org/u/evrenmturan)\
**Post date:** [March 15, 2024, 9:17am UTC](https://discourse.julialang.org/t/creating-nodes-with-the-same-variables-constraints-in-plasmo/111651/1 "2024-03-15T09:17:58Z")

</div>

I posted this on Slack, and was recommended to also post it here.

I am setting up a problem in Plasmo, in which some nodes have (some of) the same variables & constraints as other nodes. So I wanted to write a function that takes in a graph, and a symbol, and adds a node (with some constraints) to the graph with that node. Then afterwards, I could add the constraints specific to that node.

The below is a small MWE, however it doesn’t work. I was told on Slack that @optinode doesn’t see that name is actually :trial .

```julia

using Plasmo

graph = OptiGraph()

function CreateNode!(graph, name)
    @optinode(graph, name)
    @variable(name, y >= 2)
end

CreateNode!(graph, :trial)

@linkconstraint(graph, trail[:y] == 10)

```

I suppose that an alternative would be to create the node out of the function, and then have the function add variables/constraints to the node. Is there a cleaner way to do this?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 17, 2024, 8:26pm UTC](https://discourse.julialang.org/t/creating-nodes-with-the-same-variables-constraints-in-plasmo/111651/2 "2024-03-17T20:26:07Z")

</div>

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
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:

```Julia
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`.
