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+gfcd953a52
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_contextual_variables: true
supports_discrete_variables: true
supports_multi_objective: true
supports_no_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.004670 | False |
| 1 | 0.75 | 0.95 | dummy_constant | 0.75 | 0.95 | 0.494833 | 0.265 | 0.000289 | 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.004670 | False |
| 1 | 0.750000 | 0.950000 | dummy_constant | 0.750000 | 0.950000 | 0.494833 | 0.265000 | 0.000289 | False |
| 2 | 1.228318 | 1.388318 | dummy_constant | 1.228318 | 1.388318 | 2.380252 | 1.319556 | 0.008171 | False |
| 3 | 1.706636 | 1.826636 | dummy_constant | 1.706636 | 1.826636 | 5.163599 | 3.215934 | 0.005467 | False |
| 4 | 2.184954 | 2.264954 | dummy_constant | 2.184954 | 2.264954 | 8.808148 | 5.954133 | 0.002499 | False |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 97 | 0.776085 | 0.229743 | dummy_constant | 0.776085 | 0.229743 | -0.334188 | 0.149261 | 0.000265 | False |
| 98 | 0.846506 | 0.196553 | dummy_constant | 0.846506 | 0.196553 | -0.157462 | 0.212147 | 0.000245 | False |
| 99 | 0.916928 | 0.163363 | dummy_constant | 0.916928 | 0.163363 | -0.037651 | 0.287153 | 0.000253 | False |
| 100 | 0.987349 | 0.130172 | dummy_constant | 0.987349 | 0.130172 | 0.042059 | 0.374282 | 0.000258 | False |
| 101 | 1.057771 | 0.096982 | dummy_constant | 1.057771 | 0.096982 | 0.117514 | 0.473532 | 0.000267 | 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.6987, requires_grad=True) models.0.covar_module.raw_lengthscale:Parameter containing: tensor([[0.4193, 8.1520]], requires_grad=True) models.1.likelihood.noise_covar.raw_noise:Parameter containing: tensor([0.0006], requires_grad=True) models.1.mean_module.raw_constant:Parameter containing: tensor(2.7754, requires_grad=True) models.1.covar_module.raw_lengthscale:Parameter containing: tensor([[0.5533, 0.4181]], 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.6182, requires_grad=True) models.2.covar_module.raw_lengthscale:Parameter containing: tensor([[0.4276, 0.5652]], requires_grad=True)
Out[7]:
Parameter containing: tensor([[0.4276, 0.5652]], 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 | True | True | True |
| 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': 0.8549208393671022, 'x2': 0.28104625663975047},
{'x1': 0.6520706702985127, 'x2': 0.4651102978753213},
{'x1': 0.4492205012299233, 'x2': 0.6491743391108922},
{'x1': 0.24637033216133397, 'x2': 0.8332383803464631},
{'x1': 0.04352016309274459, 'x2': 1.0173024215820339}]