dimod.ConstrainedQuadraticModel.add_constraint_from_model¶
- ConstrainedQuadraticModel.add_constraint_from_model(qm: Union[dimod.binary.binary_quadratic_model.BinaryQuadraticModel, dimod.quadratic.quadratic_model.QuadraticModel], sense: Union[dimod.sym.Sense, str], rhs: Union[float, numpy.floating, numpy.integer] = 0, label: Optional[Hashable] = None, *, copy: bool = True, weight: Optional[float] = None, penalty: str = 'linear') Hashable [source]¶
Add a constraint from a quadratic model.
- Parameters
qm – Quadratic model or binary quadratic model.
sense – One of <=, >=, ==.
rhs – Right-hand side of the constraint.
label – Label for the constraint. Must be unique. If no label is provided, then one is generated using
uuid
.copy – If True, model
qm
is copied. This can be set to False to improve performance, but subsequently mutatingqm
can cause issues.weight – Weight for a soft constraint. Must be a positive number. If
None
orfloat('inf')
, the constraint is hard. In feasible solutions, all the model’s hard constraints must be met, while soft constraints might be violated to achieve overall good solutions.penalty – Penalty type for a soft constraint (a constraint with its
weight
parameter set). Supported values are'linear'
and'quadratic'
. Ignored ifweight
isNone
.'quadratic'
is supported for a constraint with binary variables only.
- Returns
Label of the added constraint.
Examples
This example adds a constraint from the single-variable binary quadratic model
x
.>>> from dimod import ConstrainedQuadraticModel, Binary >>> cqm = ConstrainedQuadraticModel() >>> x = Binary('x') >>> cqm.add_constraint_from_model(x, '>=', 0, 'Min x') 'Min x' >>> print(cqm.constraints["Min x"].to_polystring()) x >= 0.0