python-constraint is a module implementing support for handling CSPs (Constraint Solving Problems) over finite domain
Project description
python-constraint
Introduction
The Python constraint module offers solvers for Constraint Solving Problems (CSPs) 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:
>>> 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
Recursive backtracking solver
Minimum conflicts solver
Predefined constraint types currently available:
FunctionConstraint
AllDifferentConstraint
AllEqualConstraint
ExactSumConstraint
MaxSumConstraint
MinSumConstraint
InSetConstraint
NotInSetConstraint
SomeInSetConstraint
SomeNotInSetConstraint
API documentation
Documentation for the module is available at: <http://labix.org/doc/constraint/>
Download
New version (Python 2 & 3)
$ pip install git+https://github.com/python-constraint/python-constraint.git
Old version (v 1.2 - Python 2 only)
Download the module at the Python Package Index: <https://pypi-hypernode.com/pypi/python-constraint>
Original code / information
Code was taken from https://pypi-hypernode.com/pypi/python-constraint/1.2 (2014-04-04)
Roadmap
This GitHub organization and repository is a global effort to help to maintain 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 <gustavo@niemeyer.net>
Sébastien Celles <s.celles@gmail.com>
But it’s probably better to open an issue <https://github.com/python-constraint/python-constraint/issues>
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
Built Distribution
Hashes for python_constraint-1.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 973583372775fe93aa06e1ae5d2ce26458569abd35e7da4eacd1a4bdb4723e6b |
|
MD5 | 1478fc6da3bbc6feb02f4e74d18fe7f5 |
|
BLAKE2b-256 | c7c7cd77b2992c565ff70e9cd9b2d29f4b83cc754aab9936f9d1937a980b459c |