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 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: 2 <= x <= 3;}

is equivalent to

let mut problem = ProblemVariables::new();
let y = problem.add(variable().min(0));
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_eq!(solution.value(y[3]), 2.);
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_eq!(solve(ObjectiveDirection::Minimisation), 2.);
assert_eq!(solve(ObjectiveDirection::Maximisation), 3.);
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_eq!(solution.value(x), 7.);
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_eq!(solution.value(x), -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 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 Default for ProblemVariables

source§

fn default() -> ProblemVariables

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

impl IntoIterator for ProblemVariables

§

type Item = VariableDefinition

The type of the elements being iterated over.
§

type IntoIter = IntoIter<VariableDefinition, Global>

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.