# Creating three dimensional variable using JuMP

**URL:** <https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 15, 2023, 9:58am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870 "2023-05-15T09:58:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [May 15, 2023, 9:58am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/1 "2023-05-15T09:58:50Z")

</div>

I have created a two dimensional variable using the following code.

```julia
  fnew = Vector{Vector{VariableRef}}(undef, c)
  eta = Vector{Vector{VariableRef}}(undef, c)

  for i = 1:c
      newlines = nm_new[i]
      if newlines > 0
          fnew[i] = [@variable(TNEP, base_name="fnew[$(i),$k]") for k = 1:newlines]
          eta[i] = [@variable(TNEP, binary=true, base_name="η[$(i),$k]") for k = 1:newlines]
      end
  end

```

Now I want to make these two variables fnew and eta three dimensional. What’s the best way to do this.

I tried to do this in the following way. However, this results in error.

```julia
    fnew = Vector{Vector{Vector{VariableRef}}}((undef, r))
    eta = Vector{Vector{Vector{VariableRef}}}((undef, r))
    
    for i in rows
        newlines = nm_new[i]
        
        if newlines > 0
            fnew[i] = [[@variable(JSTEP, base_name="fnew[$(i),$j,$k]") for j in 1:s] for k in 1:newlines ]
            eta[i] = [[@variable(JSTEP, binary=true, base_name="η[$(i),$j,$k]") for j in 1:s] for k in 1:newlines ]
        end
      
    end

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 15, 2023, 10:16am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/2 "2023-05-15T10:16:12Z")

</div>

You can directly create arbitrary arrays of variables with a slightly easier sytax ([Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Containers-of-variables)):

```julia
@variables(model, x[1:m, 1:n, 1:p])

```

---

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [May 15, 2023, 10:27am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/3 "2023-05-15T10:27:46Z")

</div>

I have to create the variable only when the condition newlines \> 0 is met.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 15, 2023, 10:47am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/4 "2023-05-15T10:47:24Z")

</div>

The last example in the docs I linked to seems to do something similar?

```julia
julia> model = Model();

julia> @variable(model, v[i = 1:9; mod(i, 3) == 0])
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 3 entries:
  [3] = v[3]
  [6] = v[6]
  [9] = v[9]

```

---

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [May 15, 2023, 11:20am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/5 "2023-05-15T11:20:21Z")

</div>

This example puts a condition on the index itself. Can I put a condition on some value accessed using an index

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 15, 2023, 11:34am UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/6 "2023-05-15T11:34:46Z")

</div>

I haven’t tested it myself but I think it should work just fine

```julia
@variable(model, v[i = 1:9; nm_new[i] > 0])

```

---

<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:** [May 15, 2023, 9:26pm UTC](https://discourse.julialang.org/t/creating-three-dimensional-variable-using-jump/98870/7 "2023-05-15T21:26:13Z")

</div>

Something like this should work:

```julia
JSTEP = Model()
@variable(JSTEP, fnew[i=rows, 1:s, 1:nm_new[i]; nm_new[i] > 0])
@variable(JSTEP, eta[i=rows, 1:s, 1:nm_new[i]; nm_new[i] > 0], Bin)
fnew[1, 2, 3]

```

As a general hint, if you find your JuMP model becoming verbose, there’s usually a better way to write things.
