Agent

Agent (Deprecated)

class blop.agent.Agent(dofs, objectives, db=None, detectors=None, acquisition_plan=<function default_acquisition_plan>, digestion=<function default_digestion_function>, digestion_kwargs=None, verbose=False, enforce_all_objectives_valid=True, exclude_pruned=True, model_inactive_objectives=False, tolerate_acquisition_errors=False, sample_center_on_init=False, trigger_delay=0, train_every=3)[source]

Bases: BaseAgent

A Bayesian optimization agent.

Deprecated since version v0.8.0: This class is deprecated and will be removed in Blop v1.0.0. Use blop.ax.Agent instead.

Parameters:
dofsSequence[DOF]

The degrees of freedom that the agent can control, which determine the output of the model.

db: Broker | Container

A Tiled or Databroker object

objectivesSequence[Objective]

The objectives which the agent will try to optimize.

acquisition_planCallable, optional

A plan that samples the beamline for some given inputs, by default default_acquisition_plan. Called directly in Agent, used only by __name__ in BlueskyAdaptiveAgent. Default: default_acquisition_plan

digestionCallable, optional

A function to digest the output of the acquisition, taking a DataFrame as an argument, by default default_digestion_function Default: default_digestion_function

digestion_kwargsdict, optional

Some kwargs for the digestion function, by default {} Default: False

verbosebool, optional

To be verbose or not, by default False Default: False

enforce_all_objectives_validbool, optional

Whether the agent should exclude from fitting points with one or more invalid objectives. Default: True

exclude_prunedbool, optional

Whether to exclude from fitting points that have been pruned after running agent.prune(). Default: True

model_inactive_objectivesbool, optional

Whether the agent should update models for outcomes that affect inactive objectives. Default: False

tolerate_acquisition_errorsbool, optional

Whether to allow errors during acquistion. If True, errors will be caught as warnings. Default: False

sample_center_on_initbool, optional

Whether to sample the center of the DOF limits when the agent has no data yet. Default: False

train_everyint, optional

How many samples to take before retraining model hyperparameters. Default: 4

property table: dict[str, Any]
refresh()[source]
redigest()[source]
learn(acqf='qei', n=1, iterations=1, upsample=1, force_train=False, append=True, hypers=None, route=True, **acqf_kwargs)[source]

This returns a Bluesky plan which iterates the learning algorithm, looping over ask -> acquire -> tell.

For example:

RE(agent.learn(‘qr’, n=16)) RE(agent.learn(‘qei’, n=4, iterations=4))

Parameters:
acqfstr

A valid identifier for an implemented acquisition function.

nint

How many points to sample on each iteration.

iterations: int

How many iterations of the learning loop to perform.

train: bool

Whether to train the models upon telling the agent.

append: bool

If True, add the new data to the old data. If False, replace the old data with the new data.

data_file: str

If supplied, read a saved data file instead of running the acquisition plan.

hypers_file: str

If supplied, read a saved hyperparameter file instead of fitting models. NOTE: The agent will assume these hyperparameters a priori for the rest of the run, and not try to fit a model.

view(item='mean', cmap='turbo', max_inputs=65536)[source]

Use napari to see a high-dimensional array.

Parameters:
itemstr

The thing to be viewed. Either ‘mean’, ‘error’, or an acquisition function.

acquire(points)[source]

Acquire and digest according to the self’s acquisition and digestion plans.

Parameters:
acquisition_inputs

A 2D numpy array comprising inputs for the active and non-read-only DOFs to sample.

load_data(data_file, append=True)[source]
reset()[source]

Reset the agent.

benchmark(output_dir='./', iterations=16, per_iter_learn_kwargs_list=({'acqf': 'qr', 'n': 32}, {'acqf': 'qei', 'iterations': 4, 'n': 1}))[source]

Iterate over having the agent learn from scratch, and save the results to an output directory.

Parameters:
output_dir

Where to save the agent output.

iterationsint

How many benchmarks to run

per_iter_learn_kwargs_list:

A list of kwargs to pass to the agent.learn() method that the agent will run sequentially for each iteration.

property model: ModelListGP

A model encompassing all the fitnesses and constraints.

posterior(x)[source]

A model encompassing all the objectives. A single GP in the single-objective case, or a model list.

property pareto_mask: Tensor

Returns a mask of all points that satisfy all constraints and are Pareto efficient. A point is Pareto efficient if it is there is no other point that is better at every objective.

property pareto_front: Any

A subset of the data table containing only points on the Pareto front.

property min_ref_point: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]
property all_objectives_valid: Tensor

A mask of whether all objectives are valid for each data point.

save_data(path)[source]

Save the sampled inputs and targets of the agent to a file, which can be used to initialize a future agent.

forget(last=None, index=None, train=True)[source]

Make the agent forget some data.

Parameters:
index

An list of sample indexes to forget about.

lastint

Forget the last n=last points.

property hypers: dict[str, dict[str, dict[str, Tensor]]]

Returns a dict of all the hyperparameters for each model in each objective.

save_hypers(filepath)[source]

Save the agent’s fitted hyperparameters to a given filepath.

static load_hypers(filepath)[source]

Load hyperparameters from a file.

property all_acqfs: DataFrame

Description and identifiers for all supported acquisition functions.

property best: Any

Returns all data for the best point.

property best_inputs: dict[Hashable, Any]

Returns the value of each DOF at the best point.

go_to(**positions)[source]

Set all settable DOFs to a given position. DOF/value pairs should be supplied as kwargs, e.g. as

RE(agent.go_to(some_dof=x1, some_other_dof=x2, …))

go_to_best()[source]

Go to the position of the best input seen so far.

plot_objectives(axes=(0, 1), **kwargs)[source]

Plot the sampled objectives

Parameters:
axes

A tuple specifying which DOFs to plot as a function of. Can be either an int or the name of DOFs.

plot_acquisition(acqf='ei', axes=(0, 1), **kwargs)[source]

Plot an acquisition function over test inputs sampling the limits of the parameter space.

Parameters:
acqf

Which acquisition function to plot. Can also take a list of acquisition functions.

axes

A tuple specifying which DOFs to plot as a function of. Can be either an int or the name of DOFs.

plot_validity(axes=(0, 1), **kwargs)[source]

Plot the modeled constraint over test inputs sampling the limits of the parameter space.

Parameters:
axes

A tuple specifying which DOFs to plot as a function of. Can be either an int or the name of DOFs.

plot_history(**kwargs)[source]

Plot the improvement of the agent over time.

property latent_transforms: dict[str, Kernel]
plot_pareto_front(**kwargs)[source]

Plot the improvement of the agent over time.

argmax_best_f(weights='default')
ask(acqf='qei', n=1, route=True, sequential=True, upsample=1, **acqf_kwargs)

Ask the agent for the best point to sample, given an acquisition function.

Parameters:
acqf_identifier

Which acquisition function to use. Supported values can be found in agent.all_acqfs

nint

How many points you want

routebool

Whether to route the supplied points to make a more efficient path.

sequentialbool

Whether to generate points sequentially (as opposed to in parallel). Sequential generation involves finding one points and constructing a fantasy posterior about its value to generate the next point.

best_f(weights='default')

Return the best point (the one with highest fitness given that it satisfies the most constraints)

constraint(x)
property evaluated_constraints: Tensor

Return a tensor of booleans that evaluates whether each collected outcome satisfies its constraint.

property fitness_model: Model
fitness_scalarization(weights='default')
property input_normalization: Normalize

Suitably transforms model inputs to the unit hypercube.

For modeling:

Always normalize between min and max values. This is always inside the trust domain, sometimes smaller.

For sampling:

Settable: normalize between search bounds Read-only: constrain to the readback value

property n_samples: int

Returns the number of samples in the agent’s table.

prune(pruning_objs=None, thresholds=None)[source]

Prune low-fidelity datapoints from model fitting

pruned_mask()
property random_ref_point: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]
raw_inputs(index=None, **subset_kwargs)

Get the raw, untransformed inputs for a DOF (or for a subset).

raw_targets(index=None, **subset_kwargs)

Get the raw, untransformed targets for an objective (or for a subset of objectives) as a tensor.

raw_targets_dict(index=None, **subset_kwargs)

Get the raw, untransformed targets for an objective (or for a subset of objectives) as a dict.

sample(n=3200, normalize=False, method='quasi-random')

Returns a (…, 1, n_active_dofs) tensor of points sampled within the parameter space.

Parameters:
nint

How many points to sample.

methodstr

How to sample the points. Must be one of ‘quasi-random’, ‘random’, or ‘grid’.

normalize: bool

If True, sample the unit hypercube. If False, sample the parameter space of the agent.

property sample_domain: Tensor

Returns a (2, n_active_dof) array of lower and upper bounds for dofs. Read-only DOFs are set to exactly their last known value. Discrete DOFs are relaxed to some continuous domain.

scalarized_fitnesses(weights='default', constrained=True)

Return the scalar fitness for each sample, scalarized by the weighting scheme.

If constrained=True, points that satisfy the most constraints are preferred to those that satisfy fewer.

tell(data={}, x={}, y={}, metadata={}, append=True, force_train=False)

Inform the agent about new inputs and targets for the model.

If run with no arguments, it will just reconstruct all the models.

Parameters:
xdict

A dict keyed by the name of each DOF, with a list of values for each DOF.

ydict

A dict keyed by the name of each objective, with a list of values for each objective.

append: bool

If True, will append new data to old data. If False, will replace old data with new data.

train: bool

Whether to train the models on construction.

hypers:

A dict of hyperparameters for the model to assume a priori, instead of training.

train_inputs(index=None, **subset_kwargs)

A two-dimensional tensor of all DOF values for training on.

train_targets(index=None, **subset_kwargs)

Returns the values associated with an objective name as an (n_samples, n_objective) tensor.

train_targets_dict(index=None, **subset_kwargs)

Returns the values associated with an objective name.

update_models(force_train=False)

We don’t want to retrain the models on every call of everything, but if they are out of sync with the DOFs then we should.