Crate good_lp[][src]

A Linear Programming modeler that is easy to use, performant with large problems, and well-typed.

use good_lp::{variables, variable, default_solver, SolverModel, Solution};

let mut vars = variables!();
let a = vars.add(variable().max(1));
let b = vars.add(variable().min(2).max(4));
let solution = vars.maximise(10 * (a - b / 5) - b)
    .with(a + 2. << b) // or (a + 2).leq(b)
    .with(1 + a >> 4. - b)

assert_eq!(solution.value(a), 1.);
assert_eq!(solution.value(b), 3.);


This crate supports multiple solvers, that can be activated using feature flags.


You initially create your variables using variables and ProblemVariables::add.

Then you create your objective function.If it’s large, you can write rust functions to split complex expressions into components.

use good_lp::{Expression, Variable};

fn total_cost(energy: Variable, time: Variable) -> Expression {
    energy_cost(energy) + dollars_per_hour * time

fn energy_cost(energy: Variable) -> Expression {
    let price = fetch_energy_price(energy);
    energy * price

Then you create a solver problem model instance

let mut model = my_variables.minimise(my_objective).using(my_solver);

Then you add constraints and solve your problem using the methods in SolverModel.


pub use constraint::Constraint;
pub use solvers::ResolutionError;
pub use solvers::Solution;
pub use solvers::SolverModel;
pub use variable::variable;
pub use variable::ProblemVariables;
pub use variable::Variable;
pub use variable::VariableDefinition;
pub use solvers::coin_cbc::coin_cbc;
pub use solvers::minilp::minilp;
pub use solvers::lpsolve::lp_solve;
pub use solvers::coin_cbc::coin_cbc as default_solver;



Constraints define the inequalities that must hold in the solution.


Included solvers that find the actual solution to linear problems. The number of solvers available in this module depends on which cargo features you have activated.


A Variable is the base element used to create an Expression. The goal of the solver is to find optimal values for all variables in a problem.



This macro allows defining constraints using a + b <= c + d instead of (a + b).leq(c + d) or a + b << c + d


Instantiates ProblemVariables, to create a set of related variables.



Represents an affine expression, such as 2x + 3 or x + y + z



An element that can be expressed as a linear combination of variables plus a constant