#linear-programming #solver #optimization #file-format #linear-model #formulation

lp-solvers

.lp file format implementation and external solver invocation for Cbc, Gurobi, cplex, and GLPK

7 releases (2 stable)

1.0.1 Feb 24, 2024
1.0.0 Aug 19, 2023
0.0.5 Aug 14, 2023
0.0.4 Apr 1, 2021
0.0.3 Mar 29, 2021

#499 in Algorithms

Download history 91/week @ 2024-07-22 124/week @ 2024-07-29 94/week @ 2024-08-05 80/week @ 2024-08-12 50/week @ 2024-08-19 86/week @ 2024-08-26 34/week @ 2024-09-02 160/week @ 2024-09-09 131/week @ 2024-09-16 120/week @ 2024-09-23 79/week @ 2024-09-30 63/week @ 2024-10-07 87/week @ 2024-10-14 89/week @ 2024-10-21 102/week @ 2024-10-28 88/week @ 2024-11-04

371 downloads per month
Used in good_lp

MIT license

54KB
1.5K SLoC

lp-solvers

Library implementing interaction with various linear programming solvers.

It uses the .lp file format to interact with external solver binaries.

Supported solvers

  • gurobi
  • cplex (with the cplex feature)
  • cbc
  • glpk
  • auto: automatically finds which of the above solver is installed at runtime, and uses it.

You need to have the solver you want to use installed on your machine already for this library to work.

Example


use lp_solvers::lp_format::{Constraint, LpObjective};
use lp_solvers::problem::{Problem, StrExpression, Variable};
use lp_solvers::solvers::{CbcSolver, SolverTrait};
use lp_solvers::solvers::Status::Optimal;

fn solve_integer_problem_with_solver<S: SolverTrait>(solver: S) {
    let pb = Problem { // Alternatively, you can implement the LpProblem trait on your own structure
        name: "int_problem".to_string(),
        sense: LpObjective::Maximize,
        objective: StrExpression("x - y".to_string()), // You can use other expression representations
        variables: vec![
            Variable {
                name: "x".to_string(),
                is_integer: true,
                lower_bound: -10.,
                upper_bound: -1.,
            },
            Variable {
                name: "y".to_string(),
                is_integer: true,
                lower_bound: 4.,
                upper_bound: 7.,
            },
        ],
        constraints: vec![Constraint {
            lhs: StrExpression("x - y".to_string()),
            operator: Ordering::Less,
            rhs: -4.5,
        }],
    };
    let solution = solver.run(&pb).expect("Failed to run solver");
    assert_eq!(solution.status, Optimal);
    // solution.results is now {"x":-1, "y":4}
}

fn main() {
    solve_integer_problem_with_solver(CbcSolver::default())
}

Dependencies

~1.6–9.5MB
~112K SLoC