# Model constraint error

**URL:** <https://discourse.julialang.org/t/model-constraint-error/42850>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [July 10, 2020, 3:29pm UTC](https://discourse.julialang.org/t/model-constraint-error/42850 "2020-07-10T15:29:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [July 10, 2020, 3:29pm UTC](https://discourse.julialang.org/t/model-constraint-error/42850/1 "2020-07-10T15:29:44Z")

</div>

How to solve this error?

```julia
BoundsError: attempt to access 2×2 Array{Int64,2} at index [1, 3]

```

```julia
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

```

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [July 10, 2020, 3:49pm UTC](https://discourse.julialang.org/t/model-constraint-error/42850/2 "2020-07-10T15:49:51Z")

</div>

> [@RaquelSantos](#):
>
> ```julia
> 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`.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 10, 2020, 4:45pm UTC](https://discourse.julialang.org/t/model-constraint-error/42850/3 "2020-07-10T16:45:46Z")

</div>

Please note that this is the same error as your previous post: [How to build a constraint with summation? - #12 by RaquelSantos](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/12).

You should re-read the answers to that post. In particular, you should read Julia’s documentation on arrays as [explained here](https://discourse.julialang.org/t/how-to-build-a-constraint-with-summation/41618/14).
