Skip to content
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

optimize: complete _trlib and _trustregion* (private) #236

Merged
merged 3 commits into from
Dec 1, 2024
Merged
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
26 changes: 24 additions & 2 deletions scipy-stubs/optimize/_trlib/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
from scipy._typing import Untyped
from collections.abc import Callable
from typing import Protocol, type_check_only

import numpy as np
import optype.numpy as onp
from ._trlib import TRLIBQuadraticSubproblem

__all__ = ["TRLIBQuadraticSubproblem", "get_trlib_quadratic_subproblem"]

def get_trlib_quadratic_subproblem(tol_rel_i: float = -2.0, tol_rel_b: float = -3.0, disp: bool = False) -> Untyped: ...
@type_check_only
class _SubproblemFactory(Protocol):
def __call__(
self,
/,
x: onp.ToFloat1D,
fun: Callable[[onp.Array1D[np.float64]], onp.ToFloat],
jac: Callable[[onp.Array1D[np.float64]], onp.ToFloat1D],
hess: Callable[[onp.Array1D[np.float64]], onp.ToFloat2D] | None = None,
hessp: Callable[[onp.Array1D[np.float64], onp.Array1D[np.float64]], onp.ToFloat1D] | None = None,
) -> TRLIBQuadraticSubproblem: ...

###

def get_trlib_quadratic_subproblem(
tol_rel_i: onp.ToFloat = -2.0,
tol_rel_b: onp.ToFloat = -3.0,
disp: onp.ToBool = False,
) -> _SubproblemFactory: ...
31 changes: 16 additions & 15 deletions scipy-stubs/optimize/_trlib/_trlib.pyi
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
from collections.abc import Mapping
# https://github.com/scipy/scipy/blob/v1.14.1/scipy/optimize/_trlib/_trlib.pyx

from collections.abc import Callable, Mapping
from typing import Final
from typing_extensions import Never, override
from typing_extensions import Never

import numpy as np
import optype.numpy as onp
from scipy._typing import Untyped, UntypedCallable
from scipy.optimize._trustregion import BaseQuadraticSubproblem

__test__: Final[Mapping[Never, Never]]
###

__test__: Final[Mapping[Never, Never]] # undocumented

class TRLIBQuadraticSubproblem(BaseQuadraticSubproblem):
class TRLIBQuadraticSubproblem(BaseQuadraticSubproblem): # undocumented
def __init__(
self,
/,
x: Untyped,
fun: UntypedCallable,
jac: UntypedCallable,
hess: UntypedCallable | None,
hessp: UntypedCallable | None,
tol_rel_i: float = -2.0,
tol_rel_b: float = -3.0,
disp: bool = False,
x: onp.ToFloat1D,
fun: Callable[[onp.Array1D[np.float64]], onp.ToFloat],
jac: Callable[[onp.Array1D[np.float64]], onp.ToFloat1D],
hess: Callable[[onp.Array1D[np.float64]], onp.ToFloat2D] | None,
hessp: Callable[[onp.Array1D[np.float64], onp.Array1D[np.float64]], onp.ToFloat1D] | None,
tol_rel_i: onp.ToFloat = -2.0,
tol_rel_b: onp.ToFloat = -3.0,
disp: onp.ToBool = False,
) -> None: ...
@override
def solve(self, /, trust_radius: float | np.float64) -> tuple[onp.ArrayND[np.float64], bool]: ...
43 changes: 27 additions & 16 deletions scipy-stubs/optimize/_trustregion.pyi
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import abc
from collections.abc import Callable

import numpy as np
from scipy._typing import Untyped
import optype.numpy as onp

__all__: list[str] = []

class BaseQuadraticSubproblem:
def __init__(
self,
/,
x: Untyped,
fun: Untyped,
jac: Untyped,
hess: Untyped | None = None,
hessp: Untyped | None = None,
x: onp.ToFloat1D,
fun: Callable[[onp.Array1D[np.float64]], onp.ToFloat],
jac: Callable[[onp.Array1D[np.float64]], onp.ToFloat1D],
hess: Callable[[onp.Array1D[np.float64]], onp.ToFloat2D] | None = None,
hessp: Callable[[onp.Array1D[np.float64], onp.Array1D[np.float64]], onp.ToFloat1D] | None = None,
) -> None: ...
def __call__(self, /, p: Untyped) -> Untyped: ...
def __call__(self, /, p: onp.ToFloat1D) -> float | np.float64: ...

#
@property
def fun(self, /) -> Untyped: ...
def fun(self, /) -> float | np.float64: ...
@property
def jac(self, /) -> Untyped: ...
def jac_mag(self, /) -> float | np.float64: ...
@property
def hess(self, /) -> Untyped: ...
def hessp(self, /, p: Untyped) -> Untyped: ...
def jac(self, /) -> onp.Array1D[np.float64]: ...
@property
def jac_mag(self, /) -> Untyped: ...
def get_boundaries_intersections(self, /, z: Untyped, d: Untyped, trust_radius: Untyped) -> Untyped: ...
@abc.abstractmethod
def solve(self, /, trust_radius: float | np.float64) -> Untyped: ...
def hess(self, /) -> onp.Array2D[np.float64]: ...

#
def hessp(self, /, p: onp.ToFloat1D) -> onp.Array1D[np.float64]: ...
def get_boundaries_intersections(
self,
/,
z: onp.ToArray1D,
d: onp.ToArray1D,
trust_radius: onp.ToFloat,
) -> list[float | np.float64]: ... # list of size 2

#
def solve(self, /, trust_radius: onp.ToFloat) -> tuple[onp.Array1D[np.float64], bool]: ...
10 changes: 3 additions & 7 deletions scipy-stubs/optimize/_trustregion_dogleg.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from typing_extensions import override

import numpy as np
from scipy._typing import Untyped
import optype.numpy as onp
from ._trustregion import BaseQuadraticSubproblem

__all__: list[str] = []

class DoglegSubproblem(BaseQuadraticSubproblem):
def cauchy_point(self, /) -> Untyped: ...
def newton_point(self, /) -> Untyped: ...
@override
def solve(self, /, trust_radius: float | np.float64) -> Untyped: ...
def cauchy_point(self, /) -> onp.Array1D[np.float64]: ...
def newton_point(self, /) -> onp.Array1D[np.float64]: ...
76 changes: 46 additions & 30 deletions scipy-stubs/optimize/_trustregion_exact.pyi
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
from typing_extensions import override
from collections.abc import Callable
from typing import ClassVar, Concatenate, Final, TypedDict
from typing_extensions import Unpack, override

import numpy as np
import optype.numpy as onp
from scipy._typing import Untyped, UntypedCallable, UntypedTuple
from ._minimize import OptimizeResult
from ._trustregion import BaseQuadraticSubproblem

__all__ = ["IterativeSubproblem", "_minimize_trustregion_exact", "estimate_smallest_singular_value", "singular_leading_submatrix"]

class _TrustRegionOptions(TypedDict, total=False):
initial_trust_radius: onp.ToFloat
max_trust_radius: onp.ToFloat
eta: onp.ToFloat
gtol: onp.ToFloat

###

def _minimize_trustregion_exact(
fun: UntypedCallable,
x0: Untyped,
args: UntypedTuple = (),
jac: Untyped | None = None,
hess: Untyped | None = None,
**trust_region_options: Untyped,
) -> Untyped: ...
def estimate_smallest_singular_value(U: Untyped) -> Untyped: ...
def gershgorin_bounds(H: Untyped) -> Untyped: ...
def singular_leading_submatrix(A: Untyped, U: Untyped, k: Untyped) -> Untyped: ...
fun: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat],
x0: onp.ToFloat1D,
args: tuple[object, ...] = (),
jac: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat1D] | None = None,
hess: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat2D] | None = None,
**trust_region_options: Unpack[_TrustRegionOptions],
) -> OptimizeResult: ...
def estimate_smallest_singular_value(U: onp.ToFloat2D) -> tuple[float | np.float64, onp.Array1D[np.float64]]: ...
def gershgorin_bounds(H: onp.ToFloat2D) -> tuple[float | np.float64, float | np.float64]: ...
def singular_leading_submatrix(
A: onp.ToFloat2D,
U: onp.ToFloat2D,
k: onp.ToJustInt,
) -> tuple[float | np.float64, onp.Array1D[np.float64]]: ...

class IterativeSubproblem(BaseQuadraticSubproblem):
UPDATE_COEFF: float
EPS: Untyped
UPDATE_COEFF: ClassVar[float] = 0.01
EPS: ClassVar[float | np.float64] = ...

CLOSE_TO_ZERO: Final[float | np.float64]
dimension: Final[int]
previous_tr_radius: int
lambda_lb: Untyped
niter: int
k_easy: Untyped
k_hard: Untyped
dimension: Untyped
hess_inf: Untyped
hess_fro: Untyped
CLOSE_TO_ZERO: Untyped
lambda_current: Untyped
k_easy: float | np.float64
k_hard: float | np.float64
hess_inf: float | np.float64
hess_fro: float | np.float64
lambda_lb: float | np.float64 | None # intially `None`
lambda_current: float | np.float64 # set in `solve()`

def __init__(
self,
/,
x: Untyped,
fun: UntypedCallable,
jac: Untyped,
hess: Untyped,
hessp: Untyped | None = None,
k_easy: float = 0.1,
k_hard: float = 0.2,
x: onp.ToFloat1D,
fun: Callable[[onp.Array1D[np.float64]], onp.ToFloat],
jac: Callable[[onp.Array1D[np.float64]], onp.ToFloat1D],
hess: Callable[[onp.Array1D[np.float64]], onp.ToFloat2D],
hessp: Callable[[onp.Array1D[np.float64], onp.Array1D[np.float64]], onp.ToFloat1D] | None = None,
k_easy: onp.ToFloat = 0.1,
k_hard: onp.ToFloat = 0.2,
) -> None: ...
@override
def solve(self, /, trust_radius: float | np.float64) -> tuple[onp.ArrayND[np.float64], bool]: ...
def solve(self, /, tr_radius: onp.ToFloat) -> tuple[onp.ArrayND[np.float64], bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
37 changes: 27 additions & 10 deletions scipy-stubs/optimize/_trustregion_krylov.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
from collections.abc import Callable
from typing import Concatenate, TypedDict, type_check_only
from typing_extensions import Unpack

import numpy as np
import optype.numpy as onp
from ._minimize import OptimizeResult

__all__ = ["_minimize_trust_krylov"]

from scipy._typing import Untyped, UntypedCallable, UntypedTuple
@type_check_only
class _TrustRegionOptions(TypedDict, total=False):
initial_trust_radius: onp.ToFloat
max_trust_radius: onp.ToFloat
eta: onp.ToFloat
gtol: onp.ToFloat
maxiter: onp.ToJustInt
disp: onp.ToBool

###

def _minimize_trust_krylov(
fun: UntypedCallable,
x0: Untyped,
args: UntypedTuple = (),
jac: Untyped | None = None,
hess: Untyped | None = None,
hessp: Untyped | None = None,
inexact: bool = True,
**trust_region_options: Untyped,
) -> Untyped: ...
fun: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat],
x0: onp.ToFloat1D,
args: tuple[object, ...] = (),
jac: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat1D] | None = None,
hess: Callable[Concatenate[onp.Array1D[np.float64], ...], onp.ToFloat2D] | None = None,
hessp: Callable[Concatenate[onp.Array1D[np.float64], onp.Array1D[np.float64], ...], onp.ToFloat1D] | None = None,
inexact: onp.ToBool = True,
**trust_region_options: Unpack[_TrustRegionOptions],
) -> OptimizeResult: ...
8 changes: 1 addition & 7 deletions scipy-stubs/optimize/_trustregion_ncg.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from typing_extensions import override

import numpy as np
from scipy._typing import Untyped
from ._trustregion import BaseQuadraticSubproblem

__all__: list[str] = []

class CGSteihaugSubproblem(BaseQuadraticSubproblem):
@override
def solve(self, /, trust_radius: float | np.float64) -> Untyped: ...
class CGSteihaugSubproblem(BaseQuadraticSubproblem): ...