|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +r"""Abstract base module for multi-output acquisition functions.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from abc import ABC, abstractmethod |
| 12 | + |
| 13 | +import torch |
| 14 | +from botorch.acquisition.acquisition import AcquisitionFunction |
| 15 | +from botorch.exceptions.errors import UnsupportedError |
| 16 | +from botorch.models.model import Model |
| 17 | +from botorch.utils.transforms import ( |
| 18 | + average_over_ensemble_models, |
| 19 | + t_batch_mode_transform, |
| 20 | +) |
| 21 | +from torch import Tensor |
| 22 | + |
| 23 | + |
| 24 | +class MultiOutputAcquisitionFunction(AcquisitionFunction, ABC): |
| 25 | + r"""Abstract base class for multi-output acquisition functions. |
| 26 | +
|
| 27 | + These are intended to be optimized with a multi-objective optimizer (e.g. |
| 28 | + NSGA-II). |
| 29 | + """ |
| 30 | + |
| 31 | + @abstractmethod |
| 32 | + def forward(self, X: Tensor) -> Tensor: |
| 33 | + r"""Evaluate the acquisition function on the candidate set X. |
| 34 | +
|
| 35 | + Args: |
| 36 | + X: A `(b) x q x d`-dim Tensor of `(b)` t-batches with `q` `d`-dim |
| 37 | + design points each. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + A `(b) x m`-dim Tensor of acquisition function values at the given |
| 41 | + design points `X`. |
| 42 | + """ |
| 43 | + |
| 44 | + def set_X_pending(self, X_pending: Tensor | None) -> None: |
| 45 | + r"""Set the pending points. |
| 46 | +
|
| 47 | + Args: |
| 48 | + X_pending: A `batch_shape x m x d`-dim Tensor of `m` design points that |
| 49 | + have points that have been submitted for function evaluation |
| 50 | + (but may not yet have been evaluated). |
| 51 | + """ |
| 52 | + raise UnsupportedError( |
| 53 | + "X_pending is not supported for multi-output acquisition functions." |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class MultiOutputPosteriorMean(MultiOutputAcquisitionFunction): |
| 58 | + def __init__(self, model: Model, weights: Tensor | None = None) -> None: |
| 59 | + r"""Constructor for the MultiPosteriorMean. |
| 60 | +
|
| 61 | + Maximization of all outputs is assumed by default. Minimizing outputs can |
| 62 | + be achieved by setting the corresponding weights to negative. |
| 63 | +
|
| 64 | + Args: |
| 65 | + acqfs: A list of `m` acquisition functions. |
| 66 | + weights: A one-dimensional tensor with `m` elements representing the |
| 67 | + weights on the outputs. |
| 68 | + """ |
| 69 | + super().__init__(model=model) |
| 70 | + if self.model.num_outputs < 2: |
| 71 | + raise NotImplementedError( |
| 72 | + "MultiPosteriorMean only supports multi-output models." |
| 73 | + ) |
| 74 | + # TODO: this could be done via a posterior transform |
| 75 | + if weights is not None and weights.shape[0] != self.model.num_outputs: |
| 76 | + raise ValueError( |
| 77 | + f"weights must have {self.model.num_outputs} elements, but got" |
| 78 | + f" {weights.shape[0]}." |
| 79 | + ) |
| 80 | + self.register_buffer("weights", weights) |
| 81 | + |
| 82 | + @t_batch_mode_transform(expected_q=1, assert_output_shape=False) |
| 83 | + @average_over_ensemble_models |
| 84 | + def forward(self, X: Tensor) -> Tensor: |
| 85 | + r"""Evaluate the acquisition function on the candidate set X. |
| 86 | +
|
| 87 | + Args: |
| 88 | + X: A `(b) x q x d`-dim Tensor of `(b)` t-batches with `q` `d`-dim |
| 89 | + design points each. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + A `(b) x m`-dim Tensor of acquisition function values at the given |
| 93 | + design points `X`. |
| 94 | + """ |
| 95 | + mean = self.model.posterior(X).mean.squeeze(-2) |
| 96 | + if self.weights is not None: |
| 97 | + return mean * self.weights |
| 98 | + return mean |
| 99 | + |
| 100 | + |
| 101 | +class MultiOutputAcquisitionFunctionWrapper(MultiOutputAcquisitionFunction): |
| 102 | + r"""Multi-output wrapper around single-output acquisition functions.""" |
| 103 | + |
| 104 | + def __init__(self, acqfs: list[AcquisitionFunction]) -> None: |
| 105 | + r"""Constructor for the AcquisitionFunction base class. |
| 106 | +
|
| 107 | + Args: |
| 108 | + acqfs: A list of `m` acquisition functions. |
| 109 | + """ |
| 110 | + # We could set the model to be an ensemble model consistent of the |
| 111 | + # model used in each acqf |
| 112 | + super().__init__(model=acqfs[0].model) |
| 113 | + self.acqfs: list[AcquisitionFunction] = acqfs |
| 114 | + |
| 115 | + def forward(self, X: Tensor) -> Tensor: |
| 116 | + r"""Evaluate the acquisition function on the candidate set X. |
| 117 | +
|
| 118 | + Args: |
| 119 | + X: A `(b) x q x d`-dim Tensor of `(b)` t-batches with `q` `d`-dim |
| 120 | + design points each. |
| 121 | +
|
| 122 | + Returns: |
| 123 | + A `(b) x m`-dim Tensor of acquisition function values at the given |
| 124 | + design points `X`. |
| 125 | + """ |
| 126 | + return torch.stack([acqf(X) for acqf in self.acqfs], dim=-1) |
0 commit comments