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 an interface to GLPK (GNU Linear Programming Kit).
# optional but recommended to pin dev-repo as it's on quite early stage of development
opam pin lp --dev-repo
opam install lplet x = Lp.var "x"
let y = Lp.var "y"
let problem =
let open Lp in
let c0 = x ++ c 1.2 *~ y <~ c 5.0 in
let c1 = c 2.0 *~ x ++ y <~ c 1.2 in
let obj = maximize (x ++ y) in
let cnstrs = [c0; c1] in
(obj, cnstrs)
let write () =
if Lp.validate problem then
Lp.write "my_problem.lp" problem
else
print_endline "Oops, my problem is broken."
let solve () =
match Lp.Glpk.Simplex.solve problem with
| Ok (obj, tbl) ->
Printf.printf "Objective: %.2f\n" obj ;
Printf.printf "x: %.2f y: %.2f\n"
(Hashtbl.find tbl x) (Hashtbl.find tbl y) ;
| Error msg -> print_endline msg-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