python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains.
Project description
python-constraint
Introduction
The python-constraint
module offers efficient solvers for Constraint 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, …).
Examples
Basics
This interactive Python session demonstrates basic operations:
>>> from constraint import *
>>> problem = Problem()
>>> problem.addVariable("a", [1,2,3])
>>> problem.addVariable("b", [4,5,6])
>>> problem.getSolutions()
[{'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(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()
[{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3},
{'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]
Rooks problem
The following example solves the classical Eight Rooks problem:
>>> problem = Problem()
>>> numpieces = 8
>>> cols = range(numpieces)
>>> rows = range(numpieces)
>>> problem.addVariables(cols, rows)
>>> for col1 in cols:
... for col2 in cols:
... if col1 < col2:
... problem.addConstraint(lambda row1, row2: row1 != row2,
... (col1, col2))
>>> solutions = problem.getSolutions()
>>> solutions
>>> solutions
[{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},
...]
Magic squares
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])
>>> for row in range(4):
... problem.addConstraint(ExactSumConstraint(34),
[row * 4 + i for i in range(4)])
>>> for col in range(4):
... problem.addConstraint(ExactSumConstraint(34),
[col + 4 * i for i in range(4)])
>>> solutions = problem.getSolutions()
Features
The following solvers are available:
Backtracking solver
Optimized backtracking solver
Recursive backtracking solver
Minimum conflicts solver
Predefined constraint types currently available:
FunctionConstraint
AllDifferentConstraint
AllEqualConstraint
MaxSumConstraint
ExactSumConstraint
MinSumConstraint
MaxProdConstraint
MinProdConstraint
InSetConstraint
NotInSetConstraint
SomeInSetConstraint
SomeNotInSetConstraint
API documentation
Documentation for the module is available at: http://python-constraint.github.io/python-constraint/.
It can be built locally by running make clean html
from the docs folder.
For viewing RST files locally, restview is recommended.
Download and install
$ pip install python-constraint
Testing
Run nox
(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.
Run pytest
(optionally add --no-cov
if you have the C-extensions enabled).
Contributing
Feel free to contribute by submitting pull requests or opening issues. Please refer to the contribution guidelines before doing so.
Roadmap
This GitHub organization and repository is a global effort to help to maintain python-constraint
, which was written by Gustavo Niemeyer and originaly located at https://labix.org/python-constraint.
For an overview of recent changes, visit the Changelog.
Planned development:
Add a string parser for constraints
Add parallel-capable solver
Versioned documentation
Contact
But it’s probably better to open an issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for python_constraint2-2.0.0b5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 497883a0457143e3c7c591ed531c95f4aa0c5070e248bae8fc60c5b6244c21e3 |
|
MD5 | 46d38e7645f612295bc031a8f27e3607 |
|
BLAKE2b-256 | ec8e4d62f54339aac758c0689e684714743e979896d6683f76f4866d9ea9c5ac |
Hashes for python_constraint2-2.0.0b5-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e14aafd0e75d7a6e4393b07013cf4e8be4f0bf074b7324813bb8f3aac06ad0c1 |
|
MD5 | 958efeed89747bb24cf051712d34c351 |
|
BLAKE2b-256 | fc96f1425c90bd8743a378c4cfb0caab3d8011edcf4e989d689a83d3400d9593 |
Hashes for python_constraint2-2.0.0b5-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78dfc189b28d10bccdfe63803a24ef9e04761b377613fa1fec7cd60860bc3cb2 |
|
MD5 | f1cf332eebe0294b0b00f27a787634c8 |
|
BLAKE2b-256 | f6924f84446a8382fa722d0f93687b0a9358df490a627ddd89307abb434276ea |
Hashes for python_constraint2-2.0.0b5-cp312-cp312-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab22c6d88f6254aeb2f38fc21d30086394574677a928d121d8e6a067293d36bd |
|
MD5 | 896deb18077a3e01992e7ae423702980 |
|
BLAKE2b-256 | a4347651c0e73df4fdd1630045bb3efb69b1f37a02edf4e8568da58b372dd469 |
Hashes for python_constraint2-2.0.0b5-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8e1074e7149a47ef6948f8aca30b1ffee1c168f2261299134c50cf6a584dce4 |
|
MD5 | 5e46f828eb6a2b73b1b7c0789ac70996 |
|
BLAKE2b-256 | 43a4c05fbcc0a058dc73d79713a98808c6f31115552d701b644b7f5422e0d833 |
Hashes for python_constraint2-2.0.0b5-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cedd1ba784987586714dc3b238d2d8420510244304a9be40c57bfad72729680f |
|
MD5 | df5e59ffac6fc8ef590354944cba93f0 |
|
BLAKE2b-256 | 6e016be269382968e72071bf2c954b8467b8a3d8b27046475ac88404871aae90 |
Hashes for python_constraint2-2.0.0b5-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bfef994000bcb72ed92d9933962be782897b79dc482ac682fac8fa8bb926571 |
|
MD5 | d7ad94606c78e6e0942d19b30d948f44 |
|
BLAKE2b-256 | cc0d35c04d4247a4c24a5540728d04d07d5ce500b41bf373bde752587ed8d0e1 |
Hashes for python_constraint2-2.0.0b5-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada0acba60bf9bdb9fca0212c7968dee6944ab82021aa6b3f12e6673ac9fe7c6 |
|
MD5 | 7975e0d16d92e13ce3d4c70dc99397a2 |
|
BLAKE2b-256 | 0856e7b15fc1e20a87ba6fd7558bdc5fd93d07438146dd98aab3bd414e5a0df7 |
Hashes for python_constraint2-2.0.0b5-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d491ba70e4697b1c51d276b170ee718d926e225c8f3f2ddea2991c4de5b942c |
|
MD5 | 29fb0313d2f1ce9339dcf1011ea7ee58 |
|
BLAKE2b-256 | 8218122b6f8b2a8151705025808c3f0198490b9d81c6ed1330ed7f78205e58d8 |
Hashes for python_constraint2-2.0.0b5-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2ab2292ab5b38394c3709fd0f4eeb9c0df4396c4f7dfcc8782fcc6a6fc9cfd8 |
|
MD5 | 809645a9f575198ecc2caa7295b88fb2 |
|
BLAKE2b-256 | 0637c19369827244f65e1cfe1222516c19f27a96f1f3a5dd5581397cb02a00ce |
Hashes for python_constraint2-2.0.0b5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6c94148b55a724a27aed3eaa7adbb0b9a87b8135ef9873282e9d7831027a4b6 |
|
MD5 | e2b07ee8dfd2ed7fcc38e282aa7a46d2 |
|
BLAKE2b-256 | 2ec57a415409307de6c7810119a6ee3d4cc6025051a438edeb30939ef42242a6 |
Hashes for python_constraint2-2.0.0b5-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39e65ac4c2a6ce87d55bca6e9480eea666bb77769e43f7908e85ba0baedb51df |
|
MD5 | bdf3911d7bcb80ed5cc2f9e183871930 |
|
BLAKE2b-256 | 7f2bdfcd61624c5ac21ced913b246b4c25974f88257b9cd8153f1052da4cb877 |
Hashes for python_constraint2-2.0.0b5-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65e3f862db0fde06eeb4adcc44cab8541c405757ceafd7f96e64e87241028aa8 |
|
MD5 | da540dbfb1405ae6091accbf5c1ca0f9 |
|
BLAKE2b-256 | 835f501373f9b4ad6d75c84234cbf16a086859a8b1984c7a6a5f60e2f6f8ec43 |
Hashes for python_constraint2-2.0.0b5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c70341b35a2dc773e8b7dfb0410cb2e27b8d121785ad1abe9fa11f8d4de1acf7 |
|
MD5 | 2c07e44b5e2a42180d63550852a8d069 |
|
BLAKE2b-256 | a058dddddf7e4ec048a16975738b048214c83ef10a987ff9e2fba436446a9ca6 |
Hashes for python_constraint2-2.0.0b5-cp38-cp38-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e613279e3bc09e960452c5c308e221bc3f1cef0d714907ffa3d3804af3839598 |
|
MD5 | 8908e8e1ba7b5f698f6608abbbe4df4a |
|
BLAKE2b-256 | e15096908203813e60f0a817adb60799fcb7501c428a0fa6a6515c2d0b36214e |
Hashes for python_constraint2-2.0.0b5-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b69c37804a13858e96517bf9428e41680739b418916f56adcde2f82ca12d0f74 |
|
MD5 | b8864c69792b2ef802b5e086391a154d |
|
BLAKE2b-256 | 34917cde28e8489786387861002a246abf12cd2b1244f16583c212100dc23f34 |