code: def test_optimize_posterior_samples(self):
      # Restrict the random seed to prevent flaky failures.
      seed = torch.randint(high=5, size=(1,)).item()
      torch.manual_seed(seed)
      dims = 2
      dtype = torch.float64
      eps = 1e-6
      for_testing_speed_kwargs = {
          "raw_samples": 512,
          "num_restarts": 10,
      }
      nums_optima = (1, 7)
      batch_shapes = ((), (3,), (5, 2))
      for num_optima, batch_shape in itertools.product(
          nums_optima, batch_shapes
      ):
          bounds = torch.tensor([[0, 1]] * dims, dtype=dtype).T
          X = torch.rand(*batch_shape, 13, dims, dtype=dtype)
          Y = torch.pow(X - 0.5, 2).sum(dim=-1, keepdim=True)
  
          # having a noiseless model all but guarantees that the found optima
          # will be better than the observations
          model = SingleTaskGP(X, Y, torch.full_like(Y, eps))
          paths = draw_matheron_paths(
              model=model, sample_shape=torch.Size([num_optima])
          )
          X_opt, f_opt = optimize_posterior_samples(
              paths, bounds, **for_testing_speed_kwargs
          )
  
          correct_X_shape = (num_optima,) + batch_shape + (dims,)
          correct_f_shape = (num_optima,) + batch_shape + (1,)
  
          self.assertEqual(X_opt.shape, correct_X_shape)
          self.assertEqual(f_opt.shape, correct_f_shape)
          self.assertTrue(torch.all(X_opt >= bounds[0]))
          self.assertTrue(torch.all(X_opt <= bounds[1]))
  
          # Check that the all found optima are larger than the observations
          # This is not 100% deterministic, but just about.
          self.assertTrue(torch.all((f_opt > Y.max(dim=-2).values)))
  
imports: from __future__ import annotations
  import itertools
  import warnings
  from typing import Any, Dict, Type
  from unittest import mock
  import numpy
  import torch
  from botorch.exceptions.errors import BotorchError
  from botorch.models.gp_regression import SingleTaskGP
  from botorch.sampling.pathwise import draw_matheron_paths
  from botorch.utils.sampling import _convert_bounds_to_inequality_constraints, batched_multinomial, DelaunayPolytopeSampler, draw_sobol_samples, find_interior_point, get_polytope_samples, HitAndRunPolytopeSampler, manual_seed, normalize_linear_constraints, optimize_posterior_samples, PolytopeSampler, sample_hypersphere, sample_simplex, sparse_to_dense_constraints
  from botorch.utils.testing import BotorchTestCase
label_desc: Use image node_id 1 for calling the TestOptimizePosteriorSamples obj's underlying member method code with example usage: obj.test_optimize_posterior_samples() without return types