66from hyperactive .base import BaseOptimizer
77from skbase .utils .stdout_mute import StdoutMute
88
9+ from ._objective_function import ObjectiveFunction
10+
911__all__ = ["_BaseGFOadapter" ]
1012
1113
@@ -58,7 +60,10 @@ def get_search_config(self):
5860
5961 search_config = self ._handle_gfo_defaults (search_config )
6062
61- search_config ["search_space" ] = self ._to_dict_np (search_config ["search_space" ])
63+ self .search_space_hyper = search_config ["search_space" ]
64+ search_config ["search_space" ] = self ._conv_search_space (
65+ search_config ["search_space" ]
66+ )
6267
6368 return search_config
6469
@@ -85,6 +90,18 @@ def _handle_gfo_defaults(self, search_config):
8590
8691 return search_config
8792
93+ @staticmethod
94+ def _conv_search_space (search_space ):
95+ # convert hyper search-space into gfo search-space
96+ search_space_gfo = {}
97+ for key in search_space .keys ():
98+ search_space_gfo [key ] = np .array (range (len (search_space [key ])))
99+ return search_space_gfo
100+
101+ @staticmethod
102+ def _conv_objective_function (objective_function , search_space ):
103+ return ObjectiveFunction (objective_function ).convert (search_space )
104+
88105 def _to_dict_np (self , search_space ):
89106 """Coerce the search space to a format suitable for gfo optimizers.
90107
@@ -108,7 +125,7 @@ def coerce_to_numpy(arr):
108125 if not isinstance (arr , np .ndarray ):
109126 return np .array (arr )
110127 return arr
111-
128+
112129 coerced_search_space = {k : coerce_to_numpy (v ) for k , v in search_space .items ()}
113130 return coerced_search_space
114131
@@ -129,23 +146,18 @@ def _run(self, experiment, **search_config):
129146 n_iter = search_config .pop ("n_iter" , 100 )
130147 max_time = search_config .pop ("max_time" , None )
131148
132- # convert hyper search-space into gfo search-space
133- search_space_hyper = search_config ["search_space" ]
134- search_space_gfo = {}
135- for key in search_space_hyper .keys ():
136- search_space_gfo [key ] = np .array (range (len (search_space_hyper [key ])))
137- search_config ["search_space" ] = search_space_gfo
138-
139149 gfo_cls = self ._get_gfo_class ()
140- hcopt = gfo_cls (** search_config )
150+ opt = gfo_cls (** search_config )
151+
152+ score = self ._conv_objective_function (experiment , self .search_space_hyper )
141153
142154 with StdoutMute (active = not self .verbose ):
143- hcopt .search (
144- objective_function = experiment . score ,
155+ opt .search (
156+ objective_function = score ,
145157 n_iter = n_iter ,
146158 max_time = max_time ,
147159 )
148- best_params = hcopt .best_para
160+ best_params = opt .best_para
149161 return best_params
150162
151163 @classmethod
0 commit comments