Model constraint error

How to solve this error?

BoundsError: attempt to access 2×2 Array{Int64,2} at index [1, 3]
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))

t = 4
w = 2
T = 1:t
W = 1:w

WH = [2489 1447; 11073 5414]

@variable(Model1,wr[W,T], lower_bound=0)
@variable(Model1,wo[W,T], lower_bound=0)


for w = 1:2,t= 1:4
    n = @constraint(Model1,wr[w,t]+wo[w,t]==WH[w,t])
    println(n)
end

WH is a 2x2 matrix, but in the for loop you are trying to access element WH[1, 3] (with w =1, t = 3) . You need either a 2x4 matrix for WH or declare the constraints in such a way that fits with the size of WH.

1 Like

Please note that this is the same error as your previous post: How to build a constraint with summation? - #12 by RaquelSantos.

You should re-read the answers to that post. In particular, you should read Julia’s documentation on arrays as explained here.

2 Likes