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)
[WARNING 05-19 23:15:50] ax.storage.sqa_store.with_db_settings_base: Ax currently requires a sqlalchemy version below 2.0. This will be addressed in a future release. Disabling SQL storage in Ax for now, if you would like to use SQL storage please install Ax with mysql extras via `pip install ax-platform[mysql]`.
# 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)
Tiled version 0.2.9
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. After each run, Blop calls this function with the run’s unique ID and the suggestions that were tried. It returns the computed objective values:

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

    def __call__(self, uid: str, suggestions: list[dict]) -> list[dict]:
        run = self.tiled_client[uid]
        outcomes = []
        reordered_suggestions = run.start["blop_suggestions"]
        x1_data = run["primary/x1"].read()
        x2_data = run["primary/x2"].read()

        print("[Himmelblau] evaluating suggestions: ", [s["_id"] for s in suggestions], " reordered to: ", [s["_id"] for s in reordered_suggestions])
        for index, suggestion in enumerate(reordered_suggestions):
            # Special key to identify a suggestion
            suggestion_id = suggestion["_id"]
            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    c6942f97-ed61-4525-aeab-11a7ea388702                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[Himmelblau] evaluating suggestions:
[0, 1, 2, 3, 4, 5, 6, 7]
 reordered to:
[7, 1, 5, 4, 6, 3, 2, 0]
[Himmelblau] evaluating suggestions:
[8, 9, 10, 11, 12, 13, 14, 15]
 reordered to:
[12, 9, 11, 8, 10, 15, 13, 14]
[Himmelblau] evaluating suggestions:
[16, 17, 18, 19, 20, 21, 22, 23]
 reordered to:
[20, 18, 21, 16, 23, 19, 22, 17]
[Himmelblau] evaluating suggestions:
[24, 25, 26, 27, 28, 29, 30, 31]
 reordered to:
[31, 24, 27, 28, 29, 30, 26, 25]
[Himmelblau] evaluating suggestions:
[32, 33, 34, 35, 36, 37, 38, 39]
 reordered to:
[38, 36, 35, 39, 37, 32, 34, 33]
[INFO 05-19 23:15:54] ax.api.client: GenerationStrategy(name='Center+Sobol+MBM:fast', nodes=[CenterGenerationNode(next_node_name='Sobol'), 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 05-19 23:15:55] ax.api.client: Generated new trial 0 with parameters {'x1': 0.0, 'x2': 0.0} using GenerationNode CenterOfSearchSpace.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 1 with parameters {'x1': -1.608223, 'x2': 2.605162} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 2 with parameters {'x1': 0.5025, 'x2': -0.734886} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 3 with parameters {'x1': 4.180805, 'x2': 0.293182} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 4 with parameters {'x1': -3.083779, 'x2': -3.432686} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 5 with parameters {'x1': -4.74179, 'x2': 1.921829} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 6 with parameters {'x1': 3.616527, 'x2': -3.801871} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Generated new trial 7 with parameters {'x1': 2.316776, 'x2': 4.531662} using GenerationNode Sobol.
[INFO 05-19 23:15:55] ax.api.client: Trial 7 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 1 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 5 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 4 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 6 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 3 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 2 marked COMPLETED.
[INFO 05-19 23:15:55] ax.api.client: Trial 0 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 8 with parameters {'x1': -2.506582, 'x2': -1.761675} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 9 with parameters {'x1': -3.468939, 'x2': -5.0} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 10 with parameters {'x1': -1.60955, 'x2': 4.674647} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 11 with parameters {'x1': -2.101731, 'x2': -4.2846} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 12 with parameters {'x1': -4.080721, 'x2': -3.203651} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 13 with parameters {'x1': 5.0, 'x2': -1.237773} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 14 with parameters {'x1': 5.0, 'x2': 2.33333} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Generated new trial 15 with parameters {'x1': 2.953143, 'x2': -0.524931} using GenerationNode MBM.
[INFO 05-19 23:15:57] ax.api.client: Trial 12 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 9 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 11 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 8 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 10 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 15 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 13 marked COMPLETED.
[INFO 05-19 23:15:57] ax.api.client: Trial 14 marked COMPLETED.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 16 with parameters {'x1': -3.36135, 'x2': -2.81947} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 17 with parameters {'x1': 2.754279, 'x2': 0.174106} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 18 with parameters {'x1': -0.277757, 'x2': 2.181631} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 19 with parameters {'x1': -5.0, 'x2': -3.705653} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 20 with parameters {'x1': -0.319734, 'x2': 2.906774} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 21 with parameters {'x1': -1.344966, 'x2': 1.75863} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 22 with parameters {'x1': -4.971729, 'x2': -1.961868} using GenerationNode MBM.
[INFO 05-19 23:15:59] ax.api.client: Generated new trial 23 with parameters {'x1': -2.062105, 'x2': -2.745517} using GenerationNode MBM.
[INFO 05-19 23:16:00] ax.api.client: Trial 20 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 18 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 21 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 16 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 23 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 19 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 22 marked COMPLETED.
[INFO 05-19 23:16:00] ax.api.client: Trial 17 marked COMPLETED.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 24 with parameters {'x1': -3.637494, 'x2': -3.1739} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 25 with parameters {'x1': 3.42118, 'x2': -0.161671} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 26 with parameters {'x1': 2.593933, 'x2': -2.153873} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 27 with parameters {'x1': 5.0, 'x2': -5.0} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 28 with parameters {'x1': 2.101088, 'x2': -5.0} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 29 with parameters {'x1': 1.458413, 'x2': -4.150982} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 30 with parameters {'x1': 1.580937, 'x2': -3.275524} using GenerationNode MBM.
[INFO 05-19 23:16:01] ax.api.client: Generated new trial 31 with parameters {'x1': -5.0, 'x2': 5.0} using GenerationNode MBM.
[INFO 05-19 23:16:02] ax.api.client: Trial 31 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 24 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 27 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 28 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 29 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 30 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 26 marked COMPLETED.
[INFO 05-19 23:16:02] ax.api.client: Trial 25 marked COMPLETED.
/home/runner/work/blop/blop/.pixi/envs/docs/lib/python3.13/site-packages/botorch/optim/optimize.py:796: RuntimeWarning: Optimization failed in `gen_candidates_scipy` with the following warning(s):
[NumericalWarning('A not p.d., added jitter of 1.0e-08 to the diagonal'), OptimizationWarning('Optimization failed within `scipy.optimize.minimize` with status 2 and message ABNORMAL: .'), NumericalWarning('A not p.d., added jitter of 1.0e-08 to the diagonal')]
Trying again with a new set of initial conditions.
  return _optimize_acqf_batch(opt_inputs=opt_inputs)
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 32 with parameters {'x1': 3.612633, 'x2': -2.714455} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 33 with parameters {'x1': 4.003369, 'x2': -2.774954} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 34 with parameters {'x1': 3.384891, 'x2': -2.732507} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 35 with parameters {'x1': -3.569423, 'x2': -3.029674} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 36 with parameters {'x1': -3.33947, 'x2': 0.137793} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 37 with parameters {'x1': 4.888089, 'x2': -2.944837} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 38 with parameters {'x1': 5.0, 'x2': 5.0} using GenerationNode MBM.
[INFO 05-19 23:16:04] ax.api.client: Generated new trial 39 with parameters {'x1': 3.661728, 'x2': -2.567069} using GenerationNode MBM.
[INFO 05-19 23:16:05] ax.api.client: Trial 38 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 36 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 35 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 39 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 37 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 32 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 34 marked COMPLETED.
[INFO 05-19 23:16:05] ax.api.client: Trial 33 marked COMPLETED.
─────────────────────────────────────────── Iteration 1 / 5  (8 points) ───────────────────────────────────────────
  Acquire UID  df7e5da4-f1ec-4e1d-9c0f-aade055765ce
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
 Event  Suggestion ID        x1         x2  himmelblau_2d 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│     0 │             0 │        0          0            170 
│     1 │             1 │ -1.60822    2.60516        37.0555 
│     2 │             2 │   0.5025  -0.734886        167.336 
│     3 │             3 │  4.18081   0.293182        53.3349 
│     4 │             4 │ -3.08378   -3.43269        27.1244 
│     5 │             5 │ -4.74179    1.92183        244.508 
│     6 │             6 │  3.61653   -3.80187        125.529 
│     7 │             7 │  2.31678    4.53166        252.521 
└───────┴───────────────┴──────────┴───────────┴───────────────┘
  himmelblau_2d  min: 27.1244  max: 252.521  mean: 134.676
  (8 pts sampled)
─────────────────────────────────────────── Iteration 2 / 5  (8 points) ───────────────────────────────────────────
  Acquire UID  263f8acc-4927-41f3-a201-2487b7a6a5a6
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
 Event  Suggestion ID        x1         x2  himmelblau_2d 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│     0 │            10 │ -1.60955    4.67465        189.319 
│     1 │            11 │ -2.10173    -4.2846        203.774 
│     2 │            12 │ -4.08072   -3.20365        6.66386 
│     3 │            13 │        5   -1.23777        163.093 
│     4 │            14 │        5    2.33333        278.642 
│     5 │            15 │  2.95314  -0.524931        22.0845 
│     6 │             8 │ -2.50658   -1.76168        82.9733 
│     7 │             9 │ -3.46894         -5        226.885 
└───────┴───────────────┴──────────┴───────────┴───────────────┘
  himmelblau_2d  min: 6.66386  max: 278.642  mean: 140.678
  (16 pts sampled)
─────────────────────────────────────────── Iteration 3 / 5  (8 points) ───────────────────────────────────────────
  Acquire UID  85106be8-b147-4d41-92f9-4d7399c38658
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
 Event  Suggestion ID         x1        x2  himmelblau_2d 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│     0 │            16 │  -3.36135  -2.81947        12.1719 
│     1 │            17 │   2.75428  0.174106        28.2662 
│     2 │            18 │ -0.277757   2.18163        82.7505 
│     3 │            19 │        -5  -3.70565        108.973 
│     4 │            20 │ -0.319734   2.90677         65.132 
│     5 │            21 │  -1.34497   1.75863        82.8266 
│     6 │            22 │  -4.97173  -1.96187        204.189 
│     7 │            23 │  -2.06211  -2.74552        92.4449 
└───────┴───────────────┴───────────┴──────────┴───────────────┘
  himmelblau_2d  min: 6.66386  max: 278.642  mean: 121.983
  (24 pts sampled)
─────────────────────────────────────────── Iteration 4 / 5  (8 points) ───────────────────────────────────────────
  Acquire UID  df2245d7-037d-4975-a63e-99600fff9614
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
 Event  Suggestion ID        x1         x2  himmelblau_2d 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│     0 │            24 │ -3.63749    -3.1739         1.2063 
│     1 │            25 │  3.42118  -0.161671        12.9162 
│     2 │            26 │  2.59393   -2.15387        41.3399 
│     3 │            27 │        5         -5            610 
│     4 │            28 │  2.10109         -5        538.276 
│     5 │            29 │  1.45841   -4.15098        306.259 
│     6 │            30 │  1.58094   -3.27552        166.874 
│     7 │            31 │       -5          5            530 
└───────┴───────────────┴──────────┴───────────┴───────────────┘
  himmelblau_2d  min: 1.2063  max: 610  mean: 160.452
  (32 pts sampled)
─────────────────────────────────────────── Iteration 5 / 5  (8 points) ───────────────────────────────────────────
  Acquire UID  d66c4614-0892-4477-915c-32312790dba1
┏━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
 Event  Suggestion ID        x1        x2  himmelblau_2d 
┡━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│     0 │            32 │  3.61263  -2.71446        16.2876 
│     1 │            33 │  4.00337  -2.77495        27.1967 
│     2 │            34 │  3.38489  -2.73251        20.0096 
│     3 │            35 │ -3.56942  -3.02967        3.59475 
│     4 │            36 │ -3.33947  0.137793        106.596 
│     5 │            37 │  4.88809  -2.94484         142.01 
│     6 │            38 │        5         5            890 
│     7 │            39 │  3.66173  -2.56707        10.5979 
└───────┴───────────────┴──────────┴──────────┴───────────────┘
  himmelblau_2d  min: 1.2063  max: 890  mean: 158.769
  (40 pts sampled)

                          Summary Statistics                           
┏━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━┓
 Name           Type        Min  Max      Mean      Std  Count 
┡━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━╇━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━┩
 x1            │ param   │     -5    5  0.273624  3.43713 │    40 │
 x2            │ param   │     -5    5  -1.03438  2.95264 │    40 │
 himmelblau_2d │ outcome │ 1.2063  890   158.769  189.858 │    40 │
└───────────────┴─────────┴────────┴─────┴──────────┴─────────┴───────┘
────────────────────────────────────────────── Optimization Complete ──────────────────────────────────────────────

('c6942f97-ed61-4525-aeab-11a7ea388702',
 'df7e5da4-f1ec-4e1d-9c0f-aade055765ce',
 '263f8acc-4927-41f3-a201-2487b7a6a5a6',
 '85106be8-b147-4d41-92f9-4d7399c38658',
 'df2245d7-037d-4975-a63e-99600fff9614',
 'd66c4614-0892-4477-915c-32312790dba1')

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 24_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 37.055492 -1.608223 2.605162
2 2 2_0 COMPLETED Sobol 167.336150 0.502500 -0.734886
3 3 3_0 COMPLETED Sobol 53.334851 4.180805 0.293182
4 4 4_0 COMPLETED Sobol 27.124364 -3.083779 -3.432686
5 5 5_0 COMPLETED Sobol 244.507885 -4.741790 1.921829
6 6 6_0 COMPLETED Sobol 125.528850 3.616527 -3.801871
7 7 7_0 COMPLETED Sobol 252.521235 2.316776 4.531662
8 8 8_0 COMPLETED MBM 82.973309 -2.506582 -1.761675
9 9 9_0 COMPLETED MBM 226.884586 -3.468939 -5.000000
10 10 10_0 COMPLETED MBM 189.318955 -1.609550 4.674647
11 11 11_0 COMPLETED MBM 203.773637 -2.101731 -4.284600
12 12 12_0 COMPLETED MBM 6.663859 -4.080721 -3.203651
13 13 13_0 COMPLETED MBM 163.093396 5.000000 -1.237773
14 14 14_0 COMPLETED MBM 278.641757 5.000000 2.333330
15 15 15_0 COMPLETED MBM 22.084459 2.953143 -0.524931
16 16 16_0 COMPLETED MBM 12.171855 -3.361350 -2.819470
17 17 17_0 COMPLETED MBM 28.266216 2.754279 0.174106
18 18 18_0 COMPLETED MBM 82.750492 -0.277757 2.181631
19 19 19_0 COMPLETED MBM 108.972935 -5.000000 -3.705653
20 20 20_0 COMPLETED MBM 65.132019 -0.319734 2.906774
21 21 21_0 COMPLETED MBM 82.826580 -1.344966 1.758630
22 22 22_0 COMPLETED MBM 204.188782 -4.971729 -1.961868
23 23 23_0 COMPLETED MBM 92.444898 -2.062105 -2.745517
24 24 24_0 COMPLETED MBM 1.206302 -3.637494 -3.173900
25 25 25_0 COMPLETED MBM 12.916188 3.421180 -0.161671
26 26 26_0 COMPLETED MBM 41.339928 2.593933 -2.153873
27 27 27_0 COMPLETED MBM 610.000000 5.000000 -5.000000
28 28 28_0 COMPLETED MBM 538.275892 2.101088 -5.000000
29 29 29_0 COMPLETED MBM 306.259057 1.458413 -4.150982
30 30 30_0 COMPLETED MBM 166.873980 1.580937 -3.275524
31 31 31_0 COMPLETED MBM 530.000000 -5.000000 5.000000
32 32 32_0 COMPLETED MBM 16.287581 3.612633 -2.714455
33 33 33_0 COMPLETED MBM 27.196680 4.003369 -2.774954
34 34 34_0 COMPLETED MBM 20.009644 3.384891 -2.732507
35 35 35_0 COMPLETED MBM 3.594746 -3.569423 -3.029674
36 36 36_0 COMPLETED MBM 106.596389 -3.339470 0.137793
37 37 37_0 COMPLETED MBM 142.009693 4.888089 -2.944837
38 38 38_0 COMPLETED MBM 890.000000 5.000000 5.000000
39 39 39_0 COMPLETED MBM 10.597925 3.661728 -2.567069

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.