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
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: y >= 0;}is equivalent to
let mut problem = ProblemVariables::new();
let y = problem.add(variable().min(0));Sourcepub fn add_all<T, B: FromIterator<Variable>>(&mut self, var_defs: T) -> Bwhere
T: IntoIterator<Item = VariableDefinition>,
pub fn add_all<T, B: FromIterator<Variable>>(&mut self, var_defs: T) -> Bwhere
T: IntoIterator<Item = VariableDefinition>,
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);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_float_eq!(solution.value(y[3]), 2., abs <= 1e-8);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_float_eq!(solve(ObjectiveDirection::Minimisation), 2., abs<=1e-8);
assert_float_eq!(solve(ObjectiveDirection::Maximisation), 3., abs<=1e-8);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_float_eq!(solution.value(x), 7., abs <= 1e-8);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_float_eq!(solution.value(x), -8., abs <= 1e-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 initial_solution_len(&self) -> usize
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);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 guaranteedTrait Implementations§
Source§impl Clone for ProblemVariables
impl Clone for ProblemVariables
Source§fn clone(&self) -> ProblemVariables
fn clone(&self) -> ProblemVariables
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for ProblemVariables
impl Default for ProblemVariables
Source§fn default() -> ProblemVariables
fn default() -> ProblemVariables
Source§impl IntoIterator for ProblemVariables
impl IntoIterator for ProblemVariables
Auto Trait Implementations§
impl Freeze for ProblemVariables
impl RefUnwindSafe for ProblemVariables
impl Send for ProblemVariables
impl Sync for ProblemVariables
impl Unpin for ProblemVariables
impl UnsafeUnpin for ProblemVariables
impl UnwindSafe for ProblemVariables
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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