How to index a JuMP variable during optimization

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.

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:

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

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

There is no p[1]

1 Like

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

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

1 Like