dimod.ConstrainedQuadraticModel.add_constraint_from_model#
- ConstrainedQuadraticModel.add_constraint_from_model(qm: BinaryQuadraticModel | QuadraticModel, sense: Sense | str, rhs: float | floating | integer = 0, label: Hashable | None = None, *, copy: bool = True, weight: float | None = 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
qmis copied. This can be set to False to improve performance, but subsequently mutatingqmcan cause issues.weight – Weight for a soft constraint. Must be a positive number. If
Noneorfloat('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
weightparameter set). Supported values are'linear'and'quadratic'. Ignored ifweightisNone.'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