diff --git a/baybe/acquisition/__init__.py b/baybe/acquisition/__init__.py index baf1f15ad0..b19b74893f 100644 --- a/baybe/acquisition/__init__.py +++ b/baybe/acquisition/__init__.py @@ -3,6 +3,7 @@ from baybe.acquisition.acqfs import ( ExpectedImprovement, LogExpectedImprovement, + MultiFidelityUpperConfidenceBound, PosteriorMean, PosteriorStandardDeviation, ProbabilityOfImprovement, @@ -13,6 +14,7 @@ qLogNoisyExpectedHypervolumeImprovement, qLogNoisyExpectedImprovement, qLogNParEGO, + qMultiFidelityKnowledgeGradient, qNegIntegratedPosteriorVariance, qNoisyExpectedHypervolumeImprovement, qNoisyExpectedImprovement, @@ -30,6 +32,7 @@ EI = ExpectedImprovement qEI = qExpectedImprovement qKG = qKnowledgeGradient +qMFKG = qMultiFidelityKnowledgeGradient LogEI = LogExpectedImprovement qLogEI = qLogExpectedImprovement qNEI = qNoisyExpectedImprovement @@ -38,6 +41,7 @@ PI = ProbabilityOfImprovement qPI = qProbabilityOfImprovement UCB = UpperConfidenceBound +MFUCB = MultiFidelityUpperConfidenceBound qUCB = qUpperConfidenceBound qTS = qThompsonSampling qNEHVI = qNoisyExpectedHypervolumeImprovement @@ -47,6 +51,7 @@ ######################### Acquisition functions # Knowledge Gradient "qKnowledgeGradient", + "qMultiFidelityKnowledgeGradient", # Posterior Statistics "PosteriorMean", "PosteriorStandardDeviation", @@ -67,6 +72,7 @@ # Upper Confidence Bound "UpperConfidenceBound", "qUpperConfidenceBound", + "MultiFidelityUpperConfidenceBound", # Thompson Sampling "qThompsonSampling", # Hypervolume Improvement @@ -77,6 +83,7 @@ ######################### Abbreviations # Knowledge Gradient "qKG", + "qMFKG", # Posterior Statistics "PM", "PSTD", @@ -97,6 +104,7 @@ # Upper Confidence Bound "UCB", "qUCB", + "MFUCB", # Thompson Sampling "qTS", # Hypervolume Improvement diff --git a/baybe/acquisition/_builder.py b/baybe/acquisition/_builder.py index 8c92185752..040b1ae0eb 100644 --- a/baybe/acquisition/_builder.py +++ b/baybe/acquisition/_builder.py @@ -23,11 +23,13 @@ _ExpectedHypervolumeImprovement, qExpectedHypervolumeImprovement, qLogExpectedHypervolumeImprovement, + qMultiFidelityKnowledgeGradient, qNegIntegratedPosteriorVariance, qThompsonSampling, ) from baybe.acquisition.base import AcquisitionFunction, _get_botorch_acqf_class -from baybe.acquisition.utils import make_partitioning +from baybe.acquisition.custom_acqfs import MultiFidelityUpperConfidenceBound +from baybe.acquisition.utils import make_MFUCB_dicts, make_partitioning from baybe.exceptions import ( IncompatibilityError, IncompleteMeasurementsError, @@ -75,16 +77,21 @@ class BotorchAcquisitionArgs: # Optional, depending on the specific acquisition function being used best_f: float | None = _OPT_FIELD beta: float | None = _OPT_FIELD + costs_dict: dict[Any, tuple[float, ...]] = _OPT_FIELD + current_value: Tensor | None = _OPT_FIELD + fidelities_dict: dict[Any, tuple[Any, ...]] = _OPT_FIELD maximize: bool | None = _OPT_FIELD mc_points: Tensor | None = _OPT_FIELD num_fantasies: int | None = _OPT_FIELD objective: MCAcquisitionObjective | None = _OPT_FIELD partitioning: BoxDecomposition | None = _OPT_FIELD posterior_transform: PosteriorTransform | None = _OPT_FIELD + project: Callable[[Tensor], Tensor] | None = _OPT_FIELD prune_baseline: bool | None = _OPT_FIELD ref_point: Tensor | None = _OPT_FIELD X_baseline: Tensor | None = _OPT_FIELD X_pending: Tensor | None = _OPT_FIELD + zetas_dict: dict[Any, tuple[float, ...]] = _OPT_FIELD def collect(self) -> dict[str, Any]: """Collect the assigned arguments into a dictionary.""" @@ -202,6 +209,9 @@ def build(self) -> BoAcquisitionFunction: self._set_mc_points() self._set_ref_point() self._set_partitioning() + self._set_current_value() + self._set_projection() + self._set_MFUCB_dicts() botorch_acqf = self._botorch_acqf_cls(**self._args.collect()) self.set_default_sample_shape(botorch_acqf) @@ -264,6 +274,81 @@ def _set_best_f(self) -> None: case _: raise NotImplementedError("This line should be impossible to reach.") + def _set_current_value(self) -> None: + """Set current value maximising posterior mean in qMFKG.""" + if not isinstance(self.acqf, qMultiFidelityKnowledgeGradient): + return + + from botorch.optim import optimize_acqf_mixed + + if isinstance(self.acqf, qMultiFidelityKnowledgeGradient): + from botorch.acquisition import PosteriorMean + from botorch.acquisition.fixed_feature import ( + FixedFeatureAcquisitionFunction, + ) + + curr_val_acqf = FixedFeatureAcquisitionFunction( + acq_function=PosteriorMean(self._botorch_surrogate), + d=len(self.searchspace.parameters), + columns=[ + self.searchspace.fidelity_idx, + ], + values=[ + 1.0, + ], + ) + + # Jordan MHS NOTE: This is fast-and-loose use of mixed space optimization. + # Changes will be made with the next PR which uses a notion of wrapped acqfs + # for setting a current value but also for defining cost aware wrappers. + + candidates_comp = self.searchspace.discrete.comp_rep + num_comp_columns = len(candidates_comp.columns) + candidates_comp.columns = list(range(num_comp_columns)) # type: ignore + candidates_comp_dict = candidates_comp.to_dict("records") + + # Possible TODO. Align num_restarts and raw_samples with that defined by the + # user for the main acquisition function. + _, current_value = optimize_acqf_mixed( + acq_function=curr_val_acqf, + bounds=torch.from_numpy(self.searchspace.comp_rep_bounds.values), + fixed_features_list=candidates_comp_dict, # type: ignore[arg-type] + q=1, + num_restarts=10, + raw_samples=64, + ) + + self._args.current_value = current_value + + def _set_projection(self) -> None: + """Set projection to the target fidelity for qMFKG.""" + if not isinstance(self.acqf, (qMultiFidelityKnowledgeGradient)): + return + + assert self.searchspace.fidelity_idx is not None # for mypy + + target_fidelities = {self.searchspace.fidelity_idx: 1.0} + + num_dims = len(self.searchspace.parameters) + + def target_fidelity_projection(X: Tensor) -> Tensor: + from botorch.acquisition.utils import project_to_target_fidelity + + return project_to_target_fidelity(X, target_fidelities, num_dims) + + self._args.project = target_fidelity_projection + + def _set_MFUCB_dicts(self) -> None: + """Set value, fidelities and cost dictionaries for MFUCB.""" + if not isinstance(self.acqf, MultiFidelityUpperConfidenceBound): + return + + fidelities_dict, costs_dict, zetas_dict = make_MFUCB_dicts(self.searchspace) + + self._args.fidelities_dict = fidelities_dict + self._args.costs_dict = costs_dict + self._args.zetas_dict = zetas_dict + def set_default_sample_shape(self, acqf: BoAcquisitionFunction, /): """Apply temporary workaround for Thompson sampling.""" # TODO: Needs redesign once bandits are supported more generally diff --git a/baybe/acquisition/acqfs.py b/baybe/acquisition/acqfs.py index 5e3b08b1cf..eb854d98b8 100644 --- a/baybe/acquisition/acqfs.py +++ b/baybe/acquisition/acqfs.py @@ -13,7 +13,7 @@ from attr.converters import optional as optional_c from attr.validators import optional as optional_v from attrs import AttrsInstance, define, field, fields -from attrs.validators import gt, instance_of, le +from attrs.validators import ge, gt, instance_of, le from typing_extensions import override from baybe.acquisition.base import AcquisitionFunction @@ -156,6 +156,22 @@ class qKnowledgeGradient(AcquisitionFunction): memory footprint and wall time.""" +@define(frozen=True) +class qMultiFidelityKnowledgeGradient(AcquisitionFunction): + """Monte Carlo based knowledge gradient. + + This acquisition function currently only supports purely continuous spaces. + """ + + abbreviation: ClassVar[str] = "qMFKG" + + num_fantasies: int = field(validator=[instance_of(int), gt(0)], default=128) + """Number of fantasies to draw for approximating the knowledge gradient. + + More samples result in a better approximation, at the expense of both increased + memory footprint and wall time.""" + + ######################################################################################## ### Posterior Statistics @define(frozen=True) @@ -289,6 +305,31 @@ class qUpperConfidenceBound(AcquisitionFunction): """See :paramref:`UpperConfidenceBound.beta`.""" +@define(frozen=True) +class MultiFidelityUpperConfidenceBound(AcquisitionFunction): + """Two stage acquisition function of Kandasamy et al (2016). + + Stage 1: Choose design features based on argmax_x (softmin_m (UCB_m(x) + zeta_m)). + + Stage 2: Choose cheapest fidelity satisfying a cost-aware informativeness threshold. + """ + + abbreviation: ClassVar[str] = "MFUCB" + + softmin_temperature: float = field( + converter=float, validator=[finite_float, ge(0.0)], default=1e-2 + ) + """Softmin smoothing parameter.""" + + beta: float = field(converter=float, validator=finite_float, default=0.2) + """See :paramref:`UpperConfidenceBound.beta`.""" + + @override + @classproperty + def supports_batching(cls) -> bool: + return False + + ######################################################################################## ### ThompsonSampling @define(frozen=True) diff --git a/baybe/acquisition/base.py b/baybe/acquisition/base.py index 115ef1d551..013337f494 100644 --- a/baybe/acquisition/base.py +++ b/baybe/acquisition/base.py @@ -165,11 +165,14 @@ def _get_botorch_acqf_class( """Extract the BoTorch acquisition class for the given BayBE acquisition class.""" import botorch + from baybe.acquisition import custom_acqfs + for cls in baybe_acqf_cls.mro(): if ( acqf_cls := getattr(botorch.acquisition, cls.__name__, False) or getattr(botorch.acquisition.multi_objective, cls.__name__, False) or getattr(botorch.acquisition.multi_objective.parego, cls.__name__, False) + or getattr(custom_acqfs, cls.__name__, False) ): if is_abstract(acqf_cls): continue diff --git a/baybe/acquisition/custom_acqfs/__init__.py b/baybe/acquisition/custom_acqfs/__init__.py new file mode 100644 index 0000000000..43a27e0c2c --- /dev/null +++ b/baybe/acquisition/custom_acqfs/__init__.py @@ -0,0 +1,10 @@ +"""Custom acquisition functions.""" + +from baybe.acquisition.custom_acqfs.two_stage import ( + MultiFidelityUpperConfidenceBound, +) + +__all__ = [ + # Multi fidelity acquisition functions + "MultiFidelityUpperConfidenceBound", +] diff --git a/baybe/acquisition/custom_acqfs/mfucb.py b/baybe/acquisition/custom_acqfs/mfucb.py new file mode 100644 index 0000000000..faf5e661a1 --- /dev/null +++ b/baybe/acquisition/custom_acqfs/mfucb.py @@ -0,0 +1,282 @@ +"""Custom Botorch AnalyticAcquisitionFunction for multi-fidelity optimization.""" + +from __future__ import annotations + +from collections.abc import Callable, Mapping +from itertools import pairwise as iter_pairwise +from itertools import product as iter_product +from typing import Any + +import torch +from attrs import Attribute, define, field, fields_dict +from attrs.validators import deep_iterable, deep_mapping, ge, instance_of, or_ +from botorch.acquisition.analytic import AnalyticAcquisitionFunction +from botorch.acquisition.objective import PosteriorTransform +from botorch.models.model import Model +from botorch.utils.transforms import ( + average_over_ensemble_models, + t_batch_mode_transform, +) +from gpytorch.likelihoods import GaussianLikelihood +from torch import Tensor +from typing_extensions import override + +from baybe.parameters.validation import validate_contains_exactly_one +from baybe.utils.validation import finite_float + +_neg_inv_sqrt2 = -0.7071067811865476 +_log_sqrt_pi_div_2 = 0.2257913526447274 + + +def validate_dict_shape( + reference_name: str, / +) -> Callable[[Any, Attribute, Mapping[Any, Any]], None]: + """Make validator to check attribute keys/lengths against a reference attribute.""" + + def validator(obj: Any, attribute: Attribute, value: Mapping[Any, Any]) -> None: # noqa: DOC101, DOC103 + """Validate that the input has the same keys/lengths as the reference attribute. + + Raises: + ValueError: If the keys of the two attributes mismatch. + ValueError: If the tuple lengths of the two attributes mismatch at any key. + """ + other_attr = fields_dict(type(obj))[reference_name] + other_instance = getattr(obj, reference_name) + + if not ( + different_keys := set(value.keys()).symmetric_difference( + set(other_instance.keys()) + ) + ): + raise ValueError( + f"{attribute.name} and {other_attr.alias} differ in keys in " + f"{obj.name}, with the following {different_keys} in only one." + ) + + for k, tup in value.items(): + other_tup = other_instance[k] + + if len(tup) != len(other_tup): + raise ValueError( + f"The lengths of the attributes '{other_attr.alias}' and " + f"'{attribute.alias}' do not match for '{obj.name}' at the key {k}." + f"Length of '{other_attr.alias}' at key {k}: {len(other_tup)}. " + f"Length of '{attribute.alias}' at key {k}: {len(tup)}." + ) + + return validator + + +@define +class MultiFidelityUpperConfidenceBound(AnalyticAcquisitionFunction): + r"""Two-stage Multi Fidelity Upper Confidence Bound (UCB). + + First stage selects the design parameter choice through a discrepancy-parameter + adjusted upper confidence bound. Selection is done by gradient-based optimization + of a softmin over each fidelity-adjusted UCB. + Second stage makes a cost-aware decision of the fidelity parameter to be queried, by + searching through each fidelity at the chosen design parameter, which balances cost + of querying with fidelity-specific UCB. + + Only supports the case of `q=1` (i.e. greedy, non-batch + selection of design points). The model must be single-outcome. + """ + + # Declaring attribute types for variables defined via _register_buffer. + fidelity_columns: Tensor + fidelity_combinations: Tensor + zetas_comb: Tensor + costs_comb: Tensor + + model: Model = field(validator=instance_of(Model)) + """A fitted single-outcome GP model.""" + + beta: float | Tensor = field(validator=or_(instance_of(float), instance_of(Tensor))) + """Trade-off parameter between mean and covariance.""" + + fidelities: dict[int, tuple[float, ...]] = field( + validator=deep_mapping( + key_validator=instance_of(int), + value_validator=deep_iterable( + member_validator=instance_of(float), + iterable_validator=instance_of(tuple), + ), + mapping_validator=instance_of(dict), + ) + ) + """Computational representation of fidelity values.""" + + costs: dict[int, tuple[float, ...]] = field( + validator=deep_mapping( + key_validator=instance_of(int), + value_validator=deep_iterable( + member_validator=(instance_of(float), ge(0.0)), + iterable_validator=( + instance_of(tuple), + validate_contains_exactly_one(0.0), + ), + ), + mapping_validator=(instance_of(dict), validate_dict_shape("fidelities")), + ) + ) + """Cost of querying each fidelity parameter at each fidelity. Costs between + fidelity parameters are summed. + """ + + zetas: dict[int, tuple[float, ...]] = field( + validator=deep_mapping( + key_validator=instance_of(int), + value_validator=deep_iterable( + member_validator=(instance_of(float), ge(0.0)), + iterable_validator=( + instance_of(tuple), + validate_contains_exactly_one(0.0), + ), + ), + mapping_validator=(instance_of(dict), validate_dict_shape("fidelities")), + ) + ) + """Maximum absolute discrepancy between each fidelity and the + highest fidelity output. + """ + + softmin_temperature: float = field( + converter=float, validator=[finite_float, ge(0.0)], default=1e-2 + ) + """Smoothing parameter for gradient-based optimization of the design.""" + + posterior_transform: PosteriorTransform | None = field(default=None) + """PosteriorTransform used to convert multi-output posteriors to + single-output posteriors if necessary. + """ + + maximize: bool = field(default=True) + """If True, treat the problem as a maximization problem.""" + + def __post_attrs_init__(self) -> None: + super().__init__(model=self.model, posterior_transform=self.posterior_transform) + + self.register_buffer("beta", torch.as_tensor(self.beta)) + + self.register_buffer( + "softmin_temperature", torch.as_tensor(self.softmin_temperature) + ) + + self.register_buffer( + "fidelity_columns", + torch.tensor(list(self.fidelities.keys()), dtype=torch.long), + ) + + self.register_buffer( + "fidelity_combinations", + torch.tensor( + list(iter_product(*self.fidelities.values())), dtype=torch.double + ), + ) + + self.register_buffer( + "zetas_comb", + torch.tensor(list(iter_product(*self.zetas.values())), dtype=torch.double), + ) + + self.register_buffer( + "costs_comb", + torch.tensor(list(iter_product(*self.costs.values())), dtype=torch.double), + ) + + # Jordan MHS NOTE: mypy typing errors for these decorators with on + # subclasses of AcquistionFunction appear in Botorch as well as here. + @override + @t_batch_mode_transform(expected_q=1) # type: ignore + @average_over_ensemble_models # type: ignore + def forward(self, X: Tensor) -> Tensor: + r"""First optimization stage: choose optimal design design to query. + + Args: + X: A `(b1 x ... bk) x 1 x d`-dim tensor of `d`-dim design/fidelity points. + + Returns: + A `(b1 x ... bk)`-dim tensor of Upper Confidence Bound values at the + given design and fidelity points `X`. + """ + batch_size, q, d = X.shape + + n_comb, k = self.fidelity_combinations.shape + + X_extended = X.clone().unsqueeze(1).repeat(1, n_comb, 1, 1) + X_extended[..., :, self.fidelity_columns] = self.fidelity_combinations.view( + 1, n_comb, 1, k + ) + + zetas_comb_sum = self.zetas_comb.sum(dim=-1) + zetas_comb_sum = zetas_comb_sum.view(1, n_comb, 1, 1) + zetas_extended = zetas_comb_sum.expand(batch_size, n_comb, q, 1) + + X_eval = X_extended.reshape(batch_size * n_comb, q, d) + means, sigmas = self._mean_and_sigma(X_eval) + + means = means.view(batch_size, n_comb, q, 1) + # Jordan MHS NOTE: typing workaround to ignore possibility for botorch + # AnalyticAcquisitionFunction _mean_and_sigma to have compute_sigma=False. + sigmas = sigmas.view(batch_size, n_comb, q, 1) # type: ignore + + sign = 1 if self.maximize else -1 + indiv_ucbs = sign * means + (self.beta**0.5) * sigmas + zetas_extended + + ucb_mins, _ = indiv_ucbs.min(dim=1, keepdim=True) + + T = self.softmin_temperature + + acq_values = ( + ( + -T + * torch.log(torch.sum(torch.exp(-(indiv_ucbs - ucb_mins) / T), dim=1)) + + ucb_mins.squeeze(-1) + ) + .squeeze(-1) + .squeeze(-1) + ) + + return acq_values + + def optimize_stage_two(self, X: Tensor) -> Tensor: + r"""Second optimisation stage: choose optimal fidelity to query.""" + if isinstance(self.model.likelihood, GaussianLikelihood): + aleatoric_uncertainty = torch.sqrt(self.model.likelihood.noise) + else: + aleatoric_uncertainty = torch.tensor(0.0) + + found_suitable_lower_fid = False + + total_costs_comb = self.costs_comb.sum(dim=-1) + increasing_cost_order = torch.argsort(total_costs_comb) + + for prev_i, curr_i in iter_pairwise(increasing_cost_order): + prev_fid = self.fidelity_combinations[prev_i].clone() + prev_cost = self.costs_comb.sum(dim=-1)[prev_i] + curr_cost = self.costs_comb.sum(dim=-1)[curr_i] + prev_zeta = self.zetas_comb.sum(dim=-1)[prev_i] + + X_prev_fid = X.clone() + X_prev_fid[:, self.fidelity_columns] = prev_fid + + _, prev_posterior_uncertainty = self._mean_and_sigma(X_prev_fid) + + # Jordan MHS NOTE: workaround poor typing in Botorch. + # _mean_and_sigma always returns two values unless the argument + # compute_sigma is set to False. + assert prev_posterior_uncertainty is not None, "This shouldn't be accesible" + + if (self.beta**0.5) * prev_posterior_uncertainty >= ( + aleatoric_uncertainty + prev_zeta + ) * torch.sqrt(prev_cost / curr_cost): + found_suitable_lower_fid = True + optimal_X = X_prev_fid + break + + if not found_suitable_lower_fid: + optimal_X = X.clone() + last_fid = self.fidelity_combinations[curr_i].clone() + optimal_X[:, self.fidelity_columns] = last_fid + + return optimal_X diff --git a/baybe/acquisition/utils.py b/baybe/acquisition/utils.py index 5c504fa389..e3c9eb40b3 100644 --- a/baybe/acquisition/utils.py +++ b/baybe/acquisition/utils.py @@ -2,9 +2,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from baybe.acquisition.base import AcquisitionFunction +from baybe.parameters import CategoricalFidelityParameter if TYPE_CHECKING: from botorch.utils.multi_objective.box_decompositions.box_decomposition import ( @@ -12,6 +13,8 @@ ) from torch import Tensor + from baybe.searchspace import SearchSpace + def str_to_acqf(name: str, /) -> AcquisitionFunction: """Create an ACQF object from a given ACQF name.""" @@ -82,3 +85,38 @@ def make_partitioning( return FastNondominatedPartitioning(ref_point=ref_point, Y=predictions) return NondominatedPartitioning(ref_point=ref_point, Y=predictions, alpha=alpha) + + +# Jordan MHS TODO: typing for fidelities_dict awkward since integer values in +# comp_df not explicitly typed. Seek help here. +def make_MFUCB_dicts( + searchspace: SearchSpace, / +) -> tuple[ + dict[Any, tuple[Any, ...]], + dict[int, tuple[float, ...]], + dict[int, tuple[float, ...]], +]: + """Construct column indices and values of costs, fidelities and values for MFUCB.""" + fidelity_params = ( + p for p in searchspace.parameters if isinstance(p, CategoricalFidelityParameter) + ) + + fidelities_dict = { + i: tuple(p.comp_df.iloc[:, 0]) for i, p in enumerate(fidelity_params) + } + + costs_dict = { + i: p.costs + if getattr(p, "costs", None) is not None + else tuple(0 for _ in p.values) + for i, p in enumerate(fidelity_params) + } + + zetas_dict = { + i: p.zeta + if getattr(p, "zeta", None) is not None + else tuple(0 for _ in p.values) + for i, p in enumerate(fidelity_params) + } + + return fidelities_dict, costs_dict, zetas_dict diff --git a/baybe/kernels/__init__.py b/baybe/kernels/__init__.py index 9323a2b631..4e50c5728c 100644 --- a/baybe/kernels/__init__.py +++ b/baybe/kernels/__init__.py @@ -5,6 +5,7 @@ """ from baybe.kernels.basic import ( + IndexKernel, LinearKernel, MaternKernel, PeriodicKernel, @@ -18,6 +19,7 @@ __all__ = [ "AdditiveKernel", + "IndexKernel", "LinearKernel", "MaternKernel", "PeriodicKernel", diff --git a/baybe/parameters/fidelity.py b/baybe/parameters/fidelity.py index 15235c9c78..f94e0cee1e 100644 --- a/baybe/parameters/fidelity.py +++ b/baybe/parameters/fidelity.py @@ -21,8 +21,8 @@ validate_is_finite, validate_unique_values, ) +from baybe.settings import active_settings from baybe.utils.conversion import nonstring_to_tuple -from baybe.utils.numerical import DTypeFloatNumpy def _convert_zeta( @@ -88,7 +88,7 @@ class CategoricalFidelityParameter(_DiscreteLabelLikeParameter): discrepancy ``zeta``, 2 * ``zeta``, and so on.""" def __attrs_post_init__(self) -> None: - """Sort attribute values according to lexographic fidelity values.""" + """Sort attribute values according to lexicographic fidelity values.""" # Because categories can be str or bool, we sort by (type, value) idx = sorted( range(len(self._values)), @@ -103,11 +103,35 @@ def __attrs_post_init__(self) -> None: def values(self) -> tuple[str | bool, ...]: return self._values + @property + def highest_fidelity(self) -> str: + """The fidelity with discrepancy value of zero.""" + highest_fid = next( + value for value, zeta in zip(self.values, self.zeta) if zeta == 0 + ) + + assert isinstance(highest_fid, str) # for mypy + + return highest_fid + + @property + def highest_fidelity_cost(self) -> int: + """Cost of querying the fidelity with discrepancy value of zero.""" + highest_cost = next( + cost for cost, zeta in zip(self.costs, self.zeta) if zeta == 0 + ) + + assert isinstance(highest_cost, int) # for mypy + + return highest_cost + @override @cached_property def comp_df(self) -> pd.DataFrame: return pd.DataFrame( - range(len(self.values)), dtype=DTypeFloatNumpy, columns=[self.name] + range(len(self.values)), + dtype=active_settings.DTypeFloatNumpy, + columns=[self.name], ) @@ -159,5 +183,7 @@ def values(self) -> tuple[float, ...]: @cached_property def comp_df(self) -> pd.DataFrame: return pd.DataFrame( - {self.name: self.values}, index=self.values, dtype=DTypeFloatNumpy + {self.name: self.values}, + index=self.values, + dtype=active_settings.DTypeFloatNumpy, ) diff --git a/baybe/recommenders/pure/bayesian/base.py b/baybe/recommenders/pure/bayesian/base.py index 4ac5c1eed2..a1b60bfdee 100644 --- a/baybe/recommenders/pure/bayesian/base.py +++ b/baybe/recommenders/pure/bayesian/base.py @@ -12,7 +12,7 @@ from attrs.converters import optional from typing_extensions import override -from baybe.acquisition import qLogEI, qLogNEHVI +from baybe.acquisition import MFUCB, qLogEI, qLogNEHVI, qMFKG from baybe.acquisition.base import AcquisitionFunction from baybe.acquisition.utils import convert_acqf from baybe.exceptions import ( @@ -20,7 +20,8 @@ ) from baybe.objectives.base import Objective from baybe.recommenders.pure.base import PureRecommender -from baybe.searchspace import SearchSpace +from baybe.recommenders.pure.bayesian.utils import restricted_fidelity_searchspace +from baybe.searchspace import SearchSpace, SearchSpaceTaskType from baybe.settings import Settings from baybe.surrogates import GaussianProcessSurrogate from baybe.surrogates.base import ( @@ -44,6 +45,13 @@ def _autoreplicate(surrogate: SurrogateProtocol, /) -> SurrogateProtocol: class BayesianRecommender(PureRecommender, ABC): """An abstract class for Bayesian Recommenders.""" + # TODO: Factory defaults the surrogate to a GaussianProcessesSurrogate always. + # Surrogate and kernel defaults should be different for searchspaces with + # CategoricalFidelityParameter or NumericalDiscreteFidelityParameter. + # This can be achieved without the user having to specify the surroagte model, + # e.g., by + # * using a dispatcher factory which decides surrogate model on fit time + # * having a "_setup_surrogate" method similar to the acquisition function logic _surrogate_model: SurrogateProtocol = field( alias="surrogate_model", factory=GaussianProcessSurrogate, @@ -80,9 +88,17 @@ def surrogate_model(self) -> SurrogateProtocol: ) return self._surrogate_model - def _get_acquisition_function(self, objective: Objective) -> AcquisitionFunction: + def _get_acquisition_function( + self, objective: Objective, searchspace: SearchSpace + ) -> AcquisitionFunction: """Select the appropriate default acquisition function for the given context.""" if self.acquisition_function is None: + if searchspace.task_type == SearchSpaceTaskType.NUMERICALFIDELITY: + return qMFKG() + + elif searchspace.task_type == SearchSpaceTaskType.CATEGORICALFIDELITY: + return MFUCB() + return qLogNEHVI() if objective.is_multi_output else qLogEI() return self.acquisition_function @@ -106,7 +122,7 @@ def _setup_botorch_acqf( ) -> None: """Create the acquisition function for the current training data.""" # noqa: E501 self._objective = objective - acqf = self._get_acquisition_function(objective) + acqf = self._get_acquisition_function(objective, searchspace) if objective.is_multi_output and not acqf.supports_multi_output: raise IncompatibleAcquisitionFunctionError( @@ -179,10 +195,14 @@ def recommend( self._setup_botorch_acqf( searchspace, objective, measurements, pending_experiments ) + acqf = self._get_acquisition_function(objective, searchspace) try: with Settings(preprocess_dataframes=False): - return super().recommend( + if isinstance(acqf, MFUCB): + searchspace = restricted_fidelity_searchspace(searchspace) + + recommendation = super().recommend( batch_size=batch_size, searchspace=searchspace, objective=objective, @@ -209,6 +229,12 @@ def recommend( else: raise + return ( + recommendation + if not isinstance(acqf, MFUCB) + else self._botorch_acqf.optimize_stage_two(recommendation) + ) + def acquisition_values( self, candidates: pd.DataFrame, @@ -238,7 +264,9 @@ def acquisition_values( A series of individual acquisition values, one for each candidate. """ surrogate = self.get_surrogate(searchspace, objective, measurements) - acqf = acquisition_function or self._get_acquisition_function(objective) + acqf = acquisition_function or self._get_acquisition_function( + objective, searchspace + ) return acqf.evaluate( candidates, surrogate, @@ -266,7 +294,9 @@ def joint_acquisition_value( # noqa: DOC101, DOC103 The joint acquisition value of the batch. """ surrogate = self.get_surrogate(searchspace, objective, measurements) - acqf = acquisition_function or self._get_acquisition_function(objective) + acqf = acquisition_function or self._get_acquisition_function( + objective, searchspace + ) return acqf.evaluate( candidates, surrogate, diff --git a/baybe/recommenders/pure/bayesian/botorch.py b/baybe/recommenders/pure/bayesian/botorch.py index 6224466d8e..7df0db8168 100644 --- a/baybe/recommenders/pure/bayesian/botorch.py +++ b/baybe/recommenders/pure/bayesian/botorch.py @@ -154,7 +154,8 @@ def _recommend_discrete( experimental representation. """ assert self._objective is not None - acqf = self._get_acquisition_function(self._objective) + searchspace = SearchSpace(discrete=subspace_discrete) + acqf = self._get_acquisition_function(self._objective, searchspace) if batch_size > 1 and not acqf.supports_batching: raise IncompatibleAcquisitionFunctionError( f"The '{self.__class__.__name__}' only works with Monte Carlo " @@ -209,10 +210,13 @@ def _recommend_continuous( Returns: A dataframe containing the recommendations as individual rows. """ + searchspace = SearchSpace(continuous=subspace_continuous) assert self._objective is not None if ( batch_size > 1 - and not self._get_acquisition_function(self._objective).supports_batching + and not self._get_acquisition_function( + self._objective, searchspace + ).supports_batching ): raise IncompatibleAcquisitionFunctionError( f"The '{self.__class__.__name__}' only works with Monte Carlo " @@ -436,7 +440,9 @@ def _recommend_hybrid( ) if ( batch_size > 1 - and not self._get_acquisition_function(self._objective).supports_batching + and not self._get_acquisition_function( + self._objective, searchspace + ).supports_batching ): raise IncompatibleAcquisitionFunctionError( f"The '{self.__class__.__name__}' only works with Monte Carlo " diff --git a/baybe/recommenders/pure/bayesian/utils.py b/baybe/recommenders/pure/bayesian/utils.py new file mode 100644 index 0000000000..be71e7014a --- /dev/null +++ b/baybe/recommenders/pure/bayesian/utils.py @@ -0,0 +1,31 @@ +"""Utils for Bayesian recommenders.""" + +from attrs import evolve + +from baybe.parameters import CategoricalFidelityParameter +from baybe.searchspace import SearchSpace + + +def restricted_fidelity_searchspace(searchspace: SearchSpace, /) -> SearchSpace: + """Evolve a multi-fidelity searchspace so the fidelity is fixed to the highest.""" + discrete_parameters_fixed_fidelities = tuple( + evolve( + p, + values=(p.highest_fidelity,), + costs=(p.highest_fidelity_cost,), + zeta=(0.0,), + ) + if isinstance(p, CategoricalFidelityParameter) + else p + for p in searchspace.discrete.parameters + ) + + discrete_subspace_fixed_fidelities = evolve( + searchspace.discrete, parameters=discrete_parameters_fixed_fidelities + ) + + fixed_fidelity_searchspace = evolve( + searchspace, discrete=discrete_subspace_fixed_fidelities + ) + + return fixed_fidelity_searchspace diff --git a/baybe/searchspace/__init__.py b/baybe/searchspace/__init__.py index d78f7fafee..3f5d61fa9f 100644 --- a/baybe/searchspace/__init__.py +++ b/baybe/searchspace/__init__.py @@ -3,6 +3,7 @@ from baybe.searchspace.continuous import SubspaceContinuous from baybe.searchspace.core import ( SearchSpace, + SearchSpaceTaskType, SearchSpaceType, validate_searchspace_from_config, ) @@ -11,6 +12,7 @@ __all__ = [ "validate_searchspace_from_config", "SearchSpace", + "SearchSpaceTaskType", "SearchSpaceType", "SubspaceDiscrete", "SubspaceContinuous", diff --git a/baybe/searchspace/core.py b/baybe/searchspace/core.py index 5510af704f..c2a47cbdd0 100644 --- a/baybe/searchspace/core.py +++ b/baybe/searchspace/core.py @@ -15,6 +15,10 @@ from baybe.constraints.base import Constraint from baybe.parameters import TaskParameter from baybe.parameters.base import Parameter +from baybe.parameters.fidelity import ( + CategoricalFidelityParameter, + NumericalDiscreteFidelityParameter, +) from baybe.searchspace.continuous import SubspaceContinuous from baybe.searchspace.discrete import ( MemorySize, @@ -48,6 +52,29 @@ class SearchSpaceType(Enum): """Flag for hybrid search spaces resp. compatibility with hybrid search spaces.""" +class SearchSpaceTaskType(Enum): + """Enum class for different types of task and/or fidelity subspaces.""" + + SINGLETASK = "SINGLETASK" + """Flag for search spaces with no task parameters.""" + + CATEGORICALTASK = "CATEGORICALTASK" + """Flag for search spaces with a categorical task parameter.""" + + NUMERICALFIDELITY = "NUMERICALFIDELITY" + """Flag for search spaces with a discrete numerical (ordered) fidelity parameter.""" + + CATEGORICALFIDELITY = "CATEGORICALFIDELITY" + """Flag for search spaces with a categorical (unordered) fidelity parameter.""" + + # TODO: Distinguish between multiple task parameter and mixed task parameter types. + # In future versions, multiple task/fidelity parameters may be allowed. For now, + # they are disallowed, whether the task-like parameters are different or the same + # class. + MULTIPLETASKPARAMETER = "MULTIPLETASKPARAMETER" + """Flag for search spaces with mixed task and fidelity parameters.""" + + @define class SearchSpace(SerialMixin): """Class for managing the overall search space. @@ -275,6 +302,24 @@ def task_idx(self) -> int | None: # --> Fix this when refactoring the data return cast(int, self.discrete.comp_rep.columns.get_loc(task_param.name)) + @property + def fidelity_idx(self) -> int | None: + """The column index of the task parameter in computational representation.""" + try: + # See TODO [16932] and TODO [11611] + fidelity_param = next( + p + for p in self.parameters + if isinstance( + p, + (CategoricalFidelityParameter, NumericalDiscreteFidelityParameter), + ) + ) + except StopIteration: + return None + + return cast(int, self.discrete.comp_rep.columns.get_loc(fidelity_param.name)) + @property def n_tasks(self) -> int: """The number of tasks encoded in the search space.""" @@ -287,6 +332,105 @@ def n_tasks(self) -> int: return 1 return len(task_param.values) + @property + def n_fidelities(self) -> int: + """The number of tasks encoded in the search space.""" + # See TODO [16932] + try: + fidelity_param = next( + p + for p in self.parameters + if isinstance( + p, + (CategoricalFidelityParameter, NumericalDiscreteFidelityParameter), + ) + ) + return len(fidelity_param.values) + + # When there are no fidelity parameters, we effectively have a single fidelity + except StopIteration: + return 1 + + @property + def n_task_dimensions(self) -> int: + """The number of task dimensions.""" + try: + # See TODO [16932] + fidelity_param = next( + p for p in self.parameters if isinstance(p, (TaskParameter,)) + ) + except StopIteration: + fidelity_param = None + + return 1 if fidelity_param is not None else 0 + + @property + def n_fidelity_dimensions(self) -> int: + """The number of fidelity dimensions.""" + try: + # See TODO [16932] + fidelity_param = next( + p + for p in self.parameters + if isinstance( + p, + (CategoricalFidelityParameter, NumericalDiscreteFidelityParameter), + ) + ) + except StopIteration: + fidelity_param = None + + return 1 if fidelity_param is not None else 0 + + @property + def task_type(self) -> SearchSpaceTaskType: + """Return the task type of the search space. + + Raises: + ValueError: If searchspace contains more than one task/fidelity parameter. + ValueError: An unrecognised fidelity parameter type is in SearchSpace. + """ + task_like_parameters = ( + TaskParameter, + CategoricalFidelityParameter, + NumericalDiscreteFidelityParameter, + ) + + n_task_like_parameters = sum( + isinstance(p, (task_like_parameters)) for p in self.parameters + ) + + if n_task_like_parameters == 0: + return SearchSpaceTaskType.SINGLETASK + elif n_task_like_parameters > 1: + # TODO: commute this validation further downstream. + # In case of user-defined custom models which allow for multiple task + # parameters, this should be later in recommender logic. + # * Should this be an IncompatibilityError? + raise ValueError( + "SearchSpace must not contain more than one task/fidelity parameter." + ) + return SearchSpaceTaskType.MULTIPLETASKPARAMETER + + if self.n_task_dimensions == 1: + return SearchSpaceTaskType.CATEGORICALTASK + + if self.n_fidelity_dimensions == 1: + n_categorical_fidelity_dims = sum( + isinstance(p, CategoricalFidelityParameter) for p in self.parameters + ) + if n_categorical_fidelity_dims == 1: + return SearchSpaceTaskType.CATEGORICALFIDELITY + + n_numerical_disc_fidelity_dims = sum( + isinstance(p, NumericalDiscreteFidelityParameter) + for p in self.parameters + ) + if n_numerical_disc_fidelity_dims == 1: + return SearchSpaceTaskType.NUMERICALFIDELITY + + raise RuntimeError("This line should be impossible to reach.") + def get_comp_rep_parameter_indices(self, name: str, /) -> tuple[int, ...]: """Find a parameter's column indices in the computational representation. diff --git a/baybe/surrogates/bandit.py b/baybe/surrogates/bandit.py index ad6563cc43..3437e2b945 100644 --- a/baybe/surrogates/bandit.py +++ b/baybe/surrogates/bandit.py @@ -32,6 +32,9 @@ class BetaBernoulliMultiArmedBanditSurrogate(Surrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + prior: BetaPrior = field(factory=lambda: BetaPrior(1, 1)) """The beta prior for the win rates of the bandit arms. Uniform by default.""" diff --git a/baybe/surrogates/base.py b/baybe/surrogates/base.py index 205e32f703..244b0320e5 100644 --- a/baybe/surrogates/base.py +++ b/baybe/surrogates/base.py @@ -86,6 +86,10 @@ class Surrogate(ABC, SurrogateProtocol, SerialMixin): """Class variable encoding whether or not the surrogate supports transfer learning.""" + supports_multi_fidelity: ClassVar[bool] + """Class variable encoding whether or not the surrogate supports multi fidelity + Bayesian optimization.""" + supports_multi_output: ClassVar[bool] = False """Class variable encoding whether or not the surrogate is multi-output compatible.""" @@ -428,6 +432,14 @@ def fit( f"support transfer learning." ) + # Check if multi fidelity capabilities are needed + if (searchspace.n_fidelities > 1) and (not self.supports_multi_fidelity): + raise ValueError( + f"The search space contains fidelity parameters but the selected " + f"surrogate model type ({self.__class__.__name__}) does not " + f"support multi fidelity Bayesian optimisation." + ) + # Block partial measurements handle_missing_values(measurements, [t.name for t in objective.targets]) @@ -472,6 +484,11 @@ def __str__(self) -> str: self.supports_transfer_learning, single_line=True, ), + to_string( + "Supports Multi Fidelity", + self.supports_multi_fidelity, + single_line=True, + ), ] return to_string(self.__class__.__name__, *fields) diff --git a/baybe/surrogates/custom.py b/baybe/surrogates/custom.py index 79c4c6ea1c..2b65b08a5f 100644 --- a/baybe/surrogates/custom.py +++ b/baybe/surrogates/custom.py @@ -70,6 +70,9 @@ class CustomONNXSurrogate(IndependentGaussianSurrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + onnx_input_name: str = field(validator=validators.instance_of(str)) """The input name used for constructing the ONNX str.""" diff --git a/baybe/surrogates/gaussian_process/components/kernel.py b/baybe/surrogates/gaussian_process/components/kernel.py index 1f038345ff..eb70ded893 100644 --- a/baybe/surrogates/gaussian_process/components/kernel.py +++ b/baybe/surrogates/gaussian_process/components/kernel.py @@ -10,6 +10,7 @@ from baybe.kernels.base import Kernel from baybe.kernels.composite import ProductKernel from baybe.parameters.categorical import TaskParameter +from baybe.parameters.fidelity import CategoricalFidelityParameter from baybe.parameters.selector import ( ParameterSelectorProtocol, TypeSelector, @@ -79,7 +80,15 @@ def _default_base_kernel_factory(self) -> KernelFactoryProtocol: BayBENumericalKernelFactory, ) - return BayBENumericalKernelFactory(TypeSelector((TaskParameter,), exclude=True)) + return BayBENumericalKernelFactory( + TypeSelector( + ( + TaskParameter, + CategoricalFidelityParameter, + ), + exclude=True, + ) + ) @task_kernel_factory.default def _default_task_kernel_factory(self) -> KernelFactoryProtocol: @@ -87,7 +96,14 @@ def _default_task_kernel_factory(self) -> KernelFactoryProtocol: BayBETaskKernelFactory, ) - return BayBETaskKernelFactory(TypeSelector((TaskParameter,))) + return BayBETaskKernelFactory( + TypeSelector( + ( + TaskParameter, + CategoricalFidelityParameter, + ) + ) + ) @override def __call__( diff --git a/baybe/surrogates/gaussian_process/core.py b/baybe/surrogates/gaussian_process/core.py index 617a4a247c..2c70a09151 100644 --- a/baybe/surrogates/gaussian_process/core.py +++ b/baybe/surrogates/gaussian_process/core.py @@ -52,6 +52,8 @@ from torch import Tensor +# TODO Jordan MHS: _ModelContext is used by fidelity surrogate models now so may deserve +# its own file. @define class _ModelContext: """Model context for :class:`GaussianProcessSurrogate`.""" @@ -80,6 +82,27 @@ def n_tasks(self) -> int: """The number of tasks.""" return self.searchspace.n_tasks + @property + def n_fidelity_dimensions(self) -> int: + """The number of fidelity dimensions.""" + # Possible TODO: Generalize to multiple fidelity dimensions + return 1 if self.searchspace.fidelity_idx is not None else 0 + + @property + def is_multi_fidelity(self) -> bool: + """Are there any fidelity dimensions?""" + return self.n_fidelity_dimensions > 0 + + @property + def fidelity_idx(self) -> int | None: + """The computational column index of the task parameter, if available.""" + return self.searchspace.fidelity_idx + + @property + def n_fidelities(self) -> int: + """The number of fidelities.""" + return self.searchspace.n_fidelities + @property def parameter_bounds(self) -> Tensor: """Get the search space parameter bounds in BoTorch Format.""" @@ -93,7 +116,7 @@ def numerical_indices(self) -> list[int]: return [ i for i in range(len(self.searchspace.comp_rep_columns)) - if i != self.task_idx + if i not in (self.task_idx, self.fidelity_idx) ] diff --git a/baybe/surrogates/gaussian_process/multi_fidelity.py b/baybe/surrogates/gaussian_process/multi_fidelity.py new file mode 100644 index 0000000000..2cdca81801 --- /dev/null +++ b/baybe/surrogates/gaussian_process/multi_fidelity.py @@ -0,0 +1,114 @@ +"""Multi-fidelity Gaussian process surrogates.""" + +from __future__ import annotations + +import gc +from typing import TYPE_CHECKING, ClassVar + +from attrs import define, field +from typing_extensions import override + +from baybe.parameters.base import Parameter +from baybe.surrogates.base import Surrogate +from baybe.surrogates.gaussian_process.core import ( + _ModelContext, +) +from baybe.surrogates.gaussian_process.presets.core import ( + GaussianProcessPreset, + make_gp_from_preset, +) + +if TYPE_CHECKING: + from botorch.models.gpytorch import GPyTorchModel + from botorch.models.transforms.input import InputTransform + from botorch.models.transforms.outcome import OutcomeTransform + from botorch.posteriors import Posterior + from torch import Tensor + + +@define +class GaussianProcessSurrogateSTMF(Surrogate): + """Botorch's single task multi fidelity Gaussian process.""" + + supports_transfer_learning: ClassVar[bool] = False + # See base class. + + supports_multi_fidelity: ClassVar[bool] = True + # See base class. + + # TODO: type should be Optional[botorch.models.SingleTaskGP] but is currently + # omitted due to: https://github.com/python-attrs/cattrs/issues/531 + _model = field(init=False, default=None, eq=False) + """The actual model.""" + + @staticmethod + def from_preset(preset: GaussianProcessPreset) -> Surrogate: + """Create a Gaussian process surrogate from one of the defined presets.""" + return make_gp_from_preset(preset) + + @override + def to_botorch(self) -> GPyTorchModel: + return self._model + + @override + @staticmethod + def _make_parameter_scaler_factory( + parameter: Parameter, + ) -> type[InputTransform] | None: + # For GPs, we let botorch handle the scaling. See [Scaling Workaround] above. + return None + + @override + @staticmethod + def _make_target_scaler_factory() -> type[OutcomeTransform] | None: + # For GPs, we let botorch handle the scaling. See [Scaling Workaround] above. + return None + + @override + def _posterior(self, candidates_comp_scaled: Tensor, /) -> Posterior: + return self._model.posterior(candidates_comp_scaled) + + @override + def _fit(self, train_x: Tensor, train_y: Tensor) -> None: + import botorch + import gpytorch + + assert self._searchspace is not None + + context = _ModelContext(self._searchspace) + + assert context.is_multi_fidelity, ( + "GaussianProcessSurrogateSTMF can only " + "be fit on multi fidelity searchspaces." + ) + + # For GPs, we let botorch handle the scaling. See [Scaling Workaround] above. + input_transform = botorch.models.transforms.Normalize( # type: ignore[attr-defined] + train_x.shape[-1], + bounds=context.parameter_bounds, + indices=context.numerical_indices, + ) + outcome_transform = botorch.models.transforms.Standardize(train_y.shape[-1]) # type: ignore[attr-defined] + + # construct and fit the Gaussian process + self._model = botorch.models.SingleTaskMultiFidelityGP( + train_x, + train_y, + input_transform=input_transform, + outcome_transform=outcome_transform, + data_fidelities=None + if context.fidelity_idx is None + else (context.fidelity_idx,), + ) + + mll = gpytorch.ExactMarginalLogLikelihood(self._model.likelihood, self._model) + + botorch.fit.fit_gpytorch_mll(mll) + + @override + def __str__(self) -> str: + return "SingleTaskMultiFidelityGP with Botorch defaults." + + +# Collect leftover original slotted classes processed by `attrs.define` +gc.collect() diff --git a/baybe/surrogates/gaussian_process/presets/core.py b/baybe/surrogates/gaussian_process/presets/core.py index ad77df0b4d..5f659c0e01 100644 --- a/baybe/surrogates/gaussian_process/presets/core.py +++ b/baybe/surrogates/gaussian_process/presets/core.py @@ -3,6 +3,10 @@ from __future__ import annotations from enum import Enum +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from baybe.surrogates.base import Surrogate class GaussianProcessPreset(Enum): @@ -16,3 +20,20 @@ class GaussianProcessPreset(Enum): EDBO_SMOOTHED = "EDBO_SMOOTHED" """A smoothed version of the EDBO settings.""" + + BOTORCH_STMF = "BOTORCH_STMF" + """Recreates the default settings of the BOTORCH SingleTaskMultiFidelityGP.""" + + +def make_gp_from_preset(preset: GaussianProcessPreset) -> Surrogate: + """Create a :class:`GaussianProcessSurrogate` from a :class:`GaussianProcessPreset.""" # noqa: E501 + from baybe.surrogates.gaussian_process.multi_fidelity import ( + GaussianProcessSurrogateSTMF, + ) + + if preset is GaussianProcessPreset.BOTORCH_STMF: + return GaussianProcessSurrogateSTMF() + + raise ValueError( + f"Unknown '{GaussianProcessPreset.__name__}' with name '{preset.name}'." + ) diff --git a/baybe/surrogates/linear.py b/baybe/surrogates/linear.py index ba1fed5b2c..94746a16b9 100644 --- a/baybe/surrogates/linear.py +++ b/baybe/surrogates/linear.py @@ -44,6 +44,9 @@ class BayesianLinearSurrogate(IndependentGaussianSurrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + model_params: _ARDRegressionParams = field( factory=dict, converter=dict, diff --git a/baybe/surrogates/naive.py b/baybe/surrogates/naive.py index 3912c6b128..b407b48f08 100644 --- a/baybe/surrogates/naive.py +++ b/baybe/surrogates/naive.py @@ -26,6 +26,9 @@ class MeanPredictionSurrogate(IndependentGaussianSurrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + _model: float | None = field(init=False, default=None, eq=False) """The estimated posterior mean value of the training targets.""" diff --git a/baybe/surrogates/ngboost.py b/baybe/surrogates/ngboost.py index 6cb1cee135..f05a9ebc0d 100644 --- a/baybe/surrogates/ngboost.py +++ b/baybe/surrogates/ngboost.py @@ -49,6 +49,9 @@ class NGBoostSurrogate(IndependentGaussianSurrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + _default_model_params: ClassVar[dict] = {"n_estimators": 25, "verbose": False} """Class variable encoding the default model parameters.""" diff --git a/baybe/surrogates/random_forest.py b/baybe/surrogates/random_forest.py index 91ad599bb1..6ee1f7ed70 100644 --- a/baybe/surrogates/random_forest.py +++ b/baybe/surrogates/random_forest.py @@ -64,6 +64,9 @@ class RandomForestSurrogate(Surrogate): supports_transfer_learning: ClassVar[bool] = False # See base class. + supports_multi_fidelity: ClassVar[bool] = False + # See base class. + model_params: _RandomForestRegressorParams = field( factory=dict, converter=dict,