Bounds Error for loop

I was trying to run the code but in this block I am getting bounds error

for j in 1:5
@constraint(model, sum(costs[i][j] * x[i] for i in 1:14) <= budgets[j])
end

Instead of indexing 1:N, use eachindex instead.

Does that fix things?

More precisely, this might look like

for j in eachindex(budgets, first(costs))
    @constraint(
        model, sum(costs[i][j] * x[i] for i in eachindex(costs, x)) <= budgets[j]
    )
end

But maybe costs would be expressed more naturally as a matrix?

1 Like