oximo-core
Core modeling types for oximo: Model, Variable, Set, Constraint, Objective, Parameter, IndexedVar, Domain, and ModelKind.
Re-exports oximo-expr types (Expr, ExprArena, ExprId, ExprNode, ParamId, VarId) so downstream code does not need a separate oximo-expr import. End users typically depend on the umbrella oximo crate rather than this one directly.
Usage
[]
= "0.3"
Or via the umbrella crate (recommended for end users):
[]
= "0.3"
Quick example
use *;
let m = new;
// Scalar variables
variable!;
variable!;
// Constraints (incl. a two-sided range -> band_lo + band_hi)
constraint!;
constraint!;
constraint!;
// Objective
objective!;
println!; // LP
Modeling API
The modeling surface is a set of macros: variable!, constraint!, objective!,
sum!, set!, and param!. Each expands to the underlying typed model operations,
so there is no runtime cost and full compile-time type/borrow checking is preserved.
The older builder methods (
Model::var/indexed_var/constraint/minimize/maximize/param, freesum_over,add_constraints_over) are deprecated as of 0.3.0 and scheduled for removal in 0.4.0. Prefer the macros.
Model uses interior mutability (RefCell), so a macro can take &m, register
variables/constraints, and the variable!-introduced bindings (x, y, ...) are
locals you can use immediately.
let m = new;
variable!; // binds a local `x: Expr<'_>`
constraint!; // uses x while holding &m
Names are unique per registry. Registering a duplicate variable or constraint name panics.
Accessors
m.num_variables // usize
m.num_constraints // usize
m.variables // Ref<'_, Vec<Variable>>
m.constraints // Ref<'_, Vec<Constraint>>
m.arena // Ref<'_, ExprArena>
m.kind // ModelKind, cached, invalidated on change
m.try_objective // Result<Objective, Error>
m.variable_id // Option<VarId>
m.constraint_id // Option<ConstraintId>
Fixing and unfixing variables
m.fix_var; // lb = ub = 3.0
m.unfix_var; // restore bounds
Variables
Scalar variables
variable!; // free (-inf, +inf)
variable!; // lower bound only
variable!; // both bounds
variable!; // binary {0, 1} (also Binary)
variable!; // general integer (also Integer)
variable!; // semicontinuous: 0 or in [2, 10]
variable!; // semi-integer: 0 or integer in [1, 5]
// Keyword args:
variable!; // same as `0.0 <= u <= 10.0`
variable!; // keyword domain (or a positional `Int`)
variable!; // warm start (scalar only)
variable!; // fixed to 5.0 (scalar only)
Indexed variables
Creates one scalar variable per key in a Set (or range), named base[key],
and binds an IndexedVar.
let i = range;
variable!; // uniform bounds
variable!; // integer family
variable!; // multi-index (Cartesian product)
// Access by key (panics on missing key):
let expr = x; // single key (usize / "name" / (a, b))
let e2 = z; // inside the macros: multi-index sugar == z[(&a, &b)]
// Bounds may reference the index -> lowered to per-key bounds:
variable!;
// Filtered family: keep only matching keys (no trivial elements built).
variable!;
Domain
| Variant | Description |
|---|---|
Domain::Real |
Any real number (default) |
Domain::Integer |
Any integer |
Domain::Binary |
0 or 1 |
Domain::SemiContinuous { threshold } |
0 or any value >= threshold |
Domain::SemiInteger { threshold } |
0 or any integer >= threshold |
Sets
Set is an ordered finite index set. Three variants:
let i = range; // Range: i64 keys 0..5
let j = strings; // Strings
let k = product; // Tuples: (0,"a"), (0,"b"), ...
let k = &i * &j; // Same via Mul operator
// From sparse ints:
let s = from_ints;
// Filter:
let evens = i.filter;
Constraints
==, <=, and >= are written directly, the macro intercepts the tokens, so
these are real constraint operators.
constraint!; // named, also >= and ==
constraint!; // anonymous (auto-named _c0, _c1, ...)
constraint!; // two-sided range -> band_lo + band_hi
constraint!; // computed run-time name
Indexed family over a set
// One constraint per key, auto-named supply[seattle], ...
constraint!;
// Multi-index family (multi-index access sugar: x[i, j]).
constraint!;
// Filtered family: only keys passing the guard.
constraint!;
Summation
sum!(body for k in domain) reads as sum_{k in domain} body. Nest with extra
clauses and filter with a trailing if:
constraint!;
objective!;
let evens = sum!; // filtered
Objectives
objective!;
objective!;
Parameters
param!; // binds a re-bindable `rate: Expr<'_>`
rate.set_param_value; // change between solves without rebuilding
Model kind
Inferred automatically from variables and expressions, cached and invalidated on change:
| Kind | Conditions |
|---|---|
LP |
All continuous, all linear |
MILP |
Any integer/binary, all linear |
QP |
All continuous, Mul with >=2 non-const children |
MIQP |
Any integer/binary + quadratic |
NLP |
All continuous, Pow/Sin/Cos/Exp/Log/Abs |
MINLP |
Any integer/binary + nonlinear |
License
MIT OR Apache-2.0