|Build Status| |Code Health| |Code Coverage|
python-constraint
=================
Introduction
------------
The Python constraint module offers solvers for `Constraint Satisfaction Problems (CSPs) <https://en.wikipedia.org/wiki/Constraint_satisfaction_problem>`_ over finite domains in simple and pure Python. 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 the module basic operation:
.. 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
- Recursive backtracking solver
- Minimum conflicts solver
.. role:: python(code)
:language: python
Predefined constraint types currently available:
- :python:`FunctionConstraint`
- :python:`AllDifferentConstraint`
- :python:`AllEqualConstraint`
- :python:`ExactSumConstraint`
- :python:`MaxSumConstraint`
- :python:`MinSumConstraint`
- :python:`InSetConstraint`
- :python:`NotInSetConstraint`
- :python:`SomeInSetConstraint`
- :python:`SomeNotInSetConstraint`
API documentation
-----------------
Documentation for the module is available at: http://labix.org/doc/constraint/
Download and install
--------------------
.. code-block:: shell
$ pip install python-constraint
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
- Create some unit tests - DONE
- Enable continuous integration - DONE
- Port to Python 3 (Python 2 being also supported) - DONE
- Respect Style Guide for Python Code (PEP8) - DONE
- Improve code coverage writting more unit tests - ToDo
- Move doc to Sphinx or MkDocs - https://readthedocs.org/ - ToDo
Contact
-------
- `Gustavo Niemeyer <https://github.com/niemeyer/>`_ <gustavo@niemeyer.net>
- `Sébastien Celles <https://github.com/scls19fr/>`_ <s.celles@gmail.com>
But it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.
.. |Build Status| image:: https://travis-ci.org/python-constraint/python-constraint.svg?branch=master
:target: https://travis-ci.org/python-constraint/python-constraint
.. |Code Health| image:: https://landscape.io/github/python-constraint/python-constraint/master/landscape.svg?style=flat
:target: https://landscape.io/github/python-constraint/python-constraint/master
:alt: Code Health
.. |Code Coverage| image:: https://coveralls.io/repos/github/python-constraint/python-constraint/badge.svg
:target: https://coveralls.io/github/python-constraint/python-constraint
Raw data
{
"_id": null,
"home_page": "https://github.com/python-constraint/python-constraint",
"name": "python-constraint",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "csp constraint solving problems problem solver",
"author": "Gustavo Niemeyer",
"author_email": "gustavo@niemeyer.net",
"download_url": "https://files.pythonhosted.org/packages/37/8b/5f1bc2734ca611943e1d6733ee244238679f6410a10cd45ede55a61a8402/python-constraint-1.4.0.tar.bz2",
"platform": "",
"description": "|Build Status| |Code Health| |Code Coverage|\n\npython-constraint\n=================\n\nIntroduction\n------------\nThe Python constraint module offers solvers for `Constraint Satisfaction Problems (CSPs) <https://en.wikipedia.org/wiki/Constraint_satisfaction_problem>`_ over finite domains in simple and pure Python. 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, ...).\n\nExamples\n--------\n\nBasics\n~~~~~~\n\nThis interactive Python session demonstrates the module basic operation:\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- Recursive backtracking solver\n- Minimum conflicts solver\n\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:`ExactSumConstraint`\n- :python:`MaxSumConstraint`\n- :python:`MinSumConstraint`\n- :python:`InSetConstraint`\n- :python:`NotInSetConstraint`\n- :python:`SomeInSetConstraint`\n- :python:`SomeNotInSetConstraint`\n\nAPI documentation\n-----------------\nDocumentation for the module is available at: http://labix.org/doc/constraint/\n\nDownload and install\n--------------------\n\n.. code-block:: shell\n\n $ pip install python-constraint\n\nRoadmap\n-------\n\nThis GitHub organization and repository is a global effort to help to\nmaintain python-constraint which was written by Gustavo Niemeyer\nand originaly located at https://labix.org/python-constraint\n\n- Create some unit tests - DONE\n- Enable continuous integration - DONE\n- Port to Python 3 (Python 2 being also supported) - DONE\n- Respect Style Guide for Python Code (PEP8) - DONE\n- Improve code coverage writting more unit tests - ToDo\n- Move doc to Sphinx or MkDocs - https://readthedocs.org/ - ToDo\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\nBut it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.\n\n\n.. |Build Status| image:: https://travis-ci.org/python-constraint/python-constraint.svg?branch=master\n :target: https://travis-ci.org/python-constraint/python-constraint\n.. |Code Health| image:: https://landscape.io/github/python-constraint/python-constraint/master/landscape.svg?style=flat\n :target: https://landscape.io/github/python-constraint/python-constraint/master\n :alt: Code Health\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",
"bugtrack_url": null,
"license": "",
"summary": "python-constraint is a module implementing support for handling CSPs (Constraint Solving Problems) over finite domain",
"version": "1.4.0",
"split_keywords": [
"csp",
"constraint",
"solving",
"problems",
"problem",
"solver"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "53e4d375d8c84b383d9debf5e517d21b",
"sha256": "501d6f17afe0032dfc6ea6c0f8acc12e44f992733f00e8538961031ef27ccb8e"
},
"downloads": -1,
"filename": "python-constraint-1.4.0.tar.bz2",
"has_sig": false,
"md5_digest": "53e4d375d8c84b383d9debf5e517d21b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18416,
"upload_time": "2018-11-05T09:02:44",
"upload_time_iso_8601": "2018-11-05T09:02:44.334316Z",
"url": "https://files.pythonhosted.org/packages/37/8b/5f1bc2734ca611943e1d6733ee244238679f6410a10cd45ede55a61a8402/python-constraint-1.4.0.tar.bz2",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2018-11-05 09:02:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "python-constraint",
"github_project": "python-constraint",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "python-constraint"
}