# How to index a JuMP variable during optimization

**URL:** <https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658>\
**Category:** Optimization (Mathematical)\
**Created:** [December 11, 2020, 10:33am UTC](https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658 "2020-12-11T10:33:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)\
**Post date:** [December 11, 2020, 10:33am UTC](https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658/1 "2020-12-11T10:33:33Z")

</div>

I have an undetermined (K) number of optimization problems to solve, and so far I have the following code to solve the K optimization problems.

```julia
using JuMP
using MosekTools
m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))

K = 3
N = 2
penalties = [1.0, 3.9, 8.7]
function fac1(r::Number, i::Number, l::Number)
    fac1 = 1.0
    for m in 0:r-1
        fac1 *= (i-m)*(l-m)
    end
    return fac1
end

function fac2(r::Number, i::Number, l::Number, tau::Float64)
   return tau ^ (i + l - 2r + 1)/(i + l - 2r + 1)
end

function Q_r(i::Number, l::Number, r::Number, tau::Float64)
    if i >= r && l >= r
        return 2 * fac1(r, i, l) * fac2(r, i, l, tau)
    else
        return 0.0
    end
end

function Q(i::Number, l::Number, tau::Number)
    elem = 0
    for r in 0:N
        elem += penalties[r + 1] * Q_r(i, l, r, tau)
    end
    return elem
end

# discrete segment starting times
Q_mat = Array{Float64, 3}(undef, K, N+1, N+1)
for k in 0:K-1
    for i in 1:N+1
        for j in 1:N+1
            Q_mat[k+1, i, j] = Q(i, j, convert(Float64, k))
        end
    end
end

@variable(m, p[K:1:N+1])
for k in 1:K
    @objective(m, Min, p[k]'*Q_mat[k,:,:]*p[k])
    optimize!(m)
    println(value.(p))
end

```

At the very end, I want to solve for multiple objective functions and display the results. My question is, is there a way to programmatically create multiple objective functions for a single model, and solve them separately, and secondly, why does the above code give the error that:

```julia
ERROR: LoadError: KeyError: key 1 not found
Stacktrace:
 [1] getindex at ./dict.jl:467 [inlined]
 [2] lookup_index at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:140 [inlined]
 [3] _to_index_tuple at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:149 [inlined]
 [4] to_index at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:167 [inlined]
 [5] getindex(::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{StepRange{Int64,Int64}},Tuple{Dict{Int64,Int64}}}, ::Int64) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:182
 [6] macro expansion at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:227 [inlined]
 [7] macro expansion at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:830 [inlined]
 [8] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:50
 [9] include(::Function, ::Module, ::String) at ./Base.jl:380
 [10] include(::Module, ::String) at ./Base.jl:368
 [11] exec_options(::Base.JLOptions) at ./client.jl:296
 [12] _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:49

```

---

<div class="post-metadata">

**Author:** ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)\
**Post date:** [December 11, 2020, 12:03pm UTC](https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658/2 "2020-12-11T12:03:10Z")

</div>

```julia
@variable(m, p[K:1:N+1])

```

There is no `p[1]`

---

<div class="post-metadata">

**Author:** ![prit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prit/32/20187_2.png) [@prit](https://discourse.julialang.org/u/prit)\
**Post date:** [December 12, 2020, 2:10am UTC](https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658/3 "2020-12-12T02:10:52Z")

</div>

Even when I do p[1,:,:] I get the same error. My understanding is that p[1,:,:] exists.

---

<div class="post-metadata">

**Author:** ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)\
**Post date:** [December 12, 2020, 2:48am UTC](https://discourse.julialang.org/t/how-to-index-a-jump-variable-during-optimization/51658/4 "2020-12-12T02:48:39Z")

</div>

`p` is a 1-dimensional vector with subscripts K,K+1,...,N+1
