dwave.optimization.model.Model.add_constraint#

Model.add_constraint(value)#

Add a constraint to the model.

Parameters:

value – Value that must evaluate to True for the state of the model to be feasible.

Returns:

The constraint symbol.

Examples

This example adds a single constraint to a model.

>>> from dwave.optimization.model import Model
>>> model = Model()
>>> i = model.integer()
>>> c = model.constant(5)
>>> constraint_sym = model.add_constraint(i <= c)

The returned constraint symbol can be assigned and evaluated for a model state:

>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, 1) # Feasible state
...     print(constraint_sym.state(0))
1.0
>>> with model.lock():
...     i.set_state(0, 6) # Infeasible state
...     print(constraint_sym.state(0))
0.0