Selen - CSP Solver
A Constraint Satisfaction Problem (CSP) solver library written in Rust with zero external dependencies.
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 API
// Comparison constraints
m.new; // x < y
m.new; // y <= z
m.new; // z > 5
m.new; // x == 10
m.new; // x != y
m.new; // x >= 5
// Arithmetic operations (return new variables)
let sum = m.add; // sum = x + y
let diff = m.sub; // diff = x - y
let product = m.mul; // product = x * y
let quotient = m.div; // quotient = x / y
let remainder = m.modulo; // remainder = x % y
let absolute = m.abs; // absolute = |x|
// Aggregate operations
let minimum = m.min?; // minimum of variables
let maximum = m.max?; // maximum of variables
let total = m.sum; // sum of variables
// Global constraints
m.alldiff; // all variables different
m.alleq; // all variables equal
m.element; // array[index] == value
m.table; // table constraint (valid tuples)
m.count; // count occurrences of value
m.between; // lower <= middle <= upper
m.at_least; // at least n vars == value
m.at_most; // at most n vars == value
m.exactly; // exactly n vars == value
m.gcc; // global cardinality constraint
// Boolean operations (return boolean variables)
let and_result = m.bool_and; // a AND b
let or_result = m.bool_or; // a OR b
let not_result = m.bool_not; // NOT a
m.bool_clause; // a ∨ b ∨ ¬c (CNF clause)
// Fluent expression building (runtime API)
m.new; // x + y <= z
m.new; // y - x >= 0
m.new; // x * y == 12
m.new; // z / y != 0
// Linear constraints (weighted sums) - generic for int and float
m.lin_eq; // 2x + 3y == 10
m.lin_le; // x - y <= 5
m.lin_ne; // 2x + y != 8
m.lin_eq; // 1.5x + 2.0y == 7.5 (works with floats)
m.lin_le; // 0.5x + y <= 3.0 (works with floats)
// Boolean linear constraints (for boolean variables)
m.bool_lin_eq; // a + b + c == 2
m.bool_lin_le; // a + b + c <= 2
m.bool_lin_ne; // a + b + c != 3
// Reified constraints (with boolean result) - generic for int and float
m.eq_reif; // b ↔ (x == y)
m.ne_reif; // b ↔ (x != y)
m.lt_reif; // b ↔ (x < y)
m.le_reif; // b ↔ (x <= y)
m.gt_reif; // b ↔ (x > y)
m.ge_reif; // b ↔ (x >= y)
m.lin_eq_reif; // b ↔ (2x + y == 5)
m.lin_le_reif; // b ↔ (2x + y <= 5)
m.lin_ne_reif; // b ↔ (2x + y != 5)
// Boolean linear reified constraints
m.bool_lin_eq_reif; // c ↔ (a + b == 2)
m.bool_lin_le_reif; // c ↔ (a + b <= 1)
m.bool_lin_ne_reif; // c ↔ (a + b != 0)
// Type conversion functions (from constraints::functions)
let float_result = to_float; // convert int to float
let floor_result = floor; // floor(float_var)
let ceil_result = ceil; // ceil(float_var)
let round_result = round; // round(float_var)
Optimization
The solver automatically uses LP (Linear Programming) techniques for linear constraints with optimization objectives:
let mut m = default;
let x = m.float;
let y = m.float;
// Linear constraints
m.lin_le; // x + y <= 100
m.lin_le; // 2x + y <= 150
// Maximize objective
let solution = m.maximize.expect;
println!;
FlatZinc/MiniZinc Support
For FlatZinc .fzn file support, use the separate Zelen crate.
Installation
Add this to your Cargo.toml:
[]
= "0.12"
Examples
🧩 Solving Platinum Blonde 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 │
└───────┴───────┴───────┘ └───────┴───────┴───────┘
✅ Solution found in 2289.885ms!
📊 Statistics: 538 propagations, 25 nodes explored
🔍 Efficiency: 21.5 propagations/node
Basic Usage
use *;