Hi,
I’m trying to register a user-defined function in JuMP and get the following error:
ERROR: Unable to register the function :f_objective.
Common reasons for this include:
* The function takes `f(x::Vector)` as input, instead of the splatted
`f(x...)`.
* The function assumes `Float64` will be passed as input, it must work for any
generic `Real` type.
* The function allocates temporary storage using `zeros(3)` or similar. This
defaults to `Float64`, so use `zeros(T, 3)` instead.
As examples, instead of:
`my_function(x::Vector) = sum(x.^2)`
use:
`my_function(x::T...) where {T<:Real} = sum(x[i]^2 for i in 1:length(x))`
Instead of:
`function my_function(x::Float64...)
y = zeros(length(x))
for i in 1:length(x)
y[i] = x[i]^2
end
return sum(y)
end`
use:
`function my_function(x::T...) where {T<:Real}
y = zeros(T, length(x))
for i in 1:length(x)
y[i] = x[i]^2
end
return sum(y)
end`
Review the stacktrace below for more information, but it can often be hard to
understand why and where your function is failing. You can also debug this
outside of JuMP as follows:
`import ForwardDiff
# If the input dimension is 1
x = 1.0
my_function(a) = a^2
ForwardDiff.derivative(my_function, x)
# If the input dimension is more than 1
x = [1.0, 2.0]
my_function(a, b) = a^2 + b^2
ForwardDiff.gradient(x -> my_function(x...), x)`
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:33
[2] _validate_register_assumptions(f::typeof(f_objective), name::Symbol, dimension::Int64)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:267
[3] (MathOptInterface.Nonlinear._MultivariateOperator{20, F′, F′′} where {F′, F′′})(op::Symbol, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:297
[4] register_operator(registry::MathOptInterface.Nonlinear.OperatorRegistry, op::Symbol, nargs::Int64, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:350
[5] register_operator(model::MathOptInterface.Nonlinear.Model, op::Symbol, nargs::Int64, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\model.jl:217
[6] register(model::Model, op::Symbol, dimension::Int64, f::Function; autodiff::Bool)
@ JuMP C:\Users\kjaya\.julia\packages\JuMP\yYfHy\src\nlp.jl:677
[7] top-level scope
@ g:\Other computers\PC\PhD\Courses\D1000 Linear Control Theory\PhD paper 1\Git\latestOrganizedCode.jl:87
caused by: MethodError: no method matching schur!(::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}})
Closest candidates are:
schur!(::StridedMatrix{var"#s814"} where var"#s814"<:Union{Float32, Float64, ComplexF32, ComplexF64}) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\schur.jl:97
schur!(::StridedMatrix{T}, ::StridedMatrix{T}) where T<:Union{Float32, Float64, ComplexF32, ComplexF64} at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\schur.jl:288
Stacktrace:
[1] schur(A::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}})
@ LinearAlgebra C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\schur.jl:143
[2] lyapc(A::Adjoint{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}}}, C::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}})
@ MatrixEquations C:\Users\kjaya\.julia\packages\MatrixEquations\pS06e\src\lyapunov.jl:68
[3] f_objective(::ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}, ::Vararg{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}, N} where N)
@ Main g:\Other computers\PC\PhD\Courses\D1000 Linear Control Theory\PhD paper 1\Git\latestOrganizedCode.jl:82
[4] (::MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)})(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}})
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:263
[5] chunk_mode_gradient(f::MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}}})
@ ForwardDiff C:\Users\kjaya\.julia\packages\ForwardDiff\QdStj\src\gradient.jl:150
[6] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}}}, ::Val{true})
@ ForwardDiff C:\Users\kjaya\.julia\packages\ForwardDiff\QdStj\src\gradient.jl:21
[7] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10, Vector{ForwardDiff.Dual{ForwardDiff.Tag{MathOptInterface.Nonlinear.var"#15#16"{typeof(f_objective)}, Float64}, Float64, 10}}}) (repeats 2 times) @ ForwardDiff C:\Users\kjaya\.julia\packages\ForwardDiff\QdStj\src\gradient.jl:17
[8] _validate_register_assumptions(f::typeof(f_objective), name::Symbol, dimension::Int64)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:263
[9] (MathOptInterface.Nonlinear._MultivariateOperator{20, F′, F′′} where {F′, F′′})(op::Symbol, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:297
[10] register_operator(registry::MathOptInterface.Nonlinear.OperatorRegistry, op::Symbol, nargs::Int64, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\operators.jl:350
[11] register_operator(model::MathOptInterface.Nonlinear.Model, op::Symbol, nargs::Int64, f::Function)
@ MathOptInterface.Nonlinear C:\Users\kjaya\.julia\packages\MathOptInterface\NCblk\src\Nonlinear\model.jl:217
[12] register(model::Model, op::Symbol, dimension::Int64, f::Function; autodiff::Bool)
@ JuMP C:\Users\kjaya\.julia\packages\JuMP\yYfHy\src\nlp.jl:677
[13] top-level scope
@ g:\Other computers\PC\PhD\Courses\D1000 Linear Control Theory\PhD paper 1\Git\latestOrganizedCode.jl:87
Here’s a minimal working example:
using JuMP, Ipopt, HiGHS, Juniper, LinearAlgebra, MatrixEquations
# Setting up the solver
nlp_solver = optimizer_with_attributes(Ipopt.Optimizer, MOI.Silent() => true, "sb" => "yes", "max_iter" => Int(1E4), );
mip_solver = optimizer_with_attributes(HiGHS.Optimizer, "presolve" => "on", "log_to_console" => false, );
minlp_solver = optimizer_with_attributes(Juniper.Optimizer, MOI.Silent() => true, "mip_solver" => mip_solver, "nl_solver" => nlp_solver, );
m = Model(minlp_solver);
@variable(m, x[1:20], Bin);
function f_objective(x::T...) where {T<:Real}
A = diagm( 0 => (-1-1)*ones(T,20),
1 => (2-1)*ones(T,20-1),
-1 => (3-1)*ones(T,20-1),
2 => (-5-1)*ones(T,20-2),
-2 => (-4-1)*ones(T,20-2)) + ones(T,20,20);
x = [x...]
C = diagm(x)
Wo = MatrixEquations.lyapc(A', A'*C)
return(tr(Wo))
end
register(m, :f_objective, 20, f_objective; autodiff=true)
The problem seems to be in Wo = MatrixEquations.lyapc(A', A'*C)
…