# Variable in JuMP with arc index

**URL:** <https://discourse.julialang.org/t/variable-in-jump-with-arc-index/98179>\
**Category:** Optimization (Mathematical)\
**Created:** [May 1, 2023, 10:38pm UTC](https://discourse.julialang.org/t/variable-in-jump-with-arc-index/98179 "2023-05-01T22:38:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [May 1, 2023, 10:38pm UTC](https://discourse.julialang.org/t/variable-in-jump-with-arc-index/98179/1 "2023-05-01T22:38:07Z")

</div>

Dear all  
I have defined the following variable:

```julia
@variable(modelo, x[(i,j) ∈ A, t ∈ T] >= 0)

```

where

```julia
A = [
 (1, 5)
 (1, 6)
 (1, 7)
 (2, 5)
 (2, 6)
 (2, 7)
 (3, 5)
 (3, 6)
 (3, 7)
 (4, 5)
 (4, 6)
 (4, 7)
]; T=1:12

```

However, when I run the constraint:

```julia
@constraint(
modelo,
sum(x[i,j,t] for (i,j) ∈ A for t in T) <= 1000
)

```

an error is obtained.

```julia

KeyError: key 1 not found

```

Anyone can help me please?

---

<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:** [May 1, 2023, 10:47pm UTC](https://discourse.julialang.org/t/variable-in-jump-with-arc-index/98179/2 "2023-05-01T22:47:51Z")

</div>

You need the indexing to match how you’ve defined the variable. In your case, `x` is two dimension, where the first dimension is the elements in `A`, and the second dimension are the elements in `T`. But `x[i, j, t]` attempts to index `x` as if it had three dimensions. You need `x[(i, j), t]` instead.

```plaintext
using JuMP
A = [
 (1, 5)
 (1, 6)
 (1, 7)
 (2, 5)
 (2, 6)
 (2, 7)
 (3, 5)
 (3, 6)
 (3, 7)
 (4, 5)
 (4, 6)
 (4, 7)
]; 
T = 1:12
model = Model()
@variable(model, x[(i,j) ∈ A, t ∈ T] >= 0)
@constraint(
    model,
    sum(x[(i,j),t] for (i,j) ∈ A for t in T) <= 1000
)

```

I’d write this as

```plaintext
model = Model()
@variable(model, x[A, T] >= 0)
@constraint(model, sum(x[arc, t] for arc in A for t in T) <= 1000)

```

---

<div class="post-metadata">

**Author:** ![trulsf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trulsf/32/46751_2.png) [@trulsf](https://discourse.julialang.org/u/trulsf)\
**Post date:** [May 2, 2023, 7:54pm UTC](https://discourse.julialang.org/t/variable-in-jump-with-arc-index/98179/3 "2023-05-02T19:54:29Z")

</div>

I think Oscar’s proposal is the simplest solution in JuMP, but if you are curious on another way of modeling with sparse structures (e.g. graphs), you could also have a look at [SparseVariables.jl](https://github.com/hellemo/SparseVariables.jl). This package provides an alternative container for holding variables that is efficient if you are dealing with large scale sparse problems.

```julia
using JuMP
using SparseVariables

I = 1:4
J = 5:7

A = [
 (1, 5)
 (1, 6)
 (1, 7)
 (2, 5)
 (2, 6)
 (2, 7)
 (3, 5)
 (3, 6)
 (3, 7)
 (4, 5)
 (4, 6)
 (4, 7)
]; 
T = 1:12

model = Model()
@variable(model, x[start = I, end = J, time = T] >= 0; container = IndexedVarArray)
for (i,j) in A, t in T
    insertvar!(x, i, j, t)
end
@constraint(model, sum(x) <= 1000)

```

This may be more easy to work with if you have constraints that e.g, need to be provided for a specific vertex

```julia
for i in I 
    @constraint(model, sum(x[i, :, :]) <= 100)
end

```
