You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current mechanism for storing the configuration of a custom algorithm is to pass all the kwargs to super().__init__(**kwargs), like so:
def__init__(
self,
space: Space,
n_values: int|dict[str, int] =100,
seed: int|Sequence[int] |None=None,
):
super().__init__(space, n_values=n_values, seed=seed)
self.n=0
...
something=foo(self.n_values) # type checker complains that `self.n_values` can't be found
The __init__ of BaseAlgorithm then saves the names of the arguments passed in an attribute, and then sets the values on self:
def__init__(self, space, **kwargs):
log.debug(
"Creating Algorithm object of %s type with parameters:\n%s",
type(self).__name__,
kwargs,
)
self._space=spaceself._param_names=list(kwargs.keys())
# Instantiate tunable parameters of an algorithmforvarname, paraminkwargs.items():
setattr(self, varname, param)
# TODO: move this inside an initialization function.ifhasattr(self, "seed"):
self.seed_rng(self.seed)
self.registry=Registry()
I see one main disadvantage of doing things this way:
When using attributes set in this way, type checkers complain that the attribute is not defined (since it doesn't have a self.n_values = n_values, for example.
I'd also argue that it seems a bit too much responsibility to give to BaseAlgorithm.__init__ to save the names, set the values, and initialize the 'base' attributes like self.registry, etc.
Suggestion:
I suggest that we still allow this mechanism, but that we additionally allow saving the attributes directly on self instead and discover _param_names programmatically, like so:
Oh and while we're at it, I suggest we add a self.max_trials: Optional[int] = None attribute in BaseAlgorithm. Lots of tests forcefully set this attribute on the algos, even though it isn't defined.
The current mechanism for storing the configuration of a custom algorithm is to pass all the kwargs to
super().__init__(**kwargs)
, like so:The
__init__
ofBaseAlgorithm
then saves the names of the arguments passed in an attribute, and then sets the values onself
:I see one main disadvantage of doing things this way:
self.n_values = n_values
, for example.I'd also argue that it seems a bit too much responsibility to give to
BaseAlgorithm.__init__
to save the names, set the values, and initialize the 'base' attributes likeself.registry
, etc.Suggestion:
I suggest that we still allow this mechanism, but that we additionally allow saving the attributes directly on
self
instead and discover_param_names
programmatically, like so:_arg_names
will contain the names of all the constructor arguments.The text was updated successfully, but these errors were encountered: