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

Uploaded Source

Built Distributions

python_constraint2-2.0.0b3-cp311-cp311-win_amd64.whl (440.5 kB view details)

Uploaded CPython 3.11 Windows x86-64

python_constraint2-2.0.0b3-cp311-cp311-manylinux_2_35_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b3-cp311-cp311-macosx_12_0_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.11 macOS 12.0+ x86-64

python_constraint2-2.0.0b3-cp310-cp310-win_amd64.whl (440.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

python_constraint2-2.0.0b3-cp310-cp310-manylinux_2_35_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b3-cp310-cp310-macosx_12_0_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.10 macOS 12.0+ x86-64

python_constraint2-2.0.0b3-cp39-cp39-win_amd64.whl (440.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

python_constraint2-2.0.0b3-cp39-cp39-manylinux_2_35_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b3-cp39-cp39-macosx_12_0_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.9 macOS 12.0+ x86-64

python_constraint2-2.0.0b3-cp38-cp38-win_amd64.whl (440.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

python_constraint2-2.0.0b3-cp38-cp38-manylinux_2_35_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.35+ x86-64

python_constraint2-2.0.0b3-cp38-cp38-macosx_12_0_x86_64.whl (440.4 kB view details)

Uploaded CPython 3.8 macOS 12.0+ x86-64

File details

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

File metadata

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

File hashes

Hashes for python_constraint2-2.0.0b3.tar.gz
Algorithm Hash digest
SHA256 ab80ef97b96ff76ee71d965f130a427a89e0f80a27c09c0b76686a028fffb4e9
MD5 079d898b72e75d199f5c0bf29889e275
BLAKE2b-256 c181813c13384f9cad3f487250d77bc9f70ead8b3f8072bd7e4d2229a9a3b62e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2933cfdf4f6ad8e9de99dd196df40f8d6a1740d49104d379195f71ab07ecde45
MD5 9c0133b2397457918ed1bc201794565f
BLAKE2b-256 af86e86370578dc439adca68423350f1df57cf802ae5802e5d4eae31e86b142e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 01fe54bafe7d1ef8db2408d1c74f8ca7f30feb38df14b98a5003e00379ff1741
MD5 4f70e4a3383567628111db274153f676
BLAKE2b-256 cc37206ce79edbf8dc73e13d4c02e4613c73d40b096a88ac17722a162e9bd83e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 345f9217dced0412d523a573900a21d0d0c3d70fa2a179a8442f464bebf61347
MD5 3dcca230b684de8f3493ab53b542cf7d
BLAKE2b-256 cc7d378295e9973296fd5922fac804fb9b7d2de05c22651ad8008c18190f1dfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f4a6f1a4521af1853345702be33ae04fac8539571cafd6ba984da775d0d997ed
MD5 7933de1feb2f6e52e9930234b18e3b74
BLAKE2b-256 a47ebf9c13e39c659dad573786925f30d17e721b39ff0373d33032bb21eea716

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 9b21181000f6fc0bcdf459ef1571332170c4fab8911887df50328b72eecf0152
MD5 e53a7a1116540bf04fab048069aa5a88
BLAKE2b-256 27ce706e51a402ec26ec500ffaa61b8d109fe8bb2483541bba875cd190e23106

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 9098abc3cc5216e1b9b893811542d2775ddec6b12981c2af95c2b729c872440b
MD5 4853b499f2d81748e129c9309188b3dc
BLAKE2b-256 08cc9b774cf15f2fd424b49edcecd921887b02b40b61c67902adc2d2e6295501

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 864705d2896a5051ffee6185750f8530957d839e263dba71766cdb5df0d5a337
MD5 87e37b77fb706c0ac19c1793ea8e53de
BLAKE2b-256 2da1a97ad41b93bbdc2a810712a72adc1077f774379f516ada6dd9e5abf3206e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 129841a58bed0f20be48c158a7063a455984916dcd43581d214388781179dca0
MD5 eb47a929642c8dd1c3f3fda1a47189bb
BLAKE2b-256 fc233fbdad9503ccc67c61fe12444b362229cc42061400a127673aa192a06abd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 5b8542de5420282690c94965b49f016981a72b12bec508557a927839af9007e4
MD5 14b87e6aea95d1157524ff6ce5067583
BLAKE2b-256 e331bf37eed0690c11693e409fb1467c0dced963f1ae5728a9caa572a9bc42b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d4db079c52b4307c35a58681a74fa58a27728c5adbcf5ce0cefafeade85a09a3
MD5 c32d1da20569c81f66884cde2bafd571
BLAKE2b-256 fd16be0db68cb3d3aa2faae5f10f8ead266c630fef25be0714cdc35b250cb71a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp38-cp38-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 0d829bc47d0c7921791293e18aec35f5d4021ad6c4127f79901890f9263b8ea2
MD5 aec711970444a861e67fa858ee258602
BLAKE2b-256 e71ccb46febac5be843b965bee0d1dbe36e680d0c18ece0a95475790849968a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_constraint2-2.0.0b3-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 459e57d12bfd436af551099b37ffb90fc6f40e71f7d915fec551eaf46808a491
MD5 02ae712ace9d066ec8c03b9c8128486e
BLAKE2b-256 4d487061671d9f417061fed0da5f3be092d14b52f9fb40f1b82288a134975763

See more details on using hashes here.

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