python-constraint is a module implementing support for handling CSPs (Constraint Solving Problems) over finite domain
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
File details
Details for the file python_constraint2-2.0.0b1.tar.gz
.
File metadata
- Download URL: python_constraint2-2.0.0b1.tar.gz
- Upload date:
- Size: 430.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.0 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb8c4c9c8e323d24ac6b128dd4f5312a101881316a7851b71b27cc2d02b6615e |
|
MD5 | 60f9723d838bb8460a962ba1ada1326b |
|
BLAKE2b-256 | 015ca02ed692e386040eb706f62b9723b0af9e11b8c26024cf557f27f37622f4 |
File details
Details for the file python_constraint2-2.0.0b1-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: python_constraint2-2.0.0b1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 445.1 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85d6ae29294c488c6f7de63046d9d12937c454771bc47676e5bc873f42f15904 |
|
MD5 | f2719eb0eb61a3bffcb1e950faeedb60 |
|
BLAKE2b-256 | 676aaad99055208cebe4bca6d2c546ed4f0acbdba399a358eb34c3c39be80476 |
File details
Details for the file python_constraint2-2.0.0b1-cp311-cp311-manylinux_2_31_x86_64.whl
.
File metadata
- Download URL: python_constraint2-2.0.0b1-cp311-cp311-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 441.5 kB
- Tags: CPython 3.11, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.0 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8336627361864f4482e2c713c9d3cd9553301256966a870d1147cfc0f721c233 |
|
MD5 | c46122d7813ebb5cc20328ac32dc5099 |
|
BLAKE2b-256 | a38cabfb2f46d1d734e02b3d5cc316b4cf8225c0740d7fcd868f61b38fe9ac04 |
File details
Details for the file python_constraint2-2.0.0b1-cp311-cp311-macosx_12_0_x86_64.whl
.
File metadata
- Download URL: python_constraint2-2.0.0b1-cp311-cp311-macosx_12_0_x86_64.whl
- Upload date:
- Size: 441.5 kB
- Tags: CPython 3.11, macOS 12.0+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d73bcca80a2b40d4401712a9a8f7c570abe0580cc5692cf5890a7ad906e8f28 |
|
MD5 | bf0dbc89adc3dc48e11377a47524102b |
|
BLAKE2b-256 | 6aaaa474563a2612bb6ad6df87938779a836e1298d306f52888fc6dcecd20c71 |