Skip to content

Commit

Permalink
Fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschenbacher committed Jul 3, 2024
1 parent 70d961d commit 0be3285
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion baybe/constraints/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def combinatorial_counts_zero_parameters(self) -> int:
@property
def combinatorial_zero_parameters(self) -> list[tuple[str, ...]]:
"""Return a combinatorial list of all possible zero parameters."""
combinatorial_zeros = []
combinatorial_zeros: list[tuple[str, ...]] = []
for i_zeros in range(
len(self.parameters) - self.max_cardinality,
len(self.parameters) - self.min_cardinality + 1,
Expand Down
2 changes: 1 addition & 1 deletion baybe/constraints/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate_constraints( # noqa: DOC101, DOC103
param_names_continuous = [p.name for p in parameters if p.is_continuous]
param_names_non_numerical = [p.name for p in parameters if not p.is_numerical]
params_continuous: list[NumericalContinuousParameter] = [
p for p in parameters if p.is_continuous
p for p in parameters if isinstance(p, NumericalContinuousParameter)
]

for constraint in constraints:
Expand Down
51 changes: 34 additions & 17 deletions baybe/recommenders/pure/bayesian/botorch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Botorch recommender."""
import math
from collections.abc import Iterable
from typing import Any, ClassVar

import pandas as pd
Expand Down Expand Up @@ -141,6 +142,7 @@ def _recommend_continuous(
Raises:
NoMCAcquisitionFunctionError: If a non-Monte Carlo acquisition function is
used with a batch size > 1.
RuntimeError: If the combinatorial list of inactive parameters is None.
Returns:
A dataframe containing the recommendations as individual rows.
Expand Down Expand Up @@ -186,33 +188,48 @@ def _recommend_continuous_on_subspace(
acqf_values_all: list[Tensor] = []
points_all: list[Tensor] = []

# When the size of the full list of inactive parameters is not too large,
# we can iterate through the full list; otherwise we randomly set some
# parameters inactive.
_iterator = (
subspace_continuous.combinatorial_zero_parameters
if (
combinatorial_counts
:= subspace_continuous.combinatorial_counts_zero_parameters
# The key steps of handling cardinality constraint are
# * Determine several configurations of inactive parameters based on the
# cardinality constraints.
# * Optimize the acquisition function for different configurations and
# pick the best one.
# There are two mechanisms for inactive parameter configurations. The
# full list of different inactive parameter configurations is used,
# when its size is not too large; otherwise we randomly pick a
# fixed number of inactive parameter configurations.

# Create an iterable that either iterates through range() or iterates
# through the full list configuration.
if (
subspace_continuous.combinatorial_counts_zero_parameters
> N_ITER_THRESHOLD
):
_iterator: Iterable[tuple[tuple[str, ...], ...]] | range = range(
N_ITER_THRESHOLD
)
elif subspace_continuous.combinatorial_zero_parameters is not None:
_iterator = subspace_continuous.combinatorial_zero_parameters
else:
raise RuntimeError(
f"The attribute"
f"{SubspaceContinuous.combinatorial_zero_parameters.__name__}"
f"should not be None."
)
<= N_ITER_THRESHOLD
else range(N_ITER_THRESHOLD)
)

for inactive_params_generator in _iterator:
if combinatorial_counts <= N_ITER_THRESHOLD:
if isinstance(inactive_params_generator, int):
# Randomly set some parameters inactive
inactive_params_sample = (
subspace_continuous._sample_inactive_parameters(1)[0]
)
else:
# Iterate through the combinations of all possible inactive
# parameters.
inactive_params_sample = {
param
for sublist in inactive_params_generator
for param in sublist
}
else:
# Randomly set some parameters inactive
inactive_params_sample = (
subspace_continuous._sample_inactive_parameters(1)[0]
)

if len(inactive_params_sample):
# Turn inactive parameters to fixed features (used as input in
Expand Down
6 changes: 5 additions & 1 deletion baybe/searchspace/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def combinatorial_counts_zero_parameters(self) -> int:
return 0

@property
def combinatorial_zero_parameters(self) -> Iterable[tuple[str, ...]]:
def combinatorial_zero_parameters(
self
) -> Iterable[tuple[tuple[str, ...], ...]] | None:
"""Return a combinatorial list of all possible zero parameters on subspace."""
# The comments on the difference in `combinatorial_counts_zero_parameters`
# applies here as well.
Expand All @@ -141,6 +143,8 @@ def combinatorial_zero_parameters(self) -> Iterable[tuple[str, ...]]:
for con in self.constraints_cardinality
]
)
else:
return None

@constraints_nonlin.validator
def _validate_constraints_nonlin(self, _, __) -> None:
Expand Down

0 comments on commit 0be3285

Please sign in to comment.