From f68ce4f5d3ab8d7f1c9d76a693b1f680e376ccee Mon Sep 17 00:00:00 2001 From: Di Jin Date: Fri, 12 Jul 2024 11:58:42 +0200 Subject: [PATCH] Make 'n_threshold_inactive_parameters_generator' an attribute of botorch recommender --- CHANGELOG.md | 1 + baybe/recommenders/pure/bayesian/botorch.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 365b215d47..997cabe104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ _ `_optional` subpackage for managing optional dependencies `DiscreteCardinalityConstraint`/`ContinuousCardinalityConstraint` subclasses - Uniform sampling mechanism for continuous spaces with cardinality constraints - Enable `ContinuousCardinalityConstraint` in `BotorchRecommender` +- Attribute `n_threshold_inactive_parameters_generator` added to `BotorchRecommender` - Properties `combinatorial_zero_parameters` and `combinatorial_counts_zero_parameters` in both `ContinuousCardinalityConstraint`and `SubspaceContinuous` diff --git a/baybe/recommenders/pure/bayesian/botorch.py b/baybe/recommenders/pure/bayesian/botorch.py index 91a28ec1a6..d749343f64 100644 --- a/baybe/recommenders/pure/bayesian/botorch.py +++ b/baybe/recommenders/pure/bayesian/botorch.py @@ -8,6 +8,7 @@ import pandas as pd from attr.converters import optional from attrs import define, field +from attrs.validators import ge, instance_of from baybe.constraints import ContinuousCardinalityConstraint from baybe.exceptions import NoMCAcquisitionFunctionError @@ -27,12 +28,6 @@ if TYPE_CHECKING: from torch import Tensor -N_THRESHOLD_INACTIVE_PARAMETERS_GENERATOR: int = 10 -"""This threshold controls which inactive parameters generator is chosen. There are -two mechanisms: -* Iterating the combinatorial list of all possible inactive parameters, -* Iterate a fixed number of randomly generated inactive parameter configurations.""" - @define(kw_only=True) class BotorchRecommender(BayesianRecommender): @@ -70,6 +65,16 @@ class BotorchRecommender(BayesianRecommender): """Percentage of discrete search space that is sampled when performing hybrid search space optimization. Ignored when ``hybrid_sampler="None"``.""" + n_threshold_inactive_parameters_generator: int = field( + default=10, validator=[instance_of(int), ge(1)] + ) + """Threshold used for checking which inactive parameters generator is used when + cardinality constraints are present. When the size of the combinatorial list of + all possible inactive parameters is larger than the threshold, a fixed number of + randomly generated inactive parameter configurations are used and the best + optimum among them is recommended; Otherwise, we find the best one by iterating the + combinatorial list of all possible inactive parameters """ + @sampling_percentage.validator def _validate_percentage( # noqa: DOC101, DOC103 self, _: Any, value: float @@ -240,11 +245,11 @@ def append_recommendation_for_inactive_parameters_setting( # Below we start recommendation if ( subspace_continuous.n_combinatorial_inactive_parameters - > N_THRESHOLD_INACTIVE_PARAMETERS_GENERATOR + > self.n_threshold_inactive_parameters_generator ): # When the combinatorial list is too large, randomly set some parameters # inactive. - for _ in range(N_THRESHOLD_INACTIVE_PARAMETERS_GENERATOR): + for _ in range(self.n_threshold_inactive_parameters_generator): inactive_params_sample = tuple( subspace_continuous._sample_inactive_parameters(1)[0] )