@@ -248,9 +248,37 @@ def check_block_size_params_names_list(block_size_names, tune_params):
248
248
UserWarning ,
249
249
)
250
250
251
+ def check_restriction (restrict , params : dict ) -> bool :
252
+ """Check whether a configuration meets a search space restriction."""
253
+ # if it's a python-constraint, convert to function and execute
254
+ if isinstance (restrict , Constraint ):
255
+ restrict = convert_constraint_restriction (restrict )
256
+ return restrict (list (params .values ()))
257
+ # if it's a string, fill in the parameters and evaluate
258
+ elif isinstance (restrict , str ):
259
+ return eval (replace_param_occurrences (restrict , params ))
260
+ # if it's a function, call it
261
+ elif callable (restrict ):
262
+ return restrict (** params )
263
+ # if it's a tuple, use only the parameters in the second argument to call the restriction
264
+ elif (isinstance (restrict , tuple ) and len (restrict ) == 2
265
+ and callable (restrict [0 ]) and isinstance (restrict [1 ], (list , tuple ))):
266
+ # unpack the tuple
267
+ restrict , selected_params = restrict
268
+ # look up the selected parameters and their value
269
+ selected_params = dict ((key , params [key ]) for key in selected_params )
270
+ # call the restriction
271
+ if isinstance (restrict , Constraint ):
272
+ restrict = convert_constraint_restriction (restrict )
273
+ return restrict (list (selected_params .values ()))
274
+ else :
275
+ return restrict (** selected_params )
276
+ # otherwise, raise an error
277
+ else :
278
+ raise ValueError (f"Unkown restriction type { type (restrict )} ({ restrict } )" )
251
279
252
280
def check_restrictions (restrictions , params : dict , verbose : bool ) -> bool :
253
- """Check whether a specific configuration meets the search space restrictions."""
281
+ """Check whether a configuration meets the search space restrictions."""
254
282
if callable (restrictions ):
255
283
valid = restrictions (params )
256
284
if not valid and verbose is True :
@@ -260,46 +288,13 @@ def check_restrictions(restrictions, params: dict, verbose: bool) -> bool:
260
288
for restrict in restrictions :
261
289
# Check the type of each restriction and validate accordingly. Re-implement as a switch when Python >= 3.10.
262
290
try :
263
- # if it's a python-constraint, convert to function and execute
264
- if isinstance (restrict , Constraint ):
265
- restrict = convert_constraint_restriction (restrict )
266
- if not restrict (list (params .values ())):
267
- valid = False
268
- break
269
- # if it's a string, fill in the parameters and evaluate
270
- elif isinstance (restrict , str ):
271
- if not eval (replace_param_occurrences (restrict , params )):
272
- valid = False
273
- break
274
- # if it's a function, call it
275
- elif callable (restrict ):
276
- if not restrict (** params ):
277
- valid = False
278
- break
279
- # if it's a tuple, use only the parameters in the second argument to call the restriction
280
- elif (isinstance (restrict , tuple ) and len (restrict ) == 2
281
- and callable (restrict [0 ]) and isinstance (restrict [1 ], (list , tuple ))):
282
- # unpack the tuple
283
- restrict , selected_params = restrict
284
- # look up the selected parameters and their value
285
- selected_params = dict ((key , params [key ]) for key in selected_params )
286
- # call the restriction
287
- if isinstance (restrict , Constraint ):
288
- restrict = convert_constraint_restriction (restrict )
289
- if not restrict (list (selected_params .values ())):
290
- valid = False
291
- break
292
- else :
293
- if not restrict (** selected_params ):
294
- valid = False
295
- break
296
- # otherwise, raise an error
297
- else :
298
- raise ValueError (f"Unkown restriction type { type (restrict )} ({ restrict } )" )
291
+ valid = check_restriction (restrict , params )
292
+ if not valid :
293
+ break
299
294
except ZeroDivisionError :
300
295
logging .debug (f"Restriction { restrict } with configuration { get_instance_string (params )} divides by zero." )
301
296
if not valid and verbose is True :
302
- print (f"skipping config { get_instance_string (params )} , reason: config fails restriction" )
297
+ print (f"skipping config { get_instance_string (params )} , reason: config fails restriction { restrict } " )
303
298
return valid
304
299
305
300
0 commit comments