CSP Solver
A Constraint Satisfaction Problem (CSP) solver library written in Rust.
Overview
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.
Variable Types: int, float, mixed constraints
Constraint Categories:
- Mathematical:
+,-,*,/,%,abs(),min(),max(),sum() - Comparison:
==,!=,<,<=,>,>=(natural syntax) - Boolean Logic:
and(),or(),not()with array syntaxand([a,b,c])and variadic syntaxand(a,b,c,d) - Global:
alldiff(),allequal(), elementx[y] = z,count(vars, value, count),table(vars, tuples) - Ordering:
a <= b <= c,a < b < c,a >= b >= c,a > b > c(naturalbetweenconstraints) - Cardinality:
at_least(vars, value, count),at_most(vars, value, count),exactly(vars, value, count) - Conditional:
if_then(condition, constraint),if_then_else(condition, then_constraint, else_constraint)
Installation
Add this to your Cargo.toml:
[]
= "0.5.17"
Examples
๐งฉ Solving PLATINUM puzzle:
๐ Puzzle stats: 17 clues given, 64 empty cells
Puzzle: Solution:
โโโโโโโโโฌโโโโโโโโฌโโโโโโโโ โโโโโโโโโฌโโโโโโโโฌโโโโโโโโ
โ ยท ยท ยท โ ยท ยท ยท โ ยท ยท ยท โ โ 9 8 7 โ 6 5 4 โ 3 2 1 โ
โ ยท ยท ยท โ ยท ยท 3 โ ยท 8 5 โ โ 2 4 6 โ 1 7 3 โ 9 8 5 โ
โ ยท ยท 1 โ ยท 2 ยท โ ยท ยท ยท โ โ 3 5 1 โ 9 2 8 โ 7 4 6 โ
โโโโโโโโโผโโโโโโโโผโโโโโโโโค โโโโโโโโโผโโโโโโโโผโโโโโโโโค
โ ยท ยท ยท โ 5 ยท 7 โ ยท ยท ยท โ โ 1 2 8 โ 5 3 7 โ 6 9 4 โ
โ ยท ยท 4 โ ยท ยท ยท โ 1 ยท ยท โ โ 6 3 4 โ 8 9 2 โ 1 5 7 โ
โ ยท 9 ยท โ ยท ยท ยท โ ยท ยท ยท โ โ 7 9 5 โ 4 6 1 โ 8 3 2 โ
โโโโโโโโโผโโโโโโโโผโโโโโโโโค โโโโโโโโโผโโโโโโโโผโโโโโโโโค
โ 5 ยท ยท โ ยท ยท ยท โ ยท 7 3 โ โ 5 1 9 โ 2 8 6 โ 4 7 3 โ
โ ยท ยท 2 โ ยท 1 ยท โ ยท ยท ยท โ โ 4 7 2 โ 3 1 9 โ 5 6 8 โ
โ ยท ยท ยท โ ยท 4 ยท โ ยท ยท 9 โ โ 8 6 3 โ 7 4 5 โ 2 1 9 โ
โโโโโโโโโดโโโโโโโโดโโโโโโโโ โโโโโโโโโดโโโโโโโโดโโโโโโโโ
๐ Statistics: 638 propagations, 54 nodes explored
๐ Efficiency: 11.8 propagations/node
Basic Usage
use *;
Advanced Constraint Examples
use *;
New Constraint Types (v0.5.11+)
The latest version includes powerful new constraint types for advanced modeling:
Between Constraints
Enforce ternary ordering relationships with natural chained comparison syntax:
let start = m.int;
let middle = m.int;
let end = m.int;
// Natural chained comparison syntax (recommended)
post!;
// Alternative function syntax
post!;
// Other chained comparisons
post!; // Strict inequalities
post!; // Reverse order
Cardinality Constraints
Precise counting control for resource allocation and capacity planning:
let workers = vec!; // 4 workers, 3 shifts
post!; // At least 2 on night shift (3)
post!; // At most 1 on day shift (1)
post!; // Exactly 1 on evening shift (2)
Conditional Constraints
Business logic and dependency modeling with if-then-else:
let weather = m.int; // 1=sunny, 2=cloudy, 3=rainy
let activity = m.int; // 1=hiking, 2=museum, 3=shopping
let backup = m.int; // Backup activity
// If sunny (1) then hiking (1), if rainy (3) then shopping (3)
post!;
post!;
These constraints work seamlessly with the existing post! macro system and provide both helper methods and natural syntax for maximum usability.
License
This project is licensed under the MIT License - see the LICENSE file for details.