import torch
from botorch.acquisition.multi_objective.logei import (
qLogExpectedHypervolumeImprovement,
)
from botorch.models.transforms.input import Normalize
from botorch.models.transforms.outcome import Standardize
from botorch.optim import optimize_acqf
from botorch.sampling import SobolQMCNormalSampler
from botorch.test_functions.multi_objective import BraninCurrin
from botorch.utils.multi_objective.box_decompositions.dominated import (
DominatedPartitioning,
)
from botorch.utils.multi_objective.box_decompositions.non_dominated import (
FastNondominatedPartitioning,
)
from botorch.utils.sampling import draw_sobol_samples
from torch import nn
from laplacekit_bayesopt import LaplaceBoTorchSurrogate
torch.set_default_dtype(torch.float64)
torch.manual_seed(10)
problem = BraninCurrin(negate=True)
bounds = problem.bounds
noise_se = torch.tensor([15.19, 0.63])
def observe(X):
clean = problem(X)
return clean, clean + noise_se * torch.randn_like(clean)
def log_hv_gap(Y):
hv = DominatedPartitioning(ref_point=problem.ref_point, Y=Y)
return torch.log10(problem.max_hv - hv.compute_hypervolume()).item()
def make_net():
return nn.Sequential(
nn.Linear(2, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh(),
nn.Linear(50, 2), # one output per objective
)
train_X = draw_sobol_samples(bounds=bounds, n=6, q=1).squeeze(1)
clean_Y, train_Y = observe(train_X)Multi-objective
With several objectives, we look for the Pareto front: the points where no objective can improve without another getting worse. A network with one output per objective models them jointly. As in BoTorch’s multi-objective tutorial, the problem is Branin-Currin with noisy observations, we evaluate four points per round, and we track the hypervolume of the true values of the points found so far.
The acquisition’s partitioning needs the current front. Since the observations are noisy, we take it from the surrogate’s mean at the points seen so far.
gaps = []
for _ in range(10):
model = LaplaceBoTorchSurrogate(
make_net(),
train_X,
train_Y,
input_transform=Normalize(d=2),
outcome_transform=Standardize(m=2),
)
with torch.no_grad():
front = model.posterior(train_X.unsqueeze(-2)).mean.squeeze(-2)
acq = qLogExpectedHypervolumeImprovement(
model=model,
ref_point=problem.ref_point,
partitioning=FastNondominatedPartitioning(
ref_point=problem.ref_point, Y=front
),
sampler=SobolQMCNormalSampler(sample_shape=torch.Size([128])),
)
x, _ = optimize_acqf(
acq,
bounds=bounds,
q=4,
num_restarts=10,
raw_samples=20,
sequential=True,
)
clean, noisy = observe(x)
train_X = torch.cat([train_X, x])
train_Y = torch.cat([train_Y, noisy])
clean_Y = torch.cat([clean_Y, clean])
gaps.append(log_hv_gap(clean_Y))
for i, gap in enumerate(gaps):
print(f"round {i + 1:2d}: log10 hypervolume gap = {gap:.3f}")round 1: log10 hypervolume gap = 1.440
round 2: log10 hypervolume gap = 1.440
round 3: log10 hypervolume gap = 1.432
round 4: log10 hypervolume gap = 1.430
round 5: log10 hypervolume gap = 1.137
round 6: log10 hypervolume gap = 1.120
round 7: log10 hypervolume gap = 1.075
round 8: log10 hypervolume gap = 1.050
round 9: log10 hypervolume gap = 1.009
round 10: log10 hypervolume gap = 0.984
The gap is the difference between the largest possible hypervolume and the one of the points found so far, so lower is better. We only do 10 rounds here so that the page runs quickly. In practice, BO runs for hundreds or thousands of rounds.