dimod.ConstrainedQuadraticModel.iter_violations#
- ConstrainedQuadraticModel.iter_violations(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], *, skip_satisfied: bool = False, clip: bool = False, labels: Iterable[Hashable] | None = None) Iterator[tuple[Hashable, float | floating | integer]] [source]#
Yield violations for all constraints.
- Parameters:
sample_like – A sample. sample-like is an extension of NumPy’s array_like structure. See
as_samples()
..skip_satisfied – If True, does not yield constraints that are satisfied.
clip – If True, negative violations are rounded up to 0.
labels – A subset of the constraint labels over which to iterate.
- Yields:
A 2-tuple containing the constraint label and the amount of the constraint’s violation.
Example
Construct a constrained quadratic model.
>>> i, j, k = dimod.Binaries(['i', 'j', 'k']) >>> cqm = dimod.ConstrainedQuadraticModel() >>> cqm.add_constraint(i + j + k == 10, label='equal') 'equal' >>> cqm.add_constraint(i + j <= 15, label='less equal') 'less equal' >>> cqm.add_constraint(j - k >= 0, label='greater equal') 'greater equal'
Check the violations of a sample that satisfies all constraints.
>>> sample = {'i': 3, 'j': 5, 'k': 2} >>> for label, violation in cqm.iter_violations(sample, clip=True): ... print(label, violation) equal 0.0 less equal 0.0 greater equal 0.0
Check the violations for a sample that does not satisfy all of the constraints.
>>> sample = {'i': 3, 'j': 2, 'k': 5} >>> for label, violation in cqm.iter_violations(sample, clip=True): ... print(label, violation) equal 0.0 less equal 0.0 greater equal 3.0
>>> sample = {'i': 3, 'j': 2, 'k': 5} >>> for label, violation in cqm.iter_violations(sample, skip_satisfied=True): ... print(label, violation) greater equal 3.0