Promote operation; report this

I have the following code in which a constraint is giving a “promote operation error”:

using JuMP
using MosekTools
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

function Q_mat()
    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
                mat[k+1, i, j] = Q(i, j, convert(Float64, k))
            end
        end
        return mat
    end
end

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

function A_0(r::Number, n::Number)
    if r == n
        fac = 1
        for m in 1:r
            fac *= r - (m - 1)
        end
        return fac
    else
        return 0.0
    end
end

m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))

@variable(m, A[i=1:K+1,j=1:K,k=1:N+1,l=1:N+1])
@variable(m, p[i=1:K+1,j=1:N+1])

# constraint difference might be a small fractional difference.
# assuming that time difference is 1 second starting from 0.
for i in 1:K
    @constraint(m, -A_tau_mat(convert(Float64, i-1)) * p[i] .+ A_tau_mat(convert(Float64, i-1)) * p[i+1] == [0.0, 0.0, 0.0])
end

for i in 1:K+1
    @constraint(m, A_tau_mat(convert(Float64, i-1)) * p[i] == [1.0 12.0 13.0])
end
@constraint(m, A_tau_mat(convert(Float64, K+1)) * p[K+1] == [1.0 2.0 0.0])

@objective(m, Min, sum(p[i]' * Q_mat()[i] * p[i] for i in 1:K+1))

optimize!(m)
println("p value is ", value.(p))
println(A_tau_mat(0.0), A_tau_mat(1.0), A_tau_mat(2.0))

ERROR: LoadError: `promote_operation(-, Array{GenericAffExpr{Float64,VariableRef},2}, Array{Float64,1})` not implemented yet, please report this.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] promote_operation(::Function, ::Type{Array{GenericAffExpr{Float64,VariableRef},2}}, ::Type{Array{Float64,1}}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/interface.jl:16
 [3] promote_operation(::typeof(MutableArithmetics.sub_mul), ::Type{Array{GenericAffExpr{Float64,VariableRef},2}}, ::Type{Array{Float64,1}}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/shortcuts.jl:56
 [4] mutability(::Type{T} where T, ::Function, ::Type{T} where T, ::Type{T} where T) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/interface.jl:132
 [5] mutability(::Array{GenericAffExpr{Float64,VariableRef},2}, ::Function, ::Array{GenericAffExpr{Float64,VariableRef},2}, ::Array{Float64,1}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/interface.jl:138
 [6] operate!(::typeof(MutableArithmetics.sub_mul), ::Array{GenericAffExpr{Float64,VariableRef},2}, ::Array{Float64,1}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:70
 [7] macro expansion at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:227 [inlined]
 [8] macro expansion at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:440 [inlined]
 [9] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:92
 [10] include(::Function, ::Module, ::String) at ./Base.jl:380
 [11] include(::Module, ::String) at ./Base.jl:368
 [12] exec_options(::Base.JLOptions) at ./client.jl:296
 [13] _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:91

The error is in the line:

@constraint(m, -A_tau_mat(convert(Float64, i-1)) * p[i] .+ A_tau_mat(convert(Float64, i-1)) * p[i+1] == [0.0, 0.0, 0.0])

This error disappears when == is replaced with .==. Why is that and how to fix this error?

This error disappears when == is replaced with .==

In your constraint, the left-hand side is a vector; so is the right-hand side.

To compare two vectors, and as stated in the documentation, you should use the broadcast notation .== and not the regular ==.

1 Like