Your first Bayesian optimization with Blop#

In this tutorial, you will learn the three core concepts of Blop: 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 blop.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/blop/blop/.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 09-30 18:55:05] ax.storage.sqa_store.with_db_settings_base: Ax SQL storage initialized with SQLAlchemy 2.1.1
# 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. Blop 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    52f17fd7-ffd4-4115-9bba-2de9d8eaeb3b                                                                 │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[Himmelblau] evaluating acquired order:
[0, 2, 5, 4, 6, 3, 7, 1]
[Himmelblau] evaluating acquired order:
[11, 12, 9, 14, 8, 13, 10, 15]
[Himmelblau] evaluating acquired order:
[21, 22, 17, 20, 19, 23, 18, 16]
[Himmelblau] evaluating acquired order:
[24, 25, 30, 26, 29, 31, 28, 27]
[Himmelblau] evaluating acquired order:
[34, 39, 33, 36, 38, 37, 35, 32]
[INFO 09-30 18:55:10] 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 09-30 18:55:10] ax.api.client: Generated new trial 0 with parameters {'x1': 0.0, 'x2': 0.0} using GenerationNode CenterOfSearchSpace.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 1 with parameters {'x1': 3.231531, 'x2': -3.999027} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 2 with parameters {'x1': -0.656528, 'x2': 0.366559} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 3 with parameters {'x1': -2.546327, 'x2': -1.585111} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 4 with parameters {'x1': 0.1311, 'x2': 2.79639} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 5 with parameters {'x1': 1.269676, 'x2': -0.742735} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 6 with parameters {'x1': -3.865398, 'x2': 4.844031} using GenerationNode Sobol.
[INFO 09-30 18:55:10] ax.api.client: Generated new trial 7 with parameters {'x1': -1.99543, 'x2': -3.670615} using GenerationNode Sobol.
[INFO 09-30 18:55:11] ax.api.client: Trial 0 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 2 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 5 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 4 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 6 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 3 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 7 marked COMPLETED.
[INFO 09-30 18:55:11] ax.api.client: Trial 1 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 8 with parameters {'x1': -0.182044, 'x2': 2.179163} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 9 with parameters {'x1': 5.0, 'x2': 2.826694} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 10 with parameters {'x1': -5.0, 'x2': 2.651438} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 11 with parameters {'x1': 5.0, 'x2': -1.917266} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 12 with parameters {'x1': 5.0, 'x2': 2.094858} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 13 with parameters {'x1': -5.0, 'x2': 2.037472} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 14 with parameters {'x1': 3.59019, 'x2': 3.285036} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Generated new trial 15 with parameters {'x1': -5.0, 'x2': -2.491542} using GenerationNode MBM.
[INFO 09-30 18:55:18] ax.api.client: Trial 11 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 12 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 9 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 14 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 8 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 13 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 10 marked COMPLETED.
[INFO 09-30 18:55:18] ax.api.client: Trial 15 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 16 with parameters {'x1': 2.971799, 'x2': 2.726905} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 17 with parameters {'x1': -0.339957, 'x2': 4.090301} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 18 with parameters {'x1': 3.129173, 'x2': 4.924803} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 19 with parameters {'x1': 3.499395, 'x2': 1.081855} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 20 with parameters {'x1': 1.111268, 'x2': 2.358368} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 21 with parameters {'x1': -3.329902, 'x2': -3.089615} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 22 with parameters {'x1': -2.304479, 'x2': 0.855863} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Generated new trial 23 with parameters {'x1': 2.536816, 'x2': 3.896261} using GenerationNode MBM.
[INFO 09-30 18:55:24] ax.api.client: Trial 21 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 22 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 17 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 20 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 19 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 23 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 18 marked COMPLETED.
[INFO 09-30 18:55:24] ax.api.client: Trial 16 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 24 with parameters {'x1': 2.350655, 'x2': 3.013944} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 25 with parameters {'x1': 2.632008, 'x2': 1.391381} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 26 with parameters {'x1': 3.573122, 'x2': 0.200951} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 27 with parameters {'x1': -3.983966, 'x2': -3.900089} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 28 with parameters {'x1': -2.819132, 'x2': -2.594982} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 29 with parameters {'x1': 2.472734, 'x2': 0.634255} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 30 with parameters {'x1': 4.570062, 'x2': 0.34628} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Generated new trial 31 with parameters {'x1': -3.58283, 'x2': -0.296354} using GenerationNode MBM.
[INFO 09-30 18:55:30] ax.api.client: Trial 24 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 25 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 30 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 26 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 29 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 31 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 28 marked COMPLETED.
[INFO 09-30 18:55:30] ax.api.client: Trial 27 marked COMPLETED.
/home/runner/work/blop/blop/.pixi/envs/docs/lib/python3.13/site-packages/linear_operator/utils/cholesky.py:41: NumericalWarning: A not p.d., added jitter of 1.0e-08 to the diagonal
  warnings.warn(
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 32 with parameters {'x1': 3.019529, 'x2': 2.146259} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 33 with parameters {'x1': -3.464938, 'x2': -4.91865} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 34 with parameters {'x1': -3.405247, 'x2': -3.819512} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 35 with parameters {'x1': 3.118602, 'x2': -1.365729} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 36 with parameters {'x1': -4.889385, 'x2': -5.0} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 37 with parameters {'x1': 5.0, 'x2': -5.0} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 38 with parameters {'x1': 1.055793, 'x2': -5.0} using GenerationNode MBM.
[INFO 09-30 18:55:36] ax.api.client: Generated new trial 39 with parameters {'x1': -3.635477, 'x2': -3.185462} using GenerationNode MBM.
[INFO 09-30 18:55:37] ax.api.client: Trial 34 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 39 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 33 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 36 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 38 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 37 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 35 marked COMPLETED.
[INFO 09-30 18:55:37] ax.api.client: Trial 32 marked COMPLETED.
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Suggestion ID              ┃ x1                         ┃ x2                        ┃ himmelblau_2d             ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
└────────────────────────────┴────────────────────────────┴───────────────────────────┴───────────────────────────┘
┃                            ┃                            ┃                           ┃                           ┃
┃ 0                          ┃                          0 ┃                         0 ┃                       170 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 1                          ┃                    3.23153 ┃                  -3.99903 ┃                   170.179 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 2                          ┃                  -0.656528 ┃                  0.366559 ┃                   160.672 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 3                          ┃                   -2.54633 ┃                  -1.58511 ┃                   86.6999 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 4                          ┃                     0.1311 ┃                   2.79639 ┃                   67.9217 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 1 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 67.9217  max: 170.179  mean: 131.095
  (5 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 5                          ┃                    1.26968 ┃                 -0.742735 ┃                   129.449 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 6                          ┃                    -3.8654 ┃                   4.84403 ┃                   235.923 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 7                          ┃                   -1.99543 ┃                  -3.67062 ┃                   134.304 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 10                         ┃                         -5 ┃                   2.65144 ┃                    301.97 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 11                         ┃                          5 ┃                  -1.91727 ┃                   148.801 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 2 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 67.9217  max: 301.97  mean: 160.592
  (10 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 12                         ┃                          5 ┃                   2.09486 ┃                   264.749 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 13                         ┃                         -5 ┃                   2.03747 ┃                   318.803 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 14                         ┃                    3.59019 ┃                   3.28504 ┃                   81.2642 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 15                         ┃                         -5 ┃                  -2.49154 ┃                   165.994 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 8                          ┃                  -0.182044 ┃                   2.17916 ┃                   83.1445 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 2 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 67.9217  max: 318.803  mean: 167.992
  (15 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 9                          ┃                          5 ┃                   2.82669 ┃                    319.02 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 16                         ┃                     2.9718 ┃                    2.7269 ┃                   11.9251 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 17                         ┃                  -0.339957 ┃                    4.0903 ┃                   134.344 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 18                         ┃                    3.12917 ┃                    4.9248 ┃                   429.273 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 19                         ┃                    3.49939 ┃                   1.08185 ┃                   10.8476 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 3 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 10.8476  max: 429.273  mean: 171.264
  (20 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 20                         ┃                    1.11127 ┃                   2.35837 ┃                   54.9662 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 21                         ┃                    -3.3299 ┃                  -3.08961 ┃                   9.62313 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 22                         ┃                   -2.30448 ┃                  0.855863 ┃                   96.8417 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 23                         ┃                    2.53682 ┃                   3.89626 ┃                   115.315 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 24                         ┃                    2.35065 ┃                   3.01394 ┃                   25.7189 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 4 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 9.62313  max: 429.273  mean: 149.11
  (25 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 25                         ┃                    2.63201 ┃                   1.39138 ┃                   13.1035 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 26                         ┃                    3.57312 ┃                  0.200951 ┃                    15.342 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 27                         ┃                   -3.98397 ┃                  -3.90009 ┃                   18.8098 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 28                         ┃                   -2.81913 ┃                  -2.59498 ┃                   41.4124 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 29                         ┃                    2.47273 ┃                  0.634255 ┃                   35.0894 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 4 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 9.62313  max: 429.273  mean: 128.384
  (30 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 30                         ┃                    4.57006 ┃                   0.34628 ┃                   110.025 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 31                         ┃                   -3.58283 ┃                 -0.296354 ┃                   112.518 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 32                         ┃                    3.01953 ┃                   2.14626 ┃                   0.46142 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 33                         ┃                   -3.46494 ┃                  -4.91865 ┃                   203.773 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 34                         ┃                   -3.40525 ┃                  -3.81951 ┃                    27.894 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 5 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 0.46142  max: 429.273  mean: 123.034
  (35 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┃                            ┃                            ┃                           ┃                           ┃
┃ 35                         ┃                     3.1186 ┃                  -1.36573 ┃                   11.0349 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 36                         ┃                   -4.88939 ┃                        -5 ┃                   234.394 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 37                         ┃                          5 ┃                        -5 ┃                       610 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 38                         ┃                    1.05579 ┃                        -5 ┃                   584.695 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
┃                            ┃                            ┃                           ┃                           ┃
┃ 39                         ┃                   -3.63548 ┃                  -3.18546 ┃                   1.17696 ┃
┡────────────────────────────╇────────────────────────────╇───────────────────────────╇───────────────────────────┩
───────────────────────────────────────────────── Iteration 5 / 5 ─────────────────────────────────────────────────
  himmelblau_2d  min: 0.46142  max: 610  mean: 143.687
  (40 pts sampled)
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────

                             Summary Statistics                              
┏━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━┓
┃ Name          ┃ Type    ┃     Min ┃    Max ┃       Mean ┃     Std ┃ Count ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━┩
│ x1            │ param   │      -5 │      5 │    0.20656 │ 3.39207 │    40 │
│ x2            │ param   │      -5 │ 4.9248 │ -0.0456906 │ 3.02755 │    40 │
│ himmelblau_2d │ outcome │ 0.46142 │    610 │    143.687 │ 148.252 │    40 │
└───────────────┴─────────┴─────────┴────────┴────────────┴─────────┴───────┘
────────────────────────────────────────────── Optimization Complete ──────────────────────────────────────────────
('52f17fd7-ffd4-4115-9bba-2de9d8eaeb3b',
 'b2f012b0-6840-4337-ad5b-cf65e6a2bb3b',
 'b43ef418-ce94-48c7-8066-d7504b741428',
 '0e5b07eb-c312-4066-b4f9-053453fe6563',
 '35c66b4a-8cac-46a0-9caf-2947e23cc95a',
 'd999058c-d719-4a95-b83c-91e4f0d85a0c')

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 16_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 170.179260 3.231531 -3.999027
2 2 2_0 COMPLETED Sobol 160.672153 -0.656528 0.366559
3 3 3_0 COMPLETED Sobol 86.699876 -2.546327 -1.585111
4 4 4_0 COMPLETED Sobol 67.921721 0.131100 2.796390
5 5 5_0 COMPLETED Sobol 129.448861 1.269676 -0.742735
6 6 6_0 COMPLETED Sobol 235.922799 -3.865398 4.844031
7 7 7_0 COMPLETED Sobol 134.304365 -1.995430 -3.670615
8 8 8_0 COMPLETED MBM 83.144537 -0.182044 2.179163
9 9 9_0 COMPLETED MBM 319.020096 5.000000 2.826694
10 10 10_0 COMPLETED MBM 301.970056 -5.000000 2.651438
11 11 11_0 COMPLETED MBM 148.801127 5.000000 -1.917266
12 12 12_0 COMPLETED MBM 264.749044 5.000000 2.094858
13 13 13_0 COMPLETED MBM 318.802737 -5.000000 2.037472
14 14 14_0 COMPLETED MBM 81.264245 3.590190 3.285036
15 15 15_0 COMPLETED MBM 165.994369 -5.000000 -2.491542
16 16 16_0 COMPLETED MBM 11.925063 2.971799 2.726905
17 17 17_0 COMPLETED MBM 134.343617 -0.339957 4.090301
18 18 18_0 COMPLETED MBM 429.273358 3.129173 4.924803
19 19 19_0 COMPLETED MBM 10.847625 3.499395 1.081855
20 20 20_0 COMPLETED MBM 54.966246 1.111268 2.358368
21 21 21_0 COMPLETED MBM 9.623132 -3.329902 -3.089615
22 22 22_0 COMPLETED MBM 96.841670 -2.304479 0.855863
23 23 23_0 COMPLETED MBM 115.315056 2.536816 3.896261
24 24 24_0 COMPLETED MBM 25.718886 2.350655 3.013944
25 25 25_0 COMPLETED MBM 13.103466 2.632008 1.391381
26 26 26_0 COMPLETED MBM 15.341986 3.573122 0.200951
27 27 27_0 COMPLETED MBM 18.809792 -3.983966 -3.900089
28 28 28_0 COMPLETED MBM 41.412441 -2.819132 -2.594982
29 29 29_0 COMPLETED MBM 35.089357 2.472734 0.634255
30 30 30_0 COMPLETED MBM 110.024797 4.570062 0.346280
31 31 31_0 COMPLETED MBM 112.517712 -3.582830 -0.296354
32 32 32_0 COMPLETED MBM 0.461420 3.019529 2.146259
33 33 33_0 COMPLETED MBM 203.773485 -3.464938 -4.918650
34 34 34_0 COMPLETED MBM 27.893973 -3.405247 -3.819512
35 35 35_0 COMPLETED MBM 11.034857 3.118602 -1.365729
36 36 36_0 COMPLETED MBM 234.394442 -4.889385 -5.000000
37 37 37_0 COMPLETED MBM 610.000000 5.000000 -5.000000
38 38 38_0 COMPLETED MBM 584.695432 1.055793 -5.000000
39 39 39_0 COMPLETED MBM 1.176959 -3.635477 -3.185462

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 Blop:

  • 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.