Giter Club home page Giter Club logo

ocaml-lp's Introduction

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
opam pin lp --dev-repo
opam pin lp-glpk --dev-repo
opam install lp lp-glpk
## If your application is based on js_of_ocaml:
opam pin lp-glpk-js --dev-repo
opam install lp-glpk-js
## If you have an access to Gurobi:
opam pin lp-gurobi --dev-repo
opam install lp-gurobi

Example

A minimum example is shown below. More detailed description 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 other interfaces, use Lp_glpk_js or 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."

Notes on solver interfaces

  • Since lp-glpk is tested only on GLPK version 4.65 and 5+, something may fail on older versions.
  • lp-glpk-js is another interface to GLPK through glpk.js, that is useful for applications based on js_of_ocaml.
  • lp-gurobi is an interface to commercial solver Gurobi. To work with this, compile your application with -cclib -lgurobiXY flags, where XY is the version of Gurobi (e.g. 91).

Documentation

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

Development Status

Development is not quite active now because basic features are completed. However, there are several TODOs. Bug-reports, requests, or patches are always welcome via GitHub issues and pull requests.

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

ocaml-lp's People

Contributors

ktahar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

arthurrab

ocaml-lp's Issues

GLPK assumes that variables are positive

As said in the title, if no bounds are given, GLPK assumes that it's [0,+inf[. I'm not sure how the other solver behaves.
It might be worth putting explicit ]-inf,+inf[ bounds by default to ensure consistent behaviour across solvers.

An error on "opam install lp-glpk"

I have installed glpk in the dictionary of "/opt/homebrew/Cellar/glpk/5.0", which means its version is 5.0
But when I try to install lp-glpk using opam install lp-glpk, there is an error:

[ERROR] The compilation of conf-glpk.1 failed at "cc -E test.c".
⬇ retrieved lp-glpk.0.4.0  (no changes)

#=== ERROR while compiling conf-glpk.1 ========================================#
# context     2.1.4 | macos/arm64 | ocaml.4.14.1 | https://opam.ocaml.org#8aa6c0b9
# path        ~/.opam/default/.opam-switch/build/conf-glpk.1
# command     ~/.opam/opam-init/hooks/sandbox.sh build cc -E test.c
# exit-code   1
# env-file    ~/.opam/log/conf-glpk-15693-cbc34f.env
# output-file ~/.opam/log/conf-glpk-15693-cbc34f.out
### output ###
# [...]
# test.c:1:10: fatal error: 'glpk.h' file not found
# #include <glpk.h>
#          ^~~~~~~~
# # 1 "test.c"

then I use export C_INCLUDE_PATH="/opt/homebrew/Cellar/glpk/5.0/include:$C_INCLUDE_PATH" to explicitly give the path to glpk.h.
but there is still an error here:

[ERROR] The compilation of lp-glpk.0.4.0 failed at "dune build -p lp-glpk -j 7".

#=== ERROR while compiling lp-glpk.0.4.0 ======================================#
# context     2.1.4 | macos/arm64 | ocaml.4.14.1 | pinned(git+https://github.com/ktahar/ocaml-lp.git#00d2df4e)
# path        ~/.opam/default/.opam-switch/build/lp-glpk.0.4.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build dune build -p lp-glpk -j 7
# exit-code   1
# env-file    ~/.opam/log/lp-glpk-16504-ebaaf8.env
# output-file ~/.opam/log/lp-glpk-16504-ebaaf8.out
### output ###
# [...]
# 4 warnings generated.
# File "src/lp-glpk/ffi/lib/dune", line 18, characters 0-389:
# 18 | (library
# 19 |  (name lp_glpk_ffi)
# 20 |  (public_name lp-glpk.ffi)
# ....
# 28 |  (c_library_flags
# 29 |   (:standard -lglpk))
# 30 |  (libraries lp-glpk.bindings lp-glpk.types ctypes.stubs ctypes))
# (cd _build/default && /Users/wangkeyin/.opam/default/bin/ocamlmklib.opt -g -o src/lp-glpk/ffi/lib/lp_glpk_ffi_stubs src/lp-glpk/ffi/lib/lp_glpk_stubs.o -ldopt -lglpk)
# ld: library not found for -lglpk
# clang: error: linker command failed with exit code 1 (use -v to see invocation)

what's the problem here?

Bundle GLPK statically

I suggest bundling GLPK in the library, just like in ocaml-glpk. Relying on system C packages is troublesome in many ways, makes the building more complex, and doesn't guarantee the version of GLPK used. I don't think there are many reason to rely on separate linking here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.