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/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': {}}
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.000200 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000147 | False |
| 2 | 1.200572 | 0.209261 | dummy_constant | 1.200572 | 0.209261 | 0.578010 | 0.575330 | 0.005630 | False |
| 3 | 0.622550 | 2.614008 | dummy_constant | 0.622550 | 2.614008 | 6.303182 | 4.484049 | 0.006105 | False |
| 4 | 0.152128 | 0.421849 | dummy_constant | 0.152128 | 0.421849 | -0.872375 | 0.127123 | 0.005201 | False |
| 5 | 0.000000 | 1.014355 | dummy_constant | 0.000000 | 1.014355 | -0.071084 | 0.514561 | 0.008362 | False |
| 6 | 3.141590 | 0.000000 | dummy_constant | 3.141590 | 0.000000 | 8.769588 | 7.227998 | 0.002934 | False |
| 7 | 1.091615 | 0.066447 | dummy_constant | 1.091615 | 0.066447 | 0.139733 | 0.537977 | 0.005341 | False |
| 8 | 0.190775 | 1.070569 | dummy_constant | 0.190775 | 1.070569 | 0.277436 | 0.421169 | 0.000150 | False |
| 9 | 1.059136 | 0.046217 | dummy_constant | 1.059136 | 0.046217 | 0.047277 | 0.518552 | 0.000250 | False |
| 10 | 1.056371 | 0.411265 | dummy_constant | 1.056371 | 0.411265 | 0.190882 | 0.317423 | 0.004777 | False |
| 11 | 1.081684 | 0.122833 | dummy_constant | 1.081684 | 0.122833 | 0.208740 | 0.480611 | 0.005176 | False |
| 12 | 0.718721 | 0.796147 | dummy_constant | 0.718721 | 0.796147 | 0.081973 | 0.135542 | 0.002375 | False |
| 13 | 0.066638 | 1.032700 | dummy_constant | 0.066638 | 1.032700 | 0.019515 | 0.471572 | 0.002819 | False |
| 14 | 0.949060 | 0.516100 | dummy_constant | 0.949060 | 0.516100 | 0.178566 | 0.201914 | 0.000207 | False |
| 15 | 0.687087 | 0.844516 | dummy_constant | 0.687087 | 0.844516 | 0.192094 | 0.153693 | 0.000147 | False |
| 16 | 0.749951 | 0.718992 | dummy_constant | 0.749951 | 0.718992 | -0.014993 | 0.110433 | 0.000197 | False |
| 17 | 1.041533 | 0.054539 | dummy_constant | 1.041533 | 0.054539 | 0.020802 | 0.491694 | 0.002497 | False |
| 18 | 0.042266 | 1.024486 | dummy_constant | 0.042266 | 1.024486 | -0.027659 | 0.484605 | 0.002675 | False |
| 19 | 0.000000 | 0.000000 | dummy_constant | 0.000000 | 0.000000 | -1.100000 | 0.500000 | 0.002263 | False |
| 20 | 1.027577 | 0.408301 | dummy_constant | 1.027577 | 0.408301 | 0.125298 | 0.286746 | 0.002465 | False |
| 21 | 0.187828 | 1.001253 | dummy_constant | 0.187828 | 1.001253 | 0.136267 | 0.348706 | 0.002709 | False |
| 22 | 0.902561 | 0.534154 | dummy_constant | 0.902561 | 0.534154 | 0.164064 | 0.163222 | 0.003056 | False |
| 23 | 0.397843 | 0.925733 | dummy_constant | 0.397843 | 0.925733 | -0.082519 | 0.191684 | 0.000167 | False |
| 24 | 0.027704 | 1.024683 | dummy_constant | 0.027704 | 1.024683 | -0.040050 | 0.498355 | 0.002115 | False |
| 25 | 0.504045 | 0.891383 | dummy_constant | 0.504045 | 0.891383 | 0.085739 | 0.153197 | 0.000158 | False |
| 26 | 0.655820 | 0.830485 | dummy_constant | 0.655820 | 0.830485 | 0.149442 | 0.133500 | 0.000170 | False |
| 27 | 0.022662 | 1.017569 | dummy_constant | 0.022662 | 1.017569 | -0.057760 | 0.495729 | 0.000166 | False |
| 28 | 1.023018 | 0.008732 | dummy_constant | 1.023018 | 0.008732 | -0.052427 | 0.514892 | 0.000167 | False |
| 29 | 1.028859 | 0.068942 | dummy_constant | 1.028859 | 0.068942 | 0.015339 | 0.465503 | 0.000158 | False |
| 30 | 1.005291 | 0.261901 | dummy_constant | 1.005291 | 0.261901 | 0.138494 | 0.312010 | 0.000154 | False |
| 31 | 0.980664 | 0.000000 | dummy_constant | 0.980664 | 0.000000 | -0.138298 | 0.481038 | 0.000183 | 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.500000 | 2 |
| 8 | 8 | 0.740149 | 3 |
| 9 | 9 | 0.740149 | 3 |
| 10 | 10 | 0.890421 | 4 |
| 11 | 11 | 1.011077 | 5 |
| 12 | 12 | 1.070624 | 5 |
| 13 | 13 | 1.148626 | 5 |
| 14 | 14 | 1.176076 | 5 |
| 15 | 15 | 1.182030 | 6 |
| 16 | 16 | 1.182030 | 6 |
| 17 | 17 | 1.226476 | 5 |
| 18 | 18 | 1.226476 | 5 |
| 19 | 19 | 1.226476 | 5 |
| 20 | 20 | 1.227981 | 6 |
| 21 | 21 | 1.243681 | 7 |
| 22 | 22 | 1.255863 | 8 |
| 23 | 23 | 1.255863 | 8 |
| 24 | 24 | 1.255863 | 8 |
| 25 | 25 | 1.275974 | 9 |
| 26 | 26 | 1.278322 | 9 |
| 27 | 27 | 1.278322 | 9 |
| 28 | 28 | 1.278322 | 9 |
| 29 | 29 | 1.282623 | 10 |
| 30 | 30 | 1.288476 | 10 |
| 31 | 31 | 1.288476 | 10 |
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.000200 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000147 | False |
| 2 | 1.200572 | 0.209261 | dummy_constant | 1.200572 | 0.209261 | 0.578010 | 0.575330 | 0.005630 | False |
| 3 | 0.622550 | 2.614008 | dummy_constant | 0.622550 | 2.614008 | 6.303182 | 4.484049 | 0.006105 | False |
| 4 | 0.152128 | 0.421849 | dummy_constant | 0.152128 | 0.421849 | -0.872375 | 0.127123 | 0.005201 | False |
| 5 | 0.000000 | 1.014355 | dummy_constant | 0.000000 | 1.014355 | -0.071084 | 0.514561 | 0.008362 | False |
| 6 | 3.141590 | 0.000000 | dummy_constant | 3.141590 | 0.000000 | 8.769588 | 7.227998 | 0.002934 | False |
| 7 | 1.091615 | 0.066447 | dummy_constant | 1.091615 | 0.066447 | 0.139733 | 0.537977 | 0.005341 | False |
| 8 | 0.190775 | 1.070569 | dummy_constant | 0.190775 | 1.070569 | 0.277436 | 0.421169 | 0.000150 | False |
| 9 | 1.059136 | 0.046217 | dummy_constant | 1.059136 | 0.046217 | 0.047277 | 0.518552 | 0.000250 | False |
| 10 | 1.056371 | 0.411265 | dummy_constant | 1.056371 | 0.411265 | 0.190882 | 0.317423 | 0.004777 | False |
| 11 | 1.081684 | 0.122833 | dummy_constant | 1.081684 | 0.122833 | 0.208740 | 0.480611 | 0.005176 | False |
| 12 | 0.718721 | 0.796147 | dummy_constant | 0.718721 | 0.796147 | 0.081973 | 0.135542 | 0.002375 | False |
| 13 | 0.066638 | 1.032700 | dummy_constant | 0.066638 | 1.032700 | 0.019515 | 0.471572 | 0.002819 | False |
| 14 | 0.949060 | 0.516100 | dummy_constant | 0.949060 | 0.516100 | 0.178566 | 0.201914 | 0.000207 | False |
| 15 | 0.687087 | 0.844516 | dummy_constant | 0.687087 | 0.844516 | 0.192094 | 0.153693 | 0.000147 | False |
| 16 | 0.749951 | 0.718992 | dummy_constant | 0.749951 | 0.718992 | -0.014993 | 0.110433 | 0.000197 | False |
| 17 | 1.041533 | 0.054539 | dummy_constant | 1.041533 | 0.054539 | 0.020802 | 0.491694 | 0.002497 | False |
| 18 | 0.042266 | 1.024486 | dummy_constant | 0.042266 | 1.024486 | -0.027659 | 0.484605 | 0.002675 | False |
| 19 | 0.000000 | 0.000000 | dummy_constant | 0.000000 | 0.000000 | -1.100000 | 0.500000 | 0.002263 | False |
| 20 | 1.027577 | 0.408301 | dummy_constant | 1.027577 | 0.408301 | 0.125298 | 0.286746 | 0.002465 | False |
| 21 | 0.187828 | 1.001253 | dummy_constant | 0.187828 | 1.001253 | 0.136267 | 0.348706 | 0.002709 | False |
| 22 | 0.902561 | 0.534154 | dummy_constant | 0.902561 | 0.534154 | 0.164064 | 0.163222 | 0.003056 | False |
| 23 | 0.397843 | 0.925733 | dummy_constant | 0.397843 | 0.925733 | -0.082519 | 0.191684 | 0.000167 | False |
| 24 | 0.027704 | 1.024683 | dummy_constant | 0.027704 | 1.024683 | -0.040050 | 0.498355 | 0.002115 | False |
| 25 | 0.504045 | 0.891383 | dummy_constant | 0.504045 | 0.891383 | 0.085739 | 0.153197 | 0.000158 | False |
| 26 | 0.655820 | 0.830485 | dummy_constant | 0.655820 | 0.830485 | 0.149442 | 0.133500 | 0.000170 | False |
| 27 | 0.022662 | 1.017569 | dummy_constant | 0.022662 | 1.017569 | -0.057760 | 0.495729 | 0.000166 | False |
| 28 | 1.023018 | 0.008732 | dummy_constant | 1.023018 | 0.008732 | -0.052427 | 0.514892 | 0.000167 | False |
| 29 | 1.028859 | 0.068942 | dummy_constant | 1.028859 | 0.068942 | 0.015339 | 0.465503 | 0.000158 | False |
| 30 | 1.005291 | 0.261901 | dummy_constant | 1.005291 | 0.261901 | 0.138494 | 0.312010 | 0.000154 | False |
| 31 | 0.980664 | 0.000000 | dummy_constant | 0.980664 | 0.000000 | -0.138298 | 0.481038 | 0.000183 | False |