在Julia中的函数中使用Optim时,如何解决此方法错误?

在Julia中的函数中使用Optim时,如何解决此方法错误?,julia,Julia,张贴在 我用Optim编写了一个鞍形查找函数。它工作得相当好,但当我尝试使用框优化时,它给了我一个方法错误。有什么问题 这项工作: using Optim function saddle2(f::Function, initx, inity) # function is assumed to be f(xmin, ymax) ymax = similar(inity) function fx(x) optymax = optimize(y -> -

张贴在

我用Optim编写了一个鞍形查找函数。它工作得相当好,但当我尝试使用框优化时,它给了我一个方法错误。有什么问题

这项工作:

using Optim

function saddle2(f::Function, initx, inity)
    # function is assumed to be f(xmin, ymax)
    ymax = similar(inity)
    function fx(x)
        optymax = optimize(y -> -f(vcat(x, y)), inity, NelderMead())
        ymax = Optim.minimizer(optymax)
        return -Optim.minimum(optymax)
    end
    optxmin = optimize(fx, repeat([0.0], 6), initx, NelderMead())
    xmin = Optim.minimizer(optxmin)
    return (f(vcat(xmin, ymax)), xmin, ymax)
end

saddle2(Lsaddle, repeat([0.5], 6), repeat([0.5], 12)) # works!
这不起作用:

using Optim

function saddle(f::Function, initx, inity)
    # function is assumed to be f(xmin, ymax)
    ymax = similar(inity)
    function fx(x)
        optymax = optimize(y -> -f(vcat(x, y)), repeat([0.0], 12), repeat([Y], 12), inity, Fminbox(NelderMead()))
        ymax = Optim.minimizer(optymax)
        return -Optim.minimum(optymax)
    end
    optxmin = optimize(fx, repeat([0.0], 6), repeat([1.0], 6), initx, Fminbox(NelderMead()))
    xmin = Optim.minimizer(optxmin)
    return (f(vcat(xmin, ymax)), xmin, ymax)
end

saddle(Lsaddle, repeat([0.5], 6), repeat([0.5], 12)) # doesn't work:

MethodError: no method matching optimize(::getfield(Main, Symbol("##3#5")){Array{Float64,1},typeof(Lsaddle)}, ::Array{Float64,1}, ::Array{Int64,1}, ::Array{Float64,1}, ::Fminbox{NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters},Float64,getfield(Optim, Symbol("##46#48"))})
Closest candidates are:
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox) where T<:AbstractFloat at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:163
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox, !Matched::Any; inplace, autodiff) where T<:AbstractFloat at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:163
  optimize(::Any, ::AbstractArray, ::AbstractArray, ::AbstractArray, !Matched::SAMIN) at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/samin.jl:60
  ...

Stacktrace:
 [1] (::getfield(Main, Symbol("#fx#4")){typeof(Lsaddle),Array{Float64,1}})(::Array{Float64,1}) at ./In[9]:5
 [2] finite_difference_gradient! at /Users/amrods/.julia/packages/DiffEqDiffTools/visbP/src/gradients.jl:282 [inlined]
 [3] (::getfield(NLSolversBase, Symbol("#g!#15")){getfield(Main, Symbol("#fx#4")){typeof(Lsaddle),Array{Float64,1}},DiffEqDiffTools.GradientCache{Nothing,Nothing,Nothing,Val{:central},Float64,Val{true}}})(::Array{Float64,1}, ::Array{Float64,1}) at /Users/amrods/.julia/packages/NLSolversBase/KG9Ie/src/objective_types/oncedifferentiable.jl:56
 [4] gradient!!(::OnceDifferentiable{Float64,Array{Float64,1},Array{Float64,1}}, ::Array{Float64,1}) at /Users/amrods/.julia/packages/NLSolversBase/KG9Ie/src/interface.jl:63
 [5] optimize(::OnceDifferentiable{Float64,Array{Float64,1},Array{Float64,1}}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Fminbox{NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters},Float64,getfield(Optim, Symbol("##46#48"))}, ::Optim.Options{Float64,Nothing}) at /Users/amrods/.julia/packages/NLSolversBase/KG9Ie/src/interface.jl:51
 [6] #optimize#53 at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:164 [inlined]
 [7] optimize at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:163 [inlined] (repeats 2 times)
 [8] saddle(::typeof(Lsaddle), ::Array{Float64,1}, ::Array{Float64,1}) at ./In[9]:9
 [9] top-level scope at In[13]:1
使用Optim
函数鞍(f::函数,initx,inity)
#函数假定为f(xmin,ymax)
ymax=相似(相似)
函数fx(x)
optymax=optimize(y->-f(vcat(x,y)),repeat([0.0],12),repeat([y],12),inity,Fminbox(NelderMead())
ymax=最优最小值(optymax)
返回-优化最小值(optymax)
终止
optxmin=optimize(fx,repeat([0.0],6),repeat([1.0],6),initx,Fminbox(NelderMead())
xmin=最优最小值(optxmin)
返回(f(vcat(xmin,ymax)),xmin,ymax)
终止
鞍座(Lsaddle,repeat([0.5],6),repeat([0.5],12))#不起作用:
MethodError:没有方法匹配优化(::getfield(Main,Symbol(“##3#5”){Array{Float64,1},typeof(Lsaddle)},::Array{Float64,1},::Array{Int64,1},::Array{Float64,1},::Fminbox{NelderMead{Optim affinesImplesImplexer,Optim.AdaptiveParameters},64,getfield Optim,Symbol(“3546})
最接近的候选人是:

优化(::Any,::AbstractArray{T问题是在你的代码中
Y
是一个
Int
而不是
Float64
。很可能你写过类似
Y=1
的东西,你应该写
Y=1.0

如果您不影响
Y
的定义方式,请编写:

optimize(y -> -f(vcat(x, y)), repeat([0.0], 12), repeat(Float64[Y], 12), inity, Fminbox(NelderMead()))

如果你发布了一个完全可复制的代码,那么诊断问题就容易多了

编辑

您可以通过以下方式跟踪此问题。请阅读错误消息:

MethodError: no method matching optimize(::getfield(Main, Symbol("##3#5")){Array{Float64,1},typeof(Lsaddle)}, ::Array{Float64,1}, ::Array{Int64,1}, ::Array{Float64,1}, ::Fminbox{NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters},Float64,getfield(Optim, Symbol("##46#48"))})
Closest candidates are:
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox) where T<:AbstractFloat at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:163
  optimize(::Any, ::AbstractArray{T<:AbstractFloat,N} where N, !Matched::AbstractArray{T<:AbstractFloat,N} where N, ::AbstractArray{T<:AbstractFloat,N} where N, ::Fminbox, !Matched::Any; inplace, autodiff) where T<:AbstractFloat at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/fminbox.jl:163
  optimize(::Any, ::AbstractArray, ::AbstractArray, ::AbstractArray, !Matched::SAMIN) at /Users/amrods/.julia/packages/Optim/ULNLZ/src/multivariate/solvers/constrained/samin.jl:60
  ...
而且您确信所有接受
Fminbox
optimize
方法都需要
AbstractFloat
。理论上,您要求的自动升级是可能的,但正如您所看到的,它没有实现,所以您只需记住将
AbstractFloat
参数传递给
optimize

默认情况下,Julia不会像这样执行自动升级。下面是一个简单的示例:

julia> f(x::Vector{Float64}) = x
f (generic function with 1 method)

julia> f([1,2,3])
ERROR: MethodError: no method matching f(::Array{Int64,1})
Closest candidates are:
  f(::Array{Float64,1}) at REPL[9]:1
Stacktrace:
 [1] top-level scope at none:0

我想这就是问题所在(它现在正在运行)。我发布代码时错过了
Y=300
。谢谢!顺便问一下,我在错误中看到了什么?为什么
Optim
不将
Int
提升到
Float64
?我会在答案中解释。
julia> methodswith(Fminbox, supertypes=true)
[1] summary(F::Fminbox) in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:117
[2] optimize(df::OnceDifferentiable, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:175
[3] optimize(df::OnceDifferentiable, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox, options) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:175
[4] optimize(df::OnceDifferentiable, l::Array{T,N} where N, u::Array{T,N} where N, F::Fminbox{O,T,P} where P where T) where {T<:AbstractFloat, O<:AbstractOptimizer} in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\deprecate.jl:67
[5] optimize(f, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:163
[6] optimize(f, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox, options) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:163
[7] optimize(f, g, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:150
[8] optimize(f, g, l::AbstractArray{T,N} where N, u::AbstractArray{T,N} where N, initial_x::AbstractArray{T,N} where N, F::Fminbox, options) where T<:AbstractFloat in Optim at C:\Users\bogum\.julia\packages\Optim\ULNLZ\src\multivariate\solvers\constrained\fminbox.jl:150
julia> f(x::Vector{Float64}) = x
f (generic function with 1 method)

julia> f([1,2,3])
ERROR: MethodError: no method matching f(::Array{Int64,1})
Closest candidates are:
  f(::Array{Float64,1}) at REPL[9]:1
Stacktrace:
 [1] top-level scope at none:0