jbrea / CMAEvolutionStrategy.jl

Other
29 stars 0 forks source link

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

Closed Denis-Titov closed 11 months ago

Denis-Titov commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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