Your first Bayesian optimization with Bluesky Adaptive#

In this tutorial, you will learn the three core concepts of Bluesky Adaptive: DOFs (the parameters you can adjust), objectives (what you want to optimize), and the Agent (which coordinates the optimization). We’ll optimize a simple mathematical function using simulated devices—the same patterns apply to real hardware.

Setup#

First, let’s import what we need and start the data infrastructure:

import logging
import time
from typing import Any
import warnings

from bluesky_adaptive.ax import Agent, RangeDOF, Objective

from bluesky.protocols import NamedMovable, Readable, Status, Hints, HasHints, HasParent
from bluesky.run_engine import RunEngine
from bluesky_tiled_plugins import TiledWriter
from tiled.client import from_uri
from tiled.client.container import Container
from tiled.server import SimpleTiledServer

# Suppress noisy logs from httpx 
logging.getLogger("httpx").setLevel(logging.WARNING)
# Suppress noisy dependency deprecations from within Ax
warnings.filterwarnings('ignore',category=FutureWarning)
/home/runner/work/bluesky-adaptive/bluesky-adaptive/.pixi/envs/docs/lib/python3.13/site-packages/torch/jit/_script.py:1491: FutureWarning: `torch.jit.script` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
[INFO 11-19 21:47:06] ax.storage.sqa_store.with_db_settings_base: Ax SQL storage initialized with SQLAlchemy 2.1.9
# Start a local Tiled server for data storage
tiled_server = SimpleTiledServer()

# Set up the Bluesky RunEngine and connect it to Tiled
RE = RunEngine({})
tiled_client = from_uri(tiled_server.uri)
tiled_writer = TiledWriter(tiled_client)
RE.subscribe(tiled_writer)
0

Creating simulated devices#

Bluesky controls devices through protocols. For this tutorial, we create simple simulated “movable” devices. In real experiments, you would use Ophyd devices or similar—the code below is just boilerplate to simulate hardware:

class AlwaysSuccessfulStatus(Status):
    def add_callback(self, callback) -> None:
        callback(self)
    def exception(self, timeout = 0.0):
        return None
    @property
    def done(self) -> bool:
        return True
    @property
    def success(self) -> bool:
        return True

class ReadableSignal(Readable, HasHints, HasParent):
    def __init__(self, name: str) -> None:
        self._name = name
        self._value = 0.0
    @property
    def name(self) -> str:
        return self._name
    @property
    def hints(self) -> Hints:
        return {"fields": [self._name], "dimensions": [], "gridding": "rectilinear"}
    @property
    def parent(self) -> Any | None:
        return None
    def read(self):
        return {self._name: {"value": self._value, "timestamp": time.time()}}
    def describe(self):
        return {self._name: {"source": self._name, "dtype": "number", "shape": []}}

class MovableSignal(ReadableSignal, NamedMovable):
    def __init__(self, name: str, initial_value: float = 0.0) -> None:
        super().__init__(name)
        self._value: float = initial_value
    def set(self, value: float) -> Status:
        self._value = value
        return AlwaysSuccessfulStatus()

Defining DOFs and objectives#

DOFs (degrees of freedom) are the parameters the optimizer can adjust. Objectives are what you want to optimize. Here we define two DOFs (x1 and x2) that can range from -5 to 5, and one objective (the Himmelblau function) that we want to minimize:

x1 = MovableSignal("x1", initial_value=0.1)
x2 = MovableSignal("x2", initial_value=0.23)

dofs = [
    RangeDOF(actuator=x1, bounds=(-5, 5), parameter_type="float"),
    RangeDOF(actuator=x2, bounds=(-5, 5), parameter_type="float"),
]
objectives = [
    Objective(name="himmelblau_2d", minimize=True),
]
sensors = []

Writing the evaluation function#

The evaluation function computes objective values from experimental data. Bluesky Adaptive passes it the uid returned by the acquisition plan and the suggestions that were tried. This tutorial uses the default acquisition plan, so the uid is a Bluesky run UID and blop_acquisition_order associates measurements with outcomes.

from collections.abc import Mapping, Sequence

class Himmelblau2DEvaluation():
    def __init__(self, tiled_client: Container):
        self.tiled_client = tiled_client

    def __call__(self, uid: str, suggestions: Sequence[Mapping]) -> Sequence[Mapping]:
        run = self.tiled_client[uid]
        outcomes = []
        acquisition_order = run.start["blop_acquisition_order"]
        x1_data = run["primary/x1"].read()
        x2_data = run["primary/x2"].read()

        print("[Himmelblau] evaluating acquired order: ", acquisition_order)
        for index, suggestion_id in enumerate(acquisition_order):
            x1 = x1_data[index]
            x2 = x2_data[index]
            # Himmelblau function: has four global minima where value = 0
            outcomes.append({
                "himmelblau_2d": (x1 ** 2 + x2 - 11) ** 2 + (x1 + x2 ** 2 - 7) ** 2,
                "_id": suggestion_id
            })
        
        return outcomes

Running the optimization#

The Agent brings everything together. Create one with your DOFs, objectives, and evaluation function, then run the optimization:

agent = Agent(
    sensors=sensors,
    dofs=dofs,
    objectives=objectives,
    evaluation_function=Himmelblau2DEvaluation(tiled_client=tiled_client),
    name="simple-experiment",
    description="A simple experiment optimizing the Himmelblau function",
)

RE(agent.optimize(5,n_points=8))
╭───────────────────────────────────────────────── Optimization ──────────────────────────────────────────────────╮
│ Optimizer  AxOptimizer                                                                                          │
│ Actuators  x1, x2                                                                                               │
│ Sensors    N/A                                                                                                  │
│ Iterations 5  Points/iter 8                                                                                     │
│ Run UID    bf501f18-1df9-47b7-bc46-d1054c7b5abf                                                                 │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[Himmelblau] evaluating acquired order:
[0, 3, 5, 1, 4, 6, 2, 7]
[Himmelblau] evaluating acquired order:
[11, 12, 8, 10, 14, 9, 15, 13]
[Himmelblau] evaluating acquired order:
[18, 22, 19, 16, 21, 17, 23, 20]
[Himmelblau] evaluating acquired order:
[25, 24, 29, 26, 30, 31, 27, 28]
[Himmelblau] evaluating acquired order:
[33, 36, 34, 38, 32, 37, 39, 35]
[INFO 11-19 21:47:09] ax.api.client: GenerationStrategy(name='Center+Sobol+MBM:fast', nodes=[CenterGenerationNode(next_node_name='Sobol', use_existing_trials_for_initialization=True), GenerationNode(name='Sobol', generator_specs=[GeneratorSpec(generator_enum=Sobol, generator_key_override=None)], transition_criteria=[MinTrials(transition_to='MBM'), MinTrials(transition_to='MBM')], suggested_experiment_status=ExperimentStatus.INITIALIZATION, pausing_criteria=[MaxTrialsAwaitingData(threshold=5)]), GenerationNode(name='MBM', generator_specs=[GeneratorSpec(generator_enum=BoTorch, generator_key_override=None)], transition_criteria=None, suggested_experiment_status=ExperimentStatus.OPTIMIZATION, pausing_criteria=None)]) chosen based on user input and problem structure.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 0 with parameters {'x1': 0.0, 'x2': 0.0} using GenerationNode CenterOfSearchSpace.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 1 with parameters {'x1': 4.046925, 'x2': 4.01277} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 2 with parameters {'x1': -3.480828, 'x2': -2.162085} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 3 with parameters {'x1': -0.683387, 'x2': 0.959531} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 4 with parameters {'x1': 1.874501, 'x2': -2.869115} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 5 with parameters {'x1': 0.74697, 'x2': 1.978829} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 6 with parameters {'x1': -1.781813, 'x2': -3.878643} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Generated new trial 7 with parameters {'x1': -3.983173, 'x2': 2.988588} using GenerationNode Sobol.
[INFO 11-19 21:47:09] ax.api.client: Trial 0 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 3 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 5 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 1 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 4 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 6 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 2 marked COMPLETED.
[INFO 11-19 21:47:09] ax.api.client: Trial 7 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 8 with parameters {'x1': -5.0, 'x2': -1.693183} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 9 with parameters {'x1': -1.118425, 'x2': -1.842799} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 10 with parameters {'x1': -5.0, 'x2': -2.470896} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 11 with parameters {'x1': -5.0, 'x2': 2.138282} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 12 with parameters {'x1': -5.0, 'x2': 4.38668} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 13 with parameters {'x1': 5.0, 'x2': 1.476907} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 14 with parameters {'x1': -3.139222, 'x2': -1.541027} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Generated new trial 15 with parameters {'x1': 5.0, 'x2': -1.558818} using GenerationNode MBM.
[INFO 11-19 21:47:13] ax.api.client: Trial 11 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 12 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 8 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 10 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 14 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 9 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 15 marked COMPLETED.
[INFO 11-19 21:47:13] ax.api.client: Trial 13 marked COMPLETED.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 16 with parameters {'x1': -3.55752, 'x2': 1.499987} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 17 with parameters {'x1': -3.92988, 'x2': -2.984626} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 18 with parameters {'x1': 1.431361, 'x2': 5.0} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 19 with parameters {'x1': -3.352244, 'x2': 5.0} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 20 with parameters {'x1': 1.049482, 'x2': -5.0} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 21 with parameters {'x1': -3.725718, 'x2': -0.238086} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 22 with parameters {'x1': -1.565716, 'x2': 5.0} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Generated new trial 23 with parameters {'x1': -3.992706, 'x2': -5.0} using GenerationNode MBM.
[INFO 11-19 21:47:18] ax.api.client: Trial 18 marked COMPLETED.
[INFO 11-19 21:47:18] ax.api.client: Trial 22 marked COMPLETED.
[INFO 11-19 21:47:18] ax.api.client: Trial 19 marked COMPLETED.
[INFO 11-19 21:47:18] ax.api.client: Trial 16 marked COMPLETED.
[INFO 11-19 21:47:18] ax.api.client: Trial 21 marked COMPLETED.
[INFO 11-19 21:47:19] ax.api.client: Trial 17 marked COMPLETED.
[INFO 11-19 21:47:19] ax.api.client: Trial 23 marked COMPLETED.
[INFO 11-19 21:47:19] ax.api.client: Trial 20 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 24 with parameters {'x1': 1.890387, 'x2': -0.941601} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 25 with parameters {'x1': 2.711049, 'x2': -2.43469} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 26 with parameters {'x1': 0.002654, 'x2': 2.588211} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 27 with parameters {'x1': -4.026232, 'x2': -2.516124} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 28 with parameters {'x1': -3.446154, 'x2': -3.206039} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 29 with parameters {'x1': 1.444121, 'x2': 0.458399} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 30 with parameters {'x1': -2.034804, 'x2': 0.28949} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Generated new trial 31 with parameters {'x1': -3.313629, 'x2': 2.86275} using GenerationNode MBM.
[INFO 11-19 21:47:23] ax.api.client: Trial 25 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 24 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 29 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 26 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 30 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 31 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 27 marked COMPLETED.
[INFO 11-19 21:47:23] ax.api.client: Trial 28 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 32 with parameters {'x1': 3.959313, 'x2': -3.488611} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 33 with parameters {'x1': -3.666161, 'x2': -3.042869} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 34 with parameters {'x1': 3.330904, 'x2': -0.849915} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 35 with parameters {'x1': 4.911004, 'x2': -4.335235} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 36 with parameters {'x1': 2.766738, 'x2': 1.393486} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 37 with parameters {'x1': 3.960623, 'x2': -4.954731} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 38 with parameters {'x1': 3.805241, 'x2': -2.735974} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Generated new trial 39 with parameters {'x1': 3.351289, 'x2': -3.72243} using GenerationNode MBM.
[INFO 11-19 21:47:27] ax.api.client: Trial 33 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 36 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 34 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 38 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 32 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 37 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 39 marked COMPLETED.
[INFO 11-19 21:47:27] ax.api.client: Trial 35 marked COMPLETED.
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Suggestion ID              ┃ x1                         ┃ x2                        ┃ himmelblau_2d             ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
└────────────────────────────┴────────────────────────────┴───────────────────────────┴───────────────────────────┘
┃                            ┃                            ┃                           ┃                           ┃
┃ 0                          ┃                          0 ┃                         0 ┃                       170 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 1                          ┃                    4.04692 ┃                   4.01277 ┃                   261.082 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 2                          ┃                   -3.48083 ┃                  -2.16208 ┃                   34.8061 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 3                          ┃                  -0.683387 ┃                  0.959531 ┃                   137.385 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 4                          ┃                     1.8745 ┃                  -2.86911 ┃                   116.883 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 1 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 34.8061  max: 261.082  mean: 144.031
  (5 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 5                          ┃                    0.74697 ┃                   1.97883 ┃                   77.0887 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 6                          ┃                   -1.78181 ┃                  -3.87864 ┃                   176.192 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 7                          ┃                   -3.98317 ┃                   2.98859 ┃                    65.898 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 8                          ┃                         -5 ┃                  -1.69318 ┃                   234.872 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 9                          ┃                   -1.11842 ┃                   -1.8428 ┃                   156.675 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 2 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 34.8061  max: 261.082  mean: 143.088
  (10 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 10                         ┃                         -5 ┃                   -2.4709 ┃                   167.667 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 11                         ┃                         -5 ┃                   2.13828 ┃                   315.616 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 12                         ┃                         -5 ┃                   4.38668 ┃                    390.53 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 13                         ┃                          5 ┃                   1.47691 ┃                   239.567 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 14                         ┃                   -3.13922 ┃                  -1.54103 ┃                   67.5031 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 2 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 34.8061  max: 390.53  mean: 174.118
  (15 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 15                         ┃                          5 ┃                  -1.55882 ┃                   154.968 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 16                         ┃                   -3.55752 ┃                   1.49999 ┃                   78.9755 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 17                         ┃                   -3.92988 ┃                  -2.98463 ┃                   6.21765 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 18                         ┃                    1.43136 ┃                         5 ┃                    393.19 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 19                         ┃                   -3.35224 ┃                         5 ┃                   241.989 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 3 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 6.21765  max: 393.19  mean: 174.355
  (20 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 20                         ┃                    1.04948 ┃                        -5 ┃                   584.851 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 21                         ┃                   -3.72572 ┃                 -0.238086 ┃                   120.813 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 22                         ┃                   -1.56572 ┃                         5 ┃                   282.678 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 23                         ┃                   -3.99271 ┃                        -5 ┃                   196.208 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 24                         ┃                    1.89039 ┃                 -0.941601 ┃                   87.8578 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 4 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 6.21765  max: 584.851  mean: 190.38
  (25 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 25                         ┃                    2.71105 ┃                  -2.43469 ┃                   39.7116 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 26                         ┃                 0.00265422 ┃                   2.58821 ┃                   70.8472 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 27                         ┃                   -4.02623 ┃                  -2.51612 ┃                   29.3062 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 28                         ┃                   -3.44615 ┃                  -3.20604 ┃                   5.45723 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 29                         ┃                    1.44412 ┃                  0.458399 ┃                   100.083 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 4 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 5.45723  max: 584.851  mean: 166.831
  (30 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 30                         ┃                    -2.0348 ┃                   0.28949 ┃                   123.286 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 31                         ┃                   -3.31363 ┃                   2.86275 ┃                   12.5691 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 32                         ┃                    3.95931 ┃                  -3.48861 ┃                    84.762 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 33                         ┃                   -3.66616 ┃                  -3.04287 ┃                   2.34252 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 34                         ┃                     3.3309 ┃                 -0.849915 ┃                   9.25329 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 5 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 2.34252  max: 584.851  mean: 149.632
  (35 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 35                         ┃                      4.911 ┃                  -4.33524 ┃                   356.202 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 36                         ┃                    2.76674 ┃                   1.39349 ┃                   9.05982 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 37                         ┃                    3.96062 ┃                  -4.95473 ┃                   462.751 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 38                         ┃                    3.80524 ┃                  -2.73597 ┃                   18.9643 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 39                         ┃                    3.35129 ┃                  -3.72243 ┃                   116.388 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 5 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 2.34252  max: 584.851  mean: 155.012
  (40 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────

                             Summary Statistics                              
┏━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━┓
┃ Name          ┃ Type    ┃     Min ┃     Max ┃      Mean ┃     Std ┃ Count ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━┩
│ x1            │ param   │      -5 │       5 │ -0.487876 │ 3.38934 │    40 │
│ x2            │ param   │      -5 │       5 │  -0.53584 │ 3.00901 │    40 │
│ himmelblau_2d │ outcome │ 2.34252 │ 584.851 │   155.012 │ 139.067 │    40 │
└───────────────┴─────────┴─────────┴─────────┴───────────┴─────────┴───────┘
────────────────────────────────────────────── Optimization Complete ──────────────────────────────────────────────
('4c68edc8-dca7-4066-8c7e-7e6a2b27771e',
 '07d6be81-134a-464a-9b09-b44b91856ee1',
 '2808b280-8b52-4f20-ac70-b39b9277b6df',
 '6d62db56-5145-48d3-8c50-a7d2907f6142',
 'd201cc46-38b8-4de4-9a33-f4b57b4b146f')

Viewing the results#

After optimization, visualize what the Agent learned and see the best parameters found:

agent.plot_objective("x1", "x2", "himmelblau_2d")
agent.ax_client.summarize()
himmelblau_2d (Mean) vs. x1, x2
The contour plot visualizes the predicted outcomes for himmelblau_2d across a two-dimensional parameter space, with other parameters held fixed at their best trial value (Arm 28_0). This plot helps in identifying regions of optimal performance and understanding how changes in the selected parameters influence the predicted outcomes. Contour lines represent levels of constant predicted values, providing insights into the gradient and potential optima within the parameter space.
trial_index arm_name trial_status generation_node himmelblau_2d x1 x2
0 0 0_0 COMPLETED CenterOfSearchSpace 170.000000 0.000000 0.000000
1 1 1_0 COMPLETED Sobol 261.081772 4.046925 4.012770
2 2 2_0 COMPLETED Sobol 34.806107 -3.480828 -2.162085
3 3 3_0 COMPLETED Sobol 137.384909 -0.683387 0.959531
4 4 4_0 COMPLETED Sobol 116.882743 1.874501 -2.869115
5 5 5_0 COMPLETED Sobol 77.088689 0.746970 1.978829
6 6 6_0 COMPLETED Sobol 176.191947 -1.781813 -3.878643
7 7 7_0 COMPLETED Sobol 65.898008 -3.983173 2.988588
8 8 8_0 COMPLETED MBM 234.871866 -5.000000 -1.693183
9 9 9_0 COMPLETED MBM 156.674887 -1.118425 -1.842799
10 10 10_0 COMPLETED MBM 167.667372 -5.000000 -2.470896
11 11 11_0 COMPLETED MBM 315.615618 -5.000000 2.138282
12 12 12_0 COMPLETED MBM 390.530492 -5.000000 4.386680
13 13 13_0 COMPLETED MBM 239.567489 5.000000 1.476907
14 14 14_0 COMPLETED MBM 67.503090 -3.139222 -1.541027
15 15 15_0 COMPLETED MBM 154.967832 5.000000 -1.558818
16 16 16_0 COMPLETED MBM 78.975464 -3.557520 1.499987
17 17 17_0 COMPLETED MBM 6.217651 -3.929880 -2.984626
18 18 18_0 COMPLETED MBM 393.189823 1.431361 5.000000
19 19 19_0 COMPLETED MBM 241.988598 -3.352244 5.000000
20 20 20_0 COMPLETED MBM 584.850684 1.049482 -5.000000
21 21 21_0 COMPLETED MBM 120.813099 -3.725718 -0.238086
22 22 22_0 COMPLETED MBM 282.677783 -1.565716 5.000000
23 23 23_0 COMPLETED MBM 196.207683 -3.992706 -5.000000
24 24 24_0 COMPLETED MBM 87.857811 1.890387 -0.941601
25 25 25_0 COMPLETED MBM 39.711578 2.711049 -2.434690
26 26 26_0 COMPLETED MBM 70.847174 0.002654 2.588211
27 27 27_0 COMPLETED MBM 29.306240 -4.026232 -2.516124
28 28 28_0 COMPLETED MBM 5.457227 -3.446154 -3.206039
29 29 29_0 COMPLETED MBM 100.082946 1.444121 0.458399
30 30 30_0 COMPLETED MBM 123.286379 -2.034804 0.289490
31 31 31_0 COMPLETED MBM 12.569146 -3.313629 2.862750
32 32 32_0 COMPLETED MBM 84.761988 3.959313 -3.488611
33 33 33_0 COMPLETED MBM 2.342518 -3.666161 -3.042869
34 34 34_0 COMPLETED MBM 9.253292 3.330904 -0.849915
35 35 35_0 COMPLETED MBM 356.202384 4.911004 -4.335235
36 36 36_0 COMPLETED MBM 9.059819 2.766738 1.393486
37 37 37_0 COMPLETED MBM 462.751156 3.960623 -4.954731
38 38 38_0 COMPLETED MBM 18.964262 3.805241 -2.735974
39 39 39_0 COMPLETED MBM 116.387750 3.351289 -3.722430

The Himmelblau function has four global minima (all with value 0). The summarize output shows which one(s) the optimizer found.

What you learned#

You now understand the three core concepts of Bluesky Adaptive:

  • DOFs: The parameters the optimizer adjusts (here, x1 and x2 with bounds)

  • Objectives: What you’re optimizing (here, minimizing the Himmelblau function)

  • Agent: Coordinates the optimization loop between Bluesky and the evaluation function

Next steps#

For a more comprehensive tutorial with multiple objectives and diagnostic tools, see Optimizing KB Mirrors.