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

Is it possible to stop early if function value return NaN? #10

Closed
Denis-Titov opened this issue Sep 26, 2023 · 3 comments
Closed

Is it possible to stop early if function value return NaN? #10

Denis-Titov opened this issue Sep 26, 2023 · 3 comments

Comments

@Denis-Titov
Copy link

I use CMA-ES to fit many different versions of equations and sometimes a combination of parameters is such that the target function returns NaN.
Currently, the minimization just continues until it reaches maxiter and then stops.

Can I use callback or modify stopping criteria to stop the minimization if the target function returns NaN?

@Denis-Titov
Copy link
Author

Actually, never mind. For my case, the target function always returns NaN so I can just determine this before using minimize(), and that solves my issue.

@jbrea
Copy link
Owner

jbrea commented Sep 27, 2023

Glad you don't need it for your case.

But this is actually a good question! There is a hacky way of achieving this, but I should think about better ways.
The hack is to manually hit a stopping criterion, when the callback detects a NaN.
Example:

f(x) = x[1] + randn() > 1 ? NaN : sum(abs2, x)
function cb(o, x, f, p)
    @show f
    if any(isnan.(f))
        o.stop.t0 = -Inf
    end
end
minimize(f, [.9], 1., maxtime = 100, callback = cb)

@Denis-Titov
Copy link
Author

Oh cool, thanks! So basically you trigger maxtime by changing t0 to -Inf since maxtime = time()-t0.

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