1
- from collections .abc import Callable , Iterable , Mapping , Sequence
2
- from typing import Literal , TypeAlias , TypeVar
1
+ from collections .abc import Callable , Iterable , Sequence
2
+ from typing import Concatenate , Literal , TypeAlias , TypedDict , TypeVar , type_check_only
3
3
4
4
import numpy as np
5
5
import optype .numpy as onp
6
- from scipy . _typing import Untyped , UntypedCallable
6
+ from . _constraints import Bounds
7
7
from ._optimize import OptimizeResult as _OptimizeResult
8
- from ._typing import Constraints
8
+ from ._typing import Constraints , MinimizerKwargs
9
9
10
10
__all__ = ["shgo" ]
11
11
12
+ _VT = TypeVar ("_VT" )
13
+ _RT = TypeVar ("_RT" )
14
+
12
15
_Float : TypeAlias = float | np .float64
13
16
_Float1D : TypeAlias = onp .Array1D [np .float64 ]
17
+ _Fun1D : TypeAlias = Callable [Concatenate [_Float1D , ...], _RT ]
14
18
15
- _MinimizerKwargs : TypeAlias = Mapping [str , object ] # TODO(jorenham): TypedDict
16
- _Options : TypeAlias = Mapping [str , object ] # TODO(jorenham): TypedDict
17
-
18
- _SamplingMethodName : TypeAlias = Literal ["simplicial" , "halton" , "sobol" ]
19
- _SamplingMethodFunc : TypeAlias = Callable [[int , int ], onp .ArrayND [np .float64 ]]
20
- _SamplingMethod : TypeAlias = _SamplingMethodName | _SamplingMethodFunc
21
-
22
- _VT = TypeVar ("_VT" )
23
- _RT = TypeVar ("_RT" )
19
+ @type_check_only
20
+ class _SHGOOptions (TypedDict , total = False ):
21
+ f_min : _Float
22
+ f_tol : _Float
23
+ maxiter : int
24
+ maxfev : int
25
+ maxev : int
26
+ mmaxtime : _Float
27
+ minhgrd : int
28
+ symmetry : Sequence [int ] | onp .ToBool
29
+ jac : _Fun1D [onp .ToFloat1D ] | onp .ToBool # gradient
30
+ hess : _Fun1D [onp .ToFloat2D ]
31
+ hessp : Callable [Concatenate [_Float1D , _Float1D , ...], onp .ToFloat1D ]
32
+ minimize_every_iter : onp .ToBool
33
+ local_iter : int
34
+ infty_constraints : onp .ToBool
35
+ disp : onp .ToBool
24
36
25
37
###
26
38
27
39
class OptimizeResult (_OptimizeResult ):
28
40
x : _Float1D
29
- xl : list [_Float1D ]
41
+ xl : Sequence [_Float1D ]
30
42
fun : _Float
31
- funl : list [_Float ]
43
+ funl : Sequence [_Float ]
32
44
success : bool
33
45
message : str
34
46
nfev : int
@@ -38,16 +50,16 @@ class OptimizeResult(_OptimizeResult):
38
50
nit : int
39
51
40
52
def shgo (
41
- func : UntypedCallable ,
42
- bounds : Untyped ,
53
+ func : _Fun1D [ onp . ToFloat ] ,
54
+ bounds : tuple [ onp . ToFloat | onp . ToFloat1D , onp . ToFloat | onp . ToFloat1D ] | Bounds ,
43
55
args : tuple [object , ...] = (),
44
56
constraints : Constraints | None = None ,
45
- n : int = 100 ,
46
- iters : int = 1 ,
57
+ n : onp . ToJustInt = 100 ,
58
+ iters : onp . ToJustInt = 1 ,
47
59
callback : Callable [[_Float1D ], None ] | None = None ,
48
- minimizer_kwargs : _MinimizerKwargs | None = None , # TODO(jorenham): TypedDict
49
- options : _Options | None = None , # TODO(jorenham): TypedDict
50
- sampling_method : _SamplingMethod = "simplicial" ,
60
+ minimizer_kwargs : MinimizerKwargs | None = None ,
61
+ options : _SHGOOptions | None = None ,
62
+ sampling_method : Callable [[ int , int ], onp . ToFloat2D ] | Literal [ "simplicial" , "halton" , "sobol" ] = "simplicial" ,
51
63
* ,
52
- workers : int | Callable [[Callable [[_VT ], _RT ], Iterable [_VT ]], Sequence [_RT ]] = 1 ,
64
+ workers : onp . ToJustInt | Callable [[Callable [[_VT ], _RT ], Iterable [_VT ]], Sequence [_RT ]] = 1 ,
53
65
) -> OptimizeResult : ...
0 commit comments