dimod.ConstrainedQuadraticModel.check_feasible

ConstrainedQuadraticModel.check_feasible(sample_like: Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.ndarray, Sequence[Sequence[Union[float, numpy.floating, numpy.integer]]], Tuple[Sequence[Sequence[Union[float, numpy.floating, numpy.integer]]], Sequence[Hashable]], Sequence[Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.ndarray]], Iterator[Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.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
>>> 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.