# Callback in Optimization.jl

**URL:** <https://discourse.julialang.org/t/callback-in-optimization-jl/98551>\
**Category:** Modelling & Simulations\
**Tags:** optimization\
**Created:** [May 9, 2023, 1:32pm UTC](https://discourse.julialang.org/t/callback-in-optimization-jl/98551 "2023-05-09T13:32:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [May 9, 2023, 1:32pm UTC](https://discourse.julialang.org/t/callback-in-optimization-jl/98551/1 "2023-05-09T13:32:28Z")

</div>

Hi all, I am trying to use Optimization.jl package together with GCMAES.jl. The `solve` function of Optimization.jl should accept a callback function, but I am not able to make it work. Here is a MWE

```julia
using Optimization, OptimizationGCMAES
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
p = [1.0, 100.0]
f = OptimizationFunction(rosenbrock)
prob = Optimization.OptimizationProblem(f, x0, p, lb = [-1.0, -1.0], ub = [1.0, 1.0])
function cb(p, l) #callback function to observe training
    println("Running callback")
    display(p)
    display(l)
    return false
end
sol = solve(prob, GCMAESOpt(), callback=cb)

```

It seems like the `solve` does not react to the callback at all, not even when the callback function returns `true`. Is there something fundamental I am missing about the use of callbacks?  
I am doing this in order to track the changes in the optimised parameter of a more complicated function.

---

<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:** [May 15, 2023, 11:49pm UTC](https://discourse.julialang.org/t/callback-in-optimization-jl/98551/2 "2023-05-15T23:49:59Z")

</div>

Some of the solvers don’t support callbacks.

cc @Vaibhavdixit02 should it error if unsupported?

---

<div class="post-metadata">

**Author:** ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)\
**Post date:** [May 16, 2023, 3:40am UTC](https://discourse.julialang.org/t/callback-in-optimization-jl/98551/3 "2023-05-16T03:40:42Z")

</div>

Yeah most optimizer wrappers do, but it looks like GCMAES didn’t get the trait set to false and hence doesn’t throw an error.

@Avec the issue is that the GCMAES package doesn’t have support for callbacks hence it can’t use it from Optimization also.
