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 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 the variable! macro
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 set_param_idx<K, Q: Into<IndexKey>>(
&self,
params: &IndexedParam<'_, K>,
key: Q,
value: f64,
)
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.
Sourcepub fn param_value_idx<K, Q: Into<IndexKey>>(
&self,
params: &IndexedParam<'_, K>,
key: Q,
) -> Option<f64>
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.
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 (one returned by the param!
macro).
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 does not belong to a parameter registered 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 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.
pub fn constraints(&self) -> Ref<'_, Vec<Constraint>>
pub fn num_constraints(&self) -> usize
pub fn constraint_id(&self, name: &str) -> Option<ConstraintId>
Sourcepub fn add_soc_constraint<'a>(
&'a self,
name: impl Into<SmolStr>,
terms: impl IntoIterator<Item = Expr<'a>>,
bound: Expr<'a>,
) -> SocConstraintId
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.
pub fn soc_constraints(&self) -> Ref<'_, Vec<SocConstraint>>
pub fn num_soc_constraints(&self) -> usize
pub fn soc_constraint_id(&self, name: &str) -> Option<SocConstraintId>
Sourcepub fn has_cones(&self) -> bool
pub fn has_cones(&self) -> bool
Whether the model carries any explicit second-order cone constraints.
Sourcepub fn is_feasibility(&self) -> bool
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.
Sourcepub fn ensure_objective_declared(&self) -> Result<()>
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.
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.
Sourcepub fn kind(&self) -> ModelKind
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):
- any nonlinear expression (objective or constraint) ->
NLP - any quadratic constraint not recognized as SOC (see
crate::detect_soc) ->QCP - cones present (explicit or detected) ->
SOCP - quadratic objective ->
QP - otherwise ->
LP
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