I have the following code:
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))
In which I’m multiplying a matrix and a vector and equating it in a constraint:
@constraint(m, A_tau_mat(convert(Float64, i-1)) * p[i] == [1.0 12.0 13.0])
But I’m getting the following error:
ERROR: LoadError: DimensionMismatch("Cannot sum matrices of size `(3, 3)` and size `(1, 3)`, the size of the two matrices must be equal.")
Stacktrace:
[1] _check_dims at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:46 [inlined]
[2] mutable_operate!(::typeof(MutableArithmetics.sub_mul), ::Array{GenericAffExpr{Float64,VariableRef},2}, ::Array{Float64,2}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:55
[3] operate_fallback! at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/interface.jl:278 [inlined]
[4] operate!(::typeof(MutableArithmetics.sub_mul), ::Array{GenericAffExpr{Float64,VariableRef},2}, ::Array{Float64,2}) at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:70
[5] macro expansion at /Users/prikshetsharma/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:228 [inlined]
[6] macro expansion at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:440 [inlined]
[7] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:97
[8] include(::Function, ::Module, ::String) at ./Base.jl:380
[9] include(::Module, ::String) at ./Base.jl:368
[10] exec_options(::Base.JLOptions) at ./client.jl:296
[11] _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:96
How to fix this error? (This error disappears when == is replaced with .==)