@@ -57,8 +57,9 @@ class SequentialDomainReductionTransformer(DomainTransformer):
5757 Zooming parameter used to shrink the region of interest.
5858
5959 minimum_window : float or np.ndarray or dict, default=0.0
60- Minimum window size for each parameter. If a float is provided,
61- the same value is used for all parameters.
60+ Minimum window size for each float parameter, in parameter order. If a
61+ scalar is provided, the same value is used for all float parameters.
62+ Integer and categorical parameters retain their original domains.
6263 """
6364
6465 def __init__ (
@@ -69,7 +70,6 @@ def __init__(
6970 eta : float = 0.9 ,
7071 minimum_window : NDArray [Float ] | Sequence [float ] | Mapping [str , float ] | float = 0.0 ,
7172 ) -> None :
72- # TODO: Ensure that this is only applied to continuous parameters
7373 self .parameters = parameters
7474 self .gamma_osc = gamma_osc
7575 self .gamma_pan = gamma_pan
@@ -85,33 +85,31 @@ def initialize(self, target_space: TargetSpace) -> None:
8585 target_space : TargetSpace
8686 TargetSpace this DomainTransformer operates on.
8787 """
88- if isinstance (self .minimum_window_value , Mapping ):
89- self .minimum_window_value = [self .minimum_window_value [key ] for key in target_space .keys ]
90- else :
91- self .minimum_window_value = self .minimum_window_value
92-
93- any_not_float = any (not isinstance (p , FloatParameter ) for p in target_space ._params_config .values ())
94- if any_not_float :
95- msg = "Domain reduction is only supported for all-FloatParameter optimization."
88+ self ._float_keys = [
89+ key for key , param in target_space .params_config .items () if isinstance (param , FloatParameter )
90+ ]
91+ if not self ._float_keys :
92+ msg = "Domain reduction requires at least one FloatParameter."
9693 raise ValueError (msg )
97- # Set the original bounds
98- self .original_bounds = np .copy (target_space .bounds )
94+ self .original_bounds = np .array ([target_space .params_config [key ].bounds for key in self ._float_keys ])
9995 self .bounds = [self .original_bounds ]
10096
10197 self .minimum_window : NDArray [Float ] | Sequence [float ]
10298 # Set the minimum window to an array of length bounds
103- if isinstance (self .minimum_window_value , (Sequence , np .ndarray )):
104- if len (self .minimum_window_value ) != len (target_space .bounds ):
105- error_msg = "Length of minimum_window must be the same as the number of parameters"
99+ if isinstance (self .minimum_window_value , Mapping ):
100+ self .minimum_window = [self .minimum_window_value [key ] for key in self ._float_keys ]
101+ elif isinstance (self .minimum_window_value , (Sequence , np .ndarray )):
102+ if len (self .minimum_window_value ) != len (self ._float_keys ):
103+ error_msg = "Length of minimum_window must be the same as the number of float parameters"
106104 raise ValueError (error_msg )
107105 self .minimum_window = self .minimum_window_value
108106 else :
109- self .minimum_window = [self .minimum_window_value ] * len (target_space . bounds )
107+ self .minimum_window = [self .minimum_window_value ] * len (self . _float_keys )
110108
111109 # Set initial values
112- self .previous_optimal = np .mean (target_space . bounds , axis = 1 )
113- self .current_optimal = np .mean (target_space . bounds , axis = 1 )
114- self .r = target_space . bounds [:, 1 ] - target_space . bounds [:, 0 ]
110+ self .previous_optimal = np .mean (self . original_bounds , axis = 1 )
111+ self .current_optimal = np .mean (self . original_bounds , axis = 1 )
112+ self .r = self . original_bounds [:, 1 ] - self . original_bounds [:, 0 ]
115113
116114 self .previous_d = 2.0 * (self .current_optimal - self .previous_optimal ) / self .r
117115
@@ -141,7 +139,8 @@ def _update(self, target_space: TargetSpace) -> None:
141139 self .previous_optimal = self .current_optimal
142140 self .previous_d = self .current_d
143141
144- self .current_optimal = target_space .params_to_array (target_space .max ()["params" ])
142+ best_params = target_space .max ()["params" ]
143+ self .current_optimal = np .array ([best_params [key ] for key in self ._float_keys ])
145144
146145 self .current_d = 2.0 * (self .current_optimal - self .previous_optimal ) / self .r
147146
@@ -289,4 +288,4 @@ def transform(self, target_space: TargetSpace) -> dict[str, NDArray[Float]]:
289288
290289 new_bounds = self ._trim (new_bounds , self .original_bounds )
291290 self .bounds .append (new_bounds )
292- return self ._create_bounds (target_space . keys , new_bounds )
291+ return self ._create_bounds (self . _float_keys , new_bounds )
0 commit comments