Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to new API #6

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build
on: [push]
jobs:
build:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest, macos-latest]
ocaml-version: ['4.09.0']
steps:
- uses: actions/checkout@v2
- uses: ocaml/setup-ocaml@v2
with:
ocaml-version: ${{ matrix.ocaml-version }}
- name: Setup opam
run: opam pin add -n .
- name: Install dependencies
run: opam depext -yt mad
- name: Build and test
run: opam install -t .
26 changes: 26 additions & 0 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build doc

on:
push:
branches:
- master

jobs:
build_doc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup OCaml
uses: avsm/setup-ocaml@v2
- name: Pin locally
run: opam pin -y add -n .
- name: Install dependencies
run: opam install -y odoc
- name: Build doc
run: opam exec dune build @doc
- name: Deploy doc
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: _build/default/_doc/_html
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: c
dist: bionic
sudo: required
install: test -e .travis.opam.sh || wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-opam.sh
script: bash -ex .travis-opam.sh
env:
- OCAML_VERSION=4.09
os:
- linux
4 changes: 3 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
0.1.9 (unreleased)
0.2.0 (unreleased)
=====
* Adapt to new glpk API, thus many backward incompatible changes, sorry.
* Switch build system to dune.

0.1.8 (2016-11-13)
=====
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ clean:
doc:
@dune build @doc

example: build
examples: build
@dune exec examples/example.exe

install: build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- Email: [email protected]
- Homepage: https://github.com/smimram/ocaml-glpk/

Copyright (C) 2003-2010 Samuel Mimram.
Copyright (C) 2003-2019 Samuel Mimram.


## Dependencies
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

2 changes: 2 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
$(MAKE) -C .. examples
28 changes: 14 additions & 14 deletions examples/example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
open Glpk

let () =
let lp = make_problem Maximize
[|10.; 6.; 4.|]
[|
[|1.; 1.; 1.|];
[|10.; 4.; 5.|];
[|2.; 2.; 6.|]
|]
[| -.infinity, 100.; -.infinity, 600.; -.infinity, 300. |]
[| 0., infinity; 0., infinity; 0., infinity|] in
scale_problem lp;
use_presolver lp true;
simplex lp;
let prim = get_col_primals lp in
Printf.printf "Z: %g x0: %g x1: %g x2: %g\n%!" (get_obj_val lp) prim.(0) prim.(1) prim.(2)
let lp =
make_problem Maximize
[|10.; 6.; 4.|]
[|
[|1.; 1.; 1.|];
[|10.; 4.; 5.|];
[|2.; 2.; 6.|]
|]
[| -.infinity, 100.; -.infinity, 600.; -.infinity, 300. |]
[| 0., infinity; 0., infinity; 0., infinity|]
in
simplex lp;
let prim = get_col_primals lp in
Printf.printf "Z: %g x0: %g x1: %g x2: %g\n%!" (get_obj_val lp) prim.(0) prim.(1) prim.(2)
11 changes: 3 additions & 8 deletions glpk.opam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
opam-version: "2.0"
version: "0.1.8"
version: "dev"
maintainer: "Samuel Mimram <[email protected]>"
authors: "Samuel Mimram <[email protected]>"
homepage: "http://smimram.github.io/ocaml-glpk/"
Expand All @@ -9,22 +9,17 @@ bug-reports: "https://github.com/smimram/ocaml-glpk/issues"
build: [
["dune" "build" "@install" "-p" name "-j" jobs]
["dune" "build" "@doc" "-p" name "-j" jobs] {with-doc}
[make "-C" "examples"] {with-test}
]
depends: ["ocaml" "dune"]
depexts: [
["libglpk-dev"] {os-distribution = "debian"}
["libglpk-dev"] {os-distribution = "ubuntu"}
["homebrew/science/glpk"] {os = "macos" & os-distribution = "homebrew"}
]
synopsis: "Bindings for glpk"
synopsis: "Bindings for GLPK"
description: """
ocaml-glpk are OCaml bindings to GLPK (GNU Linear Programming Kit)
which is a package intended for solving large-scale linear programming
(LP), mixed integer programming (MIP), and other related problems. The
library is released under the GPL license (like GLPK itself)."""
flags: light-uninstall
url {
src:
"https://github.com/smimram/ocaml-glpk/releases/download/0.1.8/ocaml-glpk-0.1.8.tar.gz"
checksum: "md5=d760f081a25259fb0c79175bcb51d6cd"
}
13 changes: 1 addition & 12 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@

(rule
(targets lpx.h)
(deps oldapi/lpx.h)
(action (run cp %{deps} %{targets} )))

(rule
(targets lpx.c)
(deps oldapi/lpx.c)
(action (run cp %{deps} %{targets} )))

(library
(name glpk)
(public_name glpk)
(flags :standard -safe-string)
(c_flags -Wall -DCAML_NAME_SPACE -g)
(c_library_flags -lglpk)
(c_names lpx glpk_stubs))
(c_names glpk_stubs))
92 changes: 37 additions & 55 deletions src/glpk.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(*
* ocaml-glpk - OCaml bindings to glpk
* Copyright (C) 2004-2006 Samuel Mimram
* Copyright (C) 2004-2019 Samuel Mimram
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,34 +26,18 @@ type aux_var_type = Free_var | Lower_bounded_var | Upper_bounded_var | Double_bo

type prob_class = Linear_prog | Mixed_integer_prog

type var_kind = Continuous_var | Integer_var
type var_kind = Continuous_var | Integer_var | Boolean_var

type status = Optimal | Feasible | Infeasible | No_feasible | Unbounded | Undefined

type message_level = Msg_off | Msg_err | Msg_on | Msg_all

exception Fault
exception Lower_limit
exception Upper_limit
exception No_primal_feasible_solution
exception No_dual_feasible_solution
exception Iteration_limit
exception Time_limit
exception Solver_failure
exception Empty
exception Bad_basis
exception No_convergence
exception Unknown_error

let _ =
Callback.register_exception "ocaml_glpk_exn_fault" Fault;
Callback.register_exception "ocaml_glpk_exn_objll" Lower_limit;
Callback.register_exception "ocaml_glpk_exn_objul" Upper_limit;
Callback.register_exception "ocaml_glpk_exn_nopfs" No_primal_feasible_solution;
Callback.register_exception "ocaml_glpk_exn_nodfs" No_dual_feasible_solution;
Callback.register_exception "ocaml_glpk_exn_itlim" Iteration_limit;
Callback.register_exception "ocaml_glpk_exn_tmlim" Time_limit;
Callback.register_exception "ocaml_glpk_exn_sing" Solver_failure;
Callback.register_exception "ocaml_glpk_exn_empty" Empty;
Callback.register_exception "ocaml_glpk_exn_badb" Bad_basis;
Callback.register_exception "ocaml_glpk_exn_noconv" No_convergence;
Callback.register_exception "ocaml_glpk_exn_unkown" Unknown_error;
exception Unknown_error of int

let () =
Callback.register "ocaml_glpk_exn_time_limit" Time_limit;
Callback.register_exception "ocaml_glpk_exn_unknown" (Unknown_error 0)

external new_problem : unit -> lp = "ocaml_glpk_new_prob"

Expand Down Expand Up @@ -91,7 +75,17 @@ external load_matrix : lp -> float array array -> unit = "ocaml_glpk_load_matrix

external load_sparse_matrix : lp -> ((int * int) * float) array -> unit = "ocaml_glpk_load_sparse_matrix"

external simplex : lp -> unit = "ocaml_glpk_simplex"
type simplex_param =
{
message_level : message_level option;
time_limit : int option;
}

external simplex : lp -> simplex_param -> unit = "ocaml_glpk_simplex"

let simplex ?message_level ?time_limit lp =
let p = { message_level; time_limit } in
simplex lp p

external get_obj_val : lp -> float = "ocaml_glpk_get_obj_val"

Expand Down Expand Up @@ -145,42 +139,30 @@ external unscale_problem : lp -> unit = "ocaml_glpk_unscale_problem"

external interior : lp -> unit = "ocaml_glpk_interior"

external set_class : lp -> prob_class -> unit = "ocaml_glpk_set_class"

external get_class : lp -> prob_class = "ocaml_glpk_get_class"

external set_col_kind : lp -> int -> var_kind -> unit = "ocaml_glpk_set_col_kind"

external branch_and_bound : lp -> unit = "ocaml_glpk_integer"

external branch_and_bound_opt : lp -> unit = "ocaml_glpk_integer"

external warm_up : lp -> unit = "ocaml_glpk_warm_up"

external use_presolver : lp -> bool -> unit = "ocaml_glpk_set_use_presolver"

external read_cplex : string -> lp = "ocaml_glpk_read_cplex"

external write_cplex : lp -> string -> unit = "ocaml_glpk_write_cplex"

external set_simplex_iteration_count : lp -> int -> unit = "ocaml_glpk_set_iteration_count"
type intopt_param =
{
message_level : message_level option;
time_limit : int option;
}

let reset_simplex_iteration_count lp =
set_simplex_iteration_count lp 0
external branch_and_cut : lp -> intopt_param -> unit = "ocaml_glpk_intopt"

external get_simplex_iteration_count : lp -> int = "ocaml_glpk_get_iteration_count"
let branch_and_cut ?message_level ?time_limit lp =
let p = { message_level; time_limit } in
branch_and_cut lp p

external _set_message_level : lp -> int -> unit = "ocaml_glpk_set_message_level"
external mip_status : lp -> status = "ocaml_glpk_mip_status"

let set_message_level lp n =
if (n < 0 && n > 3) then
raise (Invalid_argument "set_message_level");
_set_message_level lp n
external mip_obj_val : lp -> float = "ocaml_glpk_mip_obj_val"

external set_simplex_iteration_limit : lp -> int -> unit = "ocaml_glpk_set_iteration_limit"
external mip_row_val : lp -> int -> float = "ocaml_glpk_mip_row_val"

external get_simplex_iteration_limit : lp -> int = "ocaml_glpk_get_iteration_limit"
external mip_col_val : lp -> int -> float = "ocaml_glpk_mip_col_val"

external set_simplex_time_limit : lp -> float -> unit = "ocaml_glpk_set_time_limit"
external read_cplex : string -> lp = "ocaml_glpk_read_cplex"

external get_simplex_time_limit : lp -> float = "ocaml_glpk_get_time_limit"
external write_cplex : lp -> string -> unit = "ocaml_glpk_write_cplex"
Loading