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 (via runtime API)
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
m.new; // x + y <= z
m.new; // y - x >= 0
m.new; // x * y == 12
m.new; // z / y != 0
// Linear constraints (weighted sums)
m.int_lin_eq; // 2x + 3y == 10
m.int_lin_le; // x - y <= 5
m.int_lin_ne; // 2x + y != 8
m.float_lin_eq; // 1.5x + 2.0y == 7.5
m.float_lin_le; // 0.5x + y <= 3.0
m.bool_lin_eq; // a + b + c == 2
// Reified constraints (with boolean result)
m.int_eq_reif; // b ↔ (x == y)
m.int_lt_reif; // b ↔ (x < y)
m.int_le_reif; // b ↔ (x <= y)
m.float_eq_reif; // b ↔ (x == y) for floats
m.int_lin_eq_reif; // b ↔ (2x + y == 5)
// Type conversion constraints
m.int2float; // float_var = int_var (as float)
m.float2int_floor; // int_var = floor(float_var)
m.float2int_ceil; // int_var = ceil(float_var)
m.float2int_round; // int_var = round(float_var)
// Array operations
m.array_int_element; // result = array[index]
m.array_int_minimum?; // minimum of array
m.array_int_maximum?; // maximum of array
m.array_float_element; // result = array[index] (floats)
m.array_float_minimum?; // minimum of array (floats)
m.array_float_maximum?; // maximum of array (floats)
FlatZinc/MiniZinc Support
For FlatZinc .fzn file support, use the separate Zelen crate.
Installation
Add this to your Cargo.toml:
[]
= "0.11"
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
Core Problems
Constraint Types
Advanced Features
Real Applications
Basic Usage
use *;
Programmatic API Example
For developers who prefer a more explicit, programmatic approach, the same constraints can be built using the runtime API:
use *;
License
This project is licensed under the MIT License - see the LICENSE file for details.