Skip to main content

Model

Struct Model 

Source
pub struct Model {
    pub name: String,
    /* private fields */
}
Expand description

The optimization model. Owns the expression arena, variable/parameter registries, constraints, and (optional) objective.

Model uses interior mutability so the builder API can take &self references.

Variables, constraints, and the objective are added through RefCells under the hood.

Fields§

§name: String

Implementations§

Source§

impl Model

Source

pub fn new(name: impl Into<String>) -> Self

Source

pub fn var(&self, name: impl Into<SmolStr>) -> VarBuilder<'_>

Source

pub fn indexed_var<'a>( &'a self, name: impl Into<String>, set: &Set, ) -> IndexedVarBuilder<'a>

Source

pub fn variable_id(&self, name: &str) -> Option<VarId>

Source

pub fn variables(&self) -> Ref<'_, Vec<Variable>>

Source

pub fn arena(&self) -> Ref<'_, ExprArena>

Source

pub fn num_variables(&self) -> usize

Source

pub fn fix(&self, e: Expr<'_>, value: f64)

Fix a single-variable expression to value. Convenience over Self::fix_var for handles from Model::var or crate::IndexedVar indexing.

§Panics

Panics if e is not a bare variable handle.

Source

pub fn fix_var(&self, id: VarId, value: f64)

Fix variable id to value by setting lb = ub = value.

Source

pub fn unfix_var(&self, id: VarId, lb: f64, ub: f64)

Restore bounds on variable id. Pass f64::NEG_INFINITY / f64::INFINITY to restore an unbounded direction.

Source

pub fn param<'a>(&'a self, name: impl Into<SmolStr>, value: f64) -> Expr<'a>

Register a named scalar parameter initialized to value, returning an Expr handle that references it symbolically.

A parameter behaves like a constant coefficient (param * var is linear), but stays symbolic in the expression tree so it can be re-bound with Self::set_param / Self::set_param_id between solves without rebuilding the model.

§Panics

Panics if a parameter with the same name is already registered.

Source

pub fn set_param(&self, p: Expr<'_>, value: f64)

Re-bind the parameter referenced by handle p to value.

§Panics

Panics if p is not a bare parameter handle (see Self::param).

Source

pub fn set_param_id(&self, id: ParamId, value: f64)

Re-bind parameter id to value. Takes effect on the next solve.

The value is stored only in the expression arena (its single source of truth); extraction and evaluation read it from there.

Source

pub fn param_value(&self, id: ParamId) -> f64

Current value bound to parameter id.

§Panics

Panics if id was not produced by Self::param on this model.

Source

pub fn param_value_of(&self, p: Expr<'_>) -> Option<f64>

Current value of the parameter referenced by handle p, or None if p is not a bare parameter handle.

Source

pub fn parameter_id(&self, name: &str) -> Option<ParamId>

Source

pub fn parameters(&self) -> Ref<'_, Vec<Parameter>>

Source

pub fn num_parameters(&self) -> usize

Source

pub fn constraint( &self, name: impl Into<SmolStr>, c: ConstraintExpr<'_>, ) -> ConstraintId

Register a new constraint.

§Panics

Panics if a constraint with the same name is already registered, or if the constraint count exceeds u32::MAX.

Source

pub fn add_constraints<'a, I>(&'a self, items: I)
where I: IntoIterator<Item = (SmolStr, ConstraintExpr<'a>)>,

Bulk-register constraints. Each entry is (name, ConstraintExpr). Useful with .par_iter().map(...).collect() style construction.

Source

pub fn add_constraints_over<'a, K, F>( &'a self, name_prefix: &str, set: &Set, rule: F, )
where K: FromIndexKey, F: FnMut(K) -> ConstraintExpr<'a>,

Rule-style bulk constraint registration.

The closure receives the index as a typed value K. Any type implementing FromIndexKey is accepted. Built-in impls cover i64, i32, usize, String, raw IndexKey, and tuples up to arity 4. The user states the expected shape via the closure-arg annotation.

§Example
// Scalar set: closure receives a usize directly.
m.add_constraints_over("upper", &i, |i: usize| x[i].le(b[i]));

// Tuple set: destructure inline.
m.add_constraints_over("blo", &(&tasks * &events), |(t, n): (usize, usize)| {
    (b[(t, n)] - b_min[t] * w[(t, n)]).ge(0.0)
});
Source

pub fn constraints(&self) -> Ref<'_, Vec<Constraint>>

Source

pub fn num_constraints(&self) -> usize

Source

pub fn constraint_id(&self, name: &str) -> Option<ConstraintId>

Source

pub fn minimize(&self, expr: Expr<'_>)

Source

pub fn maximize(&self, expr: Expr<'_>)

Source

pub fn objective(&self) -> Ref<'_, Option<Objective>>

Source

pub fn try_objective(&self) -> Result<Objective>

Try to get a cloned copy of the objective.

§Errors

Returns Error::NoObjective if no objective is set on this model.

Source

pub fn kind(&self) -> ModelKind

Infer the ModelKind from current variables and expressions. Result is cached and invalidated whenever variables, constraints, or the objective change.

Trait Implementations§

Source§

impl Debug for Model

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Model

§

impl !RefUnwindSafe for Model

§

impl !Sync for Model

§

impl Send for Model

§

impl Unpin for Model

§

impl UnsafeUnpin for Model

§

impl UnwindSafe for Model

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.