# Problem in Optim.jl using Fminbox and f\_calls\_limit

**URL:** <https://discourse.julialang.org/t/problem-in-optim-jl-using-fminbox-and-f-calls-limit/116820>\
**Category:** Optimization (Mathematical)\
**Created:** [July 9, 2024, 12:25pm UTC](https://discourse.julialang.org/t/problem-in-optim-jl-using-fminbox-and-f-calls-limit/116820 "2024-07-09T12:25:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [July 9, 2024, 12:25pm UTC](https://discourse.julialang.org/t/problem-in-optim-jl-using-fminbox-and-f-calls-limit/116820/1 "2024-07-09T12:25:21Z")

</div>

I have found a problem in Optim.jl using Fminbox and f\_calls\_limit, it is not well respected the limit.

For instead, using the example for the documentation:

```julia
julia> using Optim
julia> f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2;
julia> x0 = [0.0, 0.0];
julia> optimize(f, x0, LBFGS())
 * Status: success

 * Candidate solution
    Final objective value: 5.378405e-17

 * Found with
    Algorithm: L-BFGS

 * Convergence measures
    |x - x'| = 4.54e-11 ≰ 0.0e+00
    |x - x'|/|x'| = 4.54e-11 ≰ 0.0e+00
    |f(x) - f(x')| = 2.85e-19 ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 5.29e-03 ≰ 0.0e+00
    |g(x)| = 1.21e-13 ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 24
    f(x) calls: 67
    ∇f(x) calls: 67

```

If a want to restrict the f(x) calls I can use f\_calls\_limit in Options:

```julia
julia> optimize(f, x0, LBFGS(), Optim.Options(f_calls_limit=10))
 * Status: failure

 * Candidate solution
    Final objective value: 8.415971e-01

 * Found with
    Algorithm: L-BFGS

 * Convergence measures
    |x - x'| = 8.55e-02 ≰ 0.0e+00
    |x - x'|/|x'| = 1.00e+00 ≰ 0.0e+00
    |f(x) - f(x')| = 1.58e-01 ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 1.88e-01 ≰ 0.0e+00
    |g(x)| = 1.58e+00 ≰ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 1
    f(x) calls: 10
    ∇f(x) calls: 10

```

However, if a want to restrict the area this f\_calls\_limit is less respected:

```julia
julia> optimize(f, [-5., -5], [5., 5.], x0, Fminbox(LBFGS()), Optim.Options(f_calls_limit=10))
 * Status: success

 * Candidate solution
    Final objective value: 5.377764e-17

 * Found with
    Algorithm: Fminbox with L-BFGS

 * Convergence measures
    |x - x'| = 5.23e-07 ≰ 0.0e+00
    |x - x'|/|x'| = 3.70e-07 ≰ 0.0e+00
    |f(x) - f(x')| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 0.00e+00 ≤ 0.0e+00
    |g(x)| = 3.48e-10 ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 8
    f(x) calls: 81
    ∇f(x) calls: 81

```

However, if I change f\_calls\_limit the calls is updated, I am not sure if it depends on seed random, but if I does not change f\_calls\_limit is maintained fixed.

```julia
julia> optimize(f, [-5., -5], [5., 5.], x0, Fminbox(LBFGS()), Optim.Options(f_calls_limit=40))
 * Status: success

 * Candidate solution
    Final objective value: 5.850631e-17

 * Found with
    Algorithm: Fminbox with L-BFGS

 * Convergence measures
    |x - x'| = 6.99e-07 ≰ 0.0e+00
    |x - x'|/|x'| = 4.94e-07 ≰ 0.0e+00
    |f(x) - f(x')| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 0.00e+00 ≤ 0.0e+00
    |g(x)| = 2.64e-10 ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 3
    f(x) calls: 57
    ∇f(x) calls: 57

```

I am not sure if there is a better way, but I cannot find it.

Thanks in advance.

---

<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:** [July 9, 2024, 12:51pm UTC](https://discourse.julialang.org/t/problem-in-optim-jl-using-fminbox-and-f-calls-limit/116820/2 "2024-07-09T12:51:40Z")

</div>

With bounds on the parameters you have an outer loop as well with the solver’s iterations [Configurable Options · Optim](https://julianlsolvers.github.io/Optim.jl/dev/user/config/) lists some options you can use adjust the two levels of kwargs for adjusting the behaviour

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [July 9, 2024, 6:42pm UTC](https://discourse.julialang.org/t/problem-in-optim-jl-using-fminbox-and-f-calls-limit/116820/3 "2024-07-09T18:42:43Z")

</div>

Thanks a lot. You have reason, using outer\_iterations to 1, it is working:

```julia
julia> optimize(f, [-5., -5], [5., 5.], x0, Fminbox(LBFGS()), Optim.Options(outer_iterations=1, f_calls_limit=10))

 * Status: failure

 * Candidate solution
    Final objective value: 3.036937e-01

 * Found with
    Algorithm: Fminbox with L-BFGS

 * Convergence measures
    |x - x'| = 4.96e-01 ≰ 0.0e+00
    |x - x'|/|x'| = 1.00e+00 ≰ 0.0e+00
    |f(x) - f(x')| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 0.00e+00 ≤ 0.0e+00
    |g(x)| = 1.62e+00 ≰ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 1
    f(x) calls: 11
    ∇f(x) calls: 11

```
