Crate cspsolver

Crate cspsolver 

Source
Expand description

§CSP Solver

A constraint satisfaction problem (CSP) solver library.

§Variable Types

  • Integer variables: m.int(min, max) - continuous range
  • Float variables: m.float(min, max) - continuous range with precision control
  • Custom domains: m.ints(vec![values]) - specific integer values only
  • Boolean variables: m.bool() - equivalent to m.int(0, 1)

§Constraint Types

  • Arithmetic: +, -, *, /, %, abs(), min(), max(), sum()
  • Comparison: ==, !=, <, <=, >, >=
  • Boolean logic: and(), or(), not()
  • Global constraints: alldiff()

§Example 1: Basic Integer Problem

use cspsolver::prelude::*;

let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(1, 10);

post!(m, x + y == int(12));
post!(m, x > y);

if let Some(solution) = m.solve() {
    println!("x = {:?}, y = {:?}", solution[x], solution[y]);
}

§Example 2: Mixed Integer-Float Optimization

use cspsolver::prelude::*;

let mut m = Model::default();
let items = m.int(1, 100);        // Number of items
let cost = m.float(0.0, 1000.0);  // Total cost

post!(m, cost == items * float(12.5));  // $12.50 per item
post!(m, cost <= float(500.0));         // Budget constraint

// Maximize number of items within budget
if let Some(solution) = m.maximize(items) {
    println!("Optimal: {:?} items, cost: {:?}", 
             solution[items], solution[cost]);
}

§Example 3: Custom Domains and Global Constraints

use cspsolver::prelude::*;

let mut m = Model::default();
 
// Variables with custom domains
let red = m.ints(vec![1, 3, 5, 7]);      // Odd numbers
let blue = m.ints(vec![2, 4, 6, 8]);     // Even numbers  
let green = m.ints(vec![2, 3, 5, 7]);    // Prime numbers

// All must be different
post!(m, alldiff([red, blue, green]));

if let Some(solution) = m.solve() {
    println!("Red: {:?}, Blue: {:?}, Green: {:?}",
             solution[red], solution[blue], solution[green]);
}

Modules§

benchmarks
constraint_macros
Mathematical constraint posting macros
math_syntax
Mathematical constraint syntax with explicit typing
model
operators
Operator traits and implementations for convenient constraint syntax.
prelude
Prelude module for CSP solver
solution
vars
view_constraints
View-based constraint builders for advanced constraint syntax.

Macros§

post
Post a mathematical constraint to the model
postall
Batch multiple constraint references into a vector
postall_helper
Helper macro to handle constraint expressions recursively