Skip to content

Step 3: Internal algorithm interface. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2_history
Choose a base branch
from
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
78 changes: 78 additions & 0 deletions src/optimini/algorithms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from dataclasses import dataclass

import numpy as np
from numpy.typing import NDArray
from scipy.optimize import minimize as scipy_minimize

from optimini.internal_problem import InternalProblem
from optimini.utils import Algorithm, InternalResult


@dataclass(frozen=True)
class SciPyLBFGSB(Algorithm):
convergence_ftol: float = 1e-8
stopping_maxiter: int = 10_000
limited_memory_length: int = 12
# more options here ...

def _solve_internal_problem(
self, problem: InternalProblem, x0: NDArray[np.float64]
) -> InternalResult:
options = {
"maxcor": self.limited_memory_length,
"ftol": self.convergence_ftol,
"maxiter": self.stopping_maxiter,
}
res = scipy_minimize(
fun=problem.fun,
x0=x0,
method="L-BFGS-B",
options=options,
)
return InternalResult(x=res.x, fun=res.fun)


@dataclass(frozen=True)
class SciPyCG(Algorithm):
convergence_gtol: float = 1e-8
stopping_maxiter: int = 10_000
# more options here ...

def _solve_internal_problem(
self, problem: InternalProblem, x0: NDArray[np.float64]
) -> InternalResult:
options = {
"gtol": self.convergence_gtol,
"maxiter": self.stopping_maxiter,
}
res = scipy_minimize(fun=problem.fun, x0=x0, method="CG", options=options)
return InternalResult(x=res.x, fun=res.fun)


@dataclass(frozen=True)
class SciPyNelderMead(Algorithm):
stopping_maxiter: int = 10_000
convergence_ftol: float = 1e-8
adaptive: bool = True
# more options here ...

def _solve_internal_problem(
self, problem: InternalProblem, x0: NDArray[np.float64]
) -> InternalResult:
options = {
"maxiter": self.stopping_maxiter,
"fatol": self.convergence_ftol,
"adaptive": self.adaptive,
}

res = scipy_minimize(
fun=problem.fun, x0=x0, method="Nelder-Mead", options=options
)
return InternalResult(x=res.x, fun=res.fun)


OPTIMIZER_REGISTRY = {
"L-BFGS-B": SciPyLBFGSB,
"CG": SciPyCG,
"Nelder-Mead": SciPyNelderMead,
}
11 changes: 3 additions & 8 deletions src/optimini/minimize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from scipy.optimize import minimize as scipy_minimize

from optimini.algorithms import OPTIMIZER_REGISTRY
from optimini.converter import Converter
from optimini.history import History
from optimini.internal_problem import InternalProblem
Expand All @@ -13,12 +12,8 @@ def minimize(fun, params, method, options=None):
history = History()
problem = InternalProblem(fun, converter, history)
x0 = converter.flatten(params)
raw_res = scipy_minimize(
fun=problem.fun,
x0=x0,
method=method,
options=options,
)
algo = OPTIMIZER_REGISTRY[method](**options)
raw_res = algo._solve_internal_problem(problem, x0)
res = OptimizeResult(
x=converter.unflatten(raw_res.x),
history=history,
Expand Down
16 changes: 16 additions & 0 deletions src/optimini/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass

import numpy as np
from numpy.typing import NDArray

from optimini.history import History
from optimini.internal_problem import InternalProblem


@dataclass
Expand All @@ -13,3 +15,17 @@ class OptimizeResult:
x: dict | NDArray[np.float64]
history: History
fun: float


@dataclass(frozen=True)
class InternalResult:
x: NDArray[np.float64]
fun: float


class Algorithm(ABC):
@abstractmethod
def _solve_internal_problem(
self, problem: InternalProblem, x0: NDArray[np.float64]
) -> InternalResult:
pass