# JuMP error constraint forming KeyError:

**URL:** <https://discourse.julialang.org/t/jump-error-constraint-forming-keyerror/66905>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [August 24, 2021, 11:33am UTC](https://discourse.julialang.org/t/jump-error-constraint-forming-keyerror/66905 "2021-08-24T11:33:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [August 24, 2021, 11:33am UTC](https://discourse.julialang.org/t/jump-error-constraint-forming-keyerror/66905/1 "2021-08-24T11:33:50Z")

</div>

Hi,  
I wanted to implement the attached constraint with below code but getting error

```nohighlight
const Gurobix = optimizer_with_attributes(Gurobi.Optimizer,"MIPGap"=>0.0005) #"TimeLimit"=>1000
mod2 = Model(Gurobix)
  Lf_p= [18 17 30 32 3];
  J =6
  P=5
  T=6
  R=6
  @variable(mod2,EXI_jprt[j in 1:J, p in 1:P, r in 1:R, t in 1:T],Int)
  @variable(mod2,Qs_pwjirt[p in 1:P, w in 1:Lf_p[p],j in 1:J,i in 1:I, r in 1:R, t in 1:T],Int)
  @variable(mod2,Q_pwjtk[p in 1:P, w in 1:Lf_p[p], j in 1:J , t in 1:T, k in 1:K],Int)
  
for j in 1:J
    for p in 1:P
            for t in 1:T
              for r in 1:t
                @constraint(mod2,
				EXI_jprt[j,p,r,t]==
				      sum( Q_pwjtk[p,t-r+1,j,r,k] for k in 1:K)- sum(Qs_pwjirt[p,t-r+1,j,i,r,c] for i in 1:I, c in r:t) )           
              end
          end
      end
end
getting the following error
LoadError: KeyError: key (5, 4, 1, 1, 1) not found
Stacktrace:
 [1] getindex(::Dict{NTuple{5,Int64},VariableRef}, ::NTuple{5,Int64}) at .\dict.jl:477
 [2] getindex(::JuMP.Containers.SparseAxisArray{VariableRef,5,NTuple{5,Int64}}, ::Int64, ::Int64, ::Int64, ::Int64, ::Vararg{Int64,N} where N) at C:\Users\manojkumar.ram\.julia\packages\JuMP\YXK4e\src\Containers\SparseAxisArray.jl:97
 [3] macro expansion at C:\Users\manojkumar.ram\.julia\packages\MutableArithmetics\8xkW3\src\rewrite.jl:279 [inlined]
 [4] macro expansion at C:\Users\manojkumar.ram\.julia\packages\JuMP\YXK4e\src\macros.jl:416 [inlined]
 [5] top-level scope at C:\Users\manojkumar.ram\Desktop\shweta\code\test.jl:17
in expression starting at C:\Users\manojkumar.ram\Desktop\shweta\code\test.jl:13![error|443x171](upload://z5TdjetJk8IUxCxlL5SMZ4sDSJJ.jpeg)
Thanks in advance for the help
```

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [August 24, 2021, 12:55pm UTC](https://discourse.julialang.org/t/jump-error-constraint-forming-keyerror/66905/2 "2021-08-24T12:55:10Z")

</div>

A bit messy explanation, but I hope it helps. Something is wrong with the indicators in the loops. For instance for t=6 and r=3, we get t-r+1=4. The matrix `Q_pwjtk` does not have anything in the position [5, 4, 1, 1, 1], i.e. `Q_pwjtk[5, 4, 1, 1, 1]` does not exist (the second indicator is missing). This is because when you create your variable ` @variable(mod2,Q_pwjtk[p in 1:P, w in 1:Lf_p[p], j in 1:J , t in 1:T, k in 1:K],Int)` then for `p=P`, you have `w in 1:Lf_p[p]` and `Lf_p[P]=3` so you cannot then have `Q_pwjtk[5,4,1,1,1]`.

---

<div class="post-metadata">

**Author:** ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)\
**Post date:** [August 24, 2021, 1:18pm UTC](https://discourse.julialang.org/t/jump-error-constraint-forming-keyerror/66905/3 "2021-08-24T13:18:26Z")

</div>

Thank you very much for your help. it worked.
