跳转到主要内容

python-constraint 是一个模块,用于支持在有限域上处理CSP(约束求解问题)。

项目描述

Build Status Code Health Code Coverage

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 (18.4 kB 查看哈希值)

上传时间 源代码

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面