Struct good_lp::variable::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 variables! macro.
Implementations§
source§impl ProblemVariables
impl ProblemVariables
sourcepub fn add_variable(&mut self) -> Variable
pub fn add_variable(&mut self) -> Variable
Add a anonymous unbounded continuous variable to the problem
sourcepub fn add(&mut self, var_def: VariableDefinition) -> Variable
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));
sourcepub fn add_vector(
&mut self,
var_def: VariableDefinition,
len: usize
) -> Vec<Variable>
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.);
sourcepub fn optimise<E: IntoAffineExpression>(
self,
direction: ObjectiveDirection,
objective: E
) -> UnsolvedProblem
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.);
sourcepub fn maximise<E: IntoAffineExpression>(self, objective: E) -> UnsolvedProblem
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.);
sourcepub fn minimise<E: IntoAffineExpression>(self, objective: E) -> UnsolvedProblem
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.);
sourcepub fn iter_variables_with_def(
&self
) -> impl Iterator<Item = (Variable, &VariableDefinition)>
pub fn iter_variables_with_def( &self ) -> impl Iterator<Item = (Variable, &VariableDefinition)>
Iterates over the couples of variables with their properties
sourcepub fn display<'a, V: FormatWithVars>(
&'a self,
value: &'a V
) -> impl Display + 'a
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