bayes_opt.ConstraintModel

See the Constrained Optimization notebook for a complete example.

class bayes_opt.constraint.ConstraintModel(fun, lb, ub, random_state=None)

Model constraints using GP regressors.

This class takes the function to optimize as well as the parameters bounds in order to find which values for the parameters yield the maximum value using bayesian optimization.

Parameters:
fun : None or Callable -> float or np.ndarray

The constraint function. Should be float-valued or array-valued (if multiple constraints are present). Needs to take the same parameters as the optimization target with the same argument names.

lb : float or np.ndarray

The lower bound on the constraints. Should have the same dimensionality as the return value of the constraint function.

ub : float or np.ndarray

The upper bound on the constraints. Should have the same dimensionality as the return value of the constraint function.

random_state : np.random.RandomState or int or None, default=None

Random state to use.

Note

In case of multiple constraints, this model assumes conditional independence. This means that the overall probability of fulfillment is a simply the product of the individual probabilities.

allowed(constraint_values)

Check whether constraint_values fulfills the specified limits.

Parameters:
constraint_values : np.ndarray of shape (n_samples, n_constraints)

The values of the constraint function.

Returns:

np.ndarrray of shape (n_samples,) – Specifying wheter the constraints are fulfilled.

approx(X)

Approximate the constraint function using the internal GPR model.

Parameters:
X : np.ndarray of shape (n_samples, n_features)

Parameters for which to estimate the constraint function value.

Returns:

np.ndarray of shape (n_samples, n_constraints) – Constraint function value estimates.

eval(**kwargs: dict)

Evaluate the constraint function.

Parameters:
**kwargs : any

Function arguments to evaluate the constraint function on.

Returns:

Value of the constraint function.

Raises:

TypeError – If the kwargs’ keys don’t match the function argument names.

fit(X, Y)

Fit internal GPRs to the data.

Parameters:
X : np.ndarray of shape (n_samples, n_features)

Parameters of the constraint function.

Y : np.ndarray of shape (n_samples, n_constraints)

Values of the constraint function.

Returns:

None

property lb

Return lower bounds.

property model

Return GP regressors of the constraint function.

predict(X)

Calculate the probability that the constraint is fulfilled at X.

Note that this does not try to approximate the values of the constraint function (for this, see ConstraintModel.approx().), but probability that the constraint function is fulfilled. That is, this function calculates

\[p = \text{Pr}\left\{c^{\text{low}} \leq \tilde{c}(x) \leq c^{\text{up}} \right\} = \int_{c^{\text{low}}}^{c^{\text{up}}} \mathcal{N}(c, \mu(x), \sigma^2(x)) \, dc.\]

with \(\mu(x)\), \(\sigma^2(x)\) the mean and variance at \(x\) as given by the GP and \(c^{\text{low}}\), \(c^{\text{up}}\) the lower and upper bounds of the constraint respectively.

Note

In case of multiple constraints, we assume conditional independence. This means we calculate the probability of constraint fulfilment individually, with the joint probability given as their product.

Parameters:
X : np.ndarray of shape (n_samples, n_features)

Parameters for which to predict the probability of constraint fulfilment.

Returns:

np.ndarray of shape (n_samples,) – Probability of constraint fulfilment.

property ub

Return upper bounds.