|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from typing import Callable, List, Optional |
| 4 | + |
| 5 | +import torch |
| 6 | +from gpytorch import Module |
| 7 | + |
| 8 | +from .functional import ( |
| 9 | + batch_expected_improvement, |
| 10 | + batch_probability_of_improvement, |
| 11 | + batch_simple_regret, |
| 12 | + batch_upper_confidence_bound, |
| 13 | +) |
| 14 | +from .modules import AcquisitionFunction |
| 15 | + |
| 16 | + |
| 17 | +""" |
| 18 | +Wraps the batch acquisition functions defined in botorch.acquisition.functional |
| 19 | +into BatchAcquisitionFunction gpytorch modules. |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +class BatchAcquisitionFunction(AcquisitionFunction): |
| 24 | + def forward(self, candidate_set: torch.Tensor) -> torch.Tensor: |
| 25 | + """Takes in a `b x q x d` candidate_set Tensor of `b` t-batches with `q` |
| 26 | + `d`-dimensional design points each, and returns a one-dimensional Tensor |
| 27 | + with `b` elements.""" |
| 28 | + raise NotImplementedError("BatchAcquisitionFunction cannot be used directly") |
| 29 | + |
| 30 | + |
| 31 | +class qExpectedImprovement(BatchAcquisitionFunction): |
| 32 | + """TODO""" |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + model: Module, |
| 37 | + best_f: float, |
| 38 | + objective: Callable[[torch.Tensor], torch.Tensor] = lambda Y: Y, |
| 39 | + constraints: Optional[List[Callable[[torch.Tensor], torch.Tensor]]] = None, |
| 40 | + mc_samples: int = 5000, |
| 41 | + ) -> None: |
| 42 | + super(qExpectedImprovement, self).__init__(model) |
| 43 | + self.best_f = best_f |
| 44 | + self.objective = objective |
| 45 | + self.constraints = constraints |
| 46 | + self.mc_samples = mc_samples |
| 47 | + |
| 48 | + def forward(self, candidate_set: torch.Tensor) -> torch.Tensor: |
| 49 | + return batch_expected_improvement( |
| 50 | + X=candidate_set, |
| 51 | + model=self.model, |
| 52 | + best_f=self.best_f, |
| 53 | + objective=self.objective, |
| 54 | + constraints=self.constraints, |
| 55 | + mc_samples=self.mc_samples, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +class qProbabilityOfImprovement(BatchAcquisitionFunction): |
| 60 | + """TODO""" |
| 61 | + |
| 62 | + def __init__(self, model: Module, best_f: float, mc_samples: int = 5000) -> None: |
| 63 | + super(qProbabilityOfImprovement, self).__init__(model) |
| 64 | + self.best_f = best_f |
| 65 | + self.mc_samples = mc_samples |
| 66 | + |
| 67 | + def forward(self, candidate_set: torch.Tensor) -> torch.Tensor: |
| 68 | + return batch_probability_of_improvement( |
| 69 | + X=candidate_set, |
| 70 | + model=self.model, |
| 71 | + best_f=self.best_f, |
| 72 | + mc_samples=self.mc_samples, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +class qUpperConfidenceBound(BatchAcquisitionFunction): |
| 77 | + """TODO""" |
| 78 | + |
| 79 | + def __init__(self, model: Module, beta: float, mc_samples: int = 5000) -> None: |
| 80 | + super(qUpperConfidenceBound, self).__init__(model) |
| 81 | + self.beta = beta |
| 82 | + self.mc_samples = mc_samples |
| 83 | + |
| 84 | + def forward(self, candidate_set: torch.Tensor) -> torch.Tensor: |
| 85 | + return batch_upper_confidence_bound( |
| 86 | + X=candidate_set, |
| 87 | + model=self.model, |
| 88 | + beta=self.beta, |
| 89 | + mc_samples=self.mc_samples, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +class qSimpleRegret(BatchAcquisitionFunction): |
| 94 | + """TODO""" |
| 95 | + |
| 96 | + def __init__(self, model: Module, mc_samples: int = 5000) -> None: |
| 97 | + super(qSimpleRegret, self).__init__(model) |
| 98 | + self.mc_samples = mc_samples |
| 99 | + |
| 100 | + def forward(self, candidate_set: torch.Tensor) -> torch.Tensor: |
| 101 | + return batch_simple_regret( |
| 102 | + X=candidate_set, model=self.model, mc_samples=self.mc_samples |
| 103 | + ) |
0 commit comments