SafeOptSwarm

class safeopt.SafeOptSwarm(gp, fmin, bounds, beta=2, scaling='auto', threshold=0, swarm_size=20)

SafeOpt for larger dimensions using a Swarm Optimization heuristic.

Note that it doesn’t support the use of a Lipschitz constant nor contextual optimization.

You can set your logging level to INFO to get more insights on the optimization process.

Parameters:
gp: GPy Gaussian process

A Gaussian process which is initialized with safe, initial data points. If a list of GPs then the first one is the value, while all the other ones are safety constraints.

fmin: list of floats

Safety threshold for the function value. If multiple safety constraints are used this can also be a list of floats (the first one is always the one for the values, can be set to None if not wanted)

bounds: pair of floats or list of pairs of floats

If a list is given, then each pair represents the lower/upper bound in each dimension. Otherwise, we assume the same bounds for all dimensions. This is mostly important for plotting or to restrict particles to a certain domain.

beta: float or callable

A constant or a function of the time step that scales the confidence interval of the acquisition function.

threshold: float or list of floats

The algorithm will not try to expand any points that are below this threshold. This makes the algorithm stop expanding points eventually. If a list, this represents the stopping criterion for all the gps. This ignores the scaling factor.

scaling: list of floats or “auto”

A list used to scale the GP uncertainties to compensate for different input sizes. This should be set to the maximal variance of each kernel. You should probably set this to “auto” unless your kernel is non-stationary

swarm_size: int

The number of particles in each of the optimization swarms

Examples

>>> from safeopt import SafeOptSwarm
>>> import GPy
>>> import numpy as np

Define a Gaussian process prior over the performance

>>> x = np.array([[0.]])
>>> y = np.array([[1.]])
>>> gp = GPy.models.GPRegression(x, y, noise_var=0.01**2)

Initialize the Bayesian optimization and get new parameters to evaluate

>>> opt = SafeOptSwarm(gp, fmin=[0.], bounds=[[-1., 1.]])
>>> next_parameters = opt.optimize()

Add a new data point with the parameters and the performance to the GP. The performance has normally be determined through an external function call.

>>> performance = np.array([[1.]])
>>> opt.add_new_data_point(next_parameters, performance)
Attributes:
data

Return the data within the GP models.

t

Return the time step (number of measurements).

x
y

Methods

add_new_data_point(x, y[, context]) Add a new function observation to the GPs.
get_maximum() Return the current estimate for the maximum.
get_new_query_point(swarm_type) Compute a new point at which to evaluate the function.
optimize([ucb]) Run Safe Bayesian optimization and get the next parameters.
optimize_particle_velocity() Optimize the velocities of the particles.
plot(n_samples[, axis, figure, plot_3d]) Plot the current state of the optimization.
remove_last_data_point() Remove the data point that was last added to the GP.
add_new_data_point(x, y, context=None)

Add a new function observation to the GPs.

Parameters:
x: 2d-array
y: 2d-array
context: array_like

The context(s) used for the data points.

data

Return the data within the GP models.

get_maximum()

Return the current estimate for the maximum.

Returns:
x : ndarray

Location of the maximum

y : 0darray

Maximum value

get_new_query_point(swarm_type)

Compute a new point at which to evaluate the function.

This function relies on a Particle Swarm Optimization (PSO) to find the optimum of the objective function (which depends on the swarm type).

Parameters:
swarm_type: string

This parameter controls the type of point that should be found. It can take one of the following values:

  • ‘expanders’ : find a point that increases the safe set
  • ‘maximizers’ : find a point that maximizes the objective
    function within the safe set.
  • ‘greedy’ : retrieve an estimate of the best currently known
    parameters (best lower bound).
Returns:
global_best: np.array

The next parameters that should be evaluated.

max_std_dev: float

The current standard deviation in the point to be evaluated.

optimize(ucb=False)

Run Safe Bayesian optimization and get the next parameters.

Parameters:
ucb: bool

Whether to only compute maximizers (best upper bound).

Returns:
x: np.array

The next parameters that should be evaluated.

optimize_particle_velocity()

Optimize the velocities of the particles.

Note that this only works well for stationary kernels and constant mean functions. Otherwise the velocity depends on the position!

Returns:
velocities: ndarray

The estimated optimal velocities in each direction.

plot(n_samples, axis=None, figure=None, plot_3d=False, **kwargs)

Plot the current state of the optimization.

Parameters:
n_samples: int

How many samples to use for plotting

axis: matplotlib axis

The axis on which to draw (does not get cleared first)

figure: matplotlib figure

Ignored if axis is already defined

plot_3d: boolean

If set to true shows a 3D plot for 2 dimensional data

remove_last_data_point()

Remove the data point that was last added to the GP.

t

Return the time step (number of measurements).