# Key error in JuMP on domain tuples

**URL:** <https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872>\
**Category:** Optimization (Mathematical)\
**Created:** [March 14, 2019, 3:34pm UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872 "2019-03-14T15:34:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [March 14, 2019, 3:34pm UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/1 "2019-03-14T15:34:11Z")

</div>

Hi folks,

I’m having a strange problem with JuMP that I hope someone can identify…

The first formulation below results in a key error but the second doesn’t but both should, in theory involve evaluations on the same sets of keys…

It’s frustrating because in the second formulation that works, I can’t name the constraints nicely,

```julia
#results in key error on (u,ats)
        @constraint(m,
            q_unit_avail_timeseries[
                (u,ats) in unit__avail_timeseries(),
                (t,s) in T_S
            ],
            + v_ps[u,t,s]
            <=
            + production_max(unit=u)*time_series_data(time_series=ats,t=t)
        )

# works fine
        for (u,ats) in unit__avail_timeseries(),(t,s) in T_S
                @constraint(m,
                    + v_ps[u,t,s]
                    <=
                    + production_max(unit=u)*time_series_data(time_series=ats,t=t)
                )
        end

```

Edit: the error is as follows:

```julia
ERROR: LoadError: LoadError: KeyError: key (:NPV_1271_BAL_110, :PV_J) not found
Stacktrace:
 [1] getindex at .\dict.jl:478 [inlined]
 [2] lookup_index at .julia\packages\JuMP\jnmGG\src\Containers\DenseAxisArray.jl:120 [
inlined]
 [3] _to_index_tuple(::Tuple{Tuple{Symbol,Symbol},Tuple{Int64,Int64}}, ::Tuple{Dict{Array{Symbol,1},
Int64},Dict{Any,Int64}}) at .julia\packages\JuMP\jnmGG\src\Containers\DenseAxisArray.j
l:129
 [4] to_index at .julia\packages\JuMP\jnmGG\src\Containers\DenseAxisArray.jl:147 [inli
ned]
 [5] setindex!(::JuMP.Containers.DenseAxisArray{ConstraintRef{Model,C,Shape} where Shape<:AbstractSh
ape where C,2,Tuple{Array{Array{Symbol,1},1},Array{Any,1}},Tuple{Dict{Array{Symbol,1},Int64},Dict{An
y,Int64}}}, ::ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunc
tion{Float64},MathOptInterface.LessThan{Float64}},ScalarShape}, ::Tuple{Symbol,Symbol}, ::Tuple{Int6
4,Int64}) at .julia\packages\JuMP\jnmGG\src\Containers\DenseAxisArray.jl:166
 [6] top-level scope at .julia\packages\JuMP\jnmGG\src\macros.jl:621
 [7] include at .\boot.jl:326 [inlined]
 [8] include_relative(::Module, ::String) at .\loading.jl:1038
 [9] include(::Module, ::String) at .\sysimg.jl:29
 [10] include(::String) at .\client.jl:403
 [11] top-level scope at *.jl:81
in expression starting at *constraints.jl:261
in expression starting at *.jl:67

```

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 15, 2019, 11:33am UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/2 "2019-03-15T11:33:55Z")

</div>

You can try

```julia
@constraint(m, q_unit_avail_timeseries[
    uats in unit__avail_timeseries(),
    ts in T_S
], + v_ps[uats[1],ts[1],ts[2]] <= + production_max(unit=uats[1])*time_series_data(time_series=uats[2],t=ts[1]) )

```

---

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [March 15, 2019, 11:49am UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/3 "2019-03-15T11:49:18Z")

</div>

Thanks for that. It’s strange that `(t,s) in T_S` works but `(u,ats) in unit__avail_timeseries()`doesn’t… and it doesn’t appear to be because it’s a function returning the set of tuples, I used a variable here in place and that doesn’t work either

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 15, 2019, 11:56am UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/4 "2019-03-15T11:56:22Z")

</div>

It would help, if you could give what `unit__avail_timeseries()` returns, or an equivalent that give the same error that you obtain by simplifying it until it stops failing.

---

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [March 15, 2019, 12:00pm UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/5 "2019-03-15T12:00:07Z")

</div>

ok, so

```julia
julia> @show unit__avail_timeseries()
unit__avail_timeseries() = ...
154-element Array{Array{Symbol,1},1}:

```

there is no problem with T\_S which is:

```julia
julia> @show T_S
T_S = ...
48-element Array{Any,1}:

```

---

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [March 15, 2019, 12:34pm UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/6 "2019-03-15T12:34:18Z")

</div>

Ok, so I’ve determined the problem… it works fine if (a,b) is a set of tuples but if (c.d) is an array of arrays, it doesn’t work.

---

<div class="post-metadata">

**Author:** ![Di11on](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/di11on/32/6352_2.png) [@Di11on](https://discourse.julialang.org/u/Di11on)\
**Post date:** [March 15, 2019, 2:18pm UTC](https://discourse.julialang.org/t/key-error-in-jump-on-domain-tuples/21872/7 "2019-03-15T14:18:02Z")

</div>

I guess the question is, though, if it works in a for loop, shouldn’t it work in a JuMP constraint?
