package lp-glpk

  1. Overview
  2. Docs
LP and MIP modeling in OCaml (GLPK interface)

Install

Dune Dependency

Authors

Maintainers

Sources

0.3.0.tar.gz
md5=dad8525a3cef45b161698b17702a29a7
sha512=e8a36c0a87a763af6674db472dc8cf64a456eeb481cd215d9b08fc0eb59cba7e65c8df8d3d29597f65c14af63d1a93ae4843506f05aba69884006b6452c1a690

Description

This library helps the modeling of Linear Programming (LP) and Mixed Integer Programming (MIP) in OCaml. This package is an optional solver-interface to GLPK (GNU Linear Programming Kit).

Published: 14 Feb 2021

README

ocaml-lp : LP and MIP modeling in OCaml

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.

Install

# 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

Example

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

Documentation

High level APIs have comments for odoc (or ocamldoc). Generated docs can be found online or in docs directory.

Notes on GLPK interface

  • To use this, compile your application with -cclib -lglpk flags.

  • Since this is tested only on GLPK version 4.65 and 5+, something may fail on older versions.

Conformity to LP file format

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

supported

  • Single objective (linear and quadratic)

  • Constraints (linear and quadratic)

  • Bounds

  • Variable types (general and binary integers)

not-supported

  • Semi-continuous variables

  • Multi-objective

  • Lazy constraint

  • Special ordered set (SOS)

  • Piecewise-linear (PWL) objective and constraint

  • General Constraint

  • Scenario

References

Some references to LP file format.

License

MIT

Dependencies (5)

  1. ctypes-foreign
  2. ctypes
  3. lp = version
  4. dune >= "2.2.0"
  5. ocaml >= "4.08.0"

Dev Dependencies (2)

  1. conf-glpk with-test
  2. alcotest with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.