18
18
import inspect
19
19
import logging
20
20
from abc import abstractmethod
21
+ from typing import Any
21
22
22
23
from orion .algo .registry import Registry
24
+ from orion .algo .space import Space
23
25
from orion .core .utils import GenericFactory
24
26
from orion .core .worker .trial import Trial
25
27
@@ -101,7 +103,9 @@ def observe(self, points, results):
101
103
requires_shape = None
102
104
requires_dist = None
103
105
104
- def __init__ (self , space , ** kwargs ):
106
+ max_trials : int | None = None
107
+
108
+ def __init__ (self , space : Space , ** kwargs ):
105
109
log .debug (
106
110
"Creating Algorithm object of %s type with parameters:\n %s" ,
107
111
type (self ).__name__ ,
@@ -142,18 +146,19 @@ def state_dict(self):
142
146
"""Return a state dict that can be used to reset the state of the algorithm."""
143
147
return {"registry" : self .registry .state_dict }
144
148
145
- def set_state (self , state_dict ):
149
+ def set_state (self , state_dict : dict ):
146
150
"""Reset the state of the algorithm based on the given state_dict
147
151
148
152
:param state_dict: Dictionary representing state of an algorithm
149
153
"""
150
154
self .registry .set_state (state_dict ["registry" ])
151
155
152
- def get_id (self , trial , ignore_fidelity = False , ignore_parent = False ):
156
+ def get_id (
157
+ self , trial : Trial , ignore_fidelity : bool = False , ignore_parent : bool = False
158
+ ) -> str :
153
159
"""Return unique hash for a trials based on params
154
160
155
- The trial is assumed to be in the transformed space if the algorithm is working in a
156
- transformed space.
161
+ The trial is assumed to be in the optimization space of the algorithm.
157
162
158
163
Parameters
159
164
----------
@@ -170,13 +175,12 @@ def get_id(self, trial, ignore_fidelity=False, ignore_parent=False):
170
175
return trial .compute_trial_hash (
171
176
trial ,
172
177
ignore_fidelity = ignore_fidelity ,
173
- ignore_experiment = True ,
174
178
ignore_lie = True ,
175
179
ignore_parent = ignore_parent ,
176
180
)
177
181
178
182
@property
179
- def fidelity_index (self ):
183
+ def fidelity_index (self ) -> str | None :
180
184
"""Returns the name of the first fidelity dimension if there is one, otherwise `None`."""
181
185
fidelity_dims = [dim for dim in self .space .values () if dim .type == "fidelity" ]
182
186
if fidelity_dims :
@@ -209,7 +213,7 @@ def suggest(self, num: int) -> list[Trial]:
209
213
has suggested/observed, and for the auto-generated unit-tests to pass.
210
214
"""
211
215
212
- def observe (self , trials ) :
216
+ def observe (self , trials : list [ Trial ]) -> None :
213
217
"""Observe the `results` of the evaluation of the `trials` in the
214
218
process defined in user's script.
215
219
@@ -223,7 +227,7 @@ def observe(self, trials):
223
227
if not self .has_observed (trial ):
224
228
self .register (trial )
225
229
226
- def register (self , trial ) :
230
+ def register (self , trial : Trial ) -> None :
227
231
"""Save the trial as one suggested or observed by the algorithm.
228
232
229
233
Parameters
@@ -234,16 +238,16 @@ def register(self, trial):
234
238
self .registry .register (trial )
235
239
236
240
@property
237
- def n_suggested (self ):
241
+ def n_suggested (self ) -> int :
238
242
"""Number of trials suggested by the algorithm"""
239
243
return len (self .registry )
240
244
241
245
@property
242
- def n_observed (self ):
246
+ def n_observed (self ) -> int :
243
247
"""Number of completed trials observed by the algorithm."""
244
248
return sum (self .has_observed (trial ) for trial in self .registry )
245
249
246
- def has_suggested (self , trial ) :
250
+ def has_suggested (self , trial : Trial ) -> bool :
247
251
"""Whether the algorithm has suggested a given point.
248
252
249
253
Parameters
@@ -259,7 +263,7 @@ def has_suggested(self, trial):
259
263
"""
260
264
return self .registry .has_suggested (trial )
261
265
262
- def has_observed (self , trial ) :
266
+ def has_observed (self , trial : Trial ) -> bool :
263
267
"""Whether the algorithm has observed a given point objective.
264
268
265
269
This only counts observed completed trials.
@@ -312,15 +316,11 @@ def has_completed_max_trials(self) -> bool:
312
316
"""Returns True if the algorithm has a `max_trials` attribute, and has completed more trials
313
317
than its value.
314
318
"""
315
- if not hasattr (self , "max_trials" ):
316
- return False
317
- max_trials = getattr (self , "max_trials" )
318
- if max_trials is None :
319
+ if self .max_trials is None :
319
320
return False
320
321
321
322
fidelity_index = self .fidelity_index
322
323
max_fidelity_value = None
323
-
324
324
# When a fidelity dimension is present, we only count trials that have the maximum value.
325
325
if fidelity_index is not None :
326
326
_ , max_fidelity_value = self .space [fidelity_index ].interval ()
@@ -333,10 +333,11 @@ def _is_completed(trial: Trial) -> bool:
333
333
and trial .params [fidelity_index ] >= max_fidelity_value
334
334
)
335
335
336
- return sum (map (_is_completed , self .registry )) >= max_trials
336
+ return sum (map (_is_completed , self .registry )) >= self . max_trials
337
337
338
- def score (self , trial ): # pylint:disable=no-self-use,unused-argument
339
- """Allow algorithm to evaluate `point` based on a prediction about
338
+ # pylint:disable=no-self-use,unused-argument
339
+ def score (self , trial : Trial ) -> float :
340
+ """Allow algorithm to evaluate `trial` based on a prediction about
340
341
this parameter set's performance.
341
342
342
343
By default, return the same score any parameter (no preference).
@@ -353,7 +354,8 @@ def score(self, trial): # pylint:disable=no-self-use,unused-argument
353
354
"""
354
355
return 0
355
356
356
- def judge (self , trial , measurements ): # pylint:disable=no-self-use,unused-argument
357
+ # pylint:disable=no-self-use,unused-argument
358
+ def judge (self , trial : Trial , measurements : Any ) -> dict | None :
357
359
"""Inform an algorithm about online `measurements` of a running trial.
358
360
359
361
This method is to be used as a callback in a client-server communication
@@ -381,7 +383,7 @@ def judge(self, trial, measurements): # pylint:disable=no-self-use,unused-argum
381
383
"""
382
384
return None
383
385
384
- def should_suspend (self , trial ) :
386
+ def should_suspend (self , trial : Trial ) -> bool :
385
387
"""Allow algorithm to decide whether a particular running trial is still
386
388
worth to complete its evaluation, based on information provided by the
387
389
`judge` method.
@@ -390,10 +392,12 @@ def should_suspend(self, trial):
390
392
return False
391
393
392
394
@property
393
- def configuration (self ):
395
+ def configuration (self ) -> dict [ str , Any ] :
394
396
"""Return tunable elements of this algorithm in a dictionary form
395
397
appropriate for saving.
396
398
399
+ By default, returns a dictionary containing the attributes of `self` which are also
400
+ constructor arguments.
397
401
"""
398
402
dict_form = dict ()
399
403
for attrname in self ._param_names :
@@ -404,14 +408,13 @@ def configuration(self):
404
408
return {self .__class__ .__name__ .lower (): dict_form }
405
409
406
410
@property
407
- def space (self ):
411
+ def space (self ) -> Space :
408
412
"""Domain of problem associated with this algorithm's instance."""
409
413
return self ._space
410
414
411
- @space .setter
412
- def space (self , space ):
413
- """Set space."""
414
- self ._space = space
415
+ @property
416
+ def unwrapped (self ):
417
+ return self
415
418
416
419
417
420
algo_factory = GenericFactory (BaseAlgorithm )
0 commit comments