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.0b8.tar.gz (440.8 kB view details)

Uploaded Source

Built Distributions

python_constraint2-2.0.0b8-cp313-cp313-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.13 Windows x86-64

python_constraint2-2.0.0b8-cp313-cp313-manylinux_2_35_x86_64.whl (453.5 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b8-cp313-cp313-macosx_14_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.13 macOS 14.0+ ARM64

python_constraint2-2.0.0b8-cp312-cp312-win_amd64.whl (453.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

python_constraint2-2.0.0b8-cp312-cp312-manylinux_2_35_x86_64.whl (453.5 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b8-cp312-cp312-macosx_14_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.12 macOS 14.0+ ARM64

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

Uploaded CPython 3.11 Windows x86-64

python_constraint2-2.0.0b8-cp311-cp311-manylinux_2_35_x86_64.whl (453.5 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b8-cp311-cp311-macosx_14_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.11 macOS 14.0+ ARM64

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

Uploaded CPython 3.10 Windows x86-64

python_constraint2-2.0.0b8-cp310-cp310-manylinux_2_35_x86_64.whl (453.5 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b8-cp310-cp310-macosx_14_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.10 macOS 14.0+ ARM64

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

Uploaded CPython 3.9 Windows x86-64

python_constraint2-2.0.0b8-cp39-cp39-manylinux_2_35_x86_64.whl (453.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b8-cp39-cp39-macosx_14_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.9 macOS 14.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for python_constraint2-2.0.0b8.tar.gz
Algorithm Hash digest
SHA256 5bb7948c54a75c8b73b3484c3c58fbd347b65d9e12072d54b832ac12cdce35bb
MD5 70f9e6fd382c08acf9ca0dd93a1e9206
BLAKE2b-256 0309c9f80fb1acb536150546f6694fbc214c76660959646218d05a94f7f66407

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8.tar.gz:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7d136dc7dbd2a6b33b79ecd889979ecacd724473c312202bb2470bb07100cc8c
MD5 5f55cb584988d8fb6b726f94a16cb497
BLAKE2b-256 5105d9be011f2c82c769fad931a205cc3a9043c8fd3dc6b55a1c47b839ca849b

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp313-cp313-win_amd64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 008ee682ab4f85d0a1a5ee0a869ffbb1addb98c872a30de94a85248fd5a0373f
MD5 100deb57bea22432eaf4358aa58bb775
BLAKE2b-256 9ad0fd194a534febfb890324ba5f76f525b2d95e0837c9aac4ad67357149e42a

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp313-cp313-manylinux_2_35_x86_64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ed2bd9a34a5f6c4d4dd883400a426f2c97e639b8e285497523dd21d61c4ec342
MD5 198a96f48c0ebcab0f074cdb5e82ad0e
BLAKE2b-256 758fac13980913dad5404396de6bee979e2ec34da1431754f65e81c799b6042a

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp313-cp313-macosx_14_0_arm64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4a8bb2108c87214d90f9ba2cd6cb19800f8a6a1fbc1ad75661d375c83c39d7ae
MD5 b1b57a6171683dde3a308f0df242382d
BLAKE2b-256 4ad3d13f65446b0f56019544c13a071fddcc64afd134c334f854bd745ed95b16

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp312-cp312-win_amd64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8547ef9ffa4771bf2aaecabab550c9be45c645807b998755597493a1f9c6beab
MD5 edc5c1a6b89b312509fe497444037c82
BLAKE2b-256 a0cd3b8d676d740abe5108f1b1b8185e292d100ed0ab70d4160c65c2817a5742

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp312-cp312-manylinux_2_35_x86_64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a3190d2015b6e931681bd9b7f2c52a44042ad2c9f384f1e779b6f5365d84e20a
MD5 0aa5b785fdce5b8b0993379b820d049a
BLAKE2b-256 bf5d713b7fe9edb3a8af814c5cefb290fe35b738e2fa838c15bda2a21990ebcc

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp312-cp312-macosx_14_0_arm64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4cd0483dbe17bbb157996d81e655ffbf248a0e54b8f42e830dc569aa84a23626
MD5 3e7804bf3eea04fa9b079438959455b7
BLAKE2b-256 42504e98ee24f16bd2d7fc5876f3aea1ed7add3d158b2f4d93f66d465ce0841c

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp311-cp311-win_amd64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 a7644e7a13cc3661720a3011478c89e978baf8388ec88429c0697fc4526f1ae7
MD5 12280f2b06c3ccc3b619149cdb476f12
BLAKE2b-256 2720bf47ff0c5896c5f58d04c3da8ede348f80836ddd039f74416df13862101c

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp311-cp311-manylinux_2_35_x86_64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 42afe3c7b516f4edb4c4983431c371fe7912db27aa8658e37e283bbd1cfd8098
MD5 753341aba070e5ef0956d189c4b14fc1
BLAKE2b-256 dab4ed706efe63a82aa99bb1e0cc4454fe36e1ad2b8614ddc9c347a2b3768c72

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp311-cp311-macosx_14_0_arm64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b074f004651117da4bedab26101a22c6db042e55dc88c88e051833237db9eff0
MD5 d11974ebf5e11018ad811dc1d8ca29f2
BLAKE2b-256 2f5ef26a022ff1ca312e1fff9de174cf69eb386948b857918eb097c1a6d94789

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp310-cp310-win_amd64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 7c2c4a786205776ee2863e1255505194ee4a9549dd39f6475064080e536a045e
MD5 365db18aee823517f6ef3d3b379e0622
BLAKE2b-256 555f14ef91a8e6430739cc7b6faa021841bef293b07c0bf25aa86e19c163b104

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp310-cp310-manylinux_2_35_x86_64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3f1bbb1808739ca400b520cdb0f45c281bf205c1a8b41fc473d993fe1e0316bf
MD5 35f6d1bb2f461bdb57aee375e6f530de
BLAKE2b-256 165f1b04229385d33373d347a455ed3a7f36ca76f84adbb3f97ec5e3a00192e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp310-cp310-macosx_14_0_arm64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c0cc5d81b47ea56dd7e561d0967315beee8739a7c102fb948d1421c361044105
MD5 7cb73b3291ddb59dfdbdf38209094ba0
BLAKE2b-256 0efa9adc939f9fadd8f68cf2e77181ac501bb0eeba9cd079ddc6b56c69752dbb

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp39-cp39-win_amd64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 fc2197879b43d8a2e53f476f2c31c21ea323843d7ea90c10b7c0e2f67771c4f1
MD5 94a061593c16e6ff765efcf03ce7f227
BLAKE2b-256 141dd79f3281f01812f97cd4e2856e44e9e81b606725b7dc93e91134a26558cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp39-cp39-manylinux_2_35_x86_64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

File details

Details for the file python_constraint2-2.0.0b8-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for python_constraint2-2.0.0b8-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 24e15e37835068320f23d43b28874620bd5b92cbc6404906f74c3f0a3ba2eeb7
MD5 c4cde8984db8e464a25329a6f6616371
BLAKE2b-256 c26fb9d6cb0c89d5ca50ffddb5d8014f07d6824f43cd518d55b510f6a5d29d96

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_constraint2-2.0.0b8-cp39-cp39-macosx_14_0_arm64.whl:

Publisher: publish-package.yml on python-constraint/python-constraint

Attestations:

Supported by

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