|License| |Build Status| |Docs| |Python Versions| |Downloads| |Status| |Code Coverage|
.. image:: https://github.com/python-constraint/python-constraint/raw/main/docs/assets/logo/N-Queens_problem_Python.svg
:align: center
:width: 50%
python-constraint
=================
| This software is now back to active development / maintainance status.
| For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
| The complete documentation can be found `here <http://python-constraint.github.io/python-constraint/>`_.
.. contents::
:local:
:depth: 1
Introduction
------------
The :code:`python-constraint` module offers efficient solvers for `Constraint Satisfaction Problems (CSPs) <https://en.wikipedia.org/wiki/Constraint_satisfaction_problem>`_ 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:
.. code-block:: python
>>> 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:
.. code-block:: python
>>> 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:
.. code-block:: python
>>> 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
.. role:: python(code)
:language: python
Predefined constraint types currently available:
- :python:`FunctionConstraint`
- :python:`AllDifferentConstraint`
- :python:`AllEqualConstraint`
- :python:`MaxSumConstraint`
- :python:`ExactSumConstraint`
- :python:`MinSumConstraint`
- :python:`MaxProdConstraint`
- :python:`MinProdConstraint`
- :python:`InSetConstraint`
- :python:`NotInSetConstraint`
- :python:`SomeInSetConstraint`
- :python:`SomeNotInSetConstraint`
API documentation
-----------------
Documentation for the module is available at: http://python-constraint.github.io/python-constraint/.
It can be built locally by running :code:`make clean html` from the `docs` folder.
For viewing RST files locally, `restview <https://pypi.org/project/restview/>`_ is recommended.
Download and install
--------------------
.. code-block:: shell
$ pip install python-constraint
Testing
-------
Run :code:`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 :code:`pytest` (optionally add :code:`--no-cov` if you have the C-extensions enabled).
Contributing
------------
Feel free to contribute by `submitting pull requests <https://github.com/python-constraint/python-constraint/pulls>`_ or `opening issues <https://github.com/python-constraint/python-constraint/issues>`_.
Please refer to the `contribution guidelines <https://github.com/python-constraint/python-constraint/contribute>`_ before doing so.
Roadmap
-------
This GitHub organization and repository is a global effort to help to maintain :code:`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 <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
Planned development:
- Add a string parser for constraints
- Add parallel-capable solver
- Versioned documentation
Contact
-------
- `Gustavo Niemeyer <https://github.com/niemeyer/>`_ <gustavo@niemeyer.net>
- `Sébastien Celles <https://github.com/scls19fr/>`_ <s.celles@gmail.com>
- `Floris-Jan Willemsen <https://github.com/fjwillemsen>`_
But it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.
.. |License| image:: https://img.shields.io/pypi/l/python-constraint2
:alt: PyPI - License
.. |Build Status| image:: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml/badge.svg
:target: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml
:alt: Build Status
.. |Docs| image:: https://img.shields.io/github/actions/workflow/status/python-constraint/python-constraint/publish-documentation.yml?label=Docs
:target: http://python-constraint.github.io/python-constraint/
:alt: Documentation Status
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/python-constraint2
:alt: PyPI - Python Versions
.. |Downloads| image:: https://img.shields.io/pypi/dm/python-constraint
:alt: PyPI - Downloads
.. |Status| image:: https://img.shields.io/pypi/status/python-constraint2
:alt: PyPI - Status
.. |Code Coverage| image:: https://coveralls.io/repos/github/python-constraint/python-constraint/badge.svg
:target: https://coveralls.io/github/python-constraint/python-constraint
:alt: Code Coverage
Raw data
{
"_id": null,
"home_page": "https://github.com/python-constraint/python-constraint",
"name": "python-constraint2",
"maintainer": "Floris-Jan Willemsen",
"docs_url": null,
"requires_python": ">=3.8,<3.12",
"maintainer_email": "fjwillemsen97@gmail.com",
"keywords": "CSP,constraint solving problems,problem solver,SMT,satisfiability modulo theory,SAT",
"author": "Gustavo Niemeyer",
"author_email": "gustavo@niemeyer.net",
"download_url": "https://files.pythonhosted.org/packages/c1/81/813c13384f9cad3f487250d77bc9f70ead8b3f8072bd7e4d2229a9a3b62e/python_constraint2-2.0.0b3.tar.gz",
"platform": null,
"description": "|License| |Build Status| |Docs| |Python Versions| |Downloads| |Status| |Code Coverage|\n\n.. image:: https://github.com/python-constraint/python-constraint/raw/main/docs/assets/logo/N-Queens_problem_Python.svg\n :align: center\n :width: 50%\n\npython-constraint\n=================\n\n| This software is now back to active development / maintainance status.\n| For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.\n| The complete documentation can be found `here <http://python-constraint.github.io/python-constraint/>`_.\n\n.. contents::\n :local:\n :depth: 1\n\nIntroduction\n------------\nThe :code:`python-constraint` module offers efficient solvers for `Constraint Satisfaction Problems (CSPs) <https://en.wikipedia.org/wiki/Constraint_satisfaction_problem>`_ over finite domains in an accessible Python package.\nCSP is class of problems which may be represented in terms of variables (a, b, ...), domains (a in [1, 2, 3], ...), and constraints (a < b, ...).\n\nExamples\n--------\n\nBasics\n~~~~~~\n\nThis interactive Python session demonstrates basic operations:\n\n.. code-block:: python\n\n >>> from constraint import *\n >>> problem = Problem()\n >>> problem.addVariable(\"a\", [1,2,3])\n >>> problem.addVariable(\"b\", [4,5,6])\n >>> problem.getSolutions()\n [{'a': 3, 'b': 6}, {'a': 3, 'b': 5}, {'a': 3, 'b': 4},\n {'a': 2, 'b': 6}, {'a': 2, 'b': 5}, {'a': 2, 'b': 4},\n {'a': 1, 'b': 6}, {'a': 1, 'b': 5}, {'a': 1, 'b': 4}]\n\n >>> problem.addConstraint(lambda a, b: a*2 == b,\n (\"a\", \"b\"))\n >>> problem.getSolutions()\n [{'a': 3, 'b': 6}, {'a': 2, 'b': 4}]\n\n >>> problem = Problem()\n >>> problem.addVariables([\"a\", \"b\"], [1, 2, 3])\n >>> problem.addConstraint(AllDifferentConstraint())\n >>> problem.getSolutions()\n [{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3},\n {'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]\n\nRooks problem\n~~~~~~~~~~~~~\n\nThe following example solves the classical Eight Rooks problem:\n\n.. code-block:: python\n\n >>> problem = Problem()\n >>> numpieces = 8\n >>> cols = range(numpieces)\n >>> rows = range(numpieces)\n >>> problem.addVariables(cols, rows)\n >>> for col1 in cols:\n ... for col2 in cols:\n ... if col1 < col2:\n ... problem.addConstraint(lambda row1, row2: row1 != row2,\n ... (col1, col2))\n >>> solutions = problem.getSolutions()\n >>> solutions\n >>> solutions\n [{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1, 7: 0},\n {0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 0, 7: 1},\n {0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 2, 7: 0},\n {0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 0, 7: 2},\n ...\n {0: 7, 1: 5, 2: 3, 3: 6, 4: 2, 5: 1, 6: 4, 7: 0},\n {0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 0, 7: 4},\n {0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 4, 7: 0},\n {0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 2, 7: 0},\n {0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 0, 7: 2},\n ...]\n\n\nMagic squares\n~~~~~~~~~~~~~\n\nThis example solves a 4x4 magic square:\n\n.. code-block:: python\n\n >>> problem = Problem()\n >>> problem.addVariables(range(0, 16), range(1, 16 + 1))\n >>> problem.addConstraint(AllDifferentConstraint(), range(0, 16))\n >>> problem.addConstraint(ExactSumConstraint(34), [0, 5, 10, 15])\n >>> problem.addConstraint(ExactSumConstraint(34), [3, 6, 9, 12])\n >>> for row in range(4):\n ... problem.addConstraint(ExactSumConstraint(34),\n [row * 4 + i for i in range(4)])\n >>> for col in range(4):\n ... problem.addConstraint(ExactSumConstraint(34),\n [col + 4 * i for i in range(4)])\n >>> solutions = problem.getSolutions()\n\nFeatures\n--------\n\nThe following solvers are available:\n\n- Backtracking solver\n- Optimized backtracking solver\n- Recursive backtracking solver\n- Minimum conflicts solver\n\n.. role:: python(code)\n :language: python\n\nPredefined constraint types currently available:\n\n- :python:`FunctionConstraint`\n- :python:`AllDifferentConstraint`\n- :python:`AllEqualConstraint`\n- :python:`MaxSumConstraint`\n- :python:`ExactSumConstraint`\n- :python:`MinSumConstraint`\n- :python:`MaxProdConstraint`\n- :python:`MinProdConstraint`\n- :python:`InSetConstraint`\n- :python:`NotInSetConstraint`\n- :python:`SomeInSetConstraint`\n- :python:`SomeNotInSetConstraint`\n\nAPI documentation\n-----------------\nDocumentation for the module is available at: http://python-constraint.github.io/python-constraint/.\nIt can be built locally by running :code:`make clean html` from the `docs` folder.\nFor viewing RST files locally, `restview <https://pypi.org/project/restview/>`_ is recommended.\n\nDownload and install\n--------------------\n\n.. code-block:: shell\n\n $ pip install python-constraint\n\nTesting\n-------\n\nRun :code:`nox` (tests for all supported Python versions in own virtual environment).\n\nTo test against your local Python version: make sure you have the development dependencies installed.\nRun :code:`pytest` (optionally add :code:`--no-cov` if you have the C-extensions enabled).\n\nContributing\n------------\n\nFeel free to contribute by `submitting pull requests <https://github.com/python-constraint/python-constraint/pulls>`_ or `opening issues <https://github.com/python-constraint/python-constraint/issues>`_.\nPlease refer to the `contribution guidelines <https://github.com/python-constraint/python-constraint/contribute>`_ before doing so.\n\nRoadmap\n-------\n\nThis GitHub organization and repository is a global effort to help to maintain :code:`python-constraint`, which was written by Gustavo Niemeyer and originaly located at https://labix.org/python-constraint.\nFor an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.\n\nPlanned development:\n\n- Add a string parser for constraints\n- Add parallel-capable solver\n- Versioned documentation\n\nContact\n-------\n- `Gustavo Niemeyer <https://github.com/niemeyer/>`_ <gustavo@niemeyer.net>\n- `S\u00e9bastien Celles <https://github.com/scls19fr/>`_ <s.celles@gmail.com>\n- `Floris-Jan Willemsen <https://github.com/fjwillemsen>`_\n\nBut it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.\n\n.. |License| image:: https://img.shields.io/pypi/l/python-constraint2\n :alt: PyPI - License\n\n.. |Build Status| image:: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml/badge.svg\n :target: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml\n :alt: Build Status\n\n.. |Docs| image:: https://img.shields.io/github/actions/workflow/status/python-constraint/python-constraint/publish-documentation.yml?label=Docs\n :target: http://python-constraint.github.io/python-constraint/\n :alt: Documentation Status\n\n.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/python-constraint2\n :alt: PyPI - Python Versions\n\n.. |Downloads| image:: https://img.shields.io/pypi/dm/python-constraint\n :alt: PyPI - Downloads\n\n.. |Status| image:: https://img.shields.io/pypi/status/python-constraint2\n :alt: PyPI - Status\n\n.. |Code Coverage| image:: https://coveralls.io/repos/github/python-constraint/python-constraint/badge.svg\n :target: https://coveralls.io/github/python-constraint/python-constraint\n :alt: Code Coverage\n\n",
"bugtrack_url": null,
"license": "BSD-2-Clause",
"summary": "python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains.",
"version": "2.0.0b3",
"project_urls": {
"Documentation": "http://python-constraint.github.io/python-constraint/",
"Homepage": "https://github.com/python-constraint/python-constraint",
"Repository": "https://github.com/python-constraint/python-constraint"
},
"split_keywords": [
"csp",
"constraint solving problems",
"problem solver",
"smt",
"satisfiability modulo theory",
"sat"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cc37206ce79edbf8dc73e13d4c02e4613c73d40b096a88ac17722a162e9bd83e",
"md5": "4f70e4a3383567628111db274153f676",
"sha256": "01fe54bafe7d1ef8db2408d1c74f8ca7f30feb38df14b98a5003e00379ff1741"
},
"downloads": -1,
"filename": "python_constraint2-2.0.0b3-cp311-cp311-manylinux_2_35_x86_64.whl",
"has_sig": false,
"md5_digest": "4f70e4a3383567628111db274153f676",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.8,<3.12",
"size": 440379,
"upload_time": "2023-09-11T09:51:31",
"upload_time_iso_8601": "2023-09-11T09:51:31.673489Z",
"url": "https://files.pythonhosted.org/packages/cc/37/206ce79edbf8dc73e13d4c02e4613c73d40b096a88ac17722a162e9bd83e/python_constraint2-2.0.0b3-cp311-cp311-manylinux_2_35_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c181813c13384f9cad3f487250d77bc9f70ead8b3f8072bd7e4d2229a9a3b62e",
"md5": "079d898b72e75d199f5c0bf29889e275",
"sha256": "ab80ef97b96ff76ee71d965f130a427a89e0f80a27c09c0b76686a028fffb4e9"
},
"downloads": -1,
"filename": "python_constraint2-2.0.0b3.tar.gz",
"has_sig": false,
"md5_digest": "079d898b72e75d199f5c0bf29889e275",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<3.12",
"size": 429331,
"upload_time": "2023-09-11T09:51:33",
"upload_time_iso_8601": "2023-09-11T09:51:33.688748Z",
"url": "https://files.pythonhosted.org/packages/c1/81/813c13384f9cad3f487250d77bc9f70ead8b3f8072bd7e4d2229a9a3b62e/python_constraint2-2.0.0b3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-11 09:51:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "python-constraint",
"github_project": "python-constraint",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "python-constraint2"
}