# Interim values of Metaheuristics optimiziers

**URL:** https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [October 9, 2023, 8:35pm UTC](https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768 "2023-10-09T20:35:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jcusick](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@jcusick](https://discourse.julialang.org/u/jcusick)
#### Post date: [October 9, 2023, 8:35pm UTC](https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768/1 "2023-10-09T20:35:35Z")

</div>

I’m interested in getting the interim population values for algorithms within `Metaheuristics.jl` at each timestep, for example Differential Evolution.

I’ve seen [examples](https://jmejia8.github.io/Metaheuristics.jl/stable/visualization/#Convergence) where the _single_ member of the population with the best fitness can be retrieved at each iteration using `convergence()`, though I’d like to be able to interrogate _each member_ of the population at a given timestep.

Is there a clean way to do this? I looked around but it doesn’t seem like `Metaheuristics.optimize()` provides the ability to give a callback function or similar pattern.

The hacky approach I’ve found thus far is to inspect `status.population` during internal calls to `Metaheuristics.stop_criteria!`, which is based off this example from [modifying an existing metaheuristic](https://jmejia8.github.io/Metaheuristics.jl/stable/examples/#Modifying-an-Existing-Metaheuristic).

```julia

global POPULATION [];

function Metaheuristics.stop_criteria!(
        status,
        parameters::DE
        problem,
        information,
        options,
        args...;
        kargs...
    )
    
    # Allow for traditional usage of DE algo to
    # set status and determine when to stop
    if status.stop
        return
    end

    # Record state of population
    push!(POPULATION, status.population)

end

```

Perhaps there are other lesser documented internal `Metaheuristics` functions that  
would be more direct in what I’m looking for?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 10, 2023, 5:50am UTC](https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768/2 "2023-10-10T05:50:06Z")

</div>

Hi @jcusick, welcome to the forum. I don’t know the answer to the question, but @jmejia8 should be able to help.

---

<div class="post-metadata">

### Author: ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)
#### Post date: [October 10, 2023, 6:07am UTC](https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768/3 "2023-10-10T06:07:04Z")

</div>

@jcusick - Below two approaches:

# Using a logger

```julia
function logger(status)
    display(status.population)
    print(status.population[1])
end
optimize(f, bounds, DE; logger)

```

See this [Printing progress to terminal when running optimization? · Issue #78 · jmejia8/Metaheuristics.jl · GitHub](https://github.com/jmejia8/Metaheuristics.jl/issues/78#issuecomment-1504465010)

# Using `optimize!`

Example at:  
[API References · Metaheuristics.jl](https://jmejia8.github.io/Metaheuristics.jl/stable/api/#Metaheuristics.optimize)!

---

<div class="post-metadata">

### Author: ![jcusick](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@jcusick](https://discourse.julialang.org/u/jcusick)
#### Post date: [October 10, 2023, 12:45pm UTC](https://discourse.julialang.org/t/interim-values-of-metaheuristics-optimiziers/104768/4 "2023-10-10T12:45:31Z")

</div>

Ahh those approaches look great – thanks both @jmejia8 and @odow!
