77from warnings import warn
88from copy import deepcopy
99from collections import defaultdict , deque
10+ from inspect import signature
1011
1112import numpy as np
1213from scipy .stats .qmc import LatinHypercube
@@ -499,6 +500,13 @@ def __build_searchspace(self, block_size_names: list, max_threads: int, solver:
499500 def __add_restrictions (self , parameter_space : Problem ) -> Problem :
500501 """Add the user-specified restrictions as constraints on the parameter space."""
501502 restrictions = deepcopy (self .restrictions )
503+ # differentiate between old style monolithic with single 'p' argument and newer *args style
504+ if (len (restrictions ) == 1
505+ and not isinstance (restrictions [0 ], (Constraint , FunctionConstraint , str ))
506+ and callable (restrictions [0 ])
507+ and len (signature (restrictions [0 ]).parameters ) == 1
508+ and len (self .param_names ) > 1 ):
509+ restrictions = restrictions [0 ]
502510 if isinstance (restrictions , list ):
503511 for restriction in restrictions :
504512 required_params = self .param_names
@@ -508,10 +516,6 @@ def __add_restrictions(self, parameter_space: Problem) -> Problem:
508516 required_params = restriction [1 ]
509517 restriction = restriction [0 ]
510518 if callable (restriction ) and not isinstance (restriction , Constraint ):
511- # def restrictions_wrapper(*args):
512- # return check_instance_restrictions(restriction, dict(zip(self.param_names, args)), False)
513- # print(restriction, isinstance(restriction, Constraint))
514- # restriction = FunctionConstraint(restrictions_wrapper)
515519 restriction = FunctionConstraint (restriction , required_params )
516520
517521 # add as a Constraint
@@ -533,6 +537,7 @@ def __add_restrictions(self, parameter_space: Problem) -> Problem:
533537 elif callable (restrictions ):
534538
535539 def restrictions_wrapper (* args ):
540+ """Wrap old-style monolithic restrictions to work with multiple arguments."""
536541 return check_instance_restrictions (restrictions , dict (zip (self .param_names , args )), False )
537542
538543 parameter_space .addConstraint (FunctionConstraint (restrictions_wrapper ), self .param_names )
0 commit comments