# How to use NamedDimsArrays(NamedDims.jl) as containers for JuMP Variables?

**URL:** <https://discourse.julialang.org/t/how-to-use-nameddimsarrays-nameddims-jl-as-containers-for-jump-variables/45961>\
**Category:** New to Julia\
**Tags:** optimization\
**Created:** [September 2, 2020, 3:41pm UTC](https://discourse.julialang.org/t/how-to-use-nameddimsarrays-nameddims-jl-as-containers-for-jump-variables/45961 "2020-09-02T15:41:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [September 2, 2020, 3:41pm UTC](https://discourse.julialang.org/t/how-to-use-nameddimsarrays-nameddims-jl-as-containers-for-jump-variables/45961/1 "2020-09-02T15:41:01Z")

</div>

I am planning to develop a very large power system model. In AMPL it took 1-2h to generate the model and 12h to solve the model with cplex.  
In JuMP one can use symbols to create DenseAxisArrays of Variables and Constraints. But as I read it seems that these take much longer than normal arrays in the model preparation.

There fore I want to use **NamedDimsArrays (NamedDims.jl)** to ensure proper/safe coupling of JuMP Variable arrays in different constraints. (I also try to figure out if **AxisKeys.jl** or **Dictionaries.jl** can help to make a performant but easy to read and debug large JuMP optimization model.)

But I am not sure how one would best create NamedDimsArrays of JuMP Variables and constraints?

> m=Model()  
> time\_steps=(1:10)  
> temp\_levels=(5:5:95)  
> cav\_levels=(1:2)

`storageLevelBalance=NamedDimsArray{(:x, :y, :z),VariableRef,3,Array{VariableRef,3}}[]; sizehint!(storageLevelBalance, 380)`

`for ts in time_steps, tl in temp_levels,cl in cav_levels @variable(m,storageLevelBalance[ts,tl,cl]) end `

> ERROR: An object of name storageLevelBalance is already attached to this model.

But this works

> for ts in time\_steps, tl in temp\_levels,cl in cav\_levels  
> @variable(m,base\_name = “storageLevelBalance[$ts,$tl,$cl]”)  
> end

Is this the way to go?

Why gives specifying a variable name @variable(m,storageLevelBalance[ts,tl,cl]) an error while  
@variable(m,base\_name = “storageLevelBalance[$ts,$tl,$cl]”) is fine?

Further question:  
Reasons for meaningful indices:

1. Avoid model mistakes: Sum( i in Ind)A[i,j]x[i] is not the same as Sum( i in Ind)A[j,i]x[i]. Here I by accident flipped the indices in the Matrix A. If A would be defined over different sets in the first or second index then to model would complain if it is fed with a wrong index. If it is only integers how can the model know I did a mistake?
2. Easy inspection of optimization results: If I look at the optimization results, I want to know e.g. which power plant has a lot of installed capacity. Cap[“Bartimore2”]=1500 is more clear then Cap[2569]=1500  
Lineflow[“Baltimore”,”NewYork”]=2300 is more clear result than Lineflow[224,432]=2300 where I have to look up what line (224,432) is.

Are there any other suggestions how to create a very large performant JuMP model, but with the 2 features mentioned above(Prevent index coupling mistakes and easy inspection of model results)?
