#constraint #csp #combinatorial #solving #constraints

rust-constraint

Rust实现约束满足问题(CSPs)

1个不稳定版本

0.1.0-alpha.12024年7月2日

697算法

MIT 许可证

12KB
271

约束满足问题(CSPs)

此包非常适合解决调度问题、图着色和其他需要变量在特定约束下赋值的问题。

适用于教育目的、AI项目和需要形式逻辑推理的任何应用。

示例

在此上下文中,变量A、B、C等表示需要在特定星期几(周一、周二、周三)上课的不同班级,同时确保某些课程不在同一天。

use rust_constraint::prelude::*;

let mut problem: Problem<&str> = Problem::new().variables(
    vec!["A", "B", "C", "D", "E", "F", "G"],
    vec!["Monday", "Tuesday", "Wednesday"],
);

let constraints = vec![
    ("A", "B"),
    ("A", "C"),
    ("B", "C"),
    ("B", "D"),
    ("B", "E"),
    ("C", "E"),
    ("C", "F"),
    ("D", "E"),
    ("E", "F"),
    ("E", "G"),
    ("F", "G"),
];

for (x, y) in constraints {
    problem.add_constraint_binary(x, y, |x, y| x != y)
}

for solution in problem.get_solutions() {
    println!("{solution}")
}

输出

{B: Tuesday, F: Tuesday, G: Wednesday, E: Monday, D: Wednesday, A: Monday, C: Wednesday}
{B: Wednesday, F: Wednesday, G: Tuesday, E: Monday, D: Tuesday, A: Monday, C: Tuesday}
{B: Monday, F: Monday, G: Wednesday, E: Tuesday, D: Wednesday, A: Tuesday, C: Wednesday}
{B: Wednesday, F: Wednesday, G: Monday, E: Tuesday, D: Monday, A: Tuesday, C: Monday}
{B: Monday, F: Monday, G: Tuesday, E: Wednesday, D: Tuesday, A: Wednesday, C: Tuesday}
{B: Tuesday, F: Tuesday, G: Monday, E: Wednesday, D: Monday, A: Wednesday, C: Monday}

来源: CS50

致谢

灵感来源于:CS50的Python人工智能入门中使用python-constraint

无运行时依赖