Support for handling constraint-solving problems over finite domains
Project description
compwa-python-constraint
[!NOTE] This is a packaging fork of python-constraint 1.4.0, maintained by the ComPWA project. It is published as compwa-python-constraint, but preserves the public
constraintimport package and the behavior of the upstream 1.4.0 solver.
Why this fork exists
The upstream 1.4.0 release on
PyPI provides only a
.tar.bz2 source distribution.
PEP 625 standardized source distributions
as gzip-compressed .tar.gz archives. In version 0.12.0,
uv stopped accepting legacy source-distribution formats,
including .tar.bz2, and recommends rebuilding affected packages as .tar.gz
archives.
This fork makes that packaging-only update so projects can continue using the
1.4.0 implementation with current Python packaging tools. Its declarative
pyproject.toml build produces:
- a pure-Python
py3-none-anywheel, which installers can use without a local build step; and - a standards-compliant
.tar.gzsource distribution as a fallback.
The distribution is versioned as 1.4.0.post1 to identify it as a downstream
packaging revision. Solver changes continue to belong in the
upstream project.
Introduction
The Python constraint module offers solvers for Constraint Satisfaction Problems (CSPs) over finite domains in simple and pure Python. A CSP can be represented in terms of variables, their possible domains, and constraints between them.
Examples
Basics
>>> 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
>>> 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()
Magic squares
>>> problem = Problem()
>>> problem.addVariables(range(16), range(1, 17))
>>> problem.addConstraint(AllDifferentConstraint(), range(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 include:
FunctionConstraintAllDifferentConstraintAllEqualConstraintExactSumConstraintMaxSumConstraintMinSumConstraintInSetConstraintNotInSetConstraintSomeInSetConstraintSomeNotInSetConstraint
Installation
python -m pip install compwa-python-constraint
The distribution name is compwa-python-constraint, while the import remains
unchanged:
from constraint import Problem
Upstream project
python-constraint was written by Gustavo Niemeyer and is maintained at
python-constraint/python-constraint.
Please report solver bugs and propose behavioral changes
upstream.
For packaging issues specific to this fork, use the ComPWA issue tracker.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file compwa_python_constraint-1.4.0.post1.tar.gz.
File metadata
- Download URL: compwa_python_constraint-1.4.0.post1.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dea00720a05debce39f6e4f384ad04b6b9235c316d1b984a4ee543e8c6952f45
|
|
| MD5 |
93a196db85654a591a6135ddfbff314a
|
|
| BLAKE2b-256 |
3f0b773cd834c82d7822167983de32a070f9099c091980818e8327722a067eff
|
Provenance
The following attestation bundles were made for compwa_python_constraint-1.4.0.post1.tar.gz:
Publisher:
cd.yml on ComPWA/python-constraint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
compwa_python_constraint-1.4.0.post1.tar.gz -
Subject digest:
dea00720a05debce39f6e4f384ad04b6b9235c316d1b984a4ee543e8c6952f45 - Sigstore transparency entry: 2291320731
- Sigstore integration time:
-
Permalink:
ComPWA/python-constraint@02ae4cc5528de355226696ded777041719ffb1d1 -
Branch / Tag:
refs/tags/1.4.0.post1 - Owner: https://github.com/ComPWA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@02ae4cc5528de355226696ded777041719ffb1d1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file compwa_python_constraint-1.4.0.post1-py3-none-any.whl.
File metadata
- Download URL: compwa_python_constraint-1.4.0.post1-py3-none-any.whl
- Upload date:
- Size: 25.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adddd14897a0da511b7eb89be9a7081ac2ecfb41867a3d78e16f89a6eac2b5f9
|
|
| MD5 |
83da621f03d9ed8c9393c2c29c1c5f3c
|
|
| BLAKE2b-256 |
b8b2257f9852d18fdaccdced2ff741efb82bf9ac18aecdad75696d5910eec5f1
|
Provenance
The following attestation bundles were made for compwa_python_constraint-1.4.0.post1-py3-none-any.whl:
Publisher:
cd.yml on ComPWA/python-constraint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
compwa_python_constraint-1.4.0.post1-py3-none-any.whl -
Subject digest:
adddd14897a0da511b7eb89be9a7081ac2ecfb41867a3d78e16f89a6eac2b5f9 - Sigstore transparency entry: 2291320740
- Sigstore integration time:
-
Permalink:
ComPWA/python-constraint@02ae4cc5528de355226696ded777041719ffb1d1 -
Branch / Tag:
refs/tags/1.4.0.post1 - Owner: https://github.com/ComPWA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@02ae4cc5528de355226696ded777041719ffb1d1 -
Trigger Event:
release
-
Statement type: