From 046a8e2ec45baae8dfd55da655f348d21ccd197f Mon Sep 17 00:00:00 2001 From: AdrianSosic Date: Tue, 29 Oct 2024 18:12:34 +0100 Subject: [PATCH] Remove restriction on subspaces without cardinality constraints --- baybe/recommenders/pure/bayesian/botorch.py | 29 ++++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/baybe/recommenders/pure/bayesian/botorch.py b/baybe/recommenders/pure/bayesian/botorch.py index fe931888a..4c13da97d 100644 --- a/baybe/recommenders/pure/bayesian/botorch.py +++ b/baybe/recommenders/pure/bayesian/botorch.py @@ -193,17 +193,23 @@ def _recommend_continuous( f"acquisition functions for batch sizes > 1." ) + points, _ = self._recommend_continuous_torch(subspace_continuous, batch_size) + + return pd.DataFrame(points, columns=subspace_continuous.parameter_names) + + def _recommend_continuous_torch( + self, subspace_continuous: SubspaceContinuous, batch_size: int + ) -> tuple[Tensor, Tensor]: + """Dispatcher selecting continuous optimization routine.""" if subspace_continuous.constraints_cardinality: - points, _ = self._recommend_continuous_with_cardinality_constraints( + return self._recommend_continuous_with_cardinality_constraints( subspace_continuous, batch_size ) else: - points, _ = self._recommend_continuous_without_cardinality_constraints( + return self._recommend_continuous_without_cardinality_constraints( subspace_continuous, batch_size ) - return pd.DataFrame(points, columns=subspace_continuous.parameter_names) - def _recommend_continuous_with_cardinality_constraints( self, subspace_continuous: SubspaceContinuous, @@ -254,9 +260,7 @@ def _recommend_continuous_with_cardinality_constraints( for inactive_parameters in iterator ) - return self._optimize_subspaces_without_cardinality_constraints( - subspaces, batch_size - ) + return self._optimize_continuous_subspaces(subspaces, batch_size) def _recommend_continuous_without_cardinality_constraints( self, @@ -447,10 +451,10 @@ def __str__(self) -> str: ] return to_string(self.__class__.__name__, *fields) - def _optimize_subspaces_without_cardinality_constraints( + def _optimize_continuous_subspaces( self, subspaces: Iterable[SubspaceContinuous], batch_size: int ) -> tuple[Tensor, Tensor]: - """Find the optimum candidates from multiple subspaces. + """Find the optimum candidates from multiple continuous subspaces. Args: subspaces: The subspaces to consider for the optimization. @@ -465,12 +469,11 @@ def _optimize_subspaces_without_cardinality_constraints( for subspace in subspaces: try: # Optimize the acquisition function - f = self._recommend_continuous_without_cardinality_constraints - points_i, acqf_values_i = f(subspace, batch_size) + p, acqf = self._recommend_continuous_torch(subspace, batch_size) # Append optimization results - points_all.append(points_i) - acqf_values_all.append(acqf_values_i) + points_all.append(p) + acqf_values_all.append(acqf) # The optimization problem may be infeasible in certain subspaces except ValueError: