#cp #constraint #programming

cp_sat

Rust bindings to the Google CP-SAT constraint programming solver

10 releases

0.3.3 Sep 29, 2021
0.3.2 Sep 16, 2021
0.2.1 Sep 13, 2021
0.1.3 Sep 10, 2021

#1175 in Math

Download history 38/week @ 2024-03-15 6/week @ 2024-03-22 39/week @ 2024-03-29 13/week @ 2024-04-05 17/week @ 2024-04-12 4/week @ 2024-04-19 4/week @ 2024-05-03 9/week @ 2024-05-10 12/week @ 2024-05-17 12/week @ 2024-05-24 5/week @ 2024-05-31 39/week @ 2024-06-07 15/week @ 2024-06-14 39/week @ 2024-06-21 1/week @ 2024-06-28

95 downloads per month

Apache-2.0

48KB
616 lines

Google CP-SAT solver Rust bindings

Rust bindings to the Google CP-SAT constraint programming solver.

To use this library, you need a C++ compiler and an installation of google or-tools library files.

The environment variable ORTOOLS_PREFIX is used to find include files and library files. If not setted, /opt/ortools will be added to the search path (classical search path will also be used).


lib.rs:

The cp_sat crate provides an interface to Google CP SAT.

OR-Tools installation

For cp_sat to work, you need to have a working OR-Tools installation. By default, this crate will use the default C++ compiler, and add /opt/ortools in the search path. If you want to provide your OR-Tools installation directory, you can define the ORTOOL_PREFIX environment variable.

Brief overview

The builder::CpModelBuilder provides an easy interface to construct your problem. You can then solve and access to the solver response easily. Here you can find the translation of the first tutorial in the official documentation of CP SAT:

use cp_sat::builder::CpModelBuilder;
use cp_sat::proto::CpSolverStatus;

fn main() {
    let mut model = CpModelBuilder::default();

    let x = model.new_int_var_with_name([(0, 2)], "x");
    let y = model.new_int_var_with_name([(0, 2)], "y");
    let z = model.new_int_var_with_name([(0, 2)], "z");

    model.add_ne(x, y);

    let response = model.solve();
    println!(
        "{}",
        cp_sat::ffi::cp_solver_response_stats(&response, false)
    );

    if response.status() == CpSolverStatus::Optimal {
        println!("x = {}", x.solution_value(&response));
        println!("y = {}", y.solution_value(&response));
        println!("z = {}", z.solution_value(&response));
    }
}

Dependencies

~1.9–4MB
~72K SLoC