python-constraint 是一个模块,用于支持在有限域上处理CSP(约束求解问题)。
项目描述
python-constraint
简介
Python约束模块为在有限域上提供约束满足问题(CSPs)的求解器,采用简单和纯Python编写。
示例
基本操作
此交互式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}]
棋子问题
以下示例解决了经典的八皇后问题
>>> 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},
...]
幻方
此示例解决了4x4幻方问题
>>> 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()
功能
以下是一些可用的求解器:
回溯求解器
递归回溯求解器
最小冲突求解器
当前可用的预定义约束类型
函数约束
全不同约束
全相等约束
精确求和约束
最大求和约束
最小求和约束
在集合约束
不在集合约束
某些在集合约束
某些不在集合约束
API 文档
模块文档可在以下地址获取: http://labix.org/doc/constraint/
下载和安装
$ pip install python-constraint
路线图
这个 GitHub 组织和仓库是全球合作项目,旨在维护由 Gustavo Niemeyer 编写的 python-constraint,最初位于 https://labix.org/python-constraint
创建一些单元测试 - 已完成
启用持续集成 - 已完成
移植到 Python 3(同时支持 Python 2) - 已完成
遵守 Python 代码风格指南(PEP8) - 已完成
通过编写更多单元测试来提高代码覆盖率 - 待办
将文档移至 Sphinx 或 MkDocs - https://readthedocs.org/ - 待办
联系
但最好还是 打开一个 issue。
项目详情
关闭
python-constraint-1.4.0.tar.bz2 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 501d6f17afe0032dfc6ea6c0f8acc12e44f992733f00e8538961031ef27ccb8e |
|
MD5 | 53e4d375d8c84b383d9debf5e517d21b |
|
BLAKE2b-256 | 378b5f1bc2734ca611943e1d6733ee244238679f6410a10cd45ede55a61a8402 |