Working with Xopt generators¶
In [1]:
Copied!
# Import the class
from xopt.generators import generators, get_generator
from xopt.vocs import VOCS
from xopt import Xopt, Evaluator, Generator
import math
import numpy as np
# Import the class
from xopt.generators import generators, get_generator
from xopt.vocs import VOCS
from xopt import Xopt, Evaluator, Generator
import math
import numpy as np
In [2]:
Copied!
# named generators
generators.keys()
# named generators
generators.keys()
Out[2]:
dict_keys(['random'])
In [3]:
Copied!
# get default options for the upper confidence bound generator
generator_type = get_generator("upper_confidence_bound")
# get default options for the upper confidence bound generator
generator_type = get_generator("upper_confidence_bound")
In [4]:
Copied!
# define vocs for the problem
vocs = VOCS(
variables={"x": [0, 2 * math.pi]},
objectives={"f": "MINIMIZE"},
)
# define vocs for the problem
vocs = VOCS(
variables={"x": [0, 2 * math.pi]},
objectives={"f": "MINIMIZE"},
)
In [5]:
Copied!
# define a test function to optimize
def test_function(input_dict):
return {"f": np.sin(input_dict["x"])}
# define a test function to optimize
def test_function(input_dict):
return {"f": np.sin(input_dict["x"])}
In [6]:
Copied!
evaluator = Evaluator(function=test_function)
generator = generator_type(vocs=vocs)
X = Xopt(generator=generator, evaluator=evaluator, vocs=vocs)
X
evaluator = Evaluator(function=test_function)
generator = generator_type(vocs=vocs)
X = Xopt(generator=generator, evaluator=evaluator, vocs=vocs)
X
Out[6]:
Xopt ________________________________ Version: 2.4.6.dev5+ga295b108.d20250107 Data size: 0 Config as YAML: dump_file: null evaluator: function: __main__.test_function function_kwargs: {} max_workers: 1 vectorized: false generator: beta: 2.0 computation_time: null custom_objective: null fixed_features: null gp_constructor: covar_modules: {} custom_noise_prior: null mean_modules: {} name: standard trainable_mean_keys: [] transform_inputs: true use_cached_hyperparameters: false use_low_noise_prior: true log_transform_acquisition_function: false max_travel_distances: null memory_length: null model: null n_candidates: 1 n_interpolate_points: null n_monte_carlo_samples: 128 name: upper_confidence_bound numerical_optimizer: max_iter: 2000 max_time: null n_restarts: 20 name: LBFGS supports_batch_generation: true turbo_controller: null use_cuda: false max_evaluations: null serialize_inline: false serialize_torch: false strict: true vocs: constants: {} constraints: {} objectives: f: MINIMIZE observables: [] variables: x: - 0.0 - 6.283185307179586
In [7]:
Copied!
# run the optimization for a couple of iterations (see bayes_opt folder for
# more examples of ucb)
X.random_evaluate(2)
for i in range(4):
X.step()
# run the optimization for a couple of iterations (see bayes_opt folder for
# more examples of ucb)
X.random_evaluate(2)
for i in range(4):
X.step()
In [8]:
Copied!
X.data
X.data
Out[8]:
x | f | xopt_runtime | xopt_error | |
---|---|---|---|---|
0 | 1.995287 | 0.911249 | 0.000008 | False |
1 | 1.087539 | 0.885486 | 0.000002 | False |
2 | 0.513289 | 0.491045 | 0.000008 | False |
3 | 0.211815 | 0.210235 | 0.000007 | False |
4 | 0.000000 | 0.000000 | 0.000007 | False |
5 | 6.249731 | -0.033448 | 0.000006 | False |
Write your own generator¶
Here we write a generator that generates the same point every time.
In [9]:
Copied!
class MyGenerator(Generator):
def generate(self, n_candidates) -> list[dict]:
points = [{"x": 1.0}] * n_candidates
return points
my_generator = MyGenerator(vocs=vocs)
X2 = Xopt(evaluator=evaluator, vocs=vocs, generator=my_generator)
for i in range(4):
X2.step()
class MyGenerator(Generator):
def generate(self, n_candidates) -> list[dict]:
points = [{"x": 1.0}] * n_candidates
return points
my_generator = MyGenerator(vocs=vocs)
X2 = Xopt(evaluator=evaluator, vocs=vocs, generator=my_generator)
for i in range(4):
X2.step()
In [10]:
Copied!
X2.data
X2.data
Out[10]:
x | f | xopt_runtime | xopt_error | |
---|---|---|---|---|
0 | 1.0 | 0.841471 | 0.000006 | False |
1 | 1.0 | 0.841471 | 0.000005 | False |
2 | 1.0 | 0.841471 | 0.000006 | False |
3 | 1.0 | 0.841471 | 0.000005 | False |