KeyError: key not found

Hello everybody

Because when I try to compile this part of the model it returns this error to me:
KeyError: key 5 not found

using JuMP, GLPK, GLPKMathProgInterface

ModelD = Model(GLPK.Optimizer)

t = 1:4
w = 1:2
l = 1:4

T = t
W = w
L = l
NP = 18

@variable(ModelD,eh[L,T,W]>=0)

for w = w, l = l, t = t
    if  t < NP
        j1 = t+1
        h3 = @constraint(ModelD,eh[l,j1,w] <= eh[l,t,w])
        println(h3)
    end
end

You defined T = t = 1:4 and created the eh variables with that. Thus, there is no variable eh[l,5,w] defined. If now t=4 < NP you increment it to t+1=5 in your loop and therefore cannot access eh[l,5,w] because it does not exist, i.e., key 5 not found.

5 Likes