- Notifications
You must be signed in to change notification settings - Fork71
Constraint Solving Problem resolver for Python
License
python-constraint/python-constraint
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
problem.addConstraint(["50 <= x * y < 100"]) is parsed to[MinProdConstraint(50, ["x", "y"]), MaxProdConstraint(100, ["x", "y"])].problem.addConstraint(["x / y == z"]) is parsed to[ExactProdConstraint("x", ["z", "y"])].Thepython-constraint module offers efficient solvers forConstraint Satisfaction Problems (CSPs) over finite domains in an accessible Python package.CSP is class of problems which may be represented in terms of variables (a, b, ...), domains (a in [1, 2, 3], ...), and constraints (a < b, ...).
This interactive Python session demonstrates basic operations:
>>>fromconstraintimport*>>>problem=Problem()>>>problem.addVariable("a", [1,2,3])>>>problem.addVariable("b", [4,5,6])>>>problem.getSolutions()# doctest: +NORMALIZE_WHITESPACE[{'a':3,'b':6}, {'a':3,'b':5}, {'a':3,'b':4}, {'a':2,'b':6}, {'a':2,'b':5}, {'a':2,'b':4}, {'a':1,'b':6}, {'a':1,'b':5}, {'a':1,'b':4}]>>>problem.addConstraint("a*2 == b")# string constraints are preferable over the black-box problem.addConstraint(lambda a, b: a*2 == b, ("a", "b"))>>>problem.getSolutions()[{'a':3,'b':6}, {'a':2,'b':4}]>>>problem=Problem()>>>problem.addVariables(["a","b"], [1,2,3])>>>problem.addConstraint(AllDifferentConstraint())>>>problem.getSolutions()# doctest: +NORMALIZE_WHITESPACE[{'a':3,'b':2}, {'a':3,'b':1}, {'a':2,'b':3}, {'a':2,'b':1}, {'a':1,'b':2}, {'a':1,'b':3}]
The following example solves the classical Eight Rooks problem:
>>>problem=Problem()>>>numpieces=8>>>cols=range(numpieces)>>>rows=range(numpieces)>>>problem.addVariables(cols,rows)>>>forcol1incols:...forcol2incols:...ifcol1<col2:...problem.addConstraint(lambdarow1,row2:row1!=row2, (col1,col2))>>>solutions=problem.getSolutions()>>>solutions# doctest: +NORMALIZE_WHITESPACE +ELLIPSIS[{0:7,1:6,2:5,3:4,4:3,5:2,6:1,7:0}, {0:7,1:6,2:5,3:4,4:3,5:2,6:0,7:1}, {0:7,1:6,2:5,3:4,4:3,5:1,6:2,7:0}, {0:7,1:6,2:5,3:4,4:3,5:1,6:0,7:2}, ... {0:7,1:5,2:3,3:6,4:2,5:1,6:4,7:0}, {0:7,1:5,2:3,3:6,4:1,5:2,6:0,7:4}, {0:7,1:5,2:3,3:6,4:1,5:2,6:4,7:0}, {0:7,1:5,2:3,3:6,4:1,5:4,6:2,7:0}, {0:7,1:5,2:3,3:6,4:1,5:4,6:0,7:2}, ...]
This example solves a 4x4 magic square:
>>>problem=Problem()>>>problem.addVariables(range(0,16),range(1,16+1))>>>problem.addConstraint(AllDifferentConstraint(),range(0,16))>>>problem.addConstraint(ExactSumConstraint(34), [0,5,10,15])>>>problem.addConstraint(ExactSumConstraint(34), [3,6,9,12])>>>forrowinrange(4):...problem.addConstraint(ExactSumConstraint(34), [row*4+iforiinrange(4)])>>>forcolinrange(4):...problem.addConstraint(ExactSumConstraint(34), [col+4*iforiinrange(4)])>>>solutions=problem.getSolutions()# doctest: +SKIP
The following solvers are available:
OptimizedBacktrackingSolver(default)BacktrackingSolverRecursiveBacktrackingSolverMinConflictsSolverParallelSolver
Predefined constraint types currently available (use the parsing for automatic conversion to these types):
FunctionConstraintAllDifferentConstraintAllEqualConstraintExactSumConstraintMinSumConstraintMaxSumConstraintMinProdConstraintExactProdConstraintMaxProdConstraintVariableExactSumConstraintVariableMinSumConstraintVariableMaxSumConstraintVariableMinProdConstraintVariableExactProdConstraintVariableMaxProdConstraintInSetConstraintNotInSetConstraintSomeInSetConstraintSomeNotInSetConstraint
Documentation for the module is available at:http://python-constraint.github.io/python-constraint/.It can be built locally by runningmake clean html from the docs folder.For viewing RST files locally,restview is recommended.
$ pip install python-constraint2
Runnox (tests for all supported Python versions in own virtual environment).
To test against your local Python version: make sure you have the development dependencies installed.Runpytest (optionally add--no-cov if you have the C-extensions enabled).
Feel free to contribute bysubmitting pull requests oropening issues.Please refer to thecontribution guidelines before doing so.
This GitHub organization and repository is a global effort to help to maintainpython-constraint, which was written by Gustavo Niemeyer and originaly located athttps://labix.org/python-constraint.For an overview of recent changes, visit theChangelog.
Planned development:
- Support constant modifiers on parsed (variable) constraints instead defaulting to FunctionConstraint, e.g.
problem.addConstraint("a+2 == b")orproblem.addConstraint("x / y == 100") - Parse using Abstract Syntax Trees (AST) instead of the current parser to be more robust and support decomposing lambdas
- Rewrite hotspots in C / Pyx instead of pure python mode
- Improvements to make the ParallelSolver competitive (experiments reveal the freethreading mode to be promising)
- Versioned documentation
- Floris-Jan Willemsen <fjwillemsen97@gmail.com> (current maintainer)
- Sébastien Celles <s.celles@gmail.com> (former maintainer)
- Gustavo Niemeyer <gustavo@niemeyer.net> (initial developer)
But it's probably better toopen an issue.
About
Constraint Solving Problem resolver for Python
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.