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: StringImplementations§
Source§impl Model
impl Model
pub fn new(name: impl Into<String>) -> Self
pub fn var(&self, name: impl Into<SmolStr>) -> VarBuilder<'_>
use the variable! macro, the builder API is scheduled for removal in 0.4.0
pub fn indexed_var<'a, K>( &'a self, name: impl Into<String>, set: &Set<K>, ) -> IndexedVarBuilder<'a, K>
use the variable! macro, the builder API is scheduled for removal in 0.4.0
pub fn variable_id(&self, name: &str) -> Option<VarId>
pub fn variables(&self) -> Ref<'_, Vec<Variable>>
pub fn arena(&self) -> Ref<'_, ExprArena>
pub fn num_variables(&self) -> usize
Sourcepub fn fix(&self, e: Expr<'_>, value: f64)
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.
Sourcepub fn fix_var(&self, id: VarId, value: f64)
pub fn fix_var(&self, id: VarId, value: f64)
Fix variable id to value by setting lb = ub = value.
Sourcepub fn set_initial(&self, e: Expr<'_>, value: f64)
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.
Sourcepub fn unfix_var(&self, id: VarId, lb: f64, ub: f64)
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.
Sourcepub fn param<'a>(&'a self, name: impl Into<SmolStr>, value: f64) -> Expr<'a>
👎Deprecated since 0.3.0: use the param! macro, the builder API is scheduled for removal in 0.4.0
pub fn param<'a>(&'a self, name: impl Into<SmolStr>, value: f64) -> Expr<'a>
use the param! macro, the builder API is scheduled for removal in 0.4.0
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.
Sourcepub fn set_param(&self, p: Expr<'_>, value: f64)
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).
Sourcepub fn set_param_id(&self, id: ParamId, value: f64)
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.
Sourcepub fn param_value(&self, id: ParamId) -> f64
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.
Sourcepub fn param_value_of(&self, p: Expr<'_>) -> Option<f64>
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.
pub fn parameter_id(&self, name: &str) -> Option<ParamId>
pub fn parameters(&self) -> Ref<'_, Vec<Parameter>>
pub fn num_parameters(&self) -> usize
Sourcepub fn constraint(
&self,
name: impl Into<SmolStr>,
c: ConstraintExpr<'_>,
) -> ConstraintId
👎Deprecated since 0.3.0: use the constraint! macro, the builder API is scheduled for removal in 0.4.0
pub fn constraint( &self, name: impl Into<SmolStr>, c: ConstraintExpr<'_>, ) -> ConstraintId
use the constraint! macro, the builder API is scheduled for removal in 0.4.0
Register a new constraint.
§Panics
Panics if a constraint with the same name is already registered, or if
the constraint count exceeds u32::MAX.
Sourcepub fn add_constraints<'a, I>(&'a self, items: I)
pub fn add_constraints<'a, I>(&'a self, items: I)
Bulk-register constraints. Each entry is (name, ConstraintExpr).
Useful with .par_iter().map(...).collect() style construction.
Sourcepub fn add_constraints_over<'a, K, F>(
&'a self,
name_prefix: &str,
set: &Set<K>,
rule: F,
)
👎Deprecated since 0.3.0: use the indexed-family form of the constraint! macro, the builder API is scheduled for removal in 0.4.0
pub fn add_constraints_over<'a, K, F>( &'a self, name_prefix: &str, set: &Set<K>, rule: F, )
use the indexed-family form of the constraint! macro, the builder API is scheduled for removal in 0.4.0
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)
});pub fn constraints(&self) -> Ref<'_, Vec<Constraint>>
pub fn num_constraints(&self) -> usize
pub fn constraint_id(&self, name: &str) -> Option<ConstraintId>
pub fn minimize(&self, expr: Expr<'_>)
use objective!(m, Min, ..), the builder API is scheduled for removal in 0.4.0
pub fn maximize(&self, expr: Expr<'_>)
use objective!(m, Max, ..), the builder API is scheduled for removal in 0.4.0
pub fn objective(&self) -> Ref<'_, Option<Objective>>
Sourcepub fn try_objective(&self) -> Result<Objective>
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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