AC-3rm
A highly efficient incremental Arc Consistency (AC-3rm) propagator written in Rust.
AC-3rm maintains constraint consistency dynamically, supporting:
- Dynamic constraint insertion with incremental propagation
- Dynamic constraint retraction with neighborhood re-propagation
- AC-3rm algorithm with residual supports for optimal constraint checking
- Batch operations for efficient multi-constraint updates
- Listener callbacks for reactive domain change notifications
Constraint Types
- Binary equality:
var_a == var_b - Binary inequality:
var_a != var_b - Unary set:
var == value(domain becomes singleton) - Unary forbid:
var != value(value removed from domain)
Key Features
AC-3rm with Residual Supports
The engine implements the AC-3rm algorithm, which extends AC-3 with residual supports:
- Each value maintains a cached support to avoid redundant domain scans
- When a domain changes, only affected arcs are re-queued
- Incoming arcs are prioritized during propagation for efficiency
Incremental Architecture
- Constraints can be added and removed dynamically
- Retracting a constraint only re-propagates the affected neighborhood
- Multi-killer support: values can be suppressed by multiple constraints simultaneously
Batch Operations
For better performance when applying multiple constraints:
assert_batch(&[id1, id2, ...])— Assert multiple constraints with a single propagation passretract_batch(&[id1, id2, ...])— Retract multiple constraints with a single re-propagation pass
Reactive Updates
Register callbacks to monitor domain changes in real-time:
engine.set_listener;
Installation
Add to Cargo.toml:
[]
= "0.1"
Usage
Basic Constraint Propagation
use Engine;
Dynamic Constraint Retraction
use Engine;
Batch Operations
use ;
Listener Callbacks
use Engine;
use ;
Error Handling
use PropagationError;
match engine.new_eq
Performance Characteristics
- Assertion: O(d·k) in worst case, where d is domain size, k is arity (≤2 for this engine)
- Retraction: O(d·k) incremental re-propagation of affected neighborhood
- Residual supports: Amortized O(1) support lookup in typical scenarios
Testing
All functionality is thoroughly tested:
Tests cover:
- Unary and binary constraint interactions
- Incremental retraction and re-assertion
- Multi-killer scenarios (multiple constraints suppressing the same value)
- Batch operation equivalence with sequential calls
- Listener notification during propagation
- Complex propagation chains
Architecture
The engine manages:
- Variables: Each has a domain of active/suppressed values
- Constraints: Can be active (enforced) or inactive
- Residues: Cached support values for AC-3rm optimization
- Listeners: Callbacks invoked when domains change
The propagation queue processes arcs (directed constraint applications) until quiescence, ensuring arc-consistency is maintained after each update.