Method error in Optim inside a function

optim
#1

I programmed a saddle-finding function using Optim. It works fairly well, but when I try to use box optimization, it gives me a methods error. What is the problem?

This works:

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!

This doesn’t work:

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