1
votes

I have the following code in which the JuMP constraint is throwing an error.

using JuMP
using MosekTools
K = 3
N = 2
penalties = [1.0, 3.9, 8.7]

function A_tau(r::Number, n::Number, tau::Float64)
    fac = 1
    for m in 1:r
        fac *= (n - (m - 1))
    end
    if n >= r
        return fac * tau ^ (n - r)
    else
        return 0.0
    end
end

function A_tau_mat(tau::Float64)
    mat = Array{Float64, 2}(undef, N+1, N+1)
    for i in 1:N+1
        for j in 1:N+1
            mat[i, j] = A_tau(i, j, tau)
        end
    end
    return mat
end
m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))


@variable(m, p[1:1:K,1:1:N+1])
@variable(m, A[1:1:K+1,1:1:K,1:1:N+1,1:1:N+1])
@constraint(m, -A_tau_mat(0.0) * p[1, :] == [0.0, 0.0, 0.0])

optimize!(m)
println("p value is ", value.(p[1, :]))
println(A_tau_mat(0.0))

The error happens when the @constraint line is added and there's no error without it. The error is as follows. The error shows no method matching CartesianIndex ::Int64.

ERROR: LoadError: MethodError: no method matching -(::CartesianIndex{1}, ::Int64)
Closest candidates are:
  -(!Matched::Complex{Bool}, ::Real) at complex.jl:307
  -(!Matched::Missing, ::Number) at missing.jl:115
  -(!Matched::MutableArithmetics.Zero, ::Any) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:55
  ...
Stacktrace:
 [1] _add_mul_array(::Array{GenericAffExpr{Float64,VariableRef},1}, ::Array{Float64,2}, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{StepRange{Int64,Int64}},Tuple{Dict{Int64,Int64}}}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:166
 [2] mutable_operate! at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:196 [inlined]
 [3] mutable_operate_to!(::Array{GenericAffExpr{Float64,VariableRef},1}, ::typeof(*), ::Array{Float64,2}, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{StepRange{Int64,Int64}},Tuple{Dict{Int64,Int64}}}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:208
 [4] operate at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:221 [inlined]
 [5] operate at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:43 [inlined]
 [6] operate_fallback! at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/interface.jl:275 [inlined]
 [7] operate!(::typeof(MutableArithmetics.sub_mul), ::MutableArithmetics.Zero, ::Array{Float64,2}, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{StepRange{Int64,Int64}},Tuple{Dict{Int64,Int64}}}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:70
 [8] top-level scope at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:227
 [9] top-level scope at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:440
 [10] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:72
 [11] include(::Function, ::Module, ::String) at ./Base.jl:380
 [12] include(::Module, ::String) at ./Base.jl:368
 [13] exec_options(::Base.JLOptions) at ./client.jl:296
 [14] _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:72

How to fix this error and use the constraint like I want to? What's wrong with this constraint?

1
Please provide a link when cross-posting: discourse.julialang.org/t/…Oscar Dowson

1 Answers

1
votes

You can try this

@variable(m, p[1:K,1:N+1])
@variable(m, A[1:K+1,1:K,1:N+1,1:N+1])
@constraint(m, -A_tau_mat(0.0) * p[1, :] .== [0.0, 0.0, 0.0])

There are two problems. First, the p's type in your original is DenseAxisArray rather than the normal Array because a StepRange (1:1:3) rather than a UnitRange (1:3) is provided. Though having the same elements, their types are different and the matrix multiplication implementation for it is somewhat problematic. I think it is a bug that should be fixed on the JuMP side. The other change is the dot . before the ==, which indicates broadcasting.