Multi-objective Bayesian Optimization¶
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$
In [1]:
Copied!
# set values if testing
import os
import pandas as pd
import numpy as np
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import MOBOGenerator
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
N_STEPS = 1 if SMOKE_TEST else 30
MAX_ITER = 1 if SMOKE_TEST else 200
evaluator = Evaluator(function=evaluate_TNK)
print(tnk_vocs.dict())
# set values if testing
import os
import pandas as pd
import numpy as np
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import MOBOGenerator
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
N_STEPS = 1 if SMOKE_TEST else 30
MAX_ITER = 1 if SMOKE_TEST else 200
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': {}}
In [2]:
Copied!
generator = MOBOGenerator(vocs=tnk_vocs, reference_point={"y1": 1.5, "y2": 1.5})
generator.n_monte_carlo_samples = N_MC_SAMPLES
generator.numerical_optimizer.n_restarts = NUM_RESTARTS
generator.numerical_optimizer.max_iter = MAX_ITER
generator.gp_constructor.use_low_noise_prior = True
X = Xopt(generator=generator, evaluator=evaluator)
X.evaluate_data(pd.DataFrame({"x1": [1.0, 0.75], "x2": [0.75, 1.0]}))
for i in range(N_STEPS):
print(i)
X.step()
generator = MOBOGenerator(vocs=tnk_vocs, reference_point={"y1": 1.5, "y2": 1.5})
generator.n_monte_carlo_samples = N_MC_SAMPLES
generator.numerical_optimizer.n_restarts = NUM_RESTARTS
generator.numerical_optimizer.max_iter = MAX_ITER
generator.gp_constructor.use_low_noise_prior = True
X = Xopt(generator=generator, evaluator=evaluator)
X.evaluate_data(pd.DataFrame({"x1": [1.0, 0.75], "x2": [0.75, 1.0]}))
for i in range(N_STEPS):
print(i)
X.step()
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
In [3]:
Copied!
X.generator.data
X.generator.data
Out[3]:
| x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.000000 | 0.750000 | dummy_constant | 1.000000 | 0.750000 | 0.626888 | 0.312500 | 0.000171 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000141 | False |
| 2 | 1.673149 | 0.372832 | dummy_constant | 1.673149 | 0.372832 | 2.031794 | 1.392451 | 0.000168 | False |
| 3 | 0.570814 | 0.294095 | dummy_constant | 0.570814 | 0.294095 | -0.611642 | 0.047411 | 0.000164 | False |
| 4 | 0.024747 | 0.008495 | dummy_constant | 0.024747 | 0.008495 | -1.053990 | 0.467443 | 0.000163 | False |
| 5 | 0.989565 | 0.002450 | dummy_constant | 0.989565 | 0.002450 | -0.120678 | 0.487230 | 0.000166 | False |
| 6 | 0.419617 | 0.874213 | dummy_constant | 0.419617 | 0.874213 | -0.123616 | 0.146497 | 0.000159 | False |
| 7 | 1.022063 | 0.192238 | dummy_constant | 1.022063 | 0.192238 | 0.180177 | 0.367267 | 0.000162 | False |
| 8 | 0.012211 | 3.141590 | dummy_constant | 0.012211 | 3.141590 | 8.769930 | 7.215936 | 0.000166 | False |
| 9 | 0.302307 | 1.046188 | dummy_constant | 0.302307 | 1.046188 | 0.206903 | 0.337404 | 0.000167 | False |
| 10 | 0.796411 | 0.622960 | dummy_constant | 0.796411 | 0.622960 | 0.058959 | 0.102979 | 0.000163 | False |
| 11 | 0.155367 | 1.015335 | dummy_constant | 0.155367 | 1.015335 | 0.130743 | 0.384342 | 0.000175 | False |
| 12 | 1.035883 | 0.095831 | dummy_constant | 1.035883 | 0.095831 | 0.072770 | 0.450524 | 0.000165 | False |
| 13 | 0.026549 | 0.981313 | dummy_constant | 0.026549 | 0.981313 | -0.127100 | 0.455818 | 0.000166 | False |
| 14 | 0.042011 | 1.027173 | dummy_constant | 0.042011 | 1.027173 | -0.022514 | 0.487666 | 0.000161 | False |
| 15 | 0.066857 | 1.037074 | dummy_constant | 0.066857 | 1.037074 | 0.028514 | 0.476061 | 0.000194 | False |
| 16 | 0.037551 | 0.004058 | dummy_constant | 0.037551 | 0.004058 | -0.983492 | 0.459818 | 0.000159 | False |
| 17 | 0.434973 | 0.934133 | dummy_constant | 0.434973 | 0.934133 | -0.015356 | 0.192700 | 0.000164 | False |
| 18 | 0.902080 | 0.383827 | dummy_constant | 0.902080 | 0.383827 | -0.137754 | 0.175165 | 0.000157 | False |
| 19 | 1.026986 | 0.040093 | dummy_constant | 1.026986 | 0.040093 | -0.024829 | 0.489228 | 0.000165 | False |
| 20 | 0.936553 | 0.403149 | dummy_constant | 0.936553 | 0.403149 | -0.057916 | 0.199958 | 0.000168 | False |
| 21 | 0.684478 | 0.774763 | dummy_constant | 0.684478 | 0.774763 | 0.013789 | 0.109527 | 0.000158 | False |
| 22 | 0.014779 | 1.010722 | dummy_constant | 0.014779 | 1.010722 | -0.075499 | 0.496276 | 0.000158 | False |
| 23 | 0.824602 | 0.666005 | dummy_constant | 0.824602 | 0.666005 | 0.136017 | 0.132924 | 0.000182 | False |
| 24 | 0.085083 | 1.047275 | dummy_constant | 0.085083 | 1.047275 | 0.076987 | 0.471666 | 0.000180 | False |
| 25 | 0.022017 | 0.000000 | dummy_constant | 0.022017 | 0.000000 | -1.099515 | 0.478467 | 0.000177 | False |
| 26 | 1.050884 | 0.068011 | dummy_constant | 1.050884 | 0.068011 | 0.057848 | 0.490088 | 0.000177 | False |
| 27 | 0.963228 | 0.462706 | dummy_constant | 0.963228 | 0.462706 | 0.078341 | 0.215971 | 0.000161 | False |
| 28 | 0.022453 | 0.000078 | dummy_constant | 0.022453 | 0.000078 | -1.099343 | 0.477974 | 0.000170 | False |
| 29 | 0.036150 | 1.033686 | dummy_constant | 0.036150 | 1.033686 | -0.014947 | 0.499978 | 0.000331 | False |
| 30 | 1.016185 | 0.025547 | dummy_constant | 1.016185 | 0.025547 | -0.058737 | 0.491552 | 0.000161 | False |
| 31 | 0.785797 | 0.690130 | dummy_constant | 0.785797 | 0.690130 | 0.042760 | 0.117830 | 0.000164 | False |
plot results¶
In [4]:
Copied!
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")
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¶
In [5]:
Copied!
ax = history.plot("x1", "x2")
ax.set_ylim(0, 3.14)
ax.set_xlim(0, 3.14)
ax.set_aspect("equal")
ax = history.plot("x1", "x2")
ax.set_ylim(0, 3.14)
ax.set_xlim(0, 3.14)
ax.set_aspect("equal")
In [6]:
Copied!
## visualize model
X.generator.visualize_model(show_feasibility=True)
## visualize model
X.generator.visualize_model(show_feasibility=True)
Out[6]:
(<Figure size 800x1980 with 22 Axes>,
array([[<Axes: title={'center': 'Posterior Mean [y1]'}, xlabel='x1', ylabel='x2'>,
<Axes: title={'center': 'Posterior SD [y1]'}, xlabel='x1', ylabel='x2'>],
[<Axes: title={'center': 'Posterior Mean [y2]'}, xlabel='x1', ylabel='x2'>,
<Axes: title={'center': 'Posterior SD [y2]'}, xlabel='x1', ylabel='x2'>],
[<Axes: title={'center': 'Posterior Mean [c1]'}, xlabel='x1', ylabel='x2'>,
<Axes: title={'center': 'Posterior SD [c1]'}, xlabel='x1', ylabel='x2'>],
[<Axes: title={'center': 'Posterior Mean [c2]'}, xlabel='x1', ylabel='x2'>,
<Axes: title={'center': 'Posterior SD [c2]'}, xlabel='x1', ylabel='x2'>],
[<Axes: title={'center': 'Acq. Function'}, xlabel='x1', ylabel='x2'>,
<Axes: >],
[<Axes: title={'center': 'Feasibility'}, xlabel='x1', ylabel='x2'>,
<Axes: >]], dtype=object))
In [7]:
Copied!
X.generator.update_pareto_front_history()
X.generator.pareto_front_history.plot(y="hypervolume", label="Hypervolume")
X.generator.update_pareto_front_history()
X.generator.pareto_front_history.plot(y="hypervolume", label="Hypervolume")
Out[7]:
<Axes: >
In [8]:
Copied!
X.generator.pareto_front_history
X.generator.pareto_front_history
Out[8]:
| iteration | hypervolume | n_non_dominated | |
|---|---|---|---|
| 0 | 0 | 0.375000 | 1 |
| 1 | 1 | 0.500000 | 2 |
| 2 | 2 | 0.500000 | 2 |
| 3 | 3 | 0.500000 | 2 |
| 4 | 4 | 0.500000 | 2 |
| 5 | 5 | 0.500000 | 2 |
| 6 | 6 | 0.500000 | 2 |
| 7 | 7 | 0.766575 | 3 |
| 8 | 8 | 0.766575 | 3 |
| 9 | 9 | 0.969744 | 4 |
| 10 | 10 | 1.049307 | 4 |
| 11 | 11 | 1.134337 | 4 |
| 12 | 12 | 1.179081 | 5 |
| 13 | 13 | 1.179081 | 5 |
| 14 | 14 | 1.179081 | 5 |
| 15 | 15 | 1.220055 | 6 |
| 16 | 16 | 1.220055 | 6 |
| 17 | 17 | 1.220055 | 6 |
| 18 | 18 | 1.220055 | 6 |
| 19 | 19 | 1.220055 | 6 |
| 20 | 20 | 1.220055 | 6 |
| 21 | 21 | 1.246271 | 6 |
| 22 | 22 | 1.246271 | 6 |
| 23 | 23 | 1.246271 | 6 |
| 24 | 24 | 1.246271 | 6 |
| 25 | 25 | 1.246271 | 6 |
| 26 | 26 | 1.258766 | 7 |
| 27 | 27 | 1.268194 | 8 |
| 28 | 28 | 1.268194 | 8 |
| 29 | 29 | 1.268194 | 8 |
| 30 | 30 | 1.268194 | 8 |
| 31 | 31 | 1.269092 | 9 |
In [9]:
Copied!
X.data
X.data
Out[9]:
| x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.000000 | 0.750000 | dummy_constant | 1.000000 | 0.750000 | 0.626888 | 0.312500 | 0.000171 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000141 | False |
| 2 | 1.673149 | 0.372832 | dummy_constant | 1.673149 | 0.372832 | 2.031794 | 1.392451 | 0.000168 | False |
| 3 | 0.570814 | 0.294095 | dummy_constant | 0.570814 | 0.294095 | -0.611642 | 0.047411 | 0.000164 | False |
| 4 | 0.024747 | 0.008495 | dummy_constant | 0.024747 | 0.008495 | -1.053990 | 0.467443 | 0.000163 | False |
| 5 | 0.989565 | 0.002450 | dummy_constant | 0.989565 | 0.002450 | -0.120678 | 0.487230 | 0.000166 | False |
| 6 | 0.419617 | 0.874213 | dummy_constant | 0.419617 | 0.874213 | -0.123616 | 0.146497 | 0.000159 | False |
| 7 | 1.022063 | 0.192238 | dummy_constant | 1.022063 | 0.192238 | 0.180177 | 0.367267 | 0.000162 | False |
| 8 | 0.012211 | 3.141590 | dummy_constant | 0.012211 | 3.141590 | 8.769930 | 7.215936 | 0.000166 | False |
| 9 | 0.302307 | 1.046188 | dummy_constant | 0.302307 | 1.046188 | 0.206903 | 0.337404 | 0.000167 | False |
| 10 | 0.796411 | 0.622960 | dummy_constant | 0.796411 | 0.622960 | 0.058959 | 0.102979 | 0.000163 | False |
| 11 | 0.155367 | 1.015335 | dummy_constant | 0.155367 | 1.015335 | 0.130743 | 0.384342 | 0.000175 | False |
| 12 | 1.035883 | 0.095831 | dummy_constant | 1.035883 | 0.095831 | 0.072770 | 0.450524 | 0.000165 | False |
| 13 | 0.026549 | 0.981313 | dummy_constant | 0.026549 | 0.981313 | -0.127100 | 0.455818 | 0.000166 | False |
| 14 | 0.042011 | 1.027173 | dummy_constant | 0.042011 | 1.027173 | -0.022514 | 0.487666 | 0.000161 | False |
| 15 | 0.066857 | 1.037074 | dummy_constant | 0.066857 | 1.037074 | 0.028514 | 0.476061 | 0.000194 | False |
| 16 | 0.037551 | 0.004058 | dummy_constant | 0.037551 | 0.004058 | -0.983492 | 0.459818 | 0.000159 | False |
| 17 | 0.434973 | 0.934133 | dummy_constant | 0.434973 | 0.934133 | -0.015356 | 0.192700 | 0.000164 | False |
| 18 | 0.902080 | 0.383827 | dummy_constant | 0.902080 | 0.383827 | -0.137754 | 0.175165 | 0.000157 | False |
| 19 | 1.026986 | 0.040093 | dummy_constant | 1.026986 | 0.040093 | -0.024829 | 0.489228 | 0.000165 | False |
| 20 | 0.936553 | 0.403149 | dummy_constant | 0.936553 | 0.403149 | -0.057916 | 0.199958 | 0.000168 | False |
| 21 | 0.684478 | 0.774763 | dummy_constant | 0.684478 | 0.774763 | 0.013789 | 0.109527 | 0.000158 | False |
| 22 | 0.014779 | 1.010722 | dummy_constant | 0.014779 | 1.010722 | -0.075499 | 0.496276 | 0.000158 | False |
| 23 | 0.824602 | 0.666005 | dummy_constant | 0.824602 | 0.666005 | 0.136017 | 0.132924 | 0.000182 | False |
| 24 | 0.085083 | 1.047275 | dummy_constant | 0.085083 | 1.047275 | 0.076987 | 0.471666 | 0.000180 | False |
| 25 | 0.022017 | 0.000000 | dummy_constant | 0.022017 | 0.000000 | -1.099515 | 0.478467 | 0.000177 | False |
| 26 | 1.050884 | 0.068011 | dummy_constant | 1.050884 | 0.068011 | 0.057848 | 0.490088 | 0.000177 | False |
| 27 | 0.963228 | 0.462706 | dummy_constant | 0.963228 | 0.462706 | 0.078341 | 0.215971 | 0.000161 | False |
| 28 | 0.022453 | 0.000078 | dummy_constant | 0.022453 | 0.000078 | -1.099343 | 0.477974 | 0.000170 | False |
| 29 | 0.036150 | 1.033686 | dummy_constant | 0.036150 | 1.033686 | -0.014947 | 0.499978 | 0.000331 | False |
| 30 | 1.016185 | 0.025547 | dummy_constant | 1.016185 | 0.025547 | -0.058737 | 0.491552 | 0.000161 | False |
| 31 | 0.785797 | 0.690130 | dummy_constant | 0.785797 | 0.690130 | 0.042760 | 0.117830 | 0.000164 | False |