Crate good_lp

source ·
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§

Modules§

  • 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.

Macros§

  • 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.

Structs§

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

Traits§