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.000122 | False |
1 | 0.750000 | 1.000000 | dummy_constant | 0.750000 | 1.000000 | 0.626888 | 0.312500 | 0.000135 | False |
2 | 0.264314 | 1.517085 | dummy_constant | 0.264314 | 1.517085 | 1.464212 | 1.090010 | 0.000152 | False |
3 | 0.121347 | 0.634433 | dummy_constant | 0.121347 | 0.634433 | -0.483463 | 0.161450 | 0.000151 | False |
4 | 0.000000 | 0.000000 | dummy_constant | 0.000000 | 0.000000 | -1.100000 | 0.500000 | 0.000151 | False |
5 | 0.005694 | 0.927092 | dummy_constant | 0.005694 | 0.927092 | -0.239986 | 0.426746 | 0.000151 | False |
6 | 0.275152 | 0.977640 | dummy_constant | 0.275152 | 0.977640 | 0.063213 | 0.278697 | 0.000152 | False |
7 | 3.141590 | 0.000000 | dummy_constant | 3.141590 | 0.000000 | 8.769588 | 7.227998 | 0.000118 | False |
8 | 0.959644 | 0.204618 | dummy_constant | 0.959644 | 0.204618 | 0.060382 | 0.298523 | 0.000151 | False |
9 | 1.015889 | 0.124754 | dummy_constant | 1.015889 | 0.124754 | 0.085083 | 0.406951 | 0.000164 | False |
10 | 0.082141 | 1.024671 | dummy_constant | 0.082141 | 1.024671 | 0.028015 | 0.449886 | 0.000152 | False |
11 | 0.580189 | 0.817477 | dummy_constant | 0.580189 | 0.817477 | 0.094894 | 0.107222 | 0.000149 | False |
12 | 1.023306 | 0.051914 | dummy_constant | 1.023306 | 0.051914 | -0.019026 | 0.474630 | 0.000108 | False |
13 | 0.850031 | 0.564281 | dummy_constant | 0.850031 | 0.564281 | 0.140850 | 0.126654 | 0.000148 | False |
14 | 0.074521 | 1.048222 | dummy_constant | 0.074521 | 1.048222 | 0.062161 | 0.481580 | 0.000152 | False |
15 | 0.412779 | 0.888111 | dummy_constant | 0.412779 | 0.888111 | -0.118750 | 0.158238 | 0.000148 | False |
16 | 0.940702 | 0.404917 | dummy_constant | 0.940702 | 0.404917 | -0.048705 | 0.203259 | 0.000148 | False |
17 | 0.030030 | 1.018220 | dummy_constant | 0.030030 | 1.018220 | -0.051403 | 0.489424 | 0.000147 | False |
18 | 1.000415 | 0.002854 | dummy_constant | 1.000415 | 0.002854 | -0.099057 | 0.497570 | 0.000147 | False |
19 | 0.982167 | 0.000000 | dummy_constant | 0.982167 | 0.000000 | -0.135348 | 0.482485 | 0.000148 | False |
20 | 1.038867 | 0.171349 | dummy_constant | 1.038867 | 0.171349 | 0.195081 | 0.398389 | 0.000147 | False |
21 | 0.407737 | 0.918308 | dummy_constant | 0.407737 | 0.918308 | -0.082465 | 0.183494 | 0.000148 | False |
22 | 0.000000 | 0.040452 | dummy_constant | 0.000000 | 0.040452 | -1.098364 | 0.461184 | 0.000148 | False |
23 | 0.703809 | 0.738255 | dummy_constant | 0.703809 | 0.738255 | -0.052421 | 0.098303 | 0.000149 | False |
24 | 0.000240 | 0.997206 | dummy_constant | 0.000240 | 0.997206 | -0.105579 | 0.496974 | 0.000147 | False |
25 | 0.069914 | 1.056705 | dummy_constant | 0.069914 | 1.056705 | 0.072370 | 0.494895 | 0.000129 | False |
26 | 0.735013 | 0.741395 | dummy_constant | 0.735013 | 0.741395 | -0.009851 | 0.113503 | 0.000148 | False |
27 | 1.025572 | 0.039442 | dummy_constant | 1.025572 | 0.039442 | -0.028322 | 0.488339 | 0.000148 | False |
28 | 0.584059 | 0.851467 | dummy_constant | 0.584059 | 0.851467 | 0.164227 | 0.130595 | 0.000147 | False |
29 | 1.022825 | 0.294977 | dummy_constant | 1.022825 | 0.294977 | 0.155004 | 0.315381 | 0.000147 | False |
30 | 0.041583 | 1.034302 | dummy_constant | 0.041583 | 1.034302 | -0.008526 | 0.495625 | 0.000148 | False |
31 | 0.000000 | 0.986735 | dummy_constant | 0.000000 | 0.986735 | -0.126354 | 0.486911 | 0.000148 | 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()
## visualize model
X.generator.visualize_model()
Out[6]:
(<Figure size 800x1650 with 19 Axes>, array([[<Axes: title={'center': 'Posterior Mean [y1]'}, ylabel='x2'>, <Axes: title={'center': 'Posterior SD [y1]'}>], [<Axes: title={'center': 'Posterior Mean [y2]'}, ylabel='x2'>, <Axes: title={'center': 'Posterior SD [y2]'}>], [<Axes: title={'center': 'Posterior Mean [c1]'}, ylabel='x2'>, <Axes: title={'center': 'Posterior SD [c1]'}>], [<Axes: title={'center': 'Posterior Mean [c2]'}, ylabel='x2'>, <Axes: title={'center': 'Posterior SD [c2]'}>], [<Axes: title={'center': 'Acq. Function'}, xlabel='x1', ylabel='x2'>, <Axes: xlabel='x1'>]], dtype=object))