JuliaNLSolvers / Optim.jl

Optimization functions for Julia
Other
1.11k stars 213 forks source link

Cannot use Options with box constrained optimization (without providing gradient?) #1016

Open benoitseron opened 1 year ago

benoitseron commented 1 year ago

I am trying to optimize a function in a box constrained setting. I cannot provide its gradient. This seems not to be an issue if I am not asking for any options, but as soon as I put some, I have the following error.

x_0 = [1., 1.]
lower = zeros(length(x_0))
upper = ones(length(x_0))

optimize(f, x_0, Optim.Options(time_limit = 15.0))
# works
optimize(f,lower, upper, x_0)
# works

optimize(f,lower, upper, x_0 , Optim.Options(time_limit = 15.0))
# fails :

# MethodError: objects of type Vector{Float64} are not callable
# Use square brackets [] for indexing an Array.

# I assume that the problem comes from not providing "g"

I am not sure if this comes from misuse from my side.

jecs commented 1 year ago

Have you tried explicitly instantiating the solver and wrapping it in Fminbox? e.g. Fminbox(NelderMead()).

Fminbox is used in the examples under Box Constrained Optimization. It's a bit indirect to learn about it; the documentation could certainly use some updating. Also, I am not sure if Fminbox works for gradient-free solvers.

Edit: I just checked the source code and Fminbox does indeed seem to support gradient-free methods.

pkofod commented 1 year ago

There might be missing a dispatch somewhere. Try to specify Fminbox explicitly before the options as @jecs stated.