Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optim with BFGS, LBGFS gives wrong result #1045

Closed
ChrisNabold opened this issue Jul 1, 2023 · 1 comment
Closed

Optim with BFGS, LBGFS gives wrong result #1045

ChrisNabold opened this issue Jul 1, 2023 · 1 comment

Comments

@ChrisNabold
Copy link

I try to run the rosenbrock example from the doc.

julia> using Optim

julia> f(x) = 100*(x[2]-x[1]*x[1])^2 +(1-x[1])^2
f (generic function with 1 method)

julia> x0=[-1.2,1];

julia> sol=optimize(f,x0)

  • Status: success

  • Candidate solution
    Final objective value: 4.657541e-09

  • Found with
    Algorithm: Nelder-Mead

  • Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

  • Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 78
    f(x) calls: 149

julia> sol.minimizer
2-element Vector{Float64}:
1.0000117499532974
1.0000167773369513

julia> sol.minimum
4.657541291131408e-9

julia> sol.iterations
78

julia>

julia> function g!(x::Vector, storage::Vector)
storage[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
storage[2] = 200.0 * (x[2] - x[1]^2)
end
g! (generic function with 1 method)

julia> sol=optimize(f,g!,x0,BFGS())

  • Status: failure

  • Candidate solution
    Final objective value: NaN

  • Found with
    Algorithm: BFGS

  • Convergence measures
    |x - x'| = NaN ≰ 0.0e+00
    |x - x'|/|x'| = NaN ≰ 0.0e+00
    |f(x) - f(x')| = NaN ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = NaN ≰ 0.0e+00
    |g(x)| = NaN ≰ 1.0e-08

  • Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 0
    f(x) calls: 1
    ∇f(x) calls: 1

julia>

@pkofod
Copy link
Member

pkofod commented Jul 28, 2023

That is because the gradient element goes first in the gradient method/function signature... So

function g!(storage::Vector, x::Vector)
storage[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
storage[2] = 200.0 * (x[2] - x[1]^2)
end

Looks like you somehow found some really really old documentation...

@pkofod pkofod closed this as completed Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants