lp-modeler
This project provides a mathematical programming modeling library for the Rust language (v1.22+).
An optimization problem (e.g. an integer or linear programme) can be formulated using familiar Rust syntax (see examples), and written into a universal LP model format. This can then be processed by a mixed integer programming solver. Presently supported solvers are; COIN-OR CBC, Gurobi and GLPK.
This project is inspired by COIN-OR PuLP which provides such a library for Python.
Usage
These examples present a formulation (in LP model format), and demonstrate the Rust code required to generate this formulation. Code can be found in tests/problems.rs.
Example 1 - Simple model
Formulation
\ One Problem
Maximize
10 a + 20 b
Subject To
c1: 500 a + 1200 b <= 10000
c2: a - b <= 0
Generals
a c b
End
Rust code
use ;
use ;
use LpInteger;
use ;
// Define problem variables
let ref a = new;
let ref b = new;
let ref c = new;
// Define problem and objective sense
let mut problem = new;
// Objective Function: Maximize 10*a + 20*b
problem += 10.0 * a + 20.0 * b;
// Constraint: 500*a + 1200*b + 1500*c <= 10000
problem += .le;
// Constraint: a <= b
problem += .le;
// Specify solver
let solver = new;
// Run optimisation and process output hashmap
match solver.run
To generate the LP file which is shown above:
problem.write_lp
Example 2 - An Assignment model
Formulation
This more complex formulation programmatically generates the expressions for the objective and constraints.
We wish to maximise the quality of the pairing between a group of men and women, based on their mutual compatibility score. Each man must be assigned to exactly one woman, and vice versa.
Compatibility Score Matrix
| Abe | Ben | Cam | |
|---|---|---|---|
| Deb | 50 | 60 | 60 |
| Eve | 75 | 95 | 70 |
| Fay | 75 | 80 | 80 |
This problem is formulated as an Assignment Problem.
Rust code
extern crate lp_modeler;
extern crate maplit;
use HashMap;
use *;
use *;
use LpOperations;
use ;
use ;
// Problem Data
let men = vec!;
let women = vec!;
let compat_scores = hashmap!;
// Define Problem
let mut problem = new;
// Define Variables
let mut vars = new;
for m in &men
// Define Objective Function
let mut obj_vec: = Vecnew;
for in &vars
problem += lp_sum;
// Define Constraints
// Constraint 1: Each man must be assigned to exactly one woman
for m in &men
// Constraint 2: Each woman must be assigned to exactly one man
for w in &women
// Run Solver
let solver = new;
let result = solver.run;
// Terminate if error, or assign status & variable values
assert!;
let = result.unwrap;
// Compute final objective function value
let mut obj_value = 0f32;
for in &vars
// Print output
println!;
println!;
// println!("{:?}", var_values);
for in &var_values
This code computes the objective function value and processes the output to print the chosen pairing of men and women:
Status: Optimal
Objective Value: 230
B_E = 1
C_D = 1
A_F = 1
New features
0.3.1
- Add distributive property (ex:
3 * (a + b + 2) = 3*a + 3*b + 6) - Add trivial rules (ex:
3 * a * 0 = 0or3 + 0 = 3) - Add commutative property to simplify some computations
- Support for GLPK
0.3.0
- Functional lib with simple algebra properties
Contributors
- Joel Cavat (jcavat)
- Thomas Vincent (tvincent2)
- Antony Phillips (aphi)
Further work
- Config for lp_solve and CPLEX
- 'complex' algebra operations such as commutative and distributivity
- It would be great to use some constraint for binary variables such as
- a && b which is the constraint a + b = 2
- a || b which is the constraint a + b >= 1
- a <=> b which is the constraint a = b
- a => b which is the constraint a <= b
- All these cases are easy with two constraints but more complex with expressions
- ...