Skip to content

Commit 8408174

Browse files
committed
fix(types): Resolve Mypy errors in utils, functions, and visualization
- **utils.py**: Explicitly cast boolean and float return values. - **functions.py**: Cast integer-like calculations to float for \multimod\ and \schwefel2_22\. - **visualization.py**: Pass \extent\ as a tuple (not list) to \imshow\.
1 parent 43c3344 commit 8408174

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

optimization_benchmarks/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def multimod(x: np.ndarray) -> float:
558558
Global minimum: f = 0 at x = 0:contentReference[oaicite:39]{index=39}.
559559
"""
560560
x = np.asarray(x, dtype=float)
561-
return np.sum(np.abs(x)) * np.prod(np.abs(x))
561+
return float(np.sum(np.abs(x)) * np.prod(np.abs(x)))
562562

563563

564564
def rastrigin(x: np.ndarray) -> float:
@@ -676,7 +676,7 @@ def schwefel2_22(x: np.ndarray) -> float:
676676
Global minimum: f = 0 at x = 0:contentReference[oaicite:48]{index=48}.
677677
"""
678678
x = np.asarray(x, dtype=float)
679-
return np.sum(np.abs(x)) + np.prod(np.abs(x))
679+
return float(np.sum(np.abs(x)) + np.prod(np.abs(x)))
680680

681681

682682
def schwefel2_26(x: np.ndarray) -> float:

optimization_benchmarks/utils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def check_bounds(
167167
lower = bounds[:, 0]
168168
upper = bounds[:, 1]
169169

170-
return np.all((point >= lower - tolerance) & (point <= upper + tolerance))
170+
return bool(np.all((point >= lower - tolerance) & (point <= upper + tolerance)))
171171

172172

173173
def scale_to_unit(point: np.ndarray, bounds: List[Tuple[float, float]]) -> np.ndarray:
@@ -303,7 +303,9 @@ def get_bounds_center(bounds: List[Tuple[float, float]]) -> np.ndarray:
303303
return (bounds[:, 0] + bounds[:, 1]) / 2
304304

305305

306-
def generate_grid_points(bounds: List[Tuple[float, float]], points_per_dim: int = 10) -> np.ndarray:
306+
def generate_grid_points(
307+
bounds: List[Tuple[float, float]], points_per_dim: int = 10
308+
) -> np.ndarray:
307309
"""
308310
Generate a grid of points within bounds.
309311
@@ -329,7 +331,9 @@ def generate_grid_points(bounds: List[Tuple[float, float]], points_per_dim: int
329331
dim = len(bounds)
330332

331333
# Create 1D grids for each dimension
332-
grids_1d = [np.linspace(bounds[i, 0], bounds[i, 1], points_per_dim) for i in range(dim)]
334+
grids_1d = [
335+
np.linspace(bounds[i, 0], bounds[i, 1], points_per_dim) for i in range(dim)
336+
]
333337

334338
# Create meshgrid
335339
meshes = np.meshgrid(*grids_1d, indexing="ij")
@@ -366,8 +370,12 @@ def calculate_distance_to_optimum(
366370
point = np.asarray(point)
367371

368372
if isinstance(optimal_point, (list, tuple)):
369-
distances = [np.linalg.norm(point - np.asarray(opt)) for opt in optimal_point]
373+
# Handle list of optima
374+
distances = [
375+
float(np.linalg.norm(point - np.asarray(opt))) for opt in optimal_point
376+
]
370377
return min(distances)
371378
else:
379+
# Handle single optimum
372380
optimal_point = np.asarray(optimal_point)
373-
return np.linalg.norm(point - optimal_point)
381+
return float(np.linalg.norm(point - optimal_point))

optimization_benchmarks/visualization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ def plot_search_heatmap(
820820
range=[[bounds[0][0], bounds[0][1]], [bounds[1][0], bounds[1][1]]],
821821
)
822822

823-
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
823+
extent = (xedges[0], xedges[-1], yedges[0], yedges[-1])
824824
im = ax.imshow(heatmap.T, extent=extent, origin="lower", cmap=cmap, alpha=0.7)
825825

826826
cbar = plt.colorbar(im, ax=ax)

0 commit comments

Comments
 (0)