dimod.ConstrainedQuadraticModel.check_feasible#

ConstrainedQuadraticModel.check_feasible(sample_like: Sequence[float | floating | integer] | Mapping[Hashable, float | floating | integer] | tuple[Sequence[float | floating | integer], Sequence[Hashable]] | tuple[ndarray, Sequence[Hashable]] | ndarray | Sequence[Sequence[float | floating | integer]] | tuple[Sequence[Sequence[float | floating | integer]], Sequence[Hashable]] | Sequence[Sequence[float | floating | integer] | Mapping[Hashable, float | floating | integer] | tuple[Sequence[float | floating | integer], Sequence[Hashable]] | tuple[ndarray, Sequence[Hashable]] | ndarray] | Iterator[Sequence[float | floating | integer] | Mapping[Hashable, float | floating | integer] | tuple[Sequence[float | floating | integer], Sequence[Hashable]] | tuple[ndarray, Sequence[Hashable]] | ndarray], rtol: float = 1e-06, atol: float = 1e-08) bool[source]#

Return the feasibility of the given sample.

A sample is feasible if all constraints are satisfied. A constraint’s satisfaction is tested using the following equation:

\[violation <= (atol + rtol * | rhs\_energy | )\]

where violation and rhs_energy are as returned by iter_constraint_data().

Parameters:
  • sample_like – A sample. sample-like is an extension of NumPy’s array_like structure. See as_samples().

  • rtol – Relative tolerance.

  • atol – Absolute tolerance.

Returns:

True if the sample is feasible (given the tolerances).

Examples

This example violates a constraint that \(i \le 4\) by 0.2, which is greater than the absolute tolerance set by atol = 0.1 but within the relative tolerance of \(0.1 * 4 = 0.4\).

>>> cqm = dimod.ConstrainedQuadraticModel()
>>> i = dimod.Integer("i")
>>> cqm.add_constraint_from_comparison(i <= 4, label="Max i")
'Max i'
>>> cqm.check_feasible({"i": 4.2}, atol=0.1)
False
>>> next(cqm.iter_constraint_data({"i": 4.2})).rhs_energy
4.0
>>> cqm.check_feasible({"i": 4.2}, rtol=0.1)
True

Note that the next() function is used here because the model has just a single constraint.