How to visualize optimizer histories#

optimagic’s criterion_plot can visualize the history of function values for one or multiple optimizations. optimagic’s params_plot can visualize the history of parameter values for one optimization.

This can help you to understand whether your optimization actually converged and if not, which parameters are problematic.

It can also help you to find the fastest optimizer for a given optimization problem.

import numpy as np
import optimagic as om

Run two optimization to get example results#

def sphere(x):
    return x @ x


results = {}
for algo in ["scipy_lbfgsb", "scipy_neldermead"]:
    results[algo] = om.minimize(sphere, params=np.arange(5), algorithm=algo)

Make a single criterion plot#

fig = om.criterion_plot(results["scipy_neldermead"])
fig.show(renderer="png")
../_images/8a869468210e11e87f2b070436eee95af65c31cb87493ea4464dc2d50487d920.png

Compare two optimizations in a criterion plot#

fig = om.criterion_plot(results)
fig.show(renderer="png")
../_images/09094f09a7c096eb18e86d1ca1ae6159f5caf776154d65f62edbe3689f254418.png

Use some advanced options of criterion_plot#

fig = om.criterion_plot(
    results,
    # cut off after 180 evaluations
    max_evaluations=180,
    # show only the current best function value
    monotone=True,
)
fig.show(renderer="png")
../_images/6c2ab2a3baa5a340fdd3774924ecdc399f03525501b23e539744dc7f31291262.png

Make a params plot#

fig = om.params_plot(results["scipy_neldermead"])
fig.show(renderer="png")
../_images/174efaea8432292156e75334041f00bea38717659a357435f17c8f793e2d195c.png

Use advanced options of params plot#

fig = om.params_plot(
    results["scipy_neldermead"],
    # cut off after 180 evaluations
    max_evaluations=180,
    # select only the last three parameters
    selector=lambda x: x[2:],
)
fig.show(renderer="png")
../_images/f92d7799acd6808a2ac8ae1a0fa773310e1663cf1f305de165625645cba835ed.png

criterion_plot with multistart optimization#

def alpine(x):
    return np.sum(np.abs(x * np.sin(x) + 0.1 * x))


res = om.minimize(
    alpine,
    params=np.arange(7),
    bounds=om.Bounds(soft_lower=np.full(7, -3), soft_upper=np.full(7, 10)),
    algorithm="scipy_neldermead",
    multistart=om.MultistartOptions(n_samples=100, convergence_max_discoveries=3),
)
fig = om.criterion_plot(res, max_evaluations=1000, monotone=True)
fig.show(renderer="png")
../_images/7f098bdeef094c7c45979b2d866814040c8c65f5a97feb01a47363bf28d24f21.png