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.
pip install python-constraintVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple Constraint Satisfaction Problem (CSP) using the `Problem` class, add variables with their respective finite domains, and apply a string-based constraint. Finally, it shows how to retrieve and iterate through all valid solutions.
Rewrite constraints as Python-evaluable strings (e.g., `problem.addConstraint('x * 2 == y', ('x', 'y'))`) instead of lambda functions (e.g., `problem.addConstraint(lambda x, y: x*2 == y, ('x', 'y'))`).For large problems, consider reducing variable domains, adding more restrictive constraints early, or exploring alternative constraint satisfaction libraries optimized for specific problem types if `python-constraint` becomes too slow.
If your variable domains include non-numeric types (e.g., strings), you must implement custom `Constraint` classes or filter domains to ensure type compatibility before applying arithmetic operations.
Ensure 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.
Carefully 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.
Ensure 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.No dependency data recorded yet.