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));§PRINT and VARORDER
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.
- Functions to pretty print a model as a Minion file.
Structs§
- MidSearch
Context - Handle for mutating the current search from inside a solution callback.
- Propagation
- A propagation level together with minion’s
_limitmodifier. - RunOptions
- Knobs for a single solve. Extend as needed — using a struct keeps the public call surface stable when we add more options.
- Solver
Context - Opaque handle to a Minion solver context.
- Time
Limit - Time limit specification: a number of seconds and whether to measure
CPU time (
-cpulimit) or wall-clock time (-timelimit). - Work
Steal Stats - Aggregate work-stealing diagnostics, populated by
run_minion_work_stealandrun_minion_work_steal_with_options.
Enums§
- Prop
Level - 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
MidSearchContexthandle. - run_
minion_ midsearch_ with_ options - Like
run_minion_midsearchbut withRunOptions. - run_
minion_ parallel - Run Minion as a portfolio search across
num_threadsworker threads. - run_
minion_ parallel_ with_ options - Like
run_minion_parallelbut withRunOptions(random seed, var/ val orderings, propagation levels). The seed (if set) is the controller base seed; per-thread seeds are derived asbase XOR thread_index. - run_
minion_ with_ options - Like
run_minionbut lets the caller pin solver-side knobs (e.g. the random seed) viaRunOptions. - 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_stealbut withRunOptions.
Type Aliases§
- Callback
- The callback type used by
run_minion. - MidSearch
Callback - Richer callback that also receives a
MidSearchContexthandle so the caller can add variables or constraints from inside a solution callback. Seerun_minion_midsearch. - Parallel
Callback - Callback type for parallel portfolio search.