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
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
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())
{'variables': {'x1': (0.0, 3.14159), 'x2': (0.0, 3.14159)}, 'constraints': {'c1': ('GREATER_THAN', 0.0), 'c2': ('LESS_THAN', 0.5)}, 'objectives': {'y1': 'MINIMIZE', 'y2': 'MINIMIZE'}, 'constants': {'a': 'dummy_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, vocs=tnk_vocs)
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, vocs=tnk_vocs)
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.000156 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000134 | False |
| 2 | 1.548480 | 0.275010 | dummy_constant | 1.548480 | 0.275010 | 1.568048 | 1.149932 | 0.000154 | False |
| 3 | 0.241011 | 0.000000 | dummy_constant | 0.241011 | 0.000000 | -1.041914 | 0.317075 | 0.000157 | False |
| 4 | 0.031652 | 0.809338 | dummy_constant | 0.031652 | 0.809338 | -0.425042 | 0.315040 | 0.000152 | False |
| 5 | 0.000000 | 3.110180 | dummy_constant | 0.000000 | 3.110180 | 8.573218 | 7.063039 | 0.000153 | False |
| 6 | 0.233733 | 1.000078 | dummy_constant | 0.233733 | 1.000078 | 0.140971 | 0.320976 | 0.000152 | False |
| 7 | 0.925835 | 0.164186 | dummy_constant | 0.925835 | 0.164186 | -0.021378 | 0.294107 | 0.000154 | False |
| 8 | 0.967857 | 0.053549 | dummy_constant | 0.967857 | 0.053549 | -0.123765 | 0.418209 | 0.000154 | False |
| 9 | 1.027448 | 0.096386 | dummy_constant | 1.027448 | 0.096386 | 0.057525 | 0.441106 | 0.000149 | False |
| 10 | 0.783281 | 0.553075 | dummy_constant | 0.783281 | 0.553075 | 0.011047 | 0.083065 | 0.000150 | False |
| 11 | 0.062426 | 0.992776 | dummy_constant | 0.062426 | 0.992776 | -0.064126 | 0.434299 | 0.000176 | False |
| 12 | 0.647138 | 0.763456 | dummy_constant | 0.647138 | 0.763456 | -0.023514 | 0.091059 | 0.000148 | False |
| 13 | 0.068081 | 1.032383 | dummy_constant | 0.068081 | 1.032383 | 0.021005 | 0.469986 | 0.000154 | False |
| 14 | 0.885582 | 0.446461 | dummy_constant | 0.885582 | 0.446461 | -0.053756 | 0.151540 | 0.000168 | False |
| 15 | 0.765232 | 0.672965 | dummy_constant | 0.765232 | 0.672965 | -0.013443 | 0.100265 | 0.000160 | False |
| 16 | 0.583791 | 0.857496 | dummy_constant | 0.583791 | 0.857496 | 0.175150 | 0.134825 | 0.000157 | False |
| 17 | 1.006785 | 0.016167 | dummy_constant | 1.006785 | 0.016167 | -0.082842 | 0.490925 | 0.000149 | False |
| 18 | 1.029390 | 0.050055 | dummy_constant | 1.029390 | 0.050055 | -0.009124 | 0.482705 | 0.000155 | False |
| 19 | 0.021907 | 1.023518 | dummy_constant | 0.021907 | 1.023518 | -0.046127 | 0.502644 | 0.000166 | False |
| 20 | 0.947668 | 0.344929 | dummy_constant | 0.947668 | 0.344929 | -0.059560 | 0.224453 | 0.000151 | False |
| 21 | 1.061133 | 0.158745 | dummy_constant | 1.061133 | 0.158745 | 0.223297 | 0.431325 | 0.000126 | False |
| 22 | 0.060807 | 1.044455 | dummy_constant | 0.060807 | 1.044455 | 0.034836 | 0.489322 | 0.000151 | False |
| 23 | 0.780059 | 0.622825 | dummy_constant | 0.780059 | 0.622825 | 0.017738 | 0.093519 | 0.000150 | False |
| 24 | 0.976133 | 0.288116 | dummy_constant | 0.976133 | 0.288116 | 0.047839 | 0.271598 | 0.000151 | False |
| 25 | 0.037181 | 0.030208 | dummy_constant | 0.037181 | 0.030208 | -0.989816 | 0.434906 | 0.000150 | False |
| 26 | 0.995252 | 0.181084 | dummy_constant | 0.995252 | 0.181084 | 0.119908 | 0.346982 | 0.000160 | False |
| 27 | 0.006461 | 0.025072 | dummy_constant | 0.006461 | 0.025072 | -0.936689 | 0.469137 | 0.000151 | False |
| 28 | 1.042925 | 0.059027 | dummy_constant | 1.042925 | 0.059027 | 0.029376 | 0.489224 | 0.000148 | False |
| 29 | 0.037069 | 1.024146 | dummy_constant | 0.037069 | 1.024146 | -0.033458 | 0.489034 | 0.000149 | False |
| 30 | 0.869251 | 0.501421 | dummy_constant | 0.869251 | 0.501421 | 0.056491 | 0.136348 | 0.000150 | False |
| 31 | 1.023102 | 0.029574 | dummy_constant | 1.023102 | 0.029574 | -0.041887 | 0.494937 | 0.000150 | 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, tnk_vocs.feasibility_data(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, tnk_vocs.feasibility_data(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.758094 | 3 |
| 7 | 7 | 0.758094 | 3 |
| 8 | 8 | 0.758094 | 3 |
| 9 | 9 | 1.066961 | 4 |
| 10 | 10 | 1.169223 | 4 |
| 11 | 11 | 1.169223 | 4 |
| 12 | 12 | 1.169223 | 4 |
| 13 | 13 | 1.246684 | 5 |
| 14 | 14 | 1.246684 | 5 |
| 15 | 15 | 1.246684 | 5 |
| 16 | 16 | 1.275125 | 5 |
| 17 | 17 | 1.275125 | 5 |
| 18 | 18 | 1.275125 | 5 |
| 19 | 19 | 1.275125 | 5 |
| 20 | 20 | 1.275125 | 5 |
| 21 | 21 | 1.275125 | 5 |
| 22 | 22 | 1.278439 | 6 |
| 23 | 23 | 1.279195 | 7 |
| 24 | 24 | 1.292791 | 8 |
| 25 | 25 | 1.292791 | 8 |
| 26 | 26 | 1.296237 | 9 |
| 27 | 27 | 1.296237 | 9 |
| 28 | 28 | 1.313313 | 10 |
| 29 | 29 | 1.313313 | 10 |
| 30 | 30 | 1.318834 | 11 |
| 31 | 31 | 1.318834 | 11 |
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.000156 | False |
| 1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000134 | False |
| 2 | 1.548480 | 0.275010 | dummy_constant | 1.548480 | 0.275010 | 1.568048 | 1.149932 | 0.000154 | False |
| 3 | 0.241011 | 0.000000 | dummy_constant | 0.241011 | 0.000000 | -1.041914 | 0.317075 | 0.000157 | False |
| 4 | 0.031652 | 0.809338 | dummy_constant | 0.031652 | 0.809338 | -0.425042 | 0.315040 | 0.000152 | False |
| 5 | 0.000000 | 3.110180 | dummy_constant | 0.000000 | 3.110180 | 8.573218 | 7.063039 | 0.000153 | False |
| 6 | 0.233733 | 1.000078 | dummy_constant | 0.233733 | 1.000078 | 0.140971 | 0.320976 | 0.000152 | False |
| 7 | 0.925835 | 0.164186 | dummy_constant | 0.925835 | 0.164186 | -0.021378 | 0.294107 | 0.000154 | False |
| 8 | 0.967857 | 0.053549 | dummy_constant | 0.967857 | 0.053549 | -0.123765 | 0.418209 | 0.000154 | False |
| 9 | 1.027448 | 0.096386 | dummy_constant | 1.027448 | 0.096386 | 0.057525 | 0.441106 | 0.000149 | False |
| 10 | 0.783281 | 0.553075 | dummy_constant | 0.783281 | 0.553075 | 0.011047 | 0.083065 | 0.000150 | False |
| 11 | 0.062426 | 0.992776 | dummy_constant | 0.062426 | 0.992776 | -0.064126 | 0.434299 | 0.000176 | False |
| 12 | 0.647138 | 0.763456 | dummy_constant | 0.647138 | 0.763456 | -0.023514 | 0.091059 | 0.000148 | False |
| 13 | 0.068081 | 1.032383 | dummy_constant | 0.068081 | 1.032383 | 0.021005 | 0.469986 | 0.000154 | False |
| 14 | 0.885582 | 0.446461 | dummy_constant | 0.885582 | 0.446461 | -0.053756 | 0.151540 | 0.000168 | False |
| 15 | 0.765232 | 0.672965 | dummy_constant | 0.765232 | 0.672965 | -0.013443 | 0.100265 | 0.000160 | False |
| 16 | 0.583791 | 0.857496 | dummy_constant | 0.583791 | 0.857496 | 0.175150 | 0.134825 | 0.000157 | False |
| 17 | 1.006785 | 0.016167 | dummy_constant | 1.006785 | 0.016167 | -0.082842 | 0.490925 | 0.000149 | False |
| 18 | 1.029390 | 0.050055 | dummy_constant | 1.029390 | 0.050055 | -0.009124 | 0.482705 | 0.000155 | False |
| 19 | 0.021907 | 1.023518 | dummy_constant | 0.021907 | 1.023518 | -0.046127 | 0.502644 | 0.000166 | False |
| 20 | 0.947668 | 0.344929 | dummy_constant | 0.947668 | 0.344929 | -0.059560 | 0.224453 | 0.000151 | False |
| 21 | 1.061133 | 0.158745 | dummy_constant | 1.061133 | 0.158745 | 0.223297 | 0.431325 | 0.000126 | False |
| 22 | 0.060807 | 1.044455 | dummy_constant | 0.060807 | 1.044455 | 0.034836 | 0.489322 | 0.000151 | False |
| 23 | 0.780059 | 0.622825 | dummy_constant | 0.780059 | 0.622825 | 0.017738 | 0.093519 | 0.000150 | False |
| 24 | 0.976133 | 0.288116 | dummy_constant | 0.976133 | 0.288116 | 0.047839 | 0.271598 | 0.000151 | False |
| 25 | 0.037181 | 0.030208 | dummy_constant | 0.037181 | 0.030208 | -0.989816 | 0.434906 | 0.000150 | False |
| 26 | 0.995252 | 0.181084 | dummy_constant | 0.995252 | 0.181084 | 0.119908 | 0.346982 | 0.000160 | False |
| 27 | 0.006461 | 0.025072 | dummy_constant | 0.006461 | 0.025072 | -0.936689 | 0.469137 | 0.000151 | False |
| 28 | 1.042925 | 0.059027 | dummy_constant | 1.042925 | 0.059027 | 0.029376 | 0.489224 | 0.000148 | False |
| 29 | 0.037069 | 1.024146 | dummy_constant | 0.037069 | 1.024146 | -0.033458 | 0.489034 | 0.000149 | False |
| 30 | 0.869251 | 0.501421 | dummy_constant | 0.869251 | 0.501421 | 0.056491 | 0.136348 | 0.000150 | False |
| 31 | 1.023102 | 0.029574 | dummy_constant | 1.023102 | 0.029574 | -0.041887 | 0.494937 | 0.000150 | False |
In [ ]:
Copied!