Crates.io | rust-constraint |
lib.rs | rust-constraint |
version | 0.1.0-alpha.1 |
source | src |
created_at | 2024-07-02 11:17:35.37413 |
updated_at | 2024-07-02 11:17:35.37413 |
description | Rust implementation of a constraint satisfaction problem (CSPs) |
homepage | |
repository | https://github.com/PawelJastrzebski/rust-constraint |
max_upload_size | |
id | 1289506 |
size | 11,550 |
This crate is ideal for solving scheduling problems, graph coloring, and other CSPs where variables need to be assigned values under specific constraints.
Useful for educational purposes, AI projects, and any application requiring formal logical reasoning.
In this context, the variables A, B, C, etc., represent different classes that need to be scheduled on specific days of the week (Monday, Tuesday, Wednesday) while ensuring that certain classes do not occur on the same day.
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}")
}
Output
{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}
Source: CS50
Inspired by: python-constraint used in CS50’s Introduction to Artificial Intelligence with Python.