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 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 the variable! macro 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 set_initial(&self, e: Expr<'_>, value: f64)

Set the initial (warm-start) value of a single-variable expression. The macro API has no bound-style syntax for warm starts, so this is the supported way to seed variable!-declared variables.

§Panics

Panics if e is not a bare variable handle.

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 set_param_idx<K, Q: Into<IndexKey>>( &self, params: &IndexedParam<'_, K>, key: Q, value: f64, )

Re-bind the parameter at key of an indexed family to value. Takes effect on the next solve.

§Panics

Panics if key is not present in the family, or if params was built on a different Model.

Source

pub fn param_value_idx<K, Q: Into<IndexKey>>( &self, params: &IndexedParam<'_, K>, key: Q, ) -> Option<f64>

Current value bound to the parameter at key of an indexed family, or None if the key is absent.

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 (one returned by the param! macro).

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 does not belong to a parameter registered 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 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 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 add_soc_constraint<'a>( &'a self, name: impl Into<SmolStr>, terms: impl IntoIterator<Item = Expr<'a>>, bound: Expr<'a>, ) -> SocConstraintId

Register the explicit second-order cone constraint ||terms||_2 <= bound.

Every member of terms and the bound must be affine; the bound is additionally constrained to be nonnegative by the cone itself, so backends emit a bound >= 0 side condition where needed.

§Panics

Panics if a SOC constraint with the same name is already registered, if terms is empty, if any term or the bound is not affine, or if the count exceeds u32::MAX.

Source

pub fn soc_constraints(&self) -> Ref<'_, Vec<SocConstraint>>

Source

pub fn num_soc_constraints(&self) -> usize

Source

pub fn soc_constraint_id(&self, name: &str) -> Option<SocConstraintId>

Source

pub fn has_cones(&self) -> bool

Whether the model carries any explicit second-order cone constraints.

Source

pub fn is_feasibility(&self) -> bool

Whether feasibility was declared explicitly via objective!(m, Feasibility), as opposed to a model that simply has no objective set.

Source

pub fn ensure_objective_declared(&self) -> Result<()>

Ensure the model has a solve direction declared: either an objective (Min/Max) or an explicit feasibility problem.

§Errors

Returns Error::NoObjective if neither an objective nor objective!(m, Feasibility) was declared.

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.

The decision ladder, top-down (any integer variable picks the MI* column):

  1. any nonlinear expression (objective or constraint) -> NLP
  2. any quadratic constraint not recognized as SOC (see crate::detect_soc) -> QCP
  3. cones present (explicit or detected) -> SOCP
  4. quadratic objective -> QP
  5. otherwise -> LP

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.