Multi-fidelity Multi-objective Bayesian Optimization¶
Here we attempt to solve for the constrained Pareto front of the TNK multi-objective optimization problem using Multi-Fidelity Multi-Objective Bayesian optimization. For simplicity we assume that the objective and constraint functions at lower fidelities is exactly equal to the functions at higher fidelities (this is obviously not a requirement, although for the best results lower fidelity calculations should correlate with higher fidelity ones). The algorithm should learn this relationship and use information gathered at lower fidelities to gather samples to improve the hypervolume of the Pareto front at the maximum fidelity.
TNK function $n=2$ variables: $x_i \in [0, \pi], i=1,2$
Objectives:
- $f_i(x) = x_i$
Constraints:
- $g_1(x) = -x_1^2 -x_2^2 + 1 + 0.1 \cos\left(16 \arctan \frac{x_1}{x_2}\right) \le 0$
- $g_2(x) = (x_1 - 1/2)^2 + (x_2-1/2)^2 \le 0.5$
# set values if testing
import os
from copy import deepcopy
import pandas as pd
import numpy as np
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import MultiFidelityGenerator
from xopt.resources.test_functions.tnk import evaluate_TNK, tnk_vocs
from xopt.vocs import get_feasibility_data
import matplotlib.pyplot as plt
# Ignore all warnings
import warnings
warnings.filterwarnings("ignore")
SMOKE_TEST = os.environ.get("SMOKE_TEST")
N_MC_SAMPLES = 1 if SMOKE_TEST else 128
NUM_RESTARTS = 1 if SMOKE_TEST else 20
BUDGET = 0.02 if SMOKE_TEST else 10
evaluator = Evaluator(function=evaluate_TNK)
print(tnk_vocs.dict())
/home/runner/work/Xopt/Xopt/.venv/lib/python3.12/site-packages/pyro/ops/stats.py:527: SyntaxWarning: invalid escape sequence '\g'
we have :math:`ES^{*}(P,Q) \ge ES^{*}(Q,Q)` with equality holding if and only if :math:`P=Q`, i.e.
{'variables': {'x1': {'dtype': None, 'default_value': None, 'domain': [0.0, 3.14159], 'type': 'ContinuousVariable'}, 'x2': {'dtype': None, 'default_value': None, 'domain': [0.0, 3.14159], 'type': 'ContinuousVariable'}}, 'objectives': {'y1': {'dtype': None, 'type': 'MinimizeObjective'}, 'y2': {'dtype': None, 'type': 'MinimizeObjective'}}, 'constraints': {'c1': {'dtype': None, 'value': 0.0, 'type': 'GreaterThanConstraint'}, 'c2': {'dtype': None, 'value': 0.5, 'type': 'LessThanConstraint'}}, 'constants': {'a': {'dtype': None, 'value': 'dummy_constant', 'type': 'Constant'}}, 'observables': {}}
Set up the Multi-Fidelity Multi-objective optimization algorithm¶
Here we create the Multi-Fidelity generator object which can solve both single and multi-objective optimization problems depending on the number of objectives in VOCS. We specify a cost function as a function of fidelity parameter $s=[0,1]$ as $C(s) = s^{3.5} + 0.001$ as an example from a real life multi-fidelity simulation problem.
my_vocs = deepcopy(tnk_vocs)
generator = MultiFidelityGenerator(vocs=my_vocs, reference_point={"y1": 1.5, "y2": 1.5})
# set cost function according to approximate scaling of laser plasma accelerator
# problem, see https://journals.aps.org/prresearch/abstract/10.1103/PhysRevResearch.5.013063
generator.cost_function = lambda s: s**3.5 + 0.001
generator.numerical_optimizer.n_restarts = NUM_RESTARTS
generator.n_monte_carlo_samples = N_MC_SAMPLES
generator.gp_constructor.use_low_noise_prior = True
X = Xopt(generator=generator, evaluator=evaluator)
# evaluate at some explicit initial points
X.evaluate_data(pd.DataFrame({"x1": [1.0, 0.75], "x2": [0.75, 1.0], "s": [0.0, 0.1]}))
X
Xopt
________________________________
Version: 0.1.dev1+gb834d2348
Data size: 2
Config as YAML:
dump_file: null
evaluator:
function: xopt.resources.test_functions.tnk.evaluate_TNK
function_kwargs:
raise_probability: 0
random_sleep: 0
sleep: 0
max_workers: 1
vectorized: false
generator:
computation_time: null
custom_objective: null
fixed_features: null
gp_constructor:
covar_modules: {}
custom_noise_prior: null
mean_modules: {}
name: standard
train_config: null
train_kwargs: null
train_method: lbfgs
train_model: true
trainable_mean_keys: []
transform_inputs: true
use_cached_hyperparameters: false
use_low_noise_prior: true
max_travel_distances: null
model: null
n_candidates: 1
n_interpolate_points: null
n_monte_carlo_samples: 128
name: multi_fidelity
numerical_optimizer:
max_iter: 2000
max_time: 5.0
n_restarts: 20
name: LBFGS
reference_point:
s: 0.0
y1: 1.5
y2: 1.5
returns_id: false
supports_batch_generation: true
supports_constraints: true
supports_multi_objective: true
turbo_controller: null
use_cuda: false
use_pf_as_initial_points: false
vocs:
constants:
a:
dtype: null
type: Constant
value: dummy_constant
constraints:
c1:
dtype: null
type: GreaterThanConstraint
value: 0.0
c2:
dtype: null
type: LessThanConstraint
value: 0.5
objectives:
s:
dtype: null
type: MaximizeObjective
y1:
dtype: null
type: MinimizeObjective
y2:
dtype: null
type: MinimizeObjective
observables: {}
variables:
s:
default_value: null
domain:
- 0.0
- 1.0
dtype: null
type: ContinuousVariable
x1:
default_value: null
domain:
- 0.0
- 3.14159
dtype: null
type: ContinuousVariable
x2:
default_value: null
domain:
- 0.0
- 3.14159
dtype: null
type: ContinuousVariable
serialize_inline: false
serialize_torch: false
stopping_condition: null
strict: true
Run optimization routine¶
Instead of ending the optimization routine after an explict number of samples we end optimization once a given optimization budget has been exceeded. WARNING: This will slightly exceed the given budget
budget = BUDGET
while X.generator.calculate_total_cost() < budget:
X.step()
print(
f"n_samples: {len(X.data)} "
f"budget used: {X.generator.calculate_total_cost():.4} "
f"hypervolume: {X.generator.get_pareto_front_and_hypervolume()[-1]:.4}"
)
n_samples: 3 budget used: 0.00332 hypervolume: 0.0375
n_samples: 4 budget used: 0.004845 hypervolume: 0.0375
n_samples: 5 budget used: 0.006602 hypervolume: 0.0375
n_samples: 6 budget used: 0.008348 hypervolume: 0.0375
n_samples: 7 budget used: 0.01114 hypervolume: 0.0375
n_samples: 8 budget used: 0.01496 hypervolume: 0.1091
n_samples: 9 budget used: 0.02475 hypervolume: 0.1633
n_samples: 10 budget used: 0.04521 hypervolume: 0.2166
n_samples: 11 budget used: 0.05336 hypervolume: 0.2714
n_samples: 12 budget used: 0.09079 hypervolume: 0.2714
n_samples: 13 budget used: 0.1307 hypervolume: 0.3366
n_samples: 14 budget used: 0.1936 hypervolume: 0.411
n_samples: 15 budget used: 0.1966 hypervolume: 0.4298
n_samples: 16 budget used: 0.2063 hypervolume: 0.4298
n_samples: 17 budget used: 0.7499 hypervolume: 0.4298
n_samples: 18 budget used: 0.9355 hypervolume: 0.546
n_samples: 19 budget used: 1.366 hypervolume: 0.7072
n_samples: 20 budget used: 2.047 hypervolume: 0.7072
n_samples: 21 budget used: 2.437 hypervolume: 0.7072
n_samples: 22 budget used: 2.459 hypervolume: 0.7553
n_samples: 23 budget used: 2.553 hypervolume: 0.8107
n_samples: 24 budget used: 3.174 hypervolume: 0.9745
n_samples: 25 budget used: 3.456 hypervolume: 0.9745
n_samples: 26 budget used: 4.457 hypervolume: 0.9745
n_samples: 27 budget used: 5.458 hypervolume: 1.084
n_samples: 28 budget used: 6.459 hypervolume: 1.123
n_samples: 29 budget used: 7.46 hypervolume: 1.153
n_samples: 30 budget used: 8.067 hypervolume: 1.153
n_samples: 31 budget used: 8.146 hypervolume: 1.153
n_samples: 32 budget used: 8.218 hypervolume: 1.153
n_samples: 33 budget used: 8.221 hypervolume: 1.153
n_samples: 34 budget used: 8.23 hypervolume: 1.153
n_samples: 35 budget used: 8.936 hypervolume: 1.153
n_samples: 36 budget used: 9.937 hypervolume: 1.23
n_samples: 37 budget used: 10.81 hypervolume: 1.23
Show results¶
X.data
| x1 | x2 | s | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.000000 | 0.750000 | 0.000000 | dummy_constant | 1.000000 | 0.750000 | 0.626888 | 0.312500 | 0.004024 | False |
| 1 | 0.750000 | 1.000000 | 0.100000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000152 | False |
| 2 | 0.540887 | 1.314066 | 0.028709 | dummy_constant | 0.540887 | 1.314066 | 0.919392 | 0.664375 | 0.005181 | False |
| 3 | 0.403417 | 0.638485 | 0.115592 | dummy_constant | 0.403417 | 0.638485 | -0.337830 | 0.028506 | 0.000163 | False |
| 4 | 0.000000 | 0.046606 | 0.128331 | dummy_constant | 0.000000 | 0.046606 | -1.097828 | 0.455566 | 0.000160 | False |
| 5 | 3.066225 | 0.000000 | 0.127773 | dummy_constant | 3.066225 | 0.000000 | 8.301738 | 6.835512 | 0.000180 | False |
| 6 | 0.467859 | 0.847406 | 0.164242 | dummy_constant | 0.467859 | 0.847406 | -0.041444 | 0.121724 | 0.000185 | False |
| 7 | 0.963973 | 0.517001 | 0.186729 | dummy_constant | 0.963973 | 0.517001 | 0.198786 | 0.215560 | 0.000190 | False |
| 8 | 1.070426 | 0.302125 | 0.258596 | dummy_constant | 1.070426 | 0.302125 | 0.267686 | 0.364541 | 0.000179 | False |
| 9 | 1.069543 | 0.164309 | 0.324465 | dummy_constant | 1.069543 | 0.164309 | 0.247234 | 0.437068 | 0.000182 | False |
| 10 | 0.467728 | 0.984127 | 0.243771 | dummy_constant | 0.467728 | 0.984127 | 0.118727 | 0.235420 | 0.000157 | False |
| 11 | 0.994552 | 0.020178 | 0.388130 | dummy_constant | 0.994552 | 0.020178 | -0.105238 | 0.474811 | 0.000165 | False |
| 12 | 0.733148 | 0.823639 | 0.395430 | dummy_constant | 0.733148 | 0.823639 | 0.156021 | 0.159100 | 0.000156 | False |
| 13 | 1.041407 | 0.095960 | 0.451600 | dummy_constant | 1.041407 | 0.095960 | 0.083691 | 0.456369 | 0.000156 | False |
| 14 | 0.208640 | 1.072741 | 0.169582 | dummy_constant | 0.208640 | 1.072741 | 0.294073 | 0.412924 | 0.000153 | False |
| 15 | 0.023573 | 0.956861 | 0.257894 | dummy_constant | 0.023573 | 0.956861 | -0.176195 | 0.435705 | 0.000153 | False |
| 16 | 0.032853 | 1.453496 | 0.839733 | dummy_constant | 0.032853 | 1.453496 | 1.020195 | 1.127380 | 0.000152 | False |
| 17 | 0.833777 | 0.645643 | 0.617143 | dummy_constant | 0.833777 | 0.645643 | 0.155806 | 0.132619 | 0.000152 | False |
| 18 | 1.013860 | 0.118878 | 0.785202 | dummy_constant | 1.013860 | 0.118878 | 0.071282 | 0.409306 | 0.000152 | False |
| 19 | 0.892034 | 0.005274 | 0.895870 | dummy_constant | 0.892034 | 0.005274 | -0.303801 | 0.398444 | 0.000153 | False |
| 20 | 0.279911 | 0.610472 | 0.763585 | dummy_constant | 0.279911 | 0.610472 | -0.631764 | 0.060643 | 0.000154 | False |
| 21 | 0.088601 | 1.049080 | 0.330691 | dummy_constant | 0.088601 | 1.049080 | 0.086333 | 0.470738 | 0.000152 | False |
| 22 | 0.098612 | 1.049903 | 0.506997 | dummy_constant | 0.098612 | 1.049903 | 0.104787 | 0.463506 | 0.000153 | False |
| 23 | 0.099518 | 1.044628 | 0.872458 | dummy_constant | 0.099518 | 1.044628 | 0.096043 | 0.457006 | 0.000153 | False |
| 24 | 0.037846 | 0.846545 | 0.695924 | dummy_constant | 0.037846 | 0.846545 | -0.357450 | 0.333679 | 0.000153 | False |
| 25 | 0.929377 | 0.382861 | 1.000000 | dummy_constant | 0.929377 | 0.382861 | -0.089628 | 0.198086 | 0.000153 | False |
| 26 | 0.659047 | 0.842830 | 1.000000 | dummy_constant | 0.659047 | 0.842830 | 0.181557 | 0.142828 | 0.000154 | False |
| 27 | 0.286208 | 0.991652 | 1.000000 | dummy_constant | 0.286208 | 0.991652 | 0.086788 | 0.287429 | 0.000157 | False |
| 28 | 0.058236 | 1.035700 | 1.000000 | dummy_constant | 0.058236 | 1.035700 | 0.013803 | 0.482130 | 0.000158 | False |
| 29 | 0.502402 | 0.438840 | 0.866582 | dummy_constant | 0.502402 | 0.438840 | -0.602247 | 0.003746 | 0.000152 | False |
| 30 | 0.499333 | 0.456031 | 0.482440 | dummy_constant | 0.499333 | 0.456031 | -0.617571 | 0.001934 | 0.000154 | False |
| 31 | 1.694445 | 0.484055 | 0.469401 | dummy_constant | 1.694445 | 0.484055 | 2.131183 | 1.426953 | 0.000153 | False |
| 32 | 0.815753 | 0.305133 | 0.168214 | dummy_constant | 0.815753 | 0.305133 | -0.326370 | 0.137673 | 0.000153 | False |
| 33 | 0.034007 | 1.018247 | 0.254252 | dummy_constant | 0.034007 | 1.018247 | -0.048085 | 0.485729 | 0.000152 | False |
| 34 | 0.076509 | 1.406007 | 0.904960 | dummy_constant | 0.076509 | 1.406007 | 0.918211 | 1.000193 | 0.000152 | False |
| 35 | 1.062487 | 0.088607 | 1.000000 | dummy_constant | 1.062487 | 0.088607 | 0.113004 | 0.485636 | 0.000155 | False |
| 36 | 0.379703 | 1.219809 | 0.962145 | dummy_constant | 0.379703 | 1.219809 | 0.620536 | 0.532597 | 0.000155 | False |
Plot results¶
Here we plot the resulting observations in input space, colored by feasibility (neglecting the fact that these data points are at varying fidelities).
fig, ax = plt.subplots()
theta = np.linspace(0, np.pi / 2)
r = np.sqrt(1 + 0.1 * np.cos(16 * theta))
x_1 = r * np.sin(theta)
x_2_lower = r * np.cos(theta)
x_2_upper = (0.5 - (x_1 - 0.5) ** 2) ** 0.5 + 0.5
z = np.zeros_like(x_1)
# ax2.plot(x_1, x_2_lower,'r')
ax.fill_between(x_1, z, x_2_lower, fc="white")
circle = plt.Circle(
(0.5, 0.5), 0.5**0.5, color="r", alpha=0.25, zorder=0, label="Valid Region"
)
ax.add_patch(circle)
history = pd.concat(
[X.data, get_feasibility_data(tnk_vocs, X.data)], axis=1, ignore_index=False
)
ax.plot(*history[["x1", "x2"]][history["feasible"]].to_numpy().T, ".C1")
ax.plot(*history[["x1", "x2"]][~history["feasible"]].to_numpy().T, ".C2")
ax.set_xlim(0, 3.14)
ax.set_ylim(0, 3.14)
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.set_aspect("equal")
Plot path through input space¶
ax = history.hist(["x1", "x2", "s"], bins=20)
history.plot(y=["x1", "x2", "s"])
<Axes: >
Plot the acquisition function¶
Here we plot the acquisition function at a small set of fidelities $[0, 0.5, 1.0]$.
fidelities = [0.0, 0.5, 1.0]
for fidelity in fidelities:
X.generator.visualize_model(
variable_names=["x1", "x2"],
reference_point={"s": fidelity},
)
# examine lengthscale of the first objective
list(X.generator.model.models[0].named_parameters())
[('likelihood.noise_covar.raw_noise',
Parameter containing:
tensor([-74.0031], requires_grad=True)),
('mean_module.raw_constant',
Parameter containing:
tensor(1.1303, requires_grad=True)),
('covar_module.raw_lengthscale',
Parameter containing:
tensor([[ 0.4621, 15.8831, 36.9488]], requires_grad=True))]