|
19 | 19 | )
|
20 | 20 | from botorch.generation.sampling import (
|
21 | 21 | BoltzmannSampling,
|
| 22 | + ConstrainedMaxPosteriorSampling, |
22 | 23 | MaxPosteriorSampling,
|
23 | 24 | SamplingStrategy,
|
24 | 25 | )
|
| 26 | +from botorch.models import SingleTaskGP |
| 27 | +from botorch.models.model_list_gp_regression import ModelListGP |
25 | 28 | from botorch.utils.testing import BotorchTestCase, MockModel, MockPosterior
|
26 | 29 |
|
27 | 30 |
|
@@ -190,3 +193,130 @@ def test_boltzmann_sampling(self):
|
190 | 193 | BS = BoltzmannSampling(acqf, eta=10.0)
|
191 | 194 | samples = BS(X, num_samples=1)
|
192 | 195 | self.assertTrue(torch.equal(samples, X[max_idx, :]))
|
| 196 | + |
| 197 | + |
| 198 | +class TestConstrainedMaxPosteriorSampling(BotorchTestCase): |
| 199 | + def test_init(self): |
| 200 | + mm = MockModel(MockPosterior(mean=None)) |
| 201 | + cmms = MockModel(MockPosterior(mean=None)) |
| 202 | + MPS = ConstrainedMaxPosteriorSampling(mm, cmms) |
| 203 | + self.assertEqual(MPS.model, mm) |
| 204 | + self.assertTrue(MPS.replacement) |
| 205 | + self.assertIsInstance(MPS.objective, IdentityMCObjective) |
| 206 | + obj = LinearMCObjective(torch.rand(2)) |
| 207 | + MPS = ConstrainedMaxPosteriorSampling( |
| 208 | + mm, cmms, objective=obj, replacement=False |
| 209 | + ) |
| 210 | + self.assertEqual(MPS.objective, obj) |
| 211 | + self.assertFalse(MPS.replacement) |
| 212 | + |
| 213 | + def test_constrained_max_posterior_sampling(self): |
| 214 | + batch_shapes = (torch.Size(), torch.Size([3]), torch.Size([3, 2])) |
| 215 | + dtypes = (torch.float, torch.double) |
| 216 | + for batch_shape, dtype, N, num_samples, d in itertools.product( |
| 217 | + batch_shapes, dtypes, (5, 6), (1, 2), (1, 2) |
| 218 | + ): |
| 219 | + tkwargs = {"device": self.device, "dtype": dtype} |
| 220 | + # X is `batch_shape x N x d` = batch_shape x N x 1. |
| 221 | + X = torch.randn(*batch_shape, N, d, **tkwargs) |
| 222 | + # the event shape is `num_samples x batch_shape x N x m` |
| 223 | + psamples = torch.zeros(num_samples, *batch_shape, N, 1, **tkwargs) |
| 224 | + psamples[..., 0, :] = 1.0 |
| 225 | + |
| 226 | + # IdentityMCObjective, with replacement |
| 227 | + with mock.patch.object(MockPosterior, "rsample", return_value=psamples): |
| 228 | + mp = MockPosterior(None) |
| 229 | + with mock.patch.object(MockModel, "posterior", return_value=mp): |
| 230 | + mm = MockModel(None) |
| 231 | + c_model1 = SingleTaskGP(X, torch.randn(X.shape[0:-1]).unsqueeze(-1)) |
| 232 | + c_model2 = SingleTaskGP(X, torch.randn(X.shape[0:-1]).unsqueeze(-1)) |
| 233 | + c_model3 = SingleTaskGP(X, torch.randn(X.shape[0:-1]).unsqueeze(-1)) |
| 234 | + cmms1 = MockModel(MockPosterior(mean=None)) |
| 235 | + cmms2 = ModelListGP(c_model1, c_model2) |
| 236 | + cmms3 = ModelListGP(c_model1, c_model2, c_model3) |
| 237 | + for cmms in [cmms1, cmms2, cmms3]: |
| 238 | + MPS = ConstrainedMaxPosteriorSampling(mm, cmms) |
| 239 | + s1 = MPS(X, num_samples=num_samples) |
| 240 | + # run again with minimize_constraints_only |
| 241 | + MPS = ConstrainedMaxPosteriorSampling( |
| 242 | + mm, cmms, minimize_constraints_only=True |
| 243 | + ) |
| 244 | + s2 = MPS(X, num_samples=num_samples) |
| 245 | + assert s1.shape == s2.shape |
| 246 | + |
| 247 | + # ScalarizedObjective, with replacement |
| 248 | + with mock.patch.object(MockPosterior, "rsample", return_value=psamples): |
| 249 | + mp = MockPosterior(None) |
| 250 | + with mock.patch.object(MockModel, "posterior", return_value=mp): |
| 251 | + mm = MockModel(None) |
| 252 | + cmms = MockModel(None) |
| 253 | + with mock.patch.object( |
| 254 | + ScalarizedObjective, "forward", return_value=mp |
| 255 | + ): |
| 256 | + obj = ScalarizedObjective(torch.rand(2, **tkwargs)) |
| 257 | + MPS = ConstrainedMaxPosteriorSampling(mm, cmms, objective=obj) |
| 258 | + s = MPS(X, num_samples=num_samples) |
| 259 | + self.assertTrue(s.shape[-2] == num_samples) |
| 260 | + |
| 261 | + # ScalarizedPosteriorTransform w/ replacement |
| 262 | + with mock.patch.object(MockPosterior, "rsample", return_value=psamples): |
| 263 | + mp = MockPosterior(None) |
| 264 | + with mock.patch.object(MockModel, "posterior", return_value=mp): |
| 265 | + mm = MockModel(None) |
| 266 | + cmms = MockModel(None) |
| 267 | + with mock.patch.object( |
| 268 | + ScalarizedPosteriorTransform, "forward", return_value=mp |
| 269 | + ): |
| 270 | + post_tf = ScalarizedPosteriorTransform(torch.rand(2, **tkwargs)) |
| 271 | + MPS = ConstrainedMaxPosteriorSampling( |
| 272 | + mm, cmms, posterior_transform=post_tf |
| 273 | + ) |
| 274 | + s = MPS(X, num_samples=num_samples) |
| 275 | + self.assertTrue(s.shape[-2] == num_samples) |
| 276 | + |
| 277 | + # ScalarizedPosteriorTransform and Scalarized obj |
| 278 | + mp = MockPosterior(None) |
| 279 | + mm = MockModel(posterior=mp) |
| 280 | + mp = MockPosterior(None) |
| 281 | + cmms = MockModel(posterior=mp) |
| 282 | + obj = ScalarizedObjective(torch.rand(2, **tkwargs)) |
| 283 | + post_tf = ScalarizedPosteriorTransform(torch.rand(2, **tkwargs)) |
| 284 | + with self.assertRaises(RuntimeError): |
| 285 | + ConstrainedMaxPosteriorSampling( |
| 286 | + mm, cmms, posterior_transform=post_tf, objective=obj |
| 287 | + ) |
| 288 | + |
| 289 | + # without replacement |
| 290 | + psamples[..., 1, 0] = 1e-6 |
| 291 | + with mock.patch.object(MockPosterior, "rsample", return_value=psamples): |
| 292 | + mp = MockPosterior(None) |
| 293 | + with mock.patch.object(MockModel, "posterior", return_value=mp): |
| 294 | + mm = MockModel(None) |
| 295 | + cmms = MockModel(None) |
| 296 | + MPS = ConstrainedMaxPosteriorSampling(mm, cmms, replacement=False) |
| 297 | + if len(batch_shape) > 1: |
| 298 | + with self.assertRaises(NotImplementedError): |
| 299 | + MPS(X, num_samples=num_samples) |
| 300 | + else: |
| 301 | + s = MPS(X, num_samples=num_samples) |
| 302 | + self.assertTrue(s.shape[-2] == num_samples) |
| 303 | + |
| 304 | + # ScalarizedMCObjective, without replacement |
| 305 | + with mock.patch.object(MockPosterior, "rsample", return_value=psamples): |
| 306 | + mp = MockPosterior(None) |
| 307 | + with mock.patch.object(MockModel, "posterior", return_value=mp): |
| 308 | + mm = MockModel(None) |
| 309 | + cmms = MockModel(None) |
| 310 | + with mock.patch.object( |
| 311 | + ScalarizedObjective, "forward", return_value=mp |
| 312 | + ): |
| 313 | + obj = ScalarizedObjective(torch.rand(2, **tkwargs)) |
| 314 | + MPS = ConstrainedMaxPosteriorSampling( |
| 315 | + mm, cmms, objective=obj, replacement=False |
| 316 | + ) |
| 317 | + if len(batch_shape) > 1: |
| 318 | + with self.assertRaises(NotImplementedError): |
| 319 | + MPS(X, num_samples=num_samples) |
| 320 | + else: |
| 321 | + s = MPS(X, num_samples=num_samples) |
| 322 | + self.assertTrue(s.shape[-2] == num_samples) |
0 commit comments