# Save the optimization results while the optimization is running (Optim.jl)

**URL:** <https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116>\
**Category:** Optimization (Mathematical)\
**Tags:** optim\
**Created:** [September 6, 2024, 11:23am UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116 "2024-09-06T11:23:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mesonepigreco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mesonepigreco/32/46262_2.png) [@mesonepigreco](https://discourse.julialang.org/u/mesonepigreco)\
**Post date:** [September 6, 2024, 11:23am UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/1 "2024-09-06T11:23:18Z")

</div>

Hi, I am running a minimization using Optim. Since it is very slow, I would like to save the results while running so that if I need to switch off the computer and brutally interrupt the minimization, I still have something.  
However, the docs do not clearly explain how this can be achieved. I thought of using the callback function, but it seems that the callback does not know what the current minimizer is.

Some old discussions referred to the “x” element of the dictionary metadata, an attribute of the OptimizationState passed to the callback function. However, for LBFGS, this variable does not seem to exist (metadata contains only the “time”).

```julia
res = optimize(f, g!, start, LBFGS(), 
     Optim.Options(show_trace = true, show_every = 1, 
           callback = x -> begin
                println("Current parameters: ", x.metadata)
                if "x" in keys(x.metadata)
                     save_model(x.metadata["x"], "step.jld2")
                end
                false
           end)
     )

```

Is there some way to force optim to save the result?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 6, 2024, 12:21pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/2 "2024-09-06T12:21:38Z")

</div>

I usually pass an additional argument to the function to be optimized which contains information that I want to keep track of during the optimization.  
For example, I like to keep track of the optimization history for later inspection. So I initialize an object to hold that history at the start of the optimization and then update it each iteration. That way I know what the current best point is and can save it from time to time.

---

<div class="post-metadata">

**Author:** ![mesonepigreco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mesonepigreco/32/46262_2.png) [@mesonepigreco](https://discourse.julialang.org/u/mesonepigreco)\
**Post date:** [September 8, 2024, 10:16pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/3 "2024-09-08T22:16:19Z")

</div>

Yes, in the end, I did something similar with a cache mechanism, even if I didn’t find it an elegant solution.  
I find it strange there is no easier way since I would expect that saving the best model while optimizing would be something many people want to do.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 9, 2024, 12:07pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/4 "2024-09-09T12:07:28Z")

</div>

It used to be (at least with NLopt.jl) automatic. When you interrupted the optimization (Ctrl-C or objective function error), the optimizer would return the best point found up to that time. That behavior changed recently (though I rather liked it).

---

<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:** [September 9, 2024, 7:25pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/5 "2024-09-09T19:25:52Z")

</div>

> [@hendri54](#):
>
> That behavior changed recently (though I rather liked it).

What changed? I made a bunch of PRs to NLopt.jl recently. Any changes were unintentional. Can you open an issue describing?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 10, 2024, 12:29pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/6 "2024-09-10T12:29:32Z")

</div>

In the recent past (I am not sure which version), when the objective function threw an exception (including pressing Ctrl-C) NLopt caught the exception and returned the best point found so far to the caller.  
The current version (1.0.3 in my case) does not catch the exception, so execution halts.

---

<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:** [September 10, 2024, 8:06pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/7 "2024-09-10T20:06:08Z")

</div>

I’ll fix this.

Edit: see [Catch InterruptException and handle as ForcedStop by odow · Pull Request #245 · jump-dev/NLopt.jl · GitHub](https://github.com/jump-dev/NLopt.jl/pull/245). I’ll have a new version out ASAP.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 11, 2024, 4:25pm UTC](https://discourse.julialang.org/t/save-the-optimization-results-while-the-optimization-is-running-optim-jl/119116/8 "2024-09-11T16:25:51Z")

</div>

Thank you - I thought it might have been an intentional change.
