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())
{'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+g46cc86a6b
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:
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
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.003316 hypervolume: 0.03845
n_samples: 4 budget used: 0.007356 hypervolume: 0.03845
n_samples: 5 budget used: 0.01248 hypervolume: 0.03845
n_samples: 6 budget used: 0.01449 hypervolume: 0.03845
n_samples: 7 budget used: 0.04332 hypervolume: 0.03845
n_samples: 8 budget used: 0.05639 hypervolume: 0.1812
n_samples: 9 budget used: 0.1225 hypervolume: 0.2867
n_samples: 10 budget used: 0.1387 hypervolume: 0.2867
n_samples: 11 budget used: 0.1458 hypervolume: 0.2867
n_samples: 12 budget used: 0.1512 hypervolume: 0.3203
n_samples: 13 budget used: 0.1544 hypervolume: 0.3203
n_samples: 14 budget used: 0.1616 hypervolume: 0.3518
n_samples: 15 budget used: 0.1913 hypervolume: 0.4084
n_samples: 16 budget used: 0.3053 hypervolume: 0.4084
n_samples: 17 budget used: 0.4641 hypervolume: 0.5177
n_samples: 18 budget used: 0.792 hypervolume: 0.6305
n_samples: 19 budget used: 0.8108 hypervolume: 0.672
n_samples: 20 budget used: 0.9075 hypervolume: 0.7358
n_samples: 21 budget used: 1.093 hypervolume: 0.7358
n_samples: 22 budget used: 1.501 hypervolume: 0.8741
n_samples: 23 budget used: 2.196 hypervolume: 0.9903
n_samples: 24 budget used: 3.197 hypervolume: 1.114
n_samples: 25 budget used: 4.198 hypervolume: 1.166
n_samples: 26 budget used: 5.172 hypervolume: 1.166
n_samples: 27 budget used: 5.194 hypervolume: 1.166
n_samples: 28 budget used: 5.598 hypervolume: 1.166
n_samples: 29 budget used: 6.599 hypervolume: 1.166
n_samples: 30 budget used: 6.694 hypervolume: 1.166
n_samples: 31 budget used: 6.847 hypervolume: 1.166
n_samples: 32 budget used: 7.848 hypervolume: 1.256
n_samples: 33 budget used: 8.849 hypervolume: 1.256
n_samples: 34 budget used: 8.878 hypervolume: 1.256
n_samples: 35 budget used: 8.906 hypervolume: 1.256
n_samples: 36 budget used: 9.167 hypervolume: 1.256
n_samples: 37 budget used: 9.443 hypervolume: 1.256
n_samples: 38 budget used: 10.44 hypervolume: 1.267
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.005846 | False |
| 1 | 0.750000 | 1.000000 | 0.100000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.002394 | False |
| 2 | 0.534012 | 1.097060 | 0.010897 | dummy_constant | 0.534012 | 1.097060 | 0.431759 | 0.357638 | 0.005667 | False |
| 3 | 0.396694 | 0.583423 | 0.190902 | dummy_constant | 0.396694 | 0.583423 | -0.403088 | 0.017632 | 0.008982 | False |
| 4 | 0.486198 | 2.270887 | 0.208335 | dummy_constant | 0.486198 | 2.270887 | 4.490611 | 3.136230 | 0.008348 | False |
| 5 | 0.654334 | 0.253531 | 0.139211 | dummy_constant | 0.654334 | 0.253531 | -0.600847 | 0.084566 | 0.002434 | False |
| 6 | 0.169836 | 0.672649 | 0.359412 | dummy_constant | 0.169836 | 0.672649 | -0.450150 | 0.138816 | 0.005766 | False |
| 7 | 0.596597 | 0.792467 | 0.283053 | dummy_constant | 0.596597 | 0.792467 | 0.046076 | 0.094868 | 0.002807 | False |
| 8 | 0.780085 | 0.742889 | 0.458165 | dummy_constant | 0.780085 | 0.742889 | 0.067951 | 0.137442 | 0.002234 | False |
| 9 | 0.035670 | 0.006462 | 0.302303 | dummy_constant | 0.035670 | 0.006462 | -0.902421 | 0.459182 | 0.007374 | False |
| 10 | 2.610698 | 0.000000 | 0.233439 | dummy_constant | 2.610698 | 0.000000 | 5.715745 | 4.705047 | 0.002443 | False |
| 11 | 1.066680 | 0.377374 | 0.211615 | dummy_constant | 1.066680 | 0.377374 | 0.213659 | 0.336163 | 0.005067 | False |
| 12 | 0.000000 | 0.909100 | 0.174803 | dummy_constant | 0.000000 | 0.909100 | -0.273537 | 0.417363 | 0.002992 | False |
| 13 | 0.271627 | 1.080229 | 0.233408 | dummy_constant | 0.271627 | 1.080229 | 0.310351 | 0.388820 | 0.000163 | False |
| 14 | 0.105696 | 1.041231 | 0.362772 | dummy_constant | 0.105696 | 1.041231 | 0.100114 | 0.448406 | 0.000178 | False |
| 15 | 0.045462 | 0.998748 | 0.536363 | dummy_constant | 0.045462 | 0.998748 | -0.075100 | 0.455355 | 0.000159 | False |
| 16 | 0.129001 | 1.049812 | 0.590025 | dummy_constant | 0.129001 | 1.049812 | 0.156345 | 0.439934 | 0.000132 | False |
| 17 | 0.066898 | 1.029040 | 0.726516 | dummy_constant | 0.066898 | 1.029040 | 0.012666 | 0.467460 | 0.000167 | False |
| 18 | 1.101195 | 0.169803 | 0.316541 | dummy_constant | 1.101195 | 0.169803 | 0.318353 | 0.470466 | 0.000169 | False |
| 19 | 1.043747 | 0.188234 | 0.511497 | dummy_constant | 1.043747 | 0.188234 | 0.220756 | 0.392858 | 0.000178 | False |
| 20 | 1.003014 | 0.025076 | 0.617299 | dummy_constant | 1.003014 | 0.025076 | -0.085444 | 0.478576 | 0.000155 | False |
| 21 | 1.047550 | 0.099088 | 0.773120 | dummy_constant | 1.047550 | 0.099088 | 0.100998 | 0.460541 | 0.002476 | False |
| 22 | 0.826608 | 0.640181 | 0.901179 | dummy_constant | 0.826608 | 0.640181 | 0.136785 | 0.126324 | 0.000139 | False |
| 23 | 0.086514 | 1.041212 | 1.000000 | dummy_constant | 0.086514 | 1.041212 | 0.067408 | 0.463881 | 0.000161 | False |
| 24 | 0.531085 | 0.852557 | 1.000000 | dummy_constant | 0.531085 | 0.852557 | 0.096130 | 0.125263 | 0.000160 | False |
| 25 | 0.893647 | 1.260931 | 0.992021 | dummy_constant | 0.893647 | 1.260931 | 1.479024 | 0.733974 | 0.000157 | False |
| 26 | 0.619758 | 0.645187 | 0.332073 | dummy_constant | 0.619758 | 0.645187 | -0.294506 | 0.035421 | 0.000163 | False |
| 27 | 0.845821 | 0.076294 | 0.771597 | dummy_constant | 0.845821 | 0.076294 | -0.291875 | 0.299119 | 0.000180 | False |
| 28 | 0.291013 | 0.954165 | 1.000000 | dummy_constant | 0.291013 | 0.954165 | -0.007291 | 0.249941 | 0.000164 | False |
| 29 | 0.031432 | 0.933980 | 0.508126 | dummy_constant | 0.031432 | 0.933980 | -0.212554 | 0.407894 | 0.000155 | False |
| 30 | 1.109761 | 0.017497 | 0.583931 | dummy_constant | 1.109761 | 0.017497 | 0.135041 | 0.604618 | 0.000152 | False |
| 31 | 1.035281 | 0.058399 | 1.000000 | dummy_constant | 1.035281 | 0.058399 | 0.013180 | 0.481538 | 0.000168 | False |
| 32 | 0.440970 | 0.919190 | 1.000000 | dummy_constant | 0.440970 | 0.919190 | -0.024833 | 0.179205 | 0.000235 | False |
| 33 | 0.815236 | 0.154802 | 0.360436 | dummy_constant | 0.815236 | 0.154802 | -0.212393 | 0.218535 | 0.000175 | False |
| 34 | 0.815378 | 0.369851 | 0.355056 | dummy_constant | 0.815378 | 0.369851 | -0.284638 | 0.116402 | 0.000172 | False |
| 35 | 0.423354 | 1.243859 | 0.680955 | dummy_constant | 0.423354 | 1.243859 | 0.675299 | 0.559201 | 0.000163 | False |
| 36 | 0.315629 | 1.413824 | 0.691445 | dummy_constant | 0.315629 | 1.413824 | 1.191656 | 0.869068 | 0.000162 | False |
| 37 | 0.324810 | 0.980226 | 1.000000 | dummy_constant | 0.324810 | 0.980226 | 0.026739 | 0.261309 | 0.000163 | 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([-96.1147], requires_grad=True)),
('mean_module.raw_constant',
Parameter containing:
tensor(0.9341, requires_grad=True)),
('covar_module.raw_lengthscale',
Parameter containing:
tensor([[ 0.3680, 20.6058, 39.9552]], requires_grad=True))]