Specifiying generator options¶
We start with the generator defaults and modify as needed for conservative exploration, which should prevent any constraint violations.
In [1]:
Copied!
# set values if testing
import os
from copy import deepcopy
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import BayesianExplorationGenerator
from xopt.resources.test_functions.tnk import evaluate_TNK, tnk_vocs
from xopt.vocs import get_feasibility_data
# Ignore all warnings
import warnings
warnings.filterwarnings("ignore")
SMOKE_TEST = os.environ.get("SMOKE_TEST")
NUM_MC_SAMPLES = 1 if SMOKE_TEST else 128
NUM_RESTARTS = 1 if SMOKE_TEST else 20
vocs = deepcopy(tnk_vocs)
vocs.objectives = {"y1": "EXPLORE"}
generator = BayesianExplorationGenerator(vocs=vocs)
generator.numerical_optimizer.n_restarts = NUM_RESTARTS
generator.numerical_optimizer.max_iter = 100
generator.n_monte_carlo_samples = NUM_MC_SAMPLES
generator.n_interpolate_points = 5
evaluator = Evaluator(function=evaluate_TNK)
X = Xopt(generator=generator, evaluator=evaluator)
X
# set values if testing
import os
from copy import deepcopy
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import BayesianExplorationGenerator
from xopt.resources.test_functions.tnk import evaluate_TNK, tnk_vocs
from xopt.vocs import get_feasibility_data
# Ignore all warnings
import warnings
warnings.filterwarnings("ignore")
SMOKE_TEST = os.environ.get("SMOKE_TEST")
NUM_MC_SAMPLES = 1 if SMOKE_TEST else 128
NUM_RESTARTS = 1 if SMOKE_TEST else 20
vocs = deepcopy(tnk_vocs)
vocs.objectives = {"y1": "EXPLORE"}
generator = BayesianExplorationGenerator(vocs=vocs)
generator.numerical_optimizer.n_restarts = NUM_RESTARTS
generator.numerical_optimizer.max_iter = 100
generator.n_monte_carlo_samples = NUM_MC_SAMPLES
generator.n_interpolate_points = 5
evaluator = Evaluator(function=evaluate_TNK)
X = Xopt(generator=generator, evaluator=evaluator)
X
Out[1]:
Xopt
________________________________
Version: 0.1.dev1+g210ad3716
Data size: 0
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: false
max_travel_distances: null
model: null
n_candidates: 1
n_interpolate_points: 5
n_monte_carlo_samples: 128
name: bayesian_exploration
numerical_optimizer:
discrete_max_batch_size: 2048
discrete_max_choices: 4096
max_iter: 100
max_time: 5.0
mixed_max_discrete_configurations: 512
n_restarts: 20
name: LBFGS
returns_id: false
supports_batch_generation: true
supports_constraints: true
supports_discrete_variables: true
supports_multi_objective: true
supports_single_objective: true
turbo_controller: null
use_cuda: 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:
y1:
dtype: null
type: ExploreObjective
observables: {}
variables:
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 exploration¶
We start with evaluating 2 points that we know satisfy the constraints. We then run 30 exploration steps.
In [2]:
Copied!
X.evaluate_data({"x1": [1.0, 0.75], "x2": [0.7, 0.95]})
X.evaluate_data({"x1": [1.0, 0.75], "x2": [0.7, 0.95]})
Out[2]:
| x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.00 | 0.70 | dummy_constant | 1.00 | 0.70 | 0.584045 | 0.290 | 0.003915 | False |
| 1 | 0.75 | 0.95 | dummy_constant | 0.75 | 0.95 | 0.494833 | 0.265 | 0.000146 | False |
In [3]:
Copied!
for i in range(20):
print(f"step {i}")
X.step()
for i in range(20):
print(f"step {i}")
X.step()
step 0
step 1
step 2
step 3
step 4
step 5
step 6
step 7
step 8
step 9
step 10
step 11
step 12
step 13
step 14
step 15
step 16
step 17
step 18
step 19
In [4]:
Copied!
# view the data
X.data
# view the data
X.data
Out[4]:
| x1 | x2 | a | y1 | y2 | c1 | c2 | xopt_runtime | xopt_error | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.000000 | 0.700000 | dummy_constant | 1.000000 | 0.700000 | 0.584045 | 0.290000 | 0.003915 | False |
| 1 | 0.750000 | 0.950000 | dummy_constant | 0.750000 | 0.950000 | 0.494833 | 0.265000 | 0.000146 | False |
| 2 | 1.228318 | 1.388318 | dummy_constant | 1.228318 | 1.388318 | 2.380252 | 1.319556 | 0.013650 | False |
| 3 | 1.706636 | 1.826636 | dummy_constant | 1.706636 | 1.826636 | 5.163599 | 3.215934 | 0.005981 | False |
| 4 | 2.184954 | 2.264954 | dummy_constant | 2.184954 | 2.264954 | 8.808148 | 5.954133 | 0.000160 | False |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 97 | 0.246362 | 0.833021 | dummy_constant | 0.246362 | 0.833021 | -0.234245 | 0.175235 | 0.000156 | False |
| 98 | 0.443210 | 0.641547 | dummy_constant | 0.443210 | 0.641547 | -0.295045 | 0.023261 | 0.000140 | False |
| 99 | 0.640058 | 0.450073 | dummy_constant | 0.640058 | 0.450073 | -0.294926 | 0.022109 | 0.000099 | False |
| 100 | 0.836906 | 0.258599 | dummy_constant | 0.836906 | 0.258599 | -0.240967 | 0.171780 | 0.000132 | False |
| 101 | 1.033754 | 0.067125 | dummy_constant | 1.033754 | 0.067125 | 0.022313 | 0.472274 | 0.000131 | False |
102 rows × 9 columns
In [5]:
Copied!
# plot results
ax = X.data.plot("x1", "x2")
ax.set_aspect("equal")
# plot results
ax = X.data.plot("x1", "x2")
ax.set_aspect("equal")
Introspect models, acquisition function and feasibility prediction¶
During exploration we generate Gaussian Process models of each objective and constraint. We demonstrate how they are viewed below.
In [6]:
Copied!
fig, ax = X.generator.visualize_model(show_feasibility=True, n_grid=100)
fig, ax = X.generator.visualize_model(show_feasibility=True, n_grid=100)
Generator model hyperparameters¶
In [7]:
Copied!
# print generator model hyperparameters
for name, val in X.generator.model.named_parameters():
print(f"{name}:{val}")
X.generator.model.models[2].covar_module.lengthscale
# print generator model hyperparameters
for name, val in X.generator.model.named_parameters():
print(f"{name}:{val}")
X.generator.model.models[2].covar_module.lengthscale
models.0.likelihood.noise_covar.raw_noise:Parameter containing: tensor([0.0001], requires_grad=True) models.0.mean_module.raw_constant:Parameter containing: tensor(1.4424, requires_grad=True) models.0.covar_module.raw_lengthscale:Parameter containing: tensor([[0.6808, 7.8367]], requires_grad=True) models.1.likelihood.noise_covar.raw_noise:Parameter containing: tensor([0.0002], requires_grad=True) models.1.mean_module.raw_constant:Parameter containing: tensor(2.1180, requires_grad=True) models.1.covar_module.raw_lengthscale:Parameter containing: tensor([[0.4557, 0.3229]], requires_grad=True) models.2.likelihood.noise_covar.raw_noise:Parameter containing: tensor([0.0001], requires_grad=True) models.2.mean_module.raw_constant:Parameter containing: tensor(3.6843, requires_grad=True) models.2.covar_module.raw_lengthscale:Parameter containing: tensor([[0.5733, 0.5500]], requires_grad=True)
Out[7]:
Parameter containing: tensor([[0.5733, 0.5500]], requires_grad=True)
Examine the number of constraint violations¶
Using the convience function provided by the vocs object we can evaluate which samples violate either or both of our constraints.
In [8]:
Copied!
get_feasibility_data(X.vocs, X.data)
get_feasibility_data(X.vocs, X.data)
Out[8]:
| feasible_c1 | feasible_c2 | feasible | |
|---|---|---|---|
| 0 | True | True | True |
| 1 | True | True | True |
| 2 | True | False | False |
| 3 | True | False | False |
| 4 | True | False | False |
| ... | ... | ... | ... |
| 97 | False | True | False |
| 98 | False | True | False |
| 99 | False | True | False |
| 100 | False | True | False |
| 101 | True | True | True |
102 rows × 3 columns
In [9]:
Copied!
# generate next point
X.generator.generate(1)
# generate next point
X.generator.generate(1)
Out[9]:
[{'x1': 1.033461535009296, 'x2': 0.06667547223958985},
{'x1': 1.0331692739579357, 'x2': 0.06622613794857253},
{'x1': 1.0328770129065752, 'x2': 0.06577680365755523},
{'x1': 1.032584751855215, 'x2': 0.06532746936653791},
{'x1': 1.0322924908038544, 'x2': 0.0648781350755206}]