Expand description
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};
// Create variables in a readable format with a macro...
variables!{
vars:
a <= 1;
2 <= b <= 4;
}
// ... or add variables programmatically
vars.add(variable().min(2).max(9));
let solution = vars.maximise(10 * (a - b / 5) - b)
.using(default_solver)
.with(a + 2. << b) // or (a + 2).leq(b)
.with(1 + a >> 4. - b)
.solve()?;
assert_float_eq!(solution.value(a), 1., abs <= 1e-8);
assert_float_eq!(solution.value(b), 3., abs <= 1e-8);
§Solvers
This crate supports multiple solvers, that can be activated using feature flags.
§Usage
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.
Re-exports§
pub use constraint::Constraint;
pub use solvers::clarabel::clarabel;
clarabel
pub use solvers::coin_cbc::coin_cbc;
coin_cbc
pub use solvers::coin_cbc::coin_cbc as default_solver;
pub use solvers::highs::highs;
highs
pub use solvers::lp_solvers::LpSolver;
lp-solvers
pub use solvers::lpsolve::lp_solve;
lpsolve
pub use solvers::microlp::microlp;
microlp
pub use solvers::solver_name;
pub use solvers::DualValues;
pub use solvers::ModelWithSOS1;
pub use solvers::ResolutionError;
pub use solvers::Solution;
pub use solvers::SolutionStatus;
pub use solvers::SolutionWithDual;
pub use solvers::Solver;
pub use solvers::SolverModel;
pub use solvers::StaticSolver;
pub use solvers::WithInitialSolution;
pub use solvers::WithMipGap;
pub use solvers::WithTimeLimit;
pub use variable::variable;
pub use variable::ProblemVariables;
pub use variable::Variable;
pub use variable::VariableDefinition;
Modules§
- constraint
- Constraints define the inequalities that must hold in the solution.
- solvers
- 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.
- variable
- 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.
Macros§
- constraint
- This macro allows defining constraints using
a + b <= c + d
instead of(a + b).leq(c + d)
ora + b << c + d
- variables
- Instantiates ProblemVariables, to create a set of related variables.
Structs§
- Expression
- Represents an affine expression, such as
2x + 3
orx + y + z
Traits§
- Cardinality
Constraint Solver - A trait for solvers that support cardinality constraints
- Into
Affine Expression - An element that can be expressed as a linear combination of variables plus a constant