dimod.ConstrainedQuadraticModel.add_constraint_from_iterable#
- ConstrainedQuadraticModel.add_constraint_from_iterable(iterable: Iterable, sense: Sense | str, rhs: float | floating | integer = 0, label: Hashable | None = None, weight: float | None = None, penalty: str = 'linear') Hashable[source]#
- Add a constraint from an iterable of tuples. - Parameters:
- iterable – Iterable of terms as tuples. The variables must have already been added to the object. 
- sense – One of <=, >=, ==. 
- rhs – The 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.
- weight – Weight for a soft constraint. Must be a positive number. If - Noneor- float('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 if- weightis- None.- 'quadratic'is supported for a constraint with binary variables only.
 
- Returns:
- Label of the added constraint. 
 - Examples - >>> from dimod import ConstrainedQuadraticModel, Integer, Binary >>> cqm = ConstrainedQuadraticModel() >>> cqm.add_variable('INTEGER', 'i') 'i' >>> cqm.add_variable('INTEGER', 'j') 'j' >>> cqm.add_variable('BINARY', 'x') 'x' >>> cqm.add_variable('BINARY', 'y') 'y' >>> label1 = cqm.add_constraint_from_iterable([('x', 'y', 1), ('i', 2), ('j', 3), ... ('i', 'j', 1)], '<=', rhs=1) >>> print(cqm.constraints[label1].to_polystring()) 2*i + 3*j + y*x + i*j <= 1.0