Crates.io | cspsolver |
lib.rs | cspsolver |
version | 0.3.2 |
created_at | 2025-08-29 16:09:30.257889+00 |
updated_at | 2025-08-29 16:20:55.423234+00 |
description | CSP Solver |
homepage | |
repository | https://github.com/radevgit/cspsolver |
max_upload_size | |
id | 1816084 |
size | 20,123 |
A high-performance Constraint Satisfaction Problem (CSP) solver library written in Rust.
We are starting the implementation from scratch.
This library provides efficient algorithms and data structures for solving constraint satisfaction problems. CSPs are mathematical problems defined as a set of objects whose state must satisfy a number of constraints or limitations.
Add this to your Cargo.toml
:
[dependencies]
csp = "0.0.1"
use csp::*;
fn main() -> CspResult<()> {
// Create a simple CSP
let mut solver = CspSolver::new();
// Add variables with domains
let var1 = solver.add_variable("x", vec![1, 2, 3])?;
let var2 = solver.add_variable("y", vec![1, 2, 3])?;
// Add constraints
solver.add_constraint(NotEqualConstraint::new(var1, var2))?;
// Solve
if let Some(solution) = solver.solve()? {
println!("Solution found: {:?}", solution);
} else {
println!("No solution exists");
}
Ok(())
}
use csp::*;
fn solve_n_queens(n: usize) -> CspResult<Option<Solution>> {
let mut solver = CspSolver::new();
// Create variables for each queen (column position in each row)
let mut queens = Vec::new();
for i in 0..n {
let var = solver.add_variable(
&format!("queen_{}", i),
(1..=n).collect()
)?;
queens.push(var);
}
// Add constraints: no two queens can attack each other
for i in 0..n {
for j in i + 1..n {
// Different columns
solver.add_constraint(NotEqualConstraint::new(queens[i], queens[j]))?;
// Different diagonals
solver.add_constraint(DiagonalConstraint::new(queens[i], queens[j], i, j))?;
}
}
solver.solve()
}
use csp::*;
fn solve_graph_coloring(graph: &Graph, colors: usize) -> CspResult<Option<Solution>> {
let mut solver = CspSolver::new();
// Create variable for each node
let mut nodes = Vec::new();
for node in graph.nodes() {
let var = solver.add_variable(
&format!("node_{}", node.id()),
(1..=colors).collect()
)?;
nodes.push(var);
}
// Add constraints: adjacent nodes must have different colors
for edge in graph.edges() {
solver.add_constraint(NotEqualConstraint::new(
nodes[edge.from()],
nodes[edge.to()]
))?;
}
solver.solve()
}
The library is organized into several key modules:
solver
: Main CSP solver implementationvariable
: Variable representation and managementdomain
: Domain operations and constraintsconstraints
: Various constraint types and implementationspropagation
: Constraint propagation algorithmssearch
: Search strategies and heuristicsThe library is designed for high performance:
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
cargo test
cargo bench
This project follows standard Rust conventions:
rustfmt
for formattingclippy
for lintingThis project is licensed under the MIT License - see the LICENSE file for details.
The library is currently in active development. Features and APIs may change as we refine the implementation and add new functionality.