Skip to content

Commit 69f70cd

Browse files
committed
add missing type annotations
1 parent d1b7684 commit 69f70cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+382
-285
lines changed

src/surfaces/_visualize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
color_scale = None
3333

3434

35-
def _check_viz_deps():
35+
def _check_viz_deps() -> None:
3636
"""Check if visualization dependencies are available."""
3737
if not _HAS_VIZ_DEPS:
3838
raise ImportError(
@@ -41,7 +41,7 @@ def _check_viz_deps():
4141
)
4242

4343

44-
def _create_grid(objective_function, search_space: Dict[str, np.ndarray]):
44+
def _create_grid(objective_function: Any, search_space: Dict[str, np.ndarray]) -> tuple:
4545
"""Create a 2D grid for visualization from a search space and objective function.
4646
4747
Args:
@@ -53,7 +53,7 @@ def _create_grid(objective_function, search_space: Dict[str, np.ndarray]):
5353
"""
5454
_check_viz_deps()
5555

56-
def objective_function_np(*args):
56+
def objective_function_np(*args: Any) -> float:
5757
para = {}
5858
for arg, key in zip(args, search_space.keys()):
5959
para[key] = arg

src/surfaces/test_functions/algebraic/test_functions_1d/damped_sine_function.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
33
# License: MIT License
44

5+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
56

67
import numpy as np
78

89
from .._base_algebraic_function import AlgebraicFunction
910

11+
if TYPE_CHECKING:
12+
from surfaces.noise import BaseNoise
13+
1014

1115
class DampedSineFunction(AlgebraicFunction):
1216
"""Damped Sine one-dimensional test function.
@@ -76,26 +80,32 @@ class DampedSineFunction(AlgebraicFunction):
7680

7781
def __init__(
7882
self,
79-
objective="minimize",
80-
sleep=0,
81-
memory=False,
82-
collect_data=True,
83-
callbacks=None,
84-
catch_errors=None,
85-
noise=None,
86-
):
83+
objective: str = "minimize",
84+
sleep: float = 0,
85+
memory: bool = False,
86+
collect_data: bool = True,
87+
callbacks: Optional[Union[Callable, List[Callable]]] = None,
88+
catch_errors: Optional[Dict[type, float]] = None,
89+
noise: Optional["BaseNoise"] = None,
90+
) -> None:
8791
super().__init__(objective, sleep, memory, collect_data, callbacks, catch_errors, noise)
8892
self.n_dim = 1
8993

90-
def _create_objective_function(self):
91-
def damped_sine_function(params):
94+
def _create_objective_function(self) -> None:
95+
def damped_sine_function(params: Dict[str, Any]) -> float:
9296
x = params["x0"]
9397

9498
return -(x + np.sin(x)) * np.exp(-(x**2))
9599

96100
self.pure_objective_function = damped_sine_function
97101

98-
def _search_space(self, min=-10.0, max=10.0, value_types="array", size=10000):
102+
def _search_space(
103+
self,
104+
min: float = -10.0,
105+
max: float = 10.0,
106+
value_types: str = "array",
107+
size: int = 10000,
108+
) -> Dict[str, Any]:
99109
return super()._create_n_dim_search_space(
100110
min=min, max=max, size=size, value_types=value_types
101111
)

src/surfaces/test_functions/algebraic/test_functions_1d/forrester_function.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
33
# License: MIT License
44

5+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
56

67
import numpy as np
78

89
from .._base_algebraic_function import AlgebraicFunction
910

11+
if TYPE_CHECKING:
12+
from surfaces.noise import BaseNoise
13+
1014

1115
class ForresterFunction(AlgebraicFunction):
1216
"""Forrester one-dimensional test function.
@@ -77,26 +81,32 @@ class ForresterFunction(AlgebraicFunction):
7781

7882
def __init__(
7983
self,
80-
objective="minimize",
81-
sleep=0,
82-
memory=False,
83-
collect_data=True,
84-
callbacks=None,
85-
catch_errors=None,
86-
noise=None,
87-
):
84+
objective: str = "minimize",
85+
sleep: float = 0,
86+
memory: bool = False,
87+
collect_data: bool = True,
88+
callbacks: Optional[Union[Callable, List[Callable]]] = None,
89+
catch_errors: Optional[Dict[type, float]] = None,
90+
noise: Optional["BaseNoise"] = None,
91+
) -> None:
8892
super().__init__(objective, sleep, memory, collect_data, callbacks, catch_errors, noise)
8993
self.n_dim = 1
9094

91-
def _create_objective_function(self):
92-
def forrester_function(params):
95+
def _create_objective_function(self) -> None:
96+
def forrester_function(params: Dict[str, Any]) -> float:
9397
x = params["x0"]
9498

9599
return ((6 * x - 2) ** 2) * np.sin(12 * x - 4)
96100

97101
self.pure_objective_function = forrester_function
98102

99-
def _search_space(self, min=0.0, max=1.0, value_types="array", size=10000):
103+
def _search_space(
104+
self,
105+
min: float = 0.0,
106+
max: float = 1.0,
107+
value_types: str = "array",
108+
size: int = 10000,
109+
) -> Dict[str, Any]:
100110
return super()._create_n_dim_search_space(
101111
min=min, max=max, size=size, value_types=value_types
102112
)

src/surfaces/test_functions/algebraic/test_functions_1d/quadratic_exponential_function.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
33
# License: MIT License
44

5+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
56

67
import numpy as np
78

89
from .._base_algebraic_function import AlgebraicFunction
910

11+
if TYPE_CHECKING:
12+
from surfaces.noise import BaseNoise
13+
1014

1115
class QuadraticExponentialFunction(AlgebraicFunction):
1216
"""Quadratic-Exponential one-dimensional test function.
@@ -76,26 +80,32 @@ class QuadraticExponentialFunction(AlgebraicFunction):
7680

7781
def __init__(
7882
self,
79-
objective="minimize",
80-
sleep=0,
81-
memory=False,
82-
collect_data=True,
83-
callbacks=None,
84-
catch_errors=None,
85-
noise=None,
86-
):
83+
objective: str = "minimize",
84+
sleep: float = 0,
85+
memory: bool = False,
86+
collect_data: bool = True,
87+
callbacks: Optional[Union[Callable, List[Callable]]] = None,
88+
catch_errors: Optional[Dict[type, float]] = None,
89+
noise: Optional["BaseNoise"] = None,
90+
) -> None:
8791
super().__init__(objective, sleep, memory, collect_data, callbacks, catch_errors, noise)
8892
self.n_dim = 1
8993

90-
def _create_objective_function(self):
91-
def quadratic_exponential_function(params):
94+
def _create_objective_function(self) -> None:
95+
def quadratic_exponential_function(params: Dict[str, Any]) -> float:
9296
x = params["x0"]
9397

9498
return -(16 * x**2 - 24 * x + 5) * np.exp(-x)
9599

96100
self.pure_objective_function = quadratic_exponential_function
97101

98-
def _search_space(self, min=1.9, max=3.9, value_types="array", size=10000):
102+
def _search_space(
103+
self,
104+
min: float = 1.9,
105+
max: float = 3.9,
106+
value_types: str = "array",
107+
size: int = 10000,
108+
) -> Dict[str, Any]:
99109
return super()._create_n_dim_search_space(
100110
min=min, max=max, size=size, value_types=value_types
101111
)

src/surfaces/test_functions/algebraic/test_functions_1d/sine_product_function.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
33
# License: MIT License
44

5+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
56

67
import numpy as np
78

89
from .._base_algebraic_function import AlgebraicFunction
910

11+
if TYPE_CHECKING:
12+
from surfaces.noise import BaseNoise
13+
1014

1115
class SineProductFunction(AlgebraicFunction):
1216
"""Sine Product one-dimensional test function.
@@ -76,26 +80,32 @@ class SineProductFunction(AlgebraicFunction):
7680

7781
def __init__(
7882
self,
79-
objective="minimize",
80-
sleep=0,
81-
memory=False,
82-
collect_data=True,
83-
callbacks=None,
84-
catch_errors=None,
85-
noise=None,
86-
):
83+
objective: str = "minimize",
84+
sleep: float = 0,
85+
memory: bool = False,
86+
collect_data: bool = True,
87+
callbacks: Optional[Union[Callable, List[Callable]]] = None,
88+
catch_errors: Optional[Dict[type, float]] = None,
89+
noise: Optional["BaseNoise"] = None,
90+
) -> None:
8791
super().__init__(objective, sleep, memory, collect_data, callbacks, catch_errors, noise)
8892
self.n_dim = 1
8993

90-
def _create_objective_function(self):
91-
def sine_product_function(params):
94+
def _create_objective_function(self) -> None:
95+
def sine_product_function(params: Dict[str, Any]) -> float:
9296
x = params["x0"]
9397

9498
return -x * np.sin(x)
9599

96100
self.pure_objective_function = sine_product_function
97101

98-
def _search_space(self, min=0.0, max=10.0, value_types="array", size=10000):
102+
def _search_space(
103+
self,
104+
min: float = 0.0,
105+
max: float = 10.0,
106+
value_types: str = "array",
107+
size: int = 10000,
108+
) -> Dict[str, Any]:
99109
return super()._create_n_dim_search_space(
100110
min=min, max=max, size=size, value_types=value_types
101111
)

src/surfaces/test_functions/bbob/_base_bbob.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
Technical Report 2009/20, Research Center PPE.
1818
"""
1919

20-
from typing import Any, Dict, Optional
20+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
2121

2222
import numpy as np
2323

2424
from ..algebraic._base_algebraic_function import AlgebraicFunction
2525

26+
if TYPE_CHECKING:
27+
from surfaces.noise import BaseNoise
28+
2629

2730
class BBOBFunction(AlgebraicFunction):
2831
"""Base class for BBOB benchmark functions.
@@ -77,10 +80,10 @@ def __init__(
7780
sleep: float = 0,
7881
memory: bool = False,
7982
collect_data: bool = True,
80-
callbacks=None,
81-
catch_errors=None,
82-
noise=None,
83-
):
83+
callbacks: Optional[Union[Callable, List[Callable]]] = None,
84+
catch_errors: Optional[Dict[type, float]] = None,
85+
noise: Optional["BaseNoise"] = None,
86+
) -> None:
8487
self.n_dim = n_dim
8588
self.instance = instance
8689
self._rng = np.random.RandomState(self._compute_seed())

src/surfaces/test_functions/bbob/high_conditioning.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EllipsoidalRotated(BBOBFunction):
3131
"separable": False,
3232
}
3333

34-
def _create_objective_function(self):
34+
def _create_objective_function(self) -> None:
3535
i = np.arange(self.n_dim)
3636
coeffs = np.power(1e6, i / (self.n_dim - 1)) if self.n_dim > 1 else np.ones(1)
3737

@@ -63,7 +63,7 @@ class Discus(BBOBFunction):
6363
"separable": False,
6464
}
6565

66-
def _create_objective_function(self):
66+
def _create_objective_function(self) -> None:
6767
def discus(params: Dict[str, Any]) -> float:
6868
x = self._params_to_array(params)
6969
z = self.t_osz(self.R @ (x - self.x_opt))
@@ -92,7 +92,7 @@ class BentCigar(BBOBFunction):
9292
"separable": False,
9393
}
9494

95-
def _create_objective_function(self):
95+
def _create_objective_function(self) -> None:
9696
def bent_cigar(params: Dict[str, Any]) -> float:
9797
x = self._params_to_array(params)
9898
z = self.R @ self.t_asy(self.R @ (x - self.x_opt), 0.5)
@@ -120,7 +120,7 @@ class SharpRidge(BBOBFunction):
120120
"separable": False,
121121
}
122122

123-
def _create_objective_function(self):
123+
def _create_objective_function(self) -> None:
124124
Lambda = self.lambda_alpha(10)
125125

126126
def sharp_ridge(params: Dict[str, Any]) -> float:
@@ -150,7 +150,7 @@ class DifferentPowers(BBOBFunction):
150150
"separable": False,
151151
}
152152

153-
def _create_objective_function(self):
153+
def _create_objective_function(self) -> None:
154154
i = np.arange(self.n_dim)
155155
exponents = 2 + 4 * i / (self.n_dim - 1) if self.n_dim > 1 else 2 * np.ones(1)
156156

src/surfaces/test_functions/bbob/low_conditioning.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AttractiveSector(BBOBFunction):
3030
"separable": False,
3131
}
3232

33-
def _create_objective_function(self):
33+
def _create_objective_function(self) -> None:
3434
Lambda = self.lambda_alpha(10)
3535

3636
def attractive_sector(params: Dict[str, Any]) -> float:
@@ -65,7 +65,7 @@ class StepEllipsoidal(BBOBFunction):
6565
"continuous": False,
6666
}
6767

68-
def _create_objective_function(self):
68+
def _create_objective_function(self) -> None:
6969
Lambda = self.lambda_alpha(10)
7070

7171
def step_ellipsoidal(params: Dict[str, Any]) -> float:
@@ -111,7 +111,7 @@ def _generate_x_opt(self) -> np.ndarray:
111111
"""Generate x_opt constrained to [-3, 3]."""
112112
return self._rng.uniform(-3, 3, self.n_dim)
113113

114-
def _create_objective_function(self):
114+
def _create_objective_function(self) -> None:
115115
c = max(1, np.sqrt(self.n_dim) / 8)
116116

117117
def rosenbrock(params: Dict[str, Any]) -> float:
@@ -146,7 +146,7 @@ class RosenbrockRotated(BBOBFunction):
146146
"separable": False,
147147
}
148148

149-
def _create_objective_function(self):
149+
def _create_objective_function(self) -> None:
150150
c = max(1, np.sqrt(self.n_dim) / 8)
151151

152152
def rosenbrock_rotated(params: Dict[str, Any]) -> float:

0 commit comments

Comments
 (0)