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/torch/jit/_script.py:1491: FutureWarning: `torch.jit.script` is deprecated. Please switch to `torch.compile` or `torch.export`. warnings.warn(
{'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+gd4c488f75
Data size: 2
Config as YAML:
data_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:
discrete_max_batch_size: 2048
discrete_max_choices: 4096
max_iter: 1000
max_time: 5.0
mixed_max_discrete_configurations: 512
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_contextual_variables: true
supports_discrete_variables: true
supports_multi_objective: true
supports_no_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
xopt_dump_file: null
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.01473 hypervolume: 0.0375
n_samples: 4 budget used: 0.01732 hypervolume: 0.0375
n_samples: 5 budget used: 0.03505 hypervolume: 0.1127
n_samples: 6 budget used: 0.05911 hypervolume: 0.1127
n_samples: 7 budget used: 0.1461 hypervolume: 0.1127
n_samples: 8 budget used: 0.1547 hypervolume: 0.1841
n_samples: 9 budget used: 0.2066 hypervolume: 0.1841
n_samples: 10 budget used: 0.2275 hypervolume: 0.1841
n_samples: 11 budget used: 0.2409 hypervolume: 0.2846
n_samples: 12 budget used: 0.2764 hypervolume: 0.3496
n_samples: 13 budget used: 0.5328 hypervolume: 0.3496
n_samples: 14 budget used: 0.6121 hypervolume: 0.4418
n_samples: 15 budget used: 0.6632 hypervolume: 0.5061
n_samples: 16 budget used: 0.8175 hypervolume: 0.5886
n_samples: 17 budget used: 1.044 hypervolume: 0.5886
n_samples: 18 budget used: 1.398 hypervolume: 0.7353
n_samples: 19 budget used: 1.613 hypervolume: 0.7995
n_samples: 20 budget used: 2.179 hypervolume: 0.7995
n_samples: 21 budget used: 2.316 hypervolume: 0.7995
n_samples: 22 budget used: 3.317 hypervolume: 0.9872
n_samples: 23 budget used: 4.318 hypervolume: 1.104
n_samples: 24 budget used: 4.324 hypervolume: 1.104
n_samples: 25 budget used: 5.325 hypervolume: 1.147
n_samples: 26 budget used: 5.337 hypervolume: 1.147
n_samples: 27 budget used: 6.338 hypervolume: 1.194
n_samples: 28 budget used: 7.339 hypervolume: 1.194
n_samples: 29 budget used: 8.34 hypervolume: 1.194
n_samples: 30 budget used: 8.608 hypervolume: 1.194
n_samples: 31 budget used: 9.609 hypervolume: 1.216
n_samples: 32 budget used: 9.619 hypervolume: 1.22
n_samples: 33 budget used: 9.631 hypervolume: 1.223
n_samples: 34 budget used: 10.06 hypervolume: 1.223
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.005285 | False |
| 1 | 0.750000 | 1.000000 | 0.100000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000192 | False |
| 2 | 0.608686 | 1.342537 | 0.278628 | dummy_constant | 0.608686 | 1.342537 | 1.086494 | 0.721681 | 0.008890 | False |
| 3 | 0.510687 | 2.549458 | 0.158620 | dummy_constant | 0.510687 | 2.549458 | 5.860514 | 4.200392 | 0.005720 | False |
| 4 | 0.855723 | 0.963368 | 0.310762 | dummy_constant | 0.855723 | 0.963368 | 0.601822 | 0.341249 | 0.005516 | False |
| 5 | 1.784338 | 0.680038 | 0.340600 | dummy_constant | 1.784338 | 0.680038 | 2.556587 | 1.681938 | 0.006335 | False |
| 6 | 0.158465 | 0.038175 | 0.496135 | dummy_constant | 0.158465 | 0.038175 | -0.893271 | 0.329928 | 0.002780 | False |
| 7 | 0.913674 | 0.472171 | 0.247661 | dummy_constant | 0.913674 | 0.472171 | 0.035703 | 0.171901 | 0.000152 | False |
| 8 | 0.156535 | 0.830466 | 0.427041 | dummy_constant | 0.156535 | 0.830466 | -0.187112 | 0.227176 | 0.003155 | False |
| 9 | 0.088068 | 1.091102 | 0.326661 | dummy_constant | 0.088068 | 1.091102 | 0.170416 | 0.519089 | 0.005510 | False |
| 10 | 0.102631 | 1.007161 | 0.285070 | dummy_constant | 0.102631 | 1.007161 | 0.030305 | 0.415115 | 0.001514 | False |
| 11 | 1.019923 | 0.391467 | 0.382268 | dummy_constant | 1.019923 | 0.391467 | 0.102160 | 0.282100 | 0.000153 | False |
| 12 | 0.896526 | 0.002851 | 0.677052 | dummy_constant | 0.896526 | 0.002851 | -0.296104 | 0.404390 | 0.000166 | False |
| 13 | 1.083308 | 0.165148 | 0.483084 | dummy_constant | 1.083308 | 0.165148 | 0.275939 | 0.452374 | 0.003063 | False |
| 14 | 0.070596 | 1.042019 | 0.425032 | dummy_constant | 0.070596 | 1.042019 | 0.043860 | 0.478171 | 0.000151 | False |
| 15 | 0.712138 | 0.854595 | 0.585217 | dummy_constant | 0.712138 | 0.854595 | 0.225506 | 0.170740 | 0.000160 | False |
| 16 | 1.021506 | 0.046121 | 0.653112 | dummy_constant | 1.021506 | 0.046121 | -0.029453 | 0.477975 | 0.000161 | False |
| 17 | 1.062554 | 0.101368 | 0.742725 | dummy_constant | 1.062554 | 0.101368 | 0.134400 | 0.475375 | 0.000164 | False |
| 18 | 0.134089 | 1.064252 | 0.644080 | dummy_constant | 0.134089 | 1.064252 | 0.192711 | 0.452271 | 0.000144 | False |
| 19 | 0.060891 | 1.026159 | 0.849277 | dummy_constant | 0.060891 | 1.026159 | -0.001596 | 0.469659 | 0.000176 | False |
| 20 | 0.820498 | 0.210605 | 0.565984 | dummy_constant | 0.820498 | 0.210605 | -0.218596 | 0.186469 | 0.000168 | False |
| 21 | 0.133295 | 1.076737 | 1.000000 | dummy_constant | 0.133295 | 1.076737 | 0.216064 | 0.467098 | 0.000156 | False |
| 22 | 0.984489 | 0.351962 | 1.000000 | dummy_constant | 0.984489 | 0.351962 | 0.022685 | 0.256645 | 0.000160 | False |
| 23 | 0.450325 | 0.737921 | 0.220102 | dummy_constant | 0.450325 | 0.737921 | -0.173551 | 0.059074 | 0.002794 | False |
| 24 | 0.781528 | 0.723950 | 1.000000 | dummy_constant | 0.781528 | 0.723950 | 0.053018 | 0.129411 | 0.000160 | False |
| 25 | 0.728125 | 0.389979 | 0.275521 | dummy_constant | 0.728125 | 0.389979 | -0.316403 | 0.064145 | 0.000155 | False |
| 26 | 1.047410 | 0.067919 | 1.000000 | dummy_constant | 1.047410 | 0.067919 | 0.050719 | 0.486351 | 0.002189 | False |
| 27 | 0.019069 | 1.009053 | 1.000000 | dummy_constant | 0.019069 | 1.009053 | -0.076913 | 0.490429 | 0.000161 | False |
| 28 | 1.074695 | 0.099807 | 1.000000 | dummy_constant | 1.074695 | 0.099807 | 0.156030 | 0.490429 | 0.000173 | False |
| 29 | 0.619968 | 0.555609 | 0.685429 | dummy_constant | 0.619968 | 0.555609 | -0.371032 | 0.017485 | 0.000182 | False |
| 30 | 0.958902 | 0.195708 | 1.000000 | dummy_constant | 0.958902 | 0.195708 | 0.057478 | 0.303185 | 0.000161 | False |
| 31 | 0.800463 | 0.595784 | 0.266019 | dummy_constant | 0.800463 | 0.595784 | 0.064453 | 0.099453 | 0.002392 | False |
| 32 | 0.625300 | 0.877810 | 0.270549 | dummy_constant | 0.625300 | 0.877810 | 0.250315 | 0.158440 | 0.000157 | False |
| 33 | 0.616588 | 0.644239 | 0.782990 | dummy_constant | 0.616588 | 0.644239 | -0.298683 | 0.034398 | 0.000158 | 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([-116.3282], requires_grad=True)),
('mean_module.raw_constant',
Parameter containing:
tensor(0.3566, requires_grad=True)),
('covar_module.raw_lengthscale',
Parameter containing:
tensor([[ 0.3126, 18.0883, 39.5529]], requires_grad=True))]