# Callback function for Fminbox

**URL:** <https://discourse.julialang.org/t/callback-function-for-fminbox/6743>\
**Category:** Optimization (Mathematical)\
**Created:** [October 28, 2017, 3:47pm UTC](https://discourse.julialang.org/t/callback-function-for-fminbox/6743 "2017-10-28T15:47:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![depasquale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/depasquale/32/1813_2.png) [@depasquale](https://discourse.julialang.org/u/depasquale)\
**Post date:** [October 28, 2017, 3:47pm UTC](https://discourse.julialang.org/t/callback-function-for-fminbox/6743/1 "2017-10-28T15:47:20Z")

</div>

I am trying to perform a constrained optimization using fminbox in the optim.jl package and would like to execute a callback function within the inner loop AND the outer loop to terminate the optimization if too much time has passed. The callback appears to work properly in the inner loop, but not the outer loop (and thus the optimization will never terminate the way I’d like). Any suggestions about what’s going wrong, or a workaround?

```julia
# optimize
od = OnceDifferentiable(x->func(x, data), x; autodiff = :forward)
@time results = optimize(od, x, lower, upper, Fminbox{BFGS}(); iterations = 10, callback = my_callback, optimizer_o = Optim.Options(iterations = 10, callback = my_callback))

```
