# Nonconvex.jl variable names

**URL:** https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [March 8, 2024, 1:10am UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330 "2024-03-08T01:10:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![rtapia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rtapia/32/207390_2.png) [@rtapia](https://discourse.julialang.org/u/rtapia)
#### Post date: [March 8, 2024, 1:10am UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/1 "2024-03-08T01:10:38Z")

</div>

Hello,

How does Nonconvex.jl differentiate variables? To define variables the definition simply define an upper and lower bound values addvar!(model, lower, upper), so how does it differentiate between variables across multiple constraints?

Thanks

---

<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 8, 2024, 1:20am UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/2 "2024-03-08T01:20:18Z")

</div>

Variables are assigned an integer index in the order they are added, but it is up to you to keep track of this. There is also the `DictModel` interface, which is a bit more friendly.

```julia
julia> using Nonconvex

julia> Nonconvex.@load NLopt
[ Info: Attempting to load the package NonconvexNLopt.
[ Info: Loading succesful.

julia> begin
           f(x) = (@show x; sum(x))
           model = Model(f)
           addvar!(model, 3.0, 3.0)
           addvar!(model, [1.0, 2.0], [1.0, 2.0])
           optimize(model, NLoptAlg(:LD_MMA), [3.0, 1.0, 2.0]; options = NLoptOptions())
       end;
x = [3.0, 1.0, 2.0]
x = [3.0, 1.0, 2.0]

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [March 8, 2024, 9:44am UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/3 "2024-03-08T09:44:02Z")

</div>

Hi!

To add to Oscar’s answer, the relevant section in the documentation is [Overview · Nonconvex.jl](https://julianonconvex.github.io/Nonconvex.jl/stable/problem/problem/). There you will see that you can either treat all decision variables as elements in a long vector (the `Model` API) or give each variable an explicit name (the `DictModel` API). Also note that each decision variable in `Nonconvex` can itself be a collection, e.g. `NamedTuple` or `Vector`. So you can model your decision variables as a vector of NamedTuples or a vector of vectors if that makes your code more readable.

---

<div class="post-metadata">

### Author: ![rtapia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rtapia/32/207390_2.png) [@rtapia](https://discourse.julialang.org/u/rtapia)
#### Post date: [March 11, 2024, 6:16pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/4 "2024-03-11T18:16:57Z")

</div>

I noticed in the documentation that its possible to convert a JuMP model into a DictModel, but I get an error when I try to convert the following (set type not supported):

```julia
### declare JuMP model
model = JuMP.Model()

### define JuMP variables
lbx = -Inf*ones(N+1); ubx = Inf*ones(N+1) ;
@variable(model, base_name ="x", x[i=1:N+1,j=1:3], lower_bound = lbx[i], upper_bound = ubx[i]);
@variable(model, base_name = "δ", δ[i=1:N], lower_bound = lbx[i], upper_bound = ubx[i]);
@constraint(model, dyn0, x[1,:] == xinit);
@constraint(model, dyn[i=1:N],x[i+1,:] -Ad*x[i,:] + Bd*δ[i] .== zeros(n))

```

---

<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 11, 2024, 6:26pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/5 "2024-03-11T18:26:06Z")

</div>

Do you have a reproducible example and the full error message? This sounds like a missing feature in Nonconvex

---

<div class="post-metadata">

### Author: ![rtapia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rtapia/32/207390_2.png) [@rtapia](https://discourse.julialang.org/u/rtapia)
#### Post date: [March 11, 2024, 6:38pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/6 "2024-03-11T18:38:27Z")

</div>

See the following:

```julia
using JuMP
using Nonconvex

A1 = [1.0 1.0 1.0; 1.0 2.0 0.0; .0 .0 1.]
B1 = [1,1,1]
N = 5;
xinit = [0.0,.0,.0];
n = 3;

### declare JuMP model
model = JuMP.Model()

### define JuMP variables
lbx = -Inf*ones(N+1); ubx = Inf*ones(N+1) ;

@variable(model, base_name ="x", x[i=1:N+1,j=1:3], lower_bound = lbx[i], upper_bound = ubx[i]);
@variable(model, base_name = "δ", δ[i=1:N], lower_bound = lbx[i], upper_bound = ubx[i]);
@constraint(model, dyn0, x[1,:] == xinit);
@constraint(model, dyn[i=1:N],x[i+1,:] -A1*x[i,:] + B1*δ[i] .== zeros(n))

dmodel = DictModel(model)

```

outputs is

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/04291ad9f34c8375199aa35dd0367234ffdbcf1f.png)

If I remove the variable and constraint names it then seems to be okay, although it does not seem to keep the greek letter variable and instead replaces it with “d”

```julia
@variable(model, x[i=1:N+1,j=1:3], lower_bound = lbx[i], upper_bound = ubx[i]);
@variable(model, δ[i=1:N], lower_bound = lbx[i], upper_bound = ubx[i]);
@constraint(model,x[1,:] == xinit);
[@constraint(model, x[i+1,:] - A1*x[i,:] + B1*δ[i] .== zeros(n)) for i=1:3]

```

---

<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 11, 2024, 7:00pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/7 "2024-03-11T19:00:55Z")

</div>

Try `.== xinit`. JuMP has different constraint types for vector and scalar.

---

<div class="post-metadata">

### Author: ![rtapia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rtapia/32/207390_2.png) [@rtapia](https://discourse.julialang.org/u/rtapia)
#### Post date: [March 11, 2024, 7:19pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/8 "2024-03-11T19:19:59Z")

</div>

thanks!

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [March 11, 2024, 11:32pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/9 "2024-03-11T23:32:28Z")

</div>

Please open an issue in Nonconvex.jl with the original code example. I will try to fix it.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [March 11, 2024, 11:44pm UTC](https://discourse.julialang.org/t/nonconvex-jl-variable-names/111330/10 "2024-03-11T23:44:04Z")

</div>

> [@rtapia](#):
>
> If I remove the variable and constraint names

Please keep the names! See [Nonconvex JuMP model to DictModel cannot see constraints - #3 by mohamed82008](https://discourse.julialang.org/t/nonconvex-jump-model-to-dictmodel-cannot-see-constraints/111485/3)
