我使用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
答案 0 :(得分:1)
问题在于,在您的代码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()))
或
optimize(y -> -f(vcat(x, y)), repeat([0.0], 12), fill(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
...
,您会发现您可能怀疑optimize
的{{1}}仅接受Fminbox
的数组。确保运行:
AbstractFloat
,并且您确定所有接受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
的{{1}}方法都需要optimize
。从理论上讲,您要求的这种自动促销是可能的,但是正如您看到的那样,它没有实现,因此您只需要记住将Fminbox
参数传递给AbstractFloat
。
Julia默认情况下不会执行这样的自动升级。这是一个最小的示例:
AbstractFloat