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

Hessian code incorrect for maximize #1118

Closed
dgleich opened this issue Nov 12, 2024 · 3 comments
Closed

Hessian code incorrect for maximize #1118

dgleich opened this issue Nov 12, 2024 · 3 comments

Comments

@dgleich
Copy link

dgleich commented Nov 12, 2024

This code which is

    fmax = x->-f(x)
    gmax = (G,x)->(g(G,x); G.=-G)
    hmax = (H,x)->(h(G,x); H.=-H)
    MaximizationWrapper(optimize(fmax, gmax, hmax, x0, method, options; kwargs...))
end

has a typo in the Hessian.

In case you need to reproduce, this works:

f(x) = -(1.0 - x[1])^2 - 100.0 * (x[2] - x[1]^2)^2
  function g!(G, x)
    G[1] = -1*(-2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1])
    G[2] = -200.0 * (x[2] - x[1]^2)
  end
  function H!(H, x)
    H[1, 1] = -2.0 + 800.0 * x[1]^2 + 400.0 * x[2]
    H[1, 2] = 400.0 * x[1]
    H[2, 1] = 400.0 * x[1]
    H[2, 2] = -200.0
  end 

x0 = [0.0, 0.0]
soln = maximize(f, g!, H!, x0, Newton())

throws the following error

julia> soln = maximize(f, g!, H!, x0, Newton())
ERROR: UndefVarError: `G` not defined
Stacktrace:
  [1] (::Optim.var"#147#150"{typeof(H!)})(H::Matrix{Float64}, x::Vector{Float64})
    @ Optim ~/.julia/packages/Optim/ZhuZN/src/maximize.jl:41
  [2] hessian!!(obj::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, x::Vector{Float64})
    @ NLSolversBase ~/.julia/packages/NLSolversBase/kavn7/src/interface.jl:95
  [3] initial_state(method::Newton{…}, options::Optim.Options{…}, d::TwiceDifferentiable{…}, initial_x::Vector{…})
    @ Optim ~/.julia/packages/Optim/ZhuZN/src/multivariate/solvers/second_order/newton.jl:46
  [4] optimize
    @ ~/.julia/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:36 [inlined]
  [5] #optimize#94
    @ ~/.julia/packages/Optim/ZhuZN/src/multivariate/optimize/interface.jl:163 [inlined]
  [6] optimize
    @ ~/.julia/packages/Optim/ZhuZN/src/multivariate/optimize/interface.jl:158 [inlined]
  [7] maximize(f::var"#52#53", g::typeof(g!), h::typeof(H!), x0::Vector{…}, method::Newton{…}, options::Optim.Options{…}; kwargs::@Kwargs{})
    @ Optim ~/.julia/packages/Optim/ZhuZN/src/maximize.jl:42
  [8] maximize(f::Function, g::Function, h::Function, x0::Vector{…}, method::Newton{…}, options::Optim.Options{…})
    @ Optim ~/.julia/packages/Optim/ZhuZN/src/maximize.jl:38
  [9] maximize
    @ ~/.julia/packages/Optim/ZhuZN/src/maximize.jl:38 [inlined]
 [10] maximize(f::Function, g::Function, h::Function, x0::Vector{…}, method::Newton{…})
    @ TechnicalCompute ~/Dropbox/dev/TechnicalCompute/src/overrides.jl:1747
 [11] top-level scope
    @ REPL[889]:1
Some type information was truncated. Use `show(err)` to see complete types.
@pkofod
Copy link
Member

pkofod commented Nov 12, 2024

Oh, interesting. Thank you

Edit: seems like only the maximize(f, x, ...) methods were tested.

@pkofod
Copy link
Member

pkofod commented Nov 12, 2024

The fix is on master. I'll release soon.

@pkofod pkofod closed this as completed Nov 12, 2024
@dgleich
Copy link
Author

dgleich commented Nov 13, 2024

Thanks for handling it so promptly!!

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