Skip to main content

python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains.

Project description

PyPI - License Build Status Documentation Status PyPI - Python Versions PyPI - Downloads PyPI - Status Code Coverage

https://github.com/python-constraint/python-constraint/raw/main/docs/assets/logo/N-Queens_problem_Python.svg

python-constraint

This software is now back to active development / maintainance status.
For an overview of recent changes, visit the Changelog.
The complete documentation can be found here.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_constraint2-2.0.0b4.tar.gz (440.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

python_constraint2-2.0.0b4-cp311-cp311-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.11Windows x86-64

python_constraint2-2.0.0b4-cp311-cp311-manylinux_2_35_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b4-cp311-cp311-macosx_12_0_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.11macOS 12.0+ x86-64

python_constraint2-2.0.0b4-cp310-cp310-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.10Windows x86-64

python_constraint2-2.0.0b4-cp310-cp310-manylinux_2_35_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b4-cp310-cp310-macosx_12_0_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.10macOS 12.0+ x86-64

python_constraint2-2.0.0b4-cp39-cp39-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.9Windows x86-64

python_constraint2-2.0.0b4-cp39-cp39-manylinux_2_35_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b4-cp39-cp39-macosx_12_0_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.9macOS 12.0+ x86-64

python_constraint2-2.0.0b4-cp38-cp38-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.8Windows x86-64

python_constraint2-2.0.0b4-cp38-cp38-manylinux_2_35_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b4-cp38-cp38-macosx_12_0_x86_64.whl (453.6 kB view details)

Uploaded CPython 3.8macOS 12.0+ x86-64

File details

Details for the file python_constraint2-2.0.0b4.tar.gz.

File metadata

  • Download URL: python_constraint2-2.0.0b4.tar.gz
  • Upload date:
  • Size: 440.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for python_constraint2-2.0.0b4.tar.gz
Algorithm Hash digest
SHA256 2c8dd055f63b91ccd93d3145411648030a705306d18a78c3e8f739109c2e889d
MD5 4827d521151694e6afd9b6fa1b4ccbcc
BLAKE2b-256 d80157c6525b555f8de684c414c95b1dd41b9daa90a718634902db79a16b99ea

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cc0db24b33e4b127f48cb604a31553c06d180ee37152f179b6f15b01b28962ba
MD5 39ed264e0e7007c329eae9aae7a3bb99
BLAKE2b-256 e3e2df56b1dfd4f4780a2641bf9418b9e0b03bcb9881652c37f3400eb0688868

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 61222697d6fd4f91c485673f077b93b33b170dce8fcc56e0860a9f883319a1bf
MD5 ad0546516275ca8ff7592f43d5fb7562
BLAKE2b-256 aa330880ffee3e482cdf05923c09a7cc26f21391faa28672d4137b4f132be147

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp311-cp311-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 474413af601cebbe80a0acf425b8d61b706c261770c40def016ff99cd67ad408
MD5 49a4fd73994a4fff50465c2a84587b30
BLAKE2b-256 25841c59294cbf7d499471a7bc16b0cc29fbc48caf57bfe64235ab98a2f578b4

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e8d887493fe4b04e942961baca0c5d4e35fd21dca6996931d3e55a2f6e7934f3
MD5 ed21ed6c779527c62e80d9e860746474
BLAKE2b-256 fa904ab0c4e6a493ae1fe2162750b7285096a9b4151f2df8e35d8a51f25a6c2a

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d4c112f8b4f188035e4986ab0fd11569d6806eab03e6c8b9bdaa6825f5ee8008
MD5 b2b198e393304d02edfec8197b154886
BLAKE2b-256 96de59b71338180b33ca646c40a3d5667ef711e24b27e5bb4ab54f19d286871b

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp310-cp310-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 20d60b7744e71cb9dc0a9884ae1e0ad7d5283e8e6e22d46c424521353278dd8d
MD5 dfb67c17bab20bccbcccb55dd37bfe99
BLAKE2b-256 feab39fb1eeac8f4f1cbc086dde6ddb211f6e86229a80716e5040e8eb3b1efa4

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 cef91e781d0ef3f6d102ba49247ac76dd491448380ba7869b064988717ec72f4
MD5 9e519883d9f9062d8727c496c4f6a3f3
BLAKE2b-256 19f1818a78dd242ca975636f7bda53fe357ccd71c775ce4449df1f8d37926c45

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp39-cp39-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 37fd0045d807807e369cbfd1957333dee822b36ff401b3a9c51bd07034cc76c8
MD5 4d5162195200df75df78ee79ead5c0c6
BLAKE2b-256 61f252ea778cbf8248dee355fda39f987211d29243b72b613f9a97250810d58f

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp39-cp39-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 6621009bc550a97dfecf37b7f1767a1cd164a9b499c1112ca2cfbf354b06a7db
MD5 1f3e251af8f44da90b04d4c9737c6bc1
BLAKE2b-256 68cf0e3621d66564dfcf9ae1d6f034748cb3a0b38445f60944dd01949d50d6f0

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 fca41c6bc17fb8be1eac70e3eceeb66b97e687fbcd2fa93c9765733eccd43969
MD5 f19b002489685feb0ae7e017c70d2c8e
BLAKE2b-256 0151f0f65f1d63c539eac7b4d723b468222e548047567a44e9bc57fe46434599

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp38-cp38-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp38-cp38-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 1bf1315d60b6e5e9e795987e6747179ba1417261d36c2bad00125ada64f9d757
MD5 66fac6f0898befc2a2aef1ede4400a1f
BLAKE2b-256 07eb68fe00598b7a3fc82938a1e43a846eae00a195504acefe4922f29125ac79

See more details on using hashes here.

File details

Details for the file python_constraint2-2.0.0b4-cp38-cp38-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b4-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 2509bd509f72c776195719b8525e5bab58df661aaeb96c3e406c22222801f523
MD5 3f92034044cb26c8cb081bdfb48c84cb
BLAKE2b-256 5c8ff3459cdcfdefdf7ad0e51b3acd2c57e0d9120b17850364e10ef59e0d4631

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page