Reputation: 1463
I'm using the Julia JuMP solver and I'm trying to write a constraint involving the power of a matrix.
My variables are a square matrix M and an integer n. The size of M is s. We have s>n.
M being a square matrix, we can see it as the adjacency matrix of a graph with s vertices. I want to assert that this graph has n vertices that are part of a cycle with length n.
So I added the JuMP constraint
@constraint(model, tr(M^n)==n)
When I try to run the solver, I get this error, which seems to be caused by the M^n
:
MethodError: zero(::Type{Union{}}) is ambiguous. Candidates:
zero(::Type{var"#s828"} where var"#s828"<:AbstractIrrational) in Base at irrationals.jl:149
zero(::Union{Type{P}, P}) where P<:Dates.Period in Dates at /opt/julia-1.5.2/share/julia/stdlib/v1.5/Dates/src/periods.jl:53
zero(::Type{T}) where T<:Number in Base at number.jl:242
zero(F::Type{var"#s280"} where var"#s280"<:Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}) where T in MathOptInterface.Utilities at /home/n7student/.julia/packages/MathOptInterface/VjkSQ/src/Utilities/functions.jl:1987
zero(a::Type{SA}) where SA<:StaticArrays.StaticArray in StaticArrays at /home/n7student/.julia/packages/StaticArrays/LJQEe/src/linalg.jl:88
zero(::Type{V}) where V<:AbstractVariableRef in JuMP at /home/n7student/.julia/packages/JuMP/qhoVb/src/JuMP.jl:734
Possible fix, define
zero(::Type{Union{}})
Stacktrace:
[1] mutable_operate!(::typeof(zero), ::Array{Union{},2}) at /home/n7student/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:202
[2] mutable_operate_to! at /home/n7student/.julia/packages/MutableArithmetics/0tlz5/src/linear_algebra.jl:207 [inlined]
[3] _mul! at /home/n7student/.julia/packages/MutableArithmetics/0tlz5/src/dispatch.jl:63 [inlined]
[4] mul! at /home/n7student/.julia/packages/MutableArithmetics/0tlz5/src/dispatch.jl:68 [inlined]
[5] * at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:153 [inlined]
[6] power_by_squaring(::Array{VariableRef,2}, ::Int64) at ./intfuncs.jl:245
[7] ^ at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/dense.jl:408 [inlined]
[8] macro expansion at ./none:0 [inlined]
[9] literal_pow(::typeof(^), ::Array{VariableRef,2}, ::Val{2}) at ./none:0
[10] macro expansion at /home/n7student/.julia/packages/MutableArithmetics/0tlz5/src/rewrite.jl:227 [inlined]
[11] macro expansion at /home/n7student/.julia/packages/JuMP/qhoVb/src/macros.jl:440 [inlined]
[12] top-level scope at ./In[89]:55
[13] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[14] execute_code(::String, ::String) at /home/n7student/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[15] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/n7student/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[16] #invokelatest#1 at ./essentials.jl:710 [inlined]
[17] invokelatest at ./essentials.jl:709 [inlined]
[18] eventloop(::ZMQ.Socket) at /home/n7student/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[19] (::IJulia.var"#15#18")() at ./task.jl:356
Upvotes: 1
Views: 286
Reputation: 2574
You can only form linear or quadratic constraints in @constraint
.
The normal work-around is to define a series of constraints
model = Model()
@variable(model, x[i=1:2, j=1:2, k=1:3])
@constraint(model, [k=1:2], x[:, :, k+1] .== x[:, :, k] * x[:, :, 1])
Alternatively, use a nonlinear user-defined function along the likes of:
using JuMP, LinearAlgebra, Ipopt
s, n = 3, 2
f(x...) = LinearAlgebra.tr(reshape(collect(x), (s, s))^n)
model = Model(Ipopt.Optimizer)
register(model, :f, s^2, f; autodiff = true)
@variable(model, x[1:s, 1:s])
@NLconstraint(model, f(x...) == n)
optimize!(model)
Upvotes: 1