Solutions to a constraint satisfaction problem must satisfy certains conditions, the
constraints of the problem, such as equality and inequality constraints.
Constraint class defines constraints and provides functionality to
assist in constraint definition, such as verifying whether a candidate solution satisfies
Constraint(func, configurations, variables, vartype, name=None)¶
Function that returns True for configurations of variables that satisfy the constraint. Inputs to the function are ordered by
Valid configurations of the variables. Each configuration is a tuple of variable assignments ordered by
Variable type for the constraint. Accepted input values:
This example defines a constraint, named “plus1”, based on a function that is True for \((y1,y0) = (x1,x0)+1\) on binary variables, and demonstrates some of the constraint’s functionality.
>>> import dwavebinarycsp >>> def plus_one(y1, y0, x1, x0): # y=x++ for two bit binary numbers ... return (y1, y0, x1, x0) in [(0, 1, 0, 0), (1, 0, 0, 1), (1, 1, 1, 0)] ... >>> const = dwavebinarycsp.Constraint.from_func( ... plus_one, ... ['out1', 'out0', 'in1', 'in0'], ... dwavebinarycsp.BINARY, ... name='plus1') >>> print(const.name) # Check constraint defined as intended plus1 >>> len(const) 4 >>> in0, in1, out0, out1 = 0, 0, 1, 0 >>> const.func(out1, out0, in1, in0) # Order matches variables True
This example defines a constraint based on specified valid configurations that represents an AND gate for spin variables, and demonstrates some of the constraint’s functionality.
>>> import dwavebinarycsp >>> const = dwavebinarycsp.Constraint.from_configurations( ... [(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (1, 1, 1)], ... ['y', 'x1', 'x2'], ... dwavebinarycsp.SPIN) >>> print(const.name) # Check constraint defined as intended Constraint >>> isinstance(const, dwavebinarycsp.core.constraint.Constraint) True >>> (-1, 1, -1) in const.configurations # Order matches variables: y,x1,x2 True
||Construct a constraint from valid configurations.|
||Construct a constraint from a validation function.|
||Fix the value of a variable and remove it from the constraint.|
||Flip a variable in the constraint.|