How to get evaluation number in optimization using NLopt

Hi @Mary,

You can do something like this:

function gf_p_optimize(p_init; r, β, η, TOL=1e-4, MAX_ITER, fem_params)
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 0
    opt.upper_bounds = 1
    opt.xtol_rel = TOL
    opt.maxeval = MAX_ITER
    iteration_counter = 0  # <-- new
    iteration_solutions = Any[]  # <-- new
    function objective_fn(p0, grad)
        iteration_counter += 1  # <-- new
        push!(iteration_solutions, p0)  # <-- new 
        println("called from iteration $iteration_counter")
        return gf_p(p0, grad; r, β, η, fem_params)
    end
    opt.max_objective = objective_fn
    (g_opt, p_opt, ret) = optimize(opt, p_init)
    println("got $g_opt at $p_opt after $numevals iterations (returned $ret)")
    return g_opt, p_opt
end
2 Likes