Skip to main content

Crate choco_solver

Crate choco_solver 

Source
Expand description

Safe Rust wrapper around the Choco Solver API.

This crate provides an idiomatic Rust interface to model and solve constraint satisfaction/optimization problems while delegating solving to Choco through the native libchoco_capi library.

For complete solver semantics and advanced API details, refer to the official Choco documentation: https://choco-solver.org/.

§Getting libchoco_capi.dll

You can obtain the native DLL in two main ways:

  • Build/fetch it from the upstream project: choco-solver-capi
  • Clone this repository and follow BUILDING (for example, cargo xtask build-dll)

§Where to put the DLL

libchoco_capi.dll must be discoverable by the dynamic loader. Typical options:

  • Place it in a folder already present in your system DLL search path.
  • Set CHOCO_SOLVER_DLL_FOLDER to the DLL directory.
  • Call ChocoBackend::set_dll_folder before creating/using models.

§Scope of this crate

This crate is a safe wrapper on top of the Choco solver C API (choco-solver-sys is the unsafe FFI boundary). The high-level API is Rust-oriented, but solver behavior is defined by Choco itself.

§Warning: Avoid mixing variables from different models

Do not mix variables from different models. The wrapper currently does not prevent cross-model variable mixing in constraints and relies on backend behavior.

§Thread safety

This library creates one separate GraalVM isolate (independent execution environment) per process. Currently the all types are not Send and Sync until it is clarified the thread safety of Choco Solver API and GraalVM native C API. However, it is possible to create and use models from multiple threads as long as they are not shared across threads (i.e., each thread creates and uses its own models and variables). The library ensures that the GraalVM isolate is initialized and attached to each thread that interacts with the Choco backend, allowing for concurrent usage from multiple threads without sharing state between them.

§Example

use choco_solver::*;

fn main() {
    // Optional: point to directory containing `libchoco_capi.dll`.
    // ChocoBackend::set_dll_folder("C:/path/to/dll/folder".to_string());

    let model = Model::new(Some("DemoModel"));
    let x = model.int_var_bounded(0, 200, Some("x"), None);
    let y = model.int_var_bounded(0, 200, Some("y"), None);
    let sum_is_156 = (&x + &y).eq(156).reify();

    true.eq(&sum_is_156).post().expect("failed to post constraint");

    let solver = model.solver();
    let solution = solver
        .find_solution(&Criterion::default())
        .expect("failed to find solution");

    let bx = solution.get_int_var(&x).expect("x not available");
    let by = solution.get_int_var(&y).expect("y not available");
    let bsum = solution
        .get_bool_var(&sum_is_156)
        .expect("reified bool not available");

    println!("solution: x = {bx}, y = {by}, x + y = {}, reified = {bsum}", bx + by);
}

Structs§

BoolVar
A boolean decision variable (domain 0/1) belonging to a Model.
ChocoBackend
Constraint
A constraint attached to a Model.
Criterion
Search limits used when finding solutions (e.g., node, fail, restart, backtrack).
IntVar
An integer decision variable belonging to a Model.
Model
A constraint programming model that owns variables and constraints.
Solution
A solver result holding instantiated variable values.
Solver
A solver instance associated with a Model.

Enums§

ArithmeticOperator
Arithmetic operators used in constraints (addition, subtraction, multiplication, division).
ConstraintStatus
The operational status of a constraint.
ESat
Represents the satisfaction state of a constraint.
EqualityOperator
Comparison operators for arithmetic constraints.
SolverError
Errors that can occur during constraint solving.

Traits§

ArithmConstraint
Trait for creating modulo/remainder constraints.
ArrayEqualityConstraints
Trait for creating global constraints over arrays of variables.
BoolVarArrayLogicOps
Logical operations on slices of Constraints/BoolVars
ConstraintArrayLogicOps
Logical operations on slices of Constraints/BoolVars
ConstraintEquality
Trait for creating equality constraints between variables or constants.