backtrack 0.1.1

Solve hard constraints easily
Documentation

backtrack-rs 🦀

Documentation crates.io CI License

backtrack lets you define and solve backtracking problems succinctly.

Problems are defined by their scope and checks against possible solutions. A Scope determines length and allowed values in a solution. The Check or CheckInc trait determines whether a particular combination of values is satisfactory.

Usage

It is required that solutions shorter than in scope, i.e. partial solutions must satisfy if the completed solutions should as well. Solvers borrow a problem in search for candidate solutions.

Checks

We define the problem of counting down with a limited set of numbers and solve iteratively.

use backtrack::problem::{Check, Scope};
use backtrack::solvers::IterSolveNaive;
// helper trait to filter solutions of interest
use backtrack::solve::IterSolveExt;

/// Obtain permutations of some 3 descending numbers
struct CountDown {}

impl Scope for CountDown {
    fn size(&self) -> usize { 3 }
    fn domain(&self) -> Vec<usize> { (0..=3).collect() }
}

impl Check for CountDown{
    fn extends_sat(&self, solution: &[usize], x: usize) -> bool {
        solution.last().map_or(true, |last| *last > x)
    }
}

let solver = IterSolveNaive::new(&CountDown{});
let mut sats = solver.sat_iter();

assert_eq!(sats.next(), Some(vec![2, 1, 0]));
assert_eq!(sats.next(), Some(vec![3, 1, 0]));
assert_eq!(sats.next(), Some(vec![3, 2, 0]));
assert_eq!(sats.next(), Some(vec![3, 2, 1]));
assert_eq!(sats.next(), None);

Incremental Checks

If your checks can be formulated against a reduced solution, implement CheckInc instead.

The same result as above can be obtained by first "computing" the last item at each step. Such an approach makes more sense if actual work on more than one prior value needs to be peformed for any given sat check.

use backtrack::problem::{CheckInc, Scope};
// ...
impl CheckInc for CountDown{
    type Accumulator = usize;

    fn fold_acc(&self, accu: Option<Self::Accumulator>, x: &usize) -> Self::Accumulator {
        // only last value is of interest for checking
        *x
    }

    fn accu_sat(&self, accu: Option<&Self::Accumulator>, x: &usize, index: usize) -> bool {
       accu.map_or(true, |last| last > x)
    }
}
// since `CheckInc` impls `Check`, the same solver as before can be used
// todo: specialize solver to actually realize performance advantage
// ...

Examples

Checkout the examples folder for example problems.

# 4-queens solution
cargo run --example n_queens 4 | grep Sat
## n_queens.rs: NQueens { n: 4 }
## Sat([1, 3, 0, 2])
## Sat([2, 0, 3, 1])
# sequence of numbers which sum up to a minimum value but not more
cargo run --example total_sum | grep Sat

Benchmarks

backtrack-rs uses criterion for benches.

cargo bench

Todos

  • CheckInc solver
  • generic domain values
  • parallelize search