|
| 1 | +from typing import Optional, Union, List |
| 2 | + |
1 | 3 | import numpy as np |
2 | 4 | from .target_space import TargetSpace |
3 | 5 |
|
@@ -25,18 +27,26 @@ def __init__( |
25 | 27 | self, |
26 | 28 | gamma_osc: float = 0.7, |
27 | 29 | gamma_pan: float = 1.0, |
28 | | - eta: float = 0.9 |
| 30 | + eta: float = 0.9, |
| 31 | + minimum_window: Optional[Union[List[float], float]] = 0.0 |
29 | 32 | ) -> None: |
30 | 33 | self.gamma_osc = gamma_osc |
31 | 34 | self.gamma_pan = gamma_pan |
32 | 35 | self.eta = eta |
33 | | - pass |
| 36 | + self.minimum_window_value = minimum_window |
34 | 37 |
|
35 | 38 | def initialize(self, target_space: TargetSpace) -> None: |
36 | 39 | """Initialize all of the parameters""" |
37 | 40 | self.original_bounds = np.copy(target_space.bounds) |
38 | 41 | self.bounds = [self.original_bounds] |
39 | 42 |
|
| 43 | + # Set the minimum window to an array of length bounds |
| 44 | + if isinstance(self.minimum_window_value, list) or isinstance(self.minimum_window_value, np.ndarray): |
| 45 | + assert len(self.minimum_window_value) == len(target_space.bounds) |
| 46 | + self.minimum_window = self.minimum_window_value |
| 47 | + else: |
| 48 | + self.minimum_window = [self.minimum_window_value] * len(target_space.bounds) |
| 49 | + |
40 | 50 | self.previous_optimal = np.mean(target_space.bounds, axis=1) |
41 | 51 | self.current_optimal = np.mean(target_space.bounds, axis=1) |
42 | 52 | self.r = target_space.bounds[:, 1] - target_space.bounds[:, 0] |
@@ -89,6 +99,14 @@ def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array: |
89 | 99 | variable[0] = global_bounds[i, 0] |
90 | 100 | if variable[1] > global_bounds[i, 1]: |
91 | 101 | variable[1] = global_bounds[i, 1] |
| 102 | + for i, entry in enumerate(new_bounds): |
| 103 | + if entry[0] > entry[1]: |
| 104 | + new_bounds[i, 0] = entry[1] |
| 105 | + new_bounds[i, 1] = entry[0] |
| 106 | + window_width = abs(entry[0] - entry[1]) |
| 107 | + if window_width < self.minimum_window[i]: |
| 108 | + new_bounds[i, 0] -= (self.minimum_window[i] - window_width) / 2.0 |
| 109 | + new_bounds[i, 1] += (self.minimum_window[i] - window_width) / 2.0 |
92 | 110 |
|
93 | 111 | return new_bounds |
94 | 112 |
|
|
0 commit comments