Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
"\n",
"\n",
"params = {\"a\": 1, \"b\": 2}\n",
"res = om.minimize(my_fun, params, method=\"L-BFGS-B\")\n",
"res.x"
"results = {\n",
" \"L-BFGS-B\": om.minimize(my_fun, params, method=\"L-BFGS-B\"),\n",
" \"Nelder-Mead\": om.minimize(my_fun, params, method=\"Nelder-Mead\"),\n",
"}\n",
"om.history_plot(results)"
]
}
],
Expand Down
3 changes: 2 additions & 1 deletion src/optimini/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from optimini.history import history_plot
from optimini.minimize import minimize

__all__ = ["minimize"]
__all__ = ["minimize", "history_plot"]
31 changes: 31 additions & 0 deletions src/optimini/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pandas as pd
import seaborn as sns


class History:
"""History of parameters and function values."""

def __init__(self):
self.value = []
self.params = []

def add(self, value, params):
self.value.append(value)
self.params.append(params)


def history_plot(results, max_n_evals=50, monotone=True):
"""Plot the criterion values of multiple optimizations."""
data = pd.DataFrame()
for name, res in results.items():
values = res.history.value
df = pd.DataFrame({"value": values, "n_evals": range(len(values))})
df["name"] = name
if monotone:
df["value"] = df["value"].cummin()
data = pd.concat([data, df])

if max_n_evals is not None:
data = data.query(f"n_evals <= {max_n_evals}")

return sns.lineplot(data=data, x="n_evals", y="value", hue="name")
7 changes: 5 additions & 2 deletions src/optimini/internal_problem.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class InternalProblem:
"""Wraps a user provided function to add functionality"""

def __init__(self, fun, converter):
def __init__(self, fun, converter, history):
self._user_fun = fun
self._converter = converter
self._history = history

def fun(self, x):
params = self._converter.unflatten(x)
return self._user_fun(params)
value = self._user_fun(params)
self._history.add(value, params)
return value
14 changes: 9 additions & 5 deletions src/optimini/minimize.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from copy import deepcopy

from scipy.optimize import minimize as scipy_minimize

from optimini.converter import Converter
from optimini.history import History
from optimini.internal_problem import InternalProblem
from optimini.utils import OptimizeResult


def minimize(fun, params, method, options=None):
"""Minimize a function using a given method"""
options = {} if options is None else options
converter = Converter(params)
problem = InternalProblem(fun, converter)
history = History()
problem = InternalProblem(fun, converter, history)
x0 = converter.flatten(params)
raw_res = scipy_minimize(
fun=problem.fun,
x0=x0,
method=method,
options=options,
)
res = deepcopy(raw_res)
res.x = converter.unflatten(res.x)
res = OptimizeResult(
x=converter.unflatten(raw_res.x),
history=history,
fun=raw_res.fun,
)
return res
15 changes: 15 additions & 0 deletions src/optimini/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from dataclasses import dataclass

import numpy as np
from numpy.typing import NDArray

from optimini.history import History


@dataclass
class OptimizeResult:
"""An oversimplified optimization result."""

x: dict | NDArray[np.float64]
history: History
fun: float
8 changes: 8 additions & 0 deletions tests/test_optimini.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from optimini.history import History, history_plot
from optimini.minimize import minimize


Expand All @@ -24,3 +25,10 @@ def test_simple_minimize_with_array_params():
res = minimize(array_fun, params, method="L-BFGS-B")
assert isinstance(res.x, np.ndarray)
assert np.allclose(res.x, np.array([0, 0]))


def test_history_collection():
params = {"a": 1, "b": 2}
res = minimize(dict_fun, params, method="L-BFGS-B")
assert isinstance(res.history, History)
history_plot({"test": res})