# `bayes_opt.ConstraintModel`

See the Constrained Optimization notebook for a complete example.

class bayes_opt.constraint.ConstraintModel(fun: Callable[..., float] | Callable[..., NDArray[Float]] | None, lb: float | NDArray[Float], ub: float | NDArray[Float], random_state: int | RandomState | None = None) → 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: ndarray[Any, dtype[floating[Any]]]) → ndarray[Any, dtype[bool_]]

    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.

    Return type:

      `ndarray`[`Any`, `dtype`[`bool_`]]

    Returns:

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

  approx(X: ndarray[Any, dtype[floating[Any]]]) → ndarray[Any, dtype[floating[Any]]]

    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.

    Return type:

      `ndarray`[`Any`, `dtype`[`floating`[`Any`]]]

    Returns:

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

  eval(**kwargs: Any) → float | ndarray[Any, dtype[floating[Any]]]

    Evaluate the constraint function.

    Parameters:

      **kwargs (any) – Function arguments to evaluate the constraint function on.

    Return type:

      `float` | `ndarray`[`Any`, `dtype`[`floating`[`Any`]]]

    Returns:

      Value of the constraint function.

    Raises:

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

  fit(X: ndarray[Any, dtype[floating[Any]]], Y: ndarray[Any, dtype[floating[Any]]]) → None

    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.

    Return type:

      `None`

    Returns:

      None

  property lb: ndarray[Any, dtype[floating[Any]]]

    Return lower bounds.

  property model: list[GaussianProcessRegressor]

    Return GP regressors of the constraint function.

  predict(X: ndarray[Any, dtype[floating[Any]]]) → ndarray[Any, dtype[floating[Any]]]

    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.

    Return type:

      `ndarray`[`Any`, `dtype`[`floating`[`Any`]]]

    Returns:

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

  property ub: ndarray[Any, dtype[floating[Any]]]

    Return upper bounds.