python-constraint2 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_constraint2-2.0.0/LICENSE +23 -0
- python_constraint2-2.0.0/PKG-INFO +238 -0
- python_constraint2-2.0.0/README.rst +205 -0
- python_constraint2-2.0.0/constraint/__init__.py +37 -0
- python_constraint2-2.0.0/constraint/constraints.c +29807 -0
- python_constraint2-2.0.0/constraint/constraints.py +786 -0
- python_constraint2-2.0.0/constraint/domain.c +9306 -0
- python_constraint2-2.0.0/constraint/domain.py +102 -0
- python_constraint2-2.0.0/constraint/problem.c +16329 -0
- python_constraint2-2.0.0/constraint/problem.py +256 -0
- python_constraint2-2.0.0/constraint/solvers.c +23963 -0
- python_constraint2-2.0.0/constraint/solvers.py +592 -0
- python_constraint2-2.0.0/cythonize_build.py +36 -0
- python_constraint2-2.0.0/pyproject.toml +97 -0
- python_constraint2-2.0.0/tests/__init__.py +0 -0
- python_constraint2-2.0.0/tests/setup_teardown.py +40 -0
- python_constraint2-2.0.0/tests/test_compilation.py +17 -0
- python_constraint2-2.0.0/tests/test_constraint.py +127 -0
- python_constraint2-2.0.0/tests/test_doctests.py +10 -0
- python_constraint2-2.0.0/tests/test_problem.py +27 -0
- python_constraint2-2.0.0/tests/test_solvers.py +72 -0
- python_constraint2-2.0.0/tests/test_some_not_in_set.py +99 -0
- python_constraint2-2.0.0/tests/test_toml_file.py +72 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Copyright (c) 2005-2014 - Gustavo Niemeyer <gustavo@niemeyer.net>
|
|
2
|
+
|
|
3
|
+
All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
15
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
16
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
18
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
19
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
20
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
21
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
22
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
23
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: python-constraint2
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains.
|
|
5
|
+
License: BSD-2-Clause
|
|
6
|
+
Keywords: CSP,constraint solving problems,problem solver,SMT,satisfiability modulo theory,SAT
|
|
7
|
+
Author: Gustavo Niemeyer
|
|
8
|
+
Author-email: gustavo@niemeyer.net
|
|
9
|
+
Maintainer: Floris-Jan Willemsen
|
|
10
|
+
Maintainer-email: fjwillemsen97@gmail.com
|
|
11
|
+
Requires-Python: >=3.9,<3.14
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Education
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
18
|
+
Classifier: Natural Language :: English
|
|
19
|
+
Classifier: Programming Language :: C
|
|
20
|
+
Classifier: Programming Language :: Cython
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Topic :: Scientific/Engineering
|
|
28
|
+
Classifier: Topic :: Software Development
|
|
29
|
+
Project-URL: Documentation, http://python-constraint.github.io/python-constraint/
|
|
30
|
+
Project-URL: Repository, https://github.com/python-constraint/python-constraint
|
|
31
|
+
Description-Content-Type: text/x-rst
|
|
32
|
+
|
|
33
|
+
|License| |Build Status| |Docs| |Python Versions| |Downloads| |Status| |Code Coverage|
|
|
34
|
+
|
|
35
|
+
.. image:: https://github.com/python-constraint/python-constraint/raw/main/docs/assets/logo/N-Queens_problem_Python.svg
|
|
36
|
+
:align: center
|
|
37
|
+
:width: 50%
|
|
38
|
+
|
|
39
|
+
python-constraint
|
|
40
|
+
=================
|
|
41
|
+
|
|
42
|
+
| This software is now back to active development / maintainance status.
|
|
43
|
+
| IMPORTANT: the new version can be installed with `pip install python-constraint2`, as the original pip release will not be updated.
|
|
44
|
+
| For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
|
|
45
|
+
| The complete documentation can be found `here <http://python-constraint.github.io/python-constraint/>`_.
|
|
46
|
+
|
|
47
|
+
.. contents::
|
|
48
|
+
:local:
|
|
49
|
+
:depth: 1
|
|
50
|
+
|
|
51
|
+
Introduction
|
|
52
|
+
------------
|
|
53
|
+
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.
|
|
54
|
+
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, ...).
|
|
55
|
+
|
|
56
|
+
Examples
|
|
57
|
+
--------
|
|
58
|
+
|
|
59
|
+
Basics
|
|
60
|
+
~~~~~~
|
|
61
|
+
|
|
62
|
+
This interactive Python session demonstrates basic operations:
|
|
63
|
+
|
|
64
|
+
.. code-block:: python
|
|
65
|
+
|
|
66
|
+
>>> from constraint import *
|
|
67
|
+
>>> problem = Problem()
|
|
68
|
+
>>> problem.addVariable("a", [1,2,3])
|
|
69
|
+
>>> problem.addVariable("b", [4,5,6])
|
|
70
|
+
>>> problem.getSolutions()
|
|
71
|
+
[{'a': 3, 'b': 6}, {'a': 3, 'b': 5}, {'a': 3, 'b': 4},
|
|
72
|
+
{'a': 2, 'b': 6}, {'a': 2, 'b': 5}, {'a': 2, 'b': 4},
|
|
73
|
+
{'a': 1, 'b': 6}, {'a': 1, 'b': 5}, {'a': 1, 'b': 4}]
|
|
74
|
+
|
|
75
|
+
>>> problem.addConstraint(lambda a, b: a*2 == b,
|
|
76
|
+
("a", "b"))
|
|
77
|
+
>>> problem.getSolutions()
|
|
78
|
+
[{'a': 3, 'b': 6}, {'a': 2, 'b': 4}]
|
|
79
|
+
|
|
80
|
+
>>> problem = Problem()
|
|
81
|
+
>>> problem.addVariables(["a", "b"], [1, 2, 3])
|
|
82
|
+
>>> problem.addConstraint(AllDifferentConstraint())
|
|
83
|
+
>>> problem.getSolutions()
|
|
84
|
+
[{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3},
|
|
85
|
+
{'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]
|
|
86
|
+
|
|
87
|
+
Rooks problem
|
|
88
|
+
~~~~~~~~~~~~~
|
|
89
|
+
|
|
90
|
+
The following example solves the classical Eight Rooks problem:
|
|
91
|
+
|
|
92
|
+
.. code-block:: python
|
|
93
|
+
|
|
94
|
+
>>> problem = Problem()
|
|
95
|
+
>>> numpieces = 8
|
|
96
|
+
>>> cols = range(numpieces)
|
|
97
|
+
>>> rows = range(numpieces)
|
|
98
|
+
>>> problem.addVariables(cols, rows)
|
|
99
|
+
>>> for col1 in cols:
|
|
100
|
+
... for col2 in cols:
|
|
101
|
+
... if col1 < col2:
|
|
102
|
+
... problem.addConstraint(lambda row1, row2: row1 != row2,
|
|
103
|
+
... (col1, col2))
|
|
104
|
+
>>> solutions = problem.getSolutions()
|
|
105
|
+
>>> solutions
|
|
106
|
+
>>> solutions
|
|
107
|
+
[{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1, 7: 0},
|
|
108
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 0, 7: 1},
|
|
109
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 2, 7: 0},
|
|
110
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 0, 7: 2},
|
|
111
|
+
...
|
|
112
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 2, 5: 1, 6: 4, 7: 0},
|
|
113
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 0, 7: 4},
|
|
114
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 4, 7: 0},
|
|
115
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 2, 7: 0},
|
|
116
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 0, 7: 2},
|
|
117
|
+
...]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
Magic squares
|
|
121
|
+
~~~~~~~~~~~~~
|
|
122
|
+
|
|
123
|
+
This example solves a 4x4 magic square:
|
|
124
|
+
|
|
125
|
+
.. code-block:: python
|
|
126
|
+
|
|
127
|
+
>>> problem = Problem()
|
|
128
|
+
>>> problem.addVariables(range(0, 16), range(1, 16 + 1))
|
|
129
|
+
>>> problem.addConstraint(AllDifferentConstraint(), range(0, 16))
|
|
130
|
+
>>> problem.addConstraint(ExactSumConstraint(34), [0, 5, 10, 15])
|
|
131
|
+
>>> problem.addConstraint(ExactSumConstraint(34), [3, 6, 9, 12])
|
|
132
|
+
>>> for row in range(4):
|
|
133
|
+
... problem.addConstraint(ExactSumConstraint(34),
|
|
134
|
+
[row * 4 + i for i in range(4)])
|
|
135
|
+
>>> for col in range(4):
|
|
136
|
+
... problem.addConstraint(ExactSumConstraint(34),
|
|
137
|
+
[col + 4 * i for i in range(4)])
|
|
138
|
+
>>> solutions = problem.getSolutions()
|
|
139
|
+
|
|
140
|
+
Features
|
|
141
|
+
--------
|
|
142
|
+
|
|
143
|
+
The following solvers are available:
|
|
144
|
+
|
|
145
|
+
- Backtracking solver
|
|
146
|
+
- Optimized backtracking solver
|
|
147
|
+
- Recursive backtracking solver
|
|
148
|
+
- Minimum conflicts solver
|
|
149
|
+
|
|
150
|
+
.. role:: python(code)
|
|
151
|
+
:language: python
|
|
152
|
+
|
|
153
|
+
Predefined constraint types currently available:
|
|
154
|
+
|
|
155
|
+
- :python:`FunctionConstraint`
|
|
156
|
+
- :python:`AllDifferentConstraint`
|
|
157
|
+
- :python:`AllEqualConstraint`
|
|
158
|
+
- :python:`MaxSumConstraint`
|
|
159
|
+
- :python:`ExactSumConstraint`
|
|
160
|
+
- :python:`MinSumConstraint`
|
|
161
|
+
- :python:`MaxProdConstraint`
|
|
162
|
+
- :python:`MinProdConstraint`
|
|
163
|
+
- :python:`InSetConstraint`
|
|
164
|
+
- :python:`NotInSetConstraint`
|
|
165
|
+
- :python:`SomeInSetConstraint`
|
|
166
|
+
- :python:`SomeNotInSetConstraint`
|
|
167
|
+
|
|
168
|
+
API documentation
|
|
169
|
+
-----------------
|
|
170
|
+
Documentation for the module is available at: http://python-constraint.github.io/python-constraint/.
|
|
171
|
+
It can be built locally by running :code:`make clean html` from the `docs` folder.
|
|
172
|
+
For viewing RST files locally, `restview <https://pypi.org/project/restview/>`_ is recommended.
|
|
173
|
+
|
|
174
|
+
Download and install
|
|
175
|
+
--------------------
|
|
176
|
+
|
|
177
|
+
.. code-block:: shell
|
|
178
|
+
|
|
179
|
+
$ pip install python-constraint2
|
|
180
|
+
|
|
181
|
+
Testing
|
|
182
|
+
-------
|
|
183
|
+
|
|
184
|
+
Run :code:`nox` (tests for all supported Python versions in own virtual environment).
|
|
185
|
+
|
|
186
|
+
To test against your local Python version: make sure you have the development dependencies installed.
|
|
187
|
+
Run :code:`pytest` (optionally add :code:`--no-cov` if you have the C-extensions enabled).
|
|
188
|
+
|
|
189
|
+
Contributing
|
|
190
|
+
------------
|
|
191
|
+
|
|
192
|
+
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>`_.
|
|
193
|
+
Please refer to the `contribution guidelines <https://github.com/python-constraint/python-constraint/contribute>`_ before doing so.
|
|
194
|
+
|
|
195
|
+
Roadmap
|
|
196
|
+
-------
|
|
197
|
+
|
|
198
|
+
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.
|
|
199
|
+
For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
|
|
200
|
+
|
|
201
|
+
Planned development:
|
|
202
|
+
|
|
203
|
+
- Add parallel-capable solver
|
|
204
|
+
- Add a string parser for constraints
|
|
205
|
+
- Versioned documentation
|
|
206
|
+
|
|
207
|
+
Contact
|
|
208
|
+
-------
|
|
209
|
+
- `Floris-Jan Willemsen <https://github.com/fjwillemsen>`_ <fjwillemsen97@gmail.com> (current maintainer)
|
|
210
|
+
- `Sébastien Celles <https://github.com/s-celles/>`_ <s.celles@gmail.com> (former maintainer)
|
|
211
|
+
- `Gustavo Niemeyer <https://github.com/niemeyer/>`_ <gustavo@niemeyer.net> (initial developer)
|
|
212
|
+
|
|
213
|
+
But it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.
|
|
214
|
+
|
|
215
|
+
.. |License| image:: https://img.shields.io/pypi/l/python-constraint2
|
|
216
|
+
:alt: PyPI - License
|
|
217
|
+
|
|
218
|
+
.. |Build Status| image:: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml/badge.svg
|
|
219
|
+
:target: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml
|
|
220
|
+
:alt: Build Status
|
|
221
|
+
|
|
222
|
+
.. |Docs| image:: https://img.shields.io/github/actions/workflow/status/python-constraint/python-constraint/publish-documentation.yml?label=Docs
|
|
223
|
+
:target: http://python-constraint.github.io/python-constraint/
|
|
224
|
+
:alt: Documentation Status
|
|
225
|
+
|
|
226
|
+
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/python-constraint2
|
|
227
|
+
:alt: PyPI - Python Versions
|
|
228
|
+
|
|
229
|
+
.. |Downloads| image:: https://img.shields.io/pypi/dm/python-constraint2
|
|
230
|
+
:alt: PyPI - Downloads
|
|
231
|
+
|
|
232
|
+
.. |Status| image:: https://img.shields.io/pypi/status/python-constraint2
|
|
233
|
+
:alt: PyPI - Status
|
|
234
|
+
|
|
235
|
+
.. |Code Coverage| image:: https://coveralls.io/repos/github/python-constraint/python-constraint/badge.svg
|
|
236
|
+
:target: https://coveralls.io/github/python-constraint/python-constraint
|
|
237
|
+
:alt: Code Coverage
|
|
238
|
+
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
|License| |Build Status| |Docs| |Python Versions| |Downloads| |Status| |Code Coverage|
|
|
2
|
+
|
|
3
|
+
.. image:: https://github.com/python-constraint/python-constraint/raw/main/docs/assets/logo/N-Queens_problem_Python.svg
|
|
4
|
+
:align: center
|
|
5
|
+
:width: 50%
|
|
6
|
+
|
|
7
|
+
python-constraint
|
|
8
|
+
=================
|
|
9
|
+
|
|
10
|
+
| This software is now back to active development / maintainance status.
|
|
11
|
+
| IMPORTANT: the new version can be installed with `pip install python-constraint2`, as the original pip release will not be updated.
|
|
12
|
+
| For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
|
|
13
|
+
| The complete documentation can be found `here <http://python-constraint.github.io/python-constraint/>`_.
|
|
14
|
+
|
|
15
|
+
.. contents::
|
|
16
|
+
:local:
|
|
17
|
+
:depth: 1
|
|
18
|
+
|
|
19
|
+
Introduction
|
|
20
|
+
------------
|
|
21
|
+
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.
|
|
22
|
+
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, ...).
|
|
23
|
+
|
|
24
|
+
Examples
|
|
25
|
+
--------
|
|
26
|
+
|
|
27
|
+
Basics
|
|
28
|
+
~~~~~~
|
|
29
|
+
|
|
30
|
+
This interactive Python session demonstrates basic operations:
|
|
31
|
+
|
|
32
|
+
.. code-block:: python
|
|
33
|
+
|
|
34
|
+
>>> from constraint import *
|
|
35
|
+
>>> problem = Problem()
|
|
36
|
+
>>> problem.addVariable("a", [1,2,3])
|
|
37
|
+
>>> problem.addVariable("b", [4,5,6])
|
|
38
|
+
>>> problem.getSolutions()
|
|
39
|
+
[{'a': 3, 'b': 6}, {'a': 3, 'b': 5}, {'a': 3, 'b': 4},
|
|
40
|
+
{'a': 2, 'b': 6}, {'a': 2, 'b': 5}, {'a': 2, 'b': 4},
|
|
41
|
+
{'a': 1, 'b': 6}, {'a': 1, 'b': 5}, {'a': 1, 'b': 4}]
|
|
42
|
+
|
|
43
|
+
>>> problem.addConstraint(lambda a, b: a*2 == b,
|
|
44
|
+
("a", "b"))
|
|
45
|
+
>>> problem.getSolutions()
|
|
46
|
+
[{'a': 3, 'b': 6}, {'a': 2, 'b': 4}]
|
|
47
|
+
|
|
48
|
+
>>> problem = Problem()
|
|
49
|
+
>>> problem.addVariables(["a", "b"], [1, 2, 3])
|
|
50
|
+
>>> problem.addConstraint(AllDifferentConstraint())
|
|
51
|
+
>>> problem.getSolutions()
|
|
52
|
+
[{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3},
|
|
53
|
+
{'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}]
|
|
54
|
+
|
|
55
|
+
Rooks problem
|
|
56
|
+
~~~~~~~~~~~~~
|
|
57
|
+
|
|
58
|
+
The following example solves the classical Eight Rooks problem:
|
|
59
|
+
|
|
60
|
+
.. code-block:: python
|
|
61
|
+
|
|
62
|
+
>>> problem = Problem()
|
|
63
|
+
>>> numpieces = 8
|
|
64
|
+
>>> cols = range(numpieces)
|
|
65
|
+
>>> rows = range(numpieces)
|
|
66
|
+
>>> problem.addVariables(cols, rows)
|
|
67
|
+
>>> for col1 in cols:
|
|
68
|
+
... for col2 in cols:
|
|
69
|
+
... if col1 < col2:
|
|
70
|
+
... problem.addConstraint(lambda row1, row2: row1 != row2,
|
|
71
|
+
... (col1, col2))
|
|
72
|
+
>>> solutions = problem.getSolutions()
|
|
73
|
+
>>> solutions
|
|
74
|
+
>>> solutions
|
|
75
|
+
[{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1, 7: 0},
|
|
76
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 0, 7: 1},
|
|
77
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 2, 7: 0},
|
|
78
|
+
{0: 7, 1: 6, 2: 5, 3: 4, 4: 3, 5: 1, 6: 0, 7: 2},
|
|
79
|
+
...
|
|
80
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 2, 5: 1, 6: 4, 7: 0},
|
|
81
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 0, 7: 4},
|
|
82
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 2, 6: 4, 7: 0},
|
|
83
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 2, 7: 0},
|
|
84
|
+
{0: 7, 1: 5, 2: 3, 3: 6, 4: 1, 5: 4, 6: 0, 7: 2},
|
|
85
|
+
...]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
Magic squares
|
|
89
|
+
~~~~~~~~~~~~~
|
|
90
|
+
|
|
91
|
+
This example solves a 4x4 magic square:
|
|
92
|
+
|
|
93
|
+
.. code-block:: python
|
|
94
|
+
|
|
95
|
+
>>> problem = Problem()
|
|
96
|
+
>>> problem.addVariables(range(0, 16), range(1, 16 + 1))
|
|
97
|
+
>>> problem.addConstraint(AllDifferentConstraint(), range(0, 16))
|
|
98
|
+
>>> problem.addConstraint(ExactSumConstraint(34), [0, 5, 10, 15])
|
|
99
|
+
>>> problem.addConstraint(ExactSumConstraint(34), [3, 6, 9, 12])
|
|
100
|
+
>>> for row in range(4):
|
|
101
|
+
... problem.addConstraint(ExactSumConstraint(34),
|
|
102
|
+
[row * 4 + i for i in range(4)])
|
|
103
|
+
>>> for col in range(4):
|
|
104
|
+
... problem.addConstraint(ExactSumConstraint(34),
|
|
105
|
+
[col + 4 * i for i in range(4)])
|
|
106
|
+
>>> solutions = problem.getSolutions()
|
|
107
|
+
|
|
108
|
+
Features
|
|
109
|
+
--------
|
|
110
|
+
|
|
111
|
+
The following solvers are available:
|
|
112
|
+
|
|
113
|
+
- Backtracking solver
|
|
114
|
+
- Optimized backtracking solver
|
|
115
|
+
- Recursive backtracking solver
|
|
116
|
+
- Minimum conflicts solver
|
|
117
|
+
|
|
118
|
+
.. role:: python(code)
|
|
119
|
+
:language: python
|
|
120
|
+
|
|
121
|
+
Predefined constraint types currently available:
|
|
122
|
+
|
|
123
|
+
- :python:`FunctionConstraint`
|
|
124
|
+
- :python:`AllDifferentConstraint`
|
|
125
|
+
- :python:`AllEqualConstraint`
|
|
126
|
+
- :python:`MaxSumConstraint`
|
|
127
|
+
- :python:`ExactSumConstraint`
|
|
128
|
+
- :python:`MinSumConstraint`
|
|
129
|
+
- :python:`MaxProdConstraint`
|
|
130
|
+
- :python:`MinProdConstraint`
|
|
131
|
+
- :python:`InSetConstraint`
|
|
132
|
+
- :python:`NotInSetConstraint`
|
|
133
|
+
- :python:`SomeInSetConstraint`
|
|
134
|
+
- :python:`SomeNotInSetConstraint`
|
|
135
|
+
|
|
136
|
+
API documentation
|
|
137
|
+
-----------------
|
|
138
|
+
Documentation for the module is available at: http://python-constraint.github.io/python-constraint/.
|
|
139
|
+
It can be built locally by running :code:`make clean html` from the `docs` folder.
|
|
140
|
+
For viewing RST files locally, `restview <https://pypi.org/project/restview/>`_ is recommended.
|
|
141
|
+
|
|
142
|
+
Download and install
|
|
143
|
+
--------------------
|
|
144
|
+
|
|
145
|
+
.. code-block:: shell
|
|
146
|
+
|
|
147
|
+
$ pip install python-constraint2
|
|
148
|
+
|
|
149
|
+
Testing
|
|
150
|
+
-------
|
|
151
|
+
|
|
152
|
+
Run :code:`nox` (tests for all supported Python versions in own virtual environment).
|
|
153
|
+
|
|
154
|
+
To test against your local Python version: make sure you have the development dependencies installed.
|
|
155
|
+
Run :code:`pytest` (optionally add :code:`--no-cov` if you have the C-extensions enabled).
|
|
156
|
+
|
|
157
|
+
Contributing
|
|
158
|
+
------------
|
|
159
|
+
|
|
160
|
+
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>`_.
|
|
161
|
+
Please refer to the `contribution guidelines <https://github.com/python-constraint/python-constraint/contribute>`_ before doing so.
|
|
162
|
+
|
|
163
|
+
Roadmap
|
|
164
|
+
-------
|
|
165
|
+
|
|
166
|
+
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.
|
|
167
|
+
For an overview of recent changes, visit the `Changelog <https://github.com/python-constraint/python-constraint/blob/main/CHANGELOG.md>`_.
|
|
168
|
+
|
|
169
|
+
Planned development:
|
|
170
|
+
|
|
171
|
+
- Add parallel-capable solver
|
|
172
|
+
- Add a string parser for constraints
|
|
173
|
+
- Versioned documentation
|
|
174
|
+
|
|
175
|
+
Contact
|
|
176
|
+
-------
|
|
177
|
+
- `Floris-Jan Willemsen <https://github.com/fjwillemsen>`_ <fjwillemsen97@gmail.com> (current maintainer)
|
|
178
|
+
- `Sébastien Celles <https://github.com/s-celles/>`_ <s.celles@gmail.com> (former maintainer)
|
|
179
|
+
- `Gustavo Niemeyer <https://github.com/niemeyer/>`_ <gustavo@niemeyer.net> (initial developer)
|
|
180
|
+
|
|
181
|
+
But it's probably better to `open an issue <https://github.com/python-constraint/python-constraint/issues>`_.
|
|
182
|
+
|
|
183
|
+
.. |License| image:: https://img.shields.io/pypi/l/python-constraint2
|
|
184
|
+
:alt: PyPI - License
|
|
185
|
+
|
|
186
|
+
.. |Build Status| image:: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml/badge.svg
|
|
187
|
+
:target: https://github.com/python-constraint/python-constraint/actions/workflows/build-test-python-package.yml
|
|
188
|
+
:alt: Build Status
|
|
189
|
+
|
|
190
|
+
.. |Docs| image:: https://img.shields.io/github/actions/workflow/status/python-constraint/python-constraint/publish-documentation.yml?label=Docs
|
|
191
|
+
:target: http://python-constraint.github.io/python-constraint/
|
|
192
|
+
:alt: Documentation Status
|
|
193
|
+
|
|
194
|
+
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/python-constraint2
|
|
195
|
+
:alt: PyPI - Python Versions
|
|
196
|
+
|
|
197
|
+
.. |Downloads| image:: https://img.shields.io/pypi/dm/python-constraint2
|
|
198
|
+
:alt: PyPI - Downloads
|
|
199
|
+
|
|
200
|
+
.. |Status| image:: https://img.shields.io/pypi/status/python-constraint2
|
|
201
|
+
:alt: PyPI - Status
|
|
202
|
+
|
|
203
|
+
.. |Code Coverage| image:: https://coveralls.io/repos/github/python-constraint/python-constraint/badge.svg
|
|
204
|
+
:target: https://coveralls.io/github/python-constraint/python-constraint
|
|
205
|
+
:alt: Code Coverage
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""File for obtaining top-level imports from submodules.
|
|
2
|
+
|
|
3
|
+
For example: constraint.problem.Problem can be imported as `from constraint import Problem`.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
# Copyright (c) 2005-2014 - Gustavo Niemeyer <gustavo@niemeyer.net>
|
|
7
|
+
#
|
|
8
|
+
# All rights reserved.
|
|
9
|
+
#
|
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
|
12
|
+
#
|
|
13
|
+
# 1. Redistributions of source code must retain the above copyright notice, this
|
|
14
|
+
# list of conditions and the following disclaimer.
|
|
15
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
17
|
+
# and/or other materials provided with the distribution.
|
|
18
|
+
#
|
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
|
|
30
|
+
from constraint.problem import * # noqa: F403
|
|
31
|
+
from constraint.domain import * # noqa: F403
|
|
32
|
+
from constraint.constraints import * # noqa: F403
|
|
33
|
+
from constraint.solvers import * # noqa: F403
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
from tests import test_doctests
|
|
37
|
+
test_doctests()
|