Dear Community,
I got a question about functor usage with Optim.jl.
I’m currently writing a Julian package for biological modeling. To organize parameters of the biological process, I chose function-like object (functor) for development. However, I found functor type is somehow not supported with Optim.jl though it belongs to Any
type and works like function.
Here is my sample code of Optim.optimize with the input of functor type. I got MethodError due to using the functor type with Optim.jl.
Is there a reason why functor can not work like a function in this situation?
Code
# Code for finding the minimum of y-function within (0.1,0.2)
using Optim
using Test
struct y
p
end
function (s::y)(x)
return x*s.p
end
model = y(1.0)
model2(x;p=1.0) = x*p
@show model(2.0)
@show model2(2.0)
@test typeof(y) <: Any # Because Optim.jl accepts :Any object
Optim.optimize(model, 0.1, 0.2); # MethodError
Optim.optimize(model2, 0.1, 0.2); # Pass
REPL result
However, it seems like functor is not supported in Optim.jl
julia> using Optim
julia> using Test
julia> struct y
p
end
julia> function (s::y)(x)
return x*s.p
end
julia> model = y(1.0)
y(1.0)
julia> model2(x;p=1.0) = x*p
model2 (generic function with 1 method)
julia> @show model(2.0)
model(2.0) = 2.0
2.0
julia> @show model2(2.0)
model2(2.0) = 2.0
2.0
julia> @test typeof(y) <: Any # Because Optim.jl accepts :Any object
Test Passed
julia> Optim.optimize(model, 0.1, 0.2);
ERROR: MethodError: no method matching optimize(::y, ::Float64, ::Float64)
Closest candidates are:
optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N; kwargs...) where T at /Users/stevenchiu/.julia/packages/Optim/TNmSw/src/multivariate/solvers/constrained/fminbox.jl:156
optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N, ::Optim.AbstractConstrainedOptimizer) where T at /Users/stevenchiu/.julia/packages/Optim/TNmSw/src/multivariate/solvers/constrained/fminbox.jl:160
optimize(::Any, ::Number, ::Number, ::AbstractArray{T,N} where N, ::Optim.AbstractConstrainedOptimizer, ::Optim.Options; kwargs...) where T at /Users/stevenchiu/.julia/packages/Optim/TNmSw/src/multivariate/solvers/constrained/fminbox.jl:160
...
Stacktrace:
[1] top-level scope at none:1
julia> Optim.optimize(model2, 0.1, 0.2);
julia>
Thank you for reading this question.