30dim Branin Benchmark with SAASBO

This is a port from https://github.com/pytorch/botorch/blob/main/tutorials/saasbo.ipynb ## Imports

import os

import pandas as pd

import bofire.strategies.api as strategies
from bofire.benchmarks.single import Branin30
from bofire.data_models.acquisition_functions.api import qLogEI
from bofire.data_models.api import Domain
from bofire.data_models.strategies.api import RandomStrategy, SoboStrategy
from bofire.data_models.surrogates.api import (
    BotorchSurrogates,
    FullyBayesianSingleTaskGPSurrogate,
)
from bofire.runners.api import run


SMOKE_TEST = os.environ.get("SMOKE_TEST")

N_ITERATIONS = 10 if not SMOKE_TEST else 1
BATCH_SIZE = 5 if not SMOKE_TEST else 1
WARMUP_STEPS = 256 if not SMOKE_TEST else 32
NUM_SAMPLES = 128 if not SMOKE_TEST else 16
THINNING = 16

Random Optimization

def sample(domain):
    datamodel = RandomStrategy(domain=domain)
    sampler = strategies.map(data_model=datamodel)
    sampled = sampler.ask(10)
    return sampled


def best(domain: Domain, experiments: pd.DataFrame) -> float:
    return experiments.y.min()


random_results = run(
    Branin30(),
    strategy_factory=lambda domain: strategies.map(RandomStrategy(domain=domain)),
    n_iterations=N_ITERATIONS,
    metric=best,
    initial_sampler=sample,
    n_candidates_per_proposal=5,
    n_runs=1,
    n_procs=1,
)
  0%|          | 0/1 [00:00<?, ?it/s]Run 0:   0%|          | 0/1 [00:00<?, ?it/s]Run 0:   0%|          | 0/1 [00:00<?, ?it/s, Current Best:=1.148]Run 0: 100%|██████████| 1/1 [00:00<00:00, 18.38it/s, Current Best:=1.148]

SAASBO Optimization

benchmark = Branin30()


def strategy_factory(domain: Domain):
    data_model = SoboStrategy(
        domain=domain,
        acquisition_function=qLogEI(),
        surrogate_specs=BotorchSurrogates(
            surrogates=[
                FullyBayesianSingleTaskGPSurrogate(
                    inputs=benchmark.domain.inputs,
                    outputs=benchmark.domain.outputs,
                    model_type="saas",
                    # the following hyperparams do not need to be provided
                    warmup_steps=WARMUP_STEPS,
                    num_samples=NUM_SAMPLES,
                    thinning=THINNING,
                ),
            ],
        ),
    )
    return strategies.map(data_model)


random_results = run(
    Branin30(),
    strategy_factory=strategy_factory,
    n_iterations=N_ITERATIONS,
    metric=best,
    initial_sampler=sample,
    n_candidates_per_proposal=5,
    n_runs=1,
    n_procs=1,
)
/opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/bofire/surrogates/botorch.py:181: UserWarning:

The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:213.)

  0%|          | 0/1 [00:00<?, ?it/s]Run 0:   0%|          | 0/1 [00:08<?, ?it/s]Run 0:   0%|          | 0/1 [00:08<?, ?it/s, Current Best:=4.379]Run 0: 100%|██████████| 1/1 [00:08<00:00,  8.30s/it, Current Best:=4.379]Run 0: 100%|██████████| 1/1 [00:08<00:00,  8.30s/it, Current Best:=4.379]