Skip to main content

Crate minion_sys

Crate minion_sys 

Source
Expand description

This crate provides low level Rust bindings to the Minion constraint solver.

§Examples

Consider the following Minion problem:

MINION 3
**VARIABLES**
DISCRETE x #
{1..3}
DISCRETE y #
{2..4}
DISCRETE z #
{1..5}
**SEARCH**
PRINT[[x],[y],[z]]
VARORDER STATIC [x, y, z]
**CONSTRAINTS**
sumleq([x,y,z],4)
ineq(x, y, -1)
**EOF**

This can be solved in Rust like so:

use minion_sys::ast::*;
use minion_sys::run_minion;
use std::collections::HashMap;

// Collect solutions using a closure — no globals needed.
let mut all_solutions: Vec<HashMap<VarName,Constant>> = vec![];

let callback: Box<dyn FnMut(HashMap<VarName,Constant>) -> bool> =
    Box::new(|solutions| {
        all_solutions.push(solutions);
        true
    });

// Build and run the model.
let mut model = Model::new();
model
    .named_variables
    .add_var("x".to_owned(), VarDomain::Bound(1, 3));
model
    .named_variables
    .add_var("y".to_owned(), VarDomain::Bound(2, 4));
model
    .named_variables
    .add_var("z".to_owned(), VarDomain::Bound(1, 5));

let leq = Constraint::SumLeq(
    vec![
        Var::NameRef("x".to_owned()),
        Var::NameRef("y".to_owned()),
        Var::NameRef("z".to_owned()),
    ],
    Var::ConstantAsVar(4),
);

let geq = Constraint::SumGeq(
    vec![
        Var::NameRef("x".to_owned()),
        Var::NameRef("y".to_owned()),
        Var::NameRef("z".to_owned()),
    ],
    Var::ConstantAsVar(4),
);

let ineq = Constraint::Ineq(
    Var::NameRef("x".to_owned()),
    Var::NameRef("y".to_owned()),
    Constant::Integer(-1),
);

model.constraints.push(leq);
model.constraints.push(geq);
model.constraints.push(ineq);

let _solver_ctx = run_minion(model, callback).expect("Error occurred");

let solution_set_1 = &all_solutions[0];
let x1 = solution_set_1.get("x").unwrap();
let y1 = solution_set_1.get("y").unwrap();
let z1 = solution_set_1.get("z").unwrap();

assert_eq!(all_solutions.len(),1);
assert_eq!(*x1,Constant::Integer(1));
assert_eq!(*y1,Constant::Integer(2));
assert_eq!(*z1,Constant::Integer(1));

These bindings have no replacement for Minion’s PRINT and VARORDER statements — every variable added to the model (excluding auxiliary variables) is considered a search variable. Solutions are returned through the callback as a HashMap.

§Search options

Use run_minion_with_options to set a random seed, variable/value ordering heuristics, or propagation levels:

use minion_sys::{RunOptions, run_minion_with_options, VarOrder, ValOrder};
let opts = RunOptions {
    seed: Some(42),
    var_order: VarOrder::Wdeg,
    val_order: ValOrder::Random,
    ..Default::default()
};
let _ctx = run_minion_with_options(model, opts, callback).unwrap();

§Tuple tables

Extensional constraints like ast::Constraint::Str2Plus reference named tuple tables registered on the model:

use minion_sys::ast::*;
model.add_tuple_table("allowed".into(), vec![
    vec![Constant::Integer(0), Constant::Integer(1)],
    vec![Constant::Integer(1), Constant::Integer(0)],
]);
model.constraints.push(Constraint::Str2Plus(
    vec![Var::NameRef("x".into()), Var::NameRef("y".into())],
    Var::NameRef("allowed".into()),
));

§Mid-search mutation

run_minion_midsearch lets callbacks add variables or constraints during search via a MidSearchContext handle:

use minion_sys::{run_minion_midsearch, MidSearchContext};
use minion_sys::ast::*;
use std::collections::HashMap;
let _ctx = run_minion_midsearch(model, Box::new(|midctx, sol| {
    // add a fresh variable on the first solution callback
    if !sol.contains_key("y") {
        midctx.add_var("y", VarDomain::Discrete(0, 1)).unwrap();
    }
    true
})).unwrap();

Modules§

ast
Types used for representing Minion models in Rust.
error
Error types.
print
Functions to pretty print a model as a Minion file.

Structs§

MidSearchContext
Handle for mutating the current search from inside a solution callback.
Propagation
A propagation level together with minion’s _limit modifier.
RunOptions
Knobs for a single solve. Extend as needed — using a struct keeps the public call surface stable when we add more options.
SolverContext
Opaque handle to a Minion solver context.
TimeLimit
Time limit specification: a number of seconds and whether to measure CPU time (-cpulimit) or wall-clock time (-timelimit).
WorkStealStats
Aggregate work-stealing diagnostics, populated by run_minion_work_steal and run_minion_work_steal_with_options.

Enums§

PropLevel
Propagation strength, matching minion’s PropagationType.
ValOrder
Value-ordering heuristic (maps to Minion’s ValOrderEnum).
VarOrder
Variable-ordering heuristic (maps to Minion’s VarOrderEnum).

Functions§

run_minion
Run Minion on the given Model.
run_minion_midsearch
Run Minion on the given Model with a callback that can mutate the running search via a MidSearchContext handle.
run_minion_midsearch_with_options
Like run_minion_midsearch but with RunOptions.
run_minion_parallel
Run Minion as a portfolio search across num_threads worker threads.
run_minion_parallel_with_options
Like run_minion_parallel but with RunOptions (random seed, var/ val orderings, propagation levels). The seed (if set) is the controller base seed; per-thread seeds are derived as base XOR thread_index.
run_minion_with_options
Like run_minion but lets the caller pin solver-side knobs (e.g. the random seed) via RunOptions.
run_minion_work_steal
Run Minion with thread-based work-stealing: N workers cooperatively split the search tree. Worker 0 starts at the root; idle workers wait on a shared queue. Busy workers, on each search node, donate one stealable left-branch (encoded as a path-from-root) to the queue when other workers are idle. Idle workers fast-forward via worldPush + propagate replay and continue search from there.
run_minion_work_steal_with_options
Like run_minion_work_steal but with RunOptions.

Type Aliases§

Callback
The callback type used by run_minion.
MidSearchCallback
Richer callback that also receives a MidSearchContext handle so the caller can add variables or constraints from inside a solution callback. See run_minion_midsearch.
ParallelCallback
Callback type for parallel portfolio search.