JuMP error constraint forming KeyError:

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

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

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].

2 Likes

Thank you very much for your help. it worked.