JuliaNLSolvers / Optim.jl

Optimization functions for Julia
Other
1.13k stars 221 forks source link

Differences in brent implementation of brent.jl and brentmin.jl #1036

Open kaber2 opened 1 year ago

kaber2 commented 1 year ago

Hello,

I was comparing the implementations of:

And notices a few differences. I was wondering if someone can shed some light on which of the two is correct.

First, while brent.jl uses:

    if abs(new_minimizer - x_midpoint) <= 2*x_tol - (x_upper-x_lower)/2

As stopping criterium, brentmin.hl has the second condition turned around:

    if abs(x - m) > 2 * tol - (b - a) / 2 # stopping crit

(b-a instead of a-b). A quick test in my own implementation shows that brentmin.jl seems to be incorrect as even simple functions don't converge anymore.

Then, when selecting the new step, brent.jl does:

        new_x = new_minimizer + ((step > 0) ? x_tol : -x_tol)

while brentmin.jl dies:

            u = x + sign(d) * tol

So it treats the case of d == 0 differently.

pkofod commented 1 year ago

Both are correct, the conditions are turned around because in Optim.jl the code path with the condition you mention stops if true and in NLSolvers.jl the code paths continues if true, hence the swapped inequality. The reference implementation in Brent's book has it the same way NLSolvers.jl does

image

So if that condition is true (no convergence) it continues.

As per the second part, that is true, it should be copysign(tol, d) as per brent's book image

pkofod commented 1 year ago

Please provide the simple functions you're referring to.