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() - supports array and variadic syntax
  • Global constraints: alldiff(), allequal(), element()

§post() - Post a mathematical constraint to the model

Supported constraint patterns: Basic comparisons: var op var, var op literal, var op (expr), var op int(value), var op float(value) Arithmetic: var op var +/- var, var op var */÷ var, var op var % divisor Functions: func(var) op target where func is abs, min, max, sum Boolean: and(vars...), or(vars...), not(var) - supports arrays and([a,b,c]) and variadic and(a,b,c,d) Global: alldiff([vars...]), allequal([vars...]), element(array, index, value) Multiplication with constants: target op var * int(value), target op var * float(value)

Where op is any of: ==, !=, <, <=, >, >=

§postall() - Post multiple constraints to the model in a single call

Accepts comma-separated constraint expressions, each following the same patterns as post!: Basic comparisons: var op var, var op literal, var op (expr), var op int(value), var op float(value) Arithmetic: var op var +/- var, var op var */÷ var, var op var % divisor Functions: func(var) op target where func is abs, min, max, sum Boolean: and(vars...), or(vars...), not(var) - supports arrays and([a,b,c]) and variadic and(a,b,c,d) Global: alldiff([vars...]), allequal([vars...]), element(array, index, value) Multiplication with constants: target op var * int(value), target op var * float(value)

Where op is any of: ==, !=, <, <=, >, >=

§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 Ok(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 Ok(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 Ok(solution) = m.solve() {
    println!("Red: {:?}, Blue: {:?}, Green: {:?}",
             solution[red], solution[blue], solution[green]);
}

Modules§

boolean_operators
Operator overloads for VarId to provide clean syntax.
config
Configuration system for the CSP solver
error
Error handling for the CSP solver.
model
prelude
Prelude module for CSP solver
solution
Solution representation and solving statistics.
validation
Model validation and constraint analysis.
vars
Variable domains and value types.