Skip to main content

ProblemVariables

Struct ProblemVariables 

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

Represents the variables for a given problem. Each problem has a unique type, which prevents using the variables from one problem inside an other one. Instances of this type should be created exclusively using the crate::variables! macro.

Implementations§

Source§

impl ProblemVariables

Source

pub fn new() -> Self

Create an empty list of variables

Source

pub fn add_variable(&mut self) -> Variable

Add a anonymous unbounded continuous variable to the problem

Source

pub fn add(&mut self, var_def: VariableDefinition) -> Variable

Add a variable with the given definition

variables!{problem: y >= 0;}

is equivalent to

let mut problem = ProblemVariables::new();
let y = problem.add(variable().min(0));
Source

pub fn add_all<T, B: FromIterator<Variable>>(&mut self, var_defs: T) -> B

Adds many variables with the given definitions

use good_lp::*;
// Solve a problem with 11 variables: x, y0, y1, ..., y9
variables!{problem: 2 <= x <= 3;}
let vars = vec![variable().min(0); 10];
let y: Vec<Variable> = problem.add_all(vars);
let objective: Expression = y.iter().sum(); // Minimise sum(y_i for i in [0; 9])
let mut model = problem.minimise(objective).using(default_solver);
// for all i, we must have y_i >= x
for y_i in y.iter() {
  model = model.with(constraint!(y_i >= x));
}
let solution = model.solve().unwrap();
assert_float_eq!(solution.value(y[3]), 2., abs <= 1e-8);
Source

pub fn add_vector( &mut self, var_def: VariableDefinition, len: usize, ) -> Vec<Variable>

Adds a list of variables with the given definition

use good_lp::*;
// Solve a problem with 11 variables: x, y0, y1, ..., y9
variables!{problem: 2 <= x <= 3;}
let y: Vec<Variable> = problem.add_vector(variable().min(0), 10);
let objective: Expression = y.iter().sum(); // Minimise sum(y_i for i in [0; 9])
let mut model = problem.minimise(objective).using(default_solver);
// for all i, we must have y_i >= x
for y_i in y.iter() {
  model = model.with(constraint!(y_i >= x));
}
let solution = model.solve().unwrap();
assert_float_eq!(solution.value(y[3]), 2., abs <= 1e-8);
Source

pub fn optimise<E: IntoAffineExpression>( self, direction: ObjectiveDirection, objective: E, ) -> UnsolvedProblem

Creates an optimization problem with the given objective. Don’t solve it immediately.

use good_lp::{variables, variable, default_solver, SolverModel, Solution};
use good_lp::solvers::ObjectiveDirection;
fn solve(sense: ObjectiveDirection) -> f64 {
   variables!{problem: 2 <= x <= 3;}
    let solution = problem.optimise(sense, x).using(default_solver).solve().unwrap();
    solution.value(x)
}

assert_float_eq!(solve(ObjectiveDirection::Minimisation), 2., abs<=1e-8);
assert_float_eq!(solve(ObjectiveDirection::Maximisation), 3., abs<=1e-8);
Source

pub fn maximise<E: IntoAffineExpression>(self, objective: E) -> UnsolvedProblem

Creates an maximization problem with the given objective. Don’t solve it immediately

use good_lp::{variables, variable, default_solver, SolverModel, Solution};
variables!{problem: x <= 7;}
let solution = problem.maximise(x).using(default_solver).solve().unwrap();
assert_float_eq!(solution.value(x), 7., abs <= 1e-8);
Source

pub fn minimise<E: IntoAffineExpression>(self, objective: E) -> UnsolvedProblem

Creates an minimization problem with the given objective. Don’t solve it immediately

use good_lp::{variables, variable, default_solver, SolverModel, Solution};
variables!{problem: x >= -8;}
let solution = problem.minimise(x).using(default_solver).solve().unwrap();
assert_float_eq!(solution.value(x), -8., abs <= 1e-8);
Source

pub fn iter_variables_with_def( &self, ) -> impl Iterator<Item = (Variable, &VariableDefinition)>

Iterates over the couples of variables with their properties

Source

pub fn len(&self) -> usize

The number of variables

Source

pub fn is_empty(&self) -> bool

Returns true when no variables have been added

Source

pub fn initial_solution_len(&self) -> usize

Returns the number of variables with initial solution values

use good_lp::{variable, variables};
let mut vars = variables!();
vars.add(variable());
vars.add(variable().initial(5));
vars.add(variable());
assert_eq!(vars.initial_solution_len(), 1);
Source

pub fn display<'a, V: FormatWithVars>( &'a self, value: &'a V, ) -> impl Display + 'a

Display the given expression or constraint with the correct variable names

use good_lp::variables;
variables! {problem: 0 <= x; 0 <= y;}
let expression = x + 2*y;
let str = problem.display(&expression).to_string();
assert!(str == "x + 2 y" || str == "2 y + x"); // The ordering is not guaranteed

Trait Implementations§

Source§

impl Clone for ProblemVariables

Source§

fn clone(&self) -> ProblemVariables

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for ProblemVariables

Source§

fn default() -> ProblemVariables

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

impl IntoIterator for ProblemVariables

Source§

type Item = VariableDefinition

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<VariableDefinition>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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.