Python Constraint
python-constraint is a module implementing support for handling Constraint Satisfaction Problems (CSPs) over finite domains. The current version, 2.5.0, offers enhanced features like efficient string-based constraints, negative value support, and performance improvements, with releases typically occurring several times a year.
Common errors
-
TypeError: unsupported operand type(s) for *: 'str' and 'int'
cause Attempting to apply an arithmetic constraint (especially string-based ones) to variables whose domains contain non-numeric values.fixEnsure that variables involved in arithmetic operations have domains consisting only of numeric types (integers, floats). If mixed types are needed, define a custom `Constraint` class to handle the specific logic. -
No solutions found.
cause The set of variables and constraints defined in the problem are mutually exclusive, meaning no valid assignment of values satisfies all conditions simultaneously. The `getSolutions()` method will return an empty list.fixCarefully review your `addVariable` domains and `addConstraint` definitions. Look for contradictions or overly restrictive conditions. Simplify the problem or relax some constraints to test if solutions then become available. -
NameError: name 'variable_name' is not defined (when using lambda)
cause When using a lambda or function as a constraint, the argument names in the function signature do not exactly match the variable names (as strings) passed in the tuple/list as the second argument to `addConstraint`.fixEnsure the argument names of your lambda or function precisely correspond to the string variable names in the tuple/list you provide to `addConstraint`. For example, for `addConstraint(lambda x, y: ..., ('x', 'y'))`, `x` and `y` must match.
Warnings
- deprecated Using lambda or function-based constraints directly with `addConstraint` is deprecated in favor of string-based constraints for better performance and readability.
- gotcha The performance of `getSolutions()` can degrade exponentially with an increasing number of variables or larger domains, potentially leading to long computation times or memory exhaustion for complex problems.
- gotcha When using string-based arithmetic constraints (e.g., 'x + y == z'), ensure that the domains of the involved variables contain only numeric types.
Install
-
pip install python-constraint
Imports
- Problem
from constraint import Problem
- Constraint
from constraint import Constraint
- *
from constraint import *
Quickstart
from constraint import Problem
problem = Problem()
# Add variables with finite domains
problem.addVariable('x', [1, 2, 3])
problem.addVariable('y', [1, 2, 3])
# Add a string-based constraint (preferred method since v2.1.0)
# x * 2 == y
problem.addConstraint('x * 2 == y', ('x', 'y'))
# Find and print all solutions
solutions = problem.getSolutions()
# Example of printing solutions
if solutions:
print(f"Found {len(solutions)} solution(s):")
for sol in solutions:
print(sol)
else:
print("No solutions found.")