Struct VariableDefinition

Source
pub struct VariableDefinition { /* private fields */ }
Expand description

Defines the properties of a variable, such as its lower and upper bounds.

Implementations§

Source§

impl VariableDefinition

Source

pub fn new() -> Self

Creates an unbounded continuous linear variable

Source

pub fn integer(self) -> Self

Define the variable as an integer. The variable will only be able to take an integer value in the solution.

Warning: not all solvers support integer variables. Refer to the documentation of the solver you are using.

let mut problem = ProblemVariables::new();
let x = problem.add(variable().integer().min(0).max(2.5));
let solution = problem.maximise(x).using(default_solver).solve().unwrap();
// x is bound to [0; 2.5], but the solution is x=2 because x needs to be an integer
assert_eq!(solution.value(x), 2.);
Source

pub fn binary(self) -> Self

Define the variable as an integer that can only take the value 0 or 1.

Warning: not all solvers support integer variables. Refer to the documentation of the solver you are using.

let mut problem = ProblemVariables::new();
let x = problem.add(variable().binary());
let y = problem.add(variable().binary());
if cfg!(not(any(feature="clarabel"))) {
    let solution = problem.maximise(x + y).using(default_solver).solve().unwrap();
    assert_eq!(solution.value(x), 1.);
    assert_eq!(solution.value(y), 1.);
}
Source

pub fn initial<N: Into<f64>>(self, value: N) -> Self

Set the initial value of the variable. This may help the solver to find a solution significantly faster.

Note that the given value may be disregarded by some solvers if with_initial_solution is called on the problem instance. It is advisable to rely either on with_initial_solution, or to set initial values on the variables directly, but not both.

Warning: not all solvers support initial solutions. Refer to the documentation of the solver you are using.

let mut problem = ProblemVariables::new();
let x = problem.add(variable().max(3).initial(3));
let y = problem.add(variable().max(5).initial(5));
if cfg!(not(any(feature="clarabel"))) {
    let solution = problem.maximise(x + y).using(default_solver).solve().unwrap();
    assert_eq!(solution.value(x), 3.);
    assert_eq!(solution.value(y), 5.);
}
Source

pub fn name<S: Into<String>>(self, name: S) -> Self

Set the name of the variable. This is useful in particular when displaying the problem for debugging purposes.

let mut pb = ProblemVariables::new();
let x = pb.add(variable().name("x"));
assert_eq!("x", pb.display(&x).to_string());
Source

pub fn bounds<N: Into<f64> + Copy, B: RangeBounds<N>>(self, bounds: B) -> Self

Set the lower and/or higher bounds of the variable

§Examples
assert_eq!(
    variable().bounds(1..2),
    variable().min(1).max(2)
);

assert_eq!(
    variable().bounds(1..),
    variable().min(1)
);

assert_eq!(
    variable().bounds(..=2),
    variable().max(2)
);
Source

pub fn min<N: Into<f64>>(self, min: N) -> Self

Set the lower bound of the variable

Source

pub fn max<N: Into<f64>>(self, max: N) -> Self

Set the higher bound of the variable

Source

pub fn clamp<N1: Into<f64>, N2: Into<f64>>(self, min: N1, max: N2) -> Self

Set both the lower and higher bounds of the variable

Trait Implementations§

Source§

impl Clone for VariableDefinition

Source§

fn clone(&self) -> VariableDefinition

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VariableDefinition

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for VariableDefinition

Creates an unbounded continuous linear variable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for VariableDefinition

Source§

fn eq(&self, other: &VariableDefinition) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for VariableDefinition

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.