Page
Library
Module
Module type
Parameter
Class
Class type
Source
This library helps the modeling of Linear Programming (LP) and Mixed Integer Programming (MIP) in OCaml. It supports the model with not only linear terms, but also quadratic terms. The model can be imported-from / exported-to CPLEX LP file format, which can be loaded by various solvers. It also has direct interfaces to some solvers. Currently supported are GLPK (GNU Linear Programming Kit) and Gurobi.
# optional but recommended to pin dev-repo as it's on quite early stage of development
opam pin lp --dev-repo
opam pin lp-glpk --dev-repo
opam pin lp-gurobi --dev-repo # if you have an access to Gurobi
opam install lp lp-glpk # lp-gurobi
A minimum example is shown below. More examples can be found on wiki.
let x = Lp.var "x"
let y = Lp.var "y"
let problem =
let open Lp in
let obj = maximize (x ++ y) in
let c0 = x ++ (c 1.2 *~ y) <~ c 5.0 in
let c1 = (c 2.0 *~ x) ++ y <~ c 1.2 in
make obj [c0; c1]
let write () = Lp.write "my_problem.lp" problem
let solve () =
(* For Gurobi, use Lp_gurobi instead *)
match Lp_glpk.solve problem with
| Ok (obj, xs) ->
Printf.printf "Objective: %.2f\n" obj ;
Printf.printf "x: %.2f y: %.2f\n"
(Lp.PMap.find x xs) (Lp.PMap.find y xs)
| Error msg ->
print_endline msg
let () =
if Lp.validate problem then (write () ; solve ())
else print_endline "Oops, my problem is broken."
High level APIs have comments for odoc (or ocamldoc). Generated docs can be found online or in docs directory.
-cclib -lglpk
flags.Currently only basic features of LP file format are supported. Yet to be supported are advanced features, which are typically available on commercial solvers. (There is no standard of LP file, though.)
Some references to LP file format.
MIT