---
title: 30dim Branin Benchmark with SAASBO
jupyter: python3
---

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


```{python}
#| papermill: {duration: 3.001328, end_time: '2024-10-10T20:35:06.062960', exception: false, start_time: '2024-10-10T20:35:03.061632', status: completed}
#| tags: []
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

```{python}
#| papermill: {duration: 0.255252, end_time: '2024-10-10T20:35:06.320917', exception: true, start_time: '2024-10-10T20:35:06.065665', status: failed}
#| tags: []
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,
)
```

## SAASBO Optimization

```{python}
#| papermill: {duration: null, end_time: null, exception: null, start_time: null, status: pending}
#| tags: []
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,
)
```
