GNU Linear Programming Kit Java Binding: Reference Manual
GNU Linear Programming Kit Java Binding: Reference Manual
Reference Manual
Version 1.0.15
September 2010
Copyright c 2009, 2010 Heinrich Schuchardt, [email protected] Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modied versions of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modied versions. Windows is a registered trademark of Microsoft Corporation. Java is a trademark when it identies a software product of Sun Microsystems, Inc. 2
Contents
1 Introduction 2 Architecture 2.1 GLPK library . . . . . . . . 2.1.1 Source . . . . . . . . 2.1.2 Linux . . . . . . . . 2.1.3 Windows . . . . . . 2.2 GLPK for Java JNI library 2.2.1 Source . . . . . . . . 2.2.2 Linux . . . . . . . . 2.2.3 Windows . . . . . . 2.3 GLPK for Java class library 2.3.1 Linux . . . . . . . . 2.3.2 Windows . . . . . . 2.3.3 Classpath . . . . . . 3 Classes 4 Usage 4.1 Loading the JNI library . . . . 4.2 Exceptions . . . . . . . . . . . 4.2.1 Implementation details . 4.3 Callbacks . . . . . . . . . . . . 4.4 Output listener . . . . . . . . . 4.5 Aborting a GLPK library call . 4.6 Threads . . . . . . . . . . . . . 5 Examples 5.1 Lp.java . . . . . . 5.1.1 Description 5.1.2 Coding . . 5.2 Gmpl.java . . . . . 5.2.1 Description 5.2.2 Coding . . 6 License 4 5 5 5 5 5 6 6 6 6 6 6 7 7 8 10 10 10 11 11 13 13 13 14 14 14 14 16 16 17 19
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
Chapter 1
Introduction
The GNU Linear Programming Kit (GLPK)[2] package supplies a solver for large scale linear programming (LP) and mixed integer programming (MIP). The GLPK project is hosted at http://www.gnu.org/software/glpk. It has two mailing lists: [email protected] and [email protected]. To subscribe to one of these lists, please, send an empty mail with a Subject: header line of just subscribe to the list. GLPK provides a library written in C and a standalone solver. The source code provided at ftp://gnu.ftp.org/gnu/glpk/ contains the documentation of the library in le doc/glpk.pdf. The Java platform provides the Java Native Interface (JNI)[3] to integrate non-Java language libraries into Java applications. Project GLPK for Java delivers http://glpk-java.sourceforge.net/. a Java Binding for GLPK. It is hosted at
To report problems and suggestions concerning GLPK for Java, please, send an email to the author at [email protected].
Chapter 2
Architecture
A GLPK for Java application will consist of the following the GLPK library the GLPK for Java JNI library the GLPK for Java class library the application code.
2.1
2.1.1
GLPK library
Source
compile the GLPK library is provided at
2.1.2
Linux
The GLPK library can be compiled from source code. Follow the instructions in le INSTALL provided in the source distribution. Precompiled packages are available in many Linux distributions. The usual installation path for the library is /usr/local/lib/libglpk.so.
2.1.3
Windows
The GLPK library can be compiled from source code. The build and make les are in directory w32 for 32 bit Windows and in w64 for 64 bit Windows. The name of the created library is glpk 4 45.dll for revision 4.45. A precompiled version of GLPK is provided at http://winglpk.sourceforge.net. The library has to be in the search path for binaries. Either copy the library to a directory that is already in the path (e.g. C:\windows\system32) or update the path in the system settings of Windows.
2.2
2.2.1
2.2.2
Linux
The GLPK for Java JNI library can be compiled from source code. Follow the instructions in le INSTALL provided in the source distribution. The usual installation path for the library is /usr/local/lib/libglpk-java.so.
2.2.3
Windows
The GLPK for Java JNI library can be compiled from source code. The build and make les are in directory w32 for 32 bit Windows and in w64 for 64 bit Windows. The name of the created library is glpk 4 45 java.dll for revision 4.45. A precompiled version http://winglpk.sourceforge.net. of GLPK for Java is provided at
The library has to be in the search path for binaries. Either copy the library to a directory that is already in the path (e.g. C:\windows\system32) or update the path in the system settings of Windows.
2.3
2.3.1
Linux
The GLPK for Java class library can be compiled from source code. Follow the instructions in le INSTALL provided in the source distribution. The usual installation path for the library is /usr/local/share/java/glpk-java.jar. For Debian and Ubuntu the following packages are needed for compilation: libtool swig java-gcj-compat-dev
2.3.2
Windows
The GLPK for Java class library can be compiled from source code. The build and make les are in directory w32 for 32 bit Windows and in w64 for 64 bit Windows. The name of the created library is glpk-java.jar. A precompiled version http://winglpk.sourceforge.net. of GLPK including GLPK-Java is provided at
2.3.3
Classpath
The library has to be in the CLASSPATH. Update the classpath in the system settings of Windows or specify the classpath upon invocation of the application, e.g. java -classpath ./glpk-java.jar;. MyApplication
Chapter 3
Classes
GLPK for Java uses the Simplied Wrapper and Interface Generator (SWIG)[4] to create the JNI interface to GLPK. Classes are created in path org.gnu.glpk. Class GlpkCallback is called by the MIP solver callback routine. Interface GlpkCallbackListener can be implemented to register a listener for class GlpkCallback. Class GlpkTerminal is called by the MIP solver terminal output routine. Interface GlpkTerminalListener can be implemented to register a listener for class GlpkTerminal. Class GlpkException is thrown if an error occurs. Class GLPK maps the functions from include/glpk.h. Class GLPKConstants maps the constants from include/glpk.h to methods. Class GLPKJNI contains the denitions of the native functions. The following classes map structures from include/glpk.h: glp attr glp bfcp glp cpxcp glp data glp iocp glp iptcp glp long glp mpscp glp prob glp smcp glp tran glp tree 8
LPXKKT glp arc glp graph glp vertex The following classes are used to map pointers: SWIGTYPE p double SWIGTYPE p f p glp tree p void void SWIGTYPE p f p q const char v SWIGTYPE p f p void void SWIGTYPE p f p void p q const char int SWIGTYPE p int SWIGTYPE p p glp vertex SWIGTYPE p va list SWIGTYPE p void void
Chapter 4
Usage
Please, refer to le doc/glpk.pdf of the GLPK source distribution for a detailed description of the methods and constants.
4.1
To be able to use the JNI library in a Java program it has to be loaded. The path to dynamic link libaries can specied on the command line when calling the Java runtime, e.g. java -Djava.library.path=/usr/local/lib/jni/libglpk_java The following code is used in class GLPK to load the JNI library: static { try { if (System.getProperty("os.name").toLowerCase().contains("windows")) { // try to load Windows library System.loadLibrary("glpk_4_45_java"); } else { // try to load Linux library System.loadLibrary("glpk_java"); } } catch (UnsatisfiedLinkError e) { System.err.println( "The dynamic link library for GLPK for Java could not be" + "loaded.\nConsider using\njava -Djava.library.path="); throw e; } } If the JNI library could java.lang.UnsatisedLinkError. not be loaded, you will receive an exception
4.2
Exceptions
When illegal parameters are passed to a function of the GLPK native library an exception GlpkException is thrown. Due to the architecture of GLPK all GLPK objects are invalid when such an exception 10
has occured.
4.2.1
Implementation details
GLPK for Java registers a function glp java error hook() to glp error hook() before calling an GLPK API function. If an error occurs function glp free env is called and a long jump is used to return to the calling environment. Then function glp java throw() is called which throws GlpkException.
4.3
Callbacks
The MIP solver provides a callback functionality. This is used to call method callback of class GlpkCallback. A Java program can listen to the callbacks by instantiating a class implementing interface GlpkCallbackListener and registering the object with method addListener() of class GlpkCallback. The listener can be deregistered with method removeListener(). The listener can use method GLPK.glp ios reason() to nd out why it is called. For details see the GLPK library documentation.
11
12
4.4
Output listener
GLPK provides a hook for terminal output. A Java program can listen to the callbacks by instantiating a class implementing interface GlpkTerminalListener and registering the object with method addListener of class GlpkTerminal. The listener can be dregistered with method removeListener(). After a call to glp free env() the GlpkTerminal has to registered again by calling GLPK.glp term hook(null, null). glp free env() is called if an exception GlpkException occurs.
4.5
Method void GLPK.glp java error(String message) can be used to abort any call to the GLPK library. An exception GlpkException will occur. As GLPK is not threadsafe the call must be placed in the same thread as the initial call that is to be aborted. The output method of a GlpkTerminalListener can be used for this purpose.
4.6
Threads
The GLPK library is not thread safe. Never two threads should be running that access the GLPK library at the same time. When a new thread accesses the library it should call GLPK.glp free env(). When using an GlpkTerminalListener it is necessary to register GlpkTerminal again by calling GLPK.glp term hook(null, null). When writing a GUI application it is advisable to use a separate thread for the calls to GLPK. Otherwise the GUI cannot react to events during the call to the GLPK libary.
13
Chapter 5
Examples
Examples are provided in directory examples/java of the source distribution of GLPK for Java. To compile the examples the classpath must point to glpk-java.jar, e.g. javac -classpath /usr/local/shared/java/glpk-java.jar Example.java To run the examples the classpath must point to glpk-java.jar. The java.library.path must point to the directory with the dynamic link libraries, eg. java -Djava.library.path=/usr/local/lib/jni \ -classpath /usr/local/shared/java/glpk-java.jar:. \ Example
5.1
5.1.1
Lp.java
Description
This example solves a small linear problem and ouputs the solution.
5.1.2
import import import import import import import public // // // // // //
Coding
org.gnu.glpk.GLPK; org.gnu.glpk.GLPKConstants; org.gnu.glpk.GlpkException; org.gnu.glpk.SWIGTYPE_p_double; org.gnu.glpk.SWIGTYPE_p_int; org.gnu.glpk.glp_prob; org.gnu.glpk.glp_smcp; class Lp { Minimize z = (x1-x2) /2 + (1-(x1-x2)) = -.5 * x1 + .5 * x2 + 1 subject to 0.0<= x1 - x2 <= 0.2 where, 0.0 <= x1 <= 0.5 14
// 0.0 <= x2 <= 0.5 public static void main(String[] arg) { glp_prob lp; glp_smcp parm; SWIGTYPE_p_int ind; SWIGTYPE_p_double val; int ret; try { // Create problem lp = GLPK.glp_create_prob(); System.out.println("Problem created"); GLPK.glp_set_prob_name(lp, "myProblem"); // Define columns GLPK.glp_add_cols(lp, 2); GLPK.glp_set_col_name(lp, GLPK.glp_set_col_kind(lp, GLPK.glp_set_col_bnds(lp, GLPK.glp_set_col_name(lp, GLPK.glp_set_col_kind(lp, GLPK.glp_set_col_bnds(lp, // Create constraints GLPK.glp_add_rows(lp, 1); GLPK.glp_set_row_name(lp, 1, "c1"); GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_DB, 0, 0.2); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); val = GLPK.new_doubleArray(3); GLPK.doubleArray_setitem(val, 1, 1.); GLPK.doubleArray_setitem(val, 2, -1.); GLPK.glp_set_mat_row(lp, 1, 2, ind, val); // Define objective GLPK.glp_set_obj_name(lp, "z"); GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN); GLPK.glp_set_obj_coef(lp, 0, 1.); GLPK.glp_set_obj_coef(lp, 1, -.5); GLPK.glp_set_obj_coef(lp, 2, .5); // Solve model parm = new glp_smcp(); GLPK.glp_init_smcp(parm); ret = GLPK.glp_simplex(lp, parm); // Retrieve solution if (ret == 0) { write_lp_solution(lp); } else {
1, 1, 1, 2, 2, 2,
15
System.out.println("The problem could not be solved"); } // Free memory GLPK.glp_delete_prob(lp); } catch (GlpkException ex) { ex.printStackTrace(); } } /** * write simplex solution * @param lp problem */ static void write_lp_solution(glp_prob lp) { int i; int n; String name; double val; name = GLPK.glp_get_obj_name(lp); val = GLPK.glp_get_obj_val(lp); System.out.print(name); System.out.print(" = "); System.out.println(val); n = GLPK.glp_get_num_cols(lp); for (i = 1; i <= n; i++) { name = GLPK.glp_get_col_name(lp, i); val = GLPK.glp_get_col_prim(lp, i); System.out.print(name); System.out.print(" = "); System.out.println(val); } } }
5.2
5.2.1
Gmpl.java
Description
This example reads a GMPL le and executes it. The callback function is used to write an output line when a better MIP soluton has been found. Run the program with the model le as parameter. java -Djava.library.path=/usr/local/lib \ -classpath /usr/local/shared/java/glpk-java.jar:. \ GLPKSwig marbles.mod
16
5.2.2
import import import import import import import import
Coding
org.gnu.glpk.GLPK; org.gnu.glpk.GLPKConstants; org.gnu.glpk.GlpkCallback; org.gnu.glpk.GlpkCallbackListener; org.gnu.glpk.glp_iocp; org.gnu.glpk.glp_prob; org.gnu.glpk.glp_tran; org.gnu.glpk.glp_tree;
public class Gmpl implements GlpkCallbackListener { public static void main(String[] arg) { if (1 != arg.length) { System.out.println("Usage: java Gmpl model.mod"); return; } new Gmpl().solve(arg); } public void solve(String[] arg) { glp_prob lp = null; glp_tran tran; glp_iocp iocp; String fname; int skip = 0; int ret; GlpkCallback.addListener(this); fname = new String(arg[0]); lp = GLPK.glp_create_prob(); System.out.println("Problem created"); tran = GLPK.glp_mpl_alloc_wksp(); ret = GLPK.glp_mpl_read_model(tran, fname, skip); if (ret != 0) { GLPK.glp_mpl_free_wksp(tran); GLPK.glp_delete_prob(lp); throw new RuntimeException("Model file not found: " + fname); } // generate model GLPK.glp_mpl_generate(tran, null); // build model GLPK.glp_mpl_build_prob(tran, lp); // set solver parameters iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); iocp.setPresolve(GLPKConstants.GLP_ON);
17
// solve model ret = GLPK.glp_intopt(lp, iocp); // postsolve model if (ret == 0) { GLPK.glp_mpl_postsolve(tran, lp, GLPKConstants.GLP_MIP); } // free memory GLPK.glp_mpl_free_wksp(tran); GLPK.glp_delete_prob(lp); } public void callback(glp_tree tree) { int reason = GLPK.glp_ios_reason(tree); if (reason == GLPKConstants.GLP_IBINGO) { System.out.println("Better solution found"); } } }
18
Chapter 6
License
GLPK for Java is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License[1] as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. GLPK for Java is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GLPK for Java. If not, see http://www.gnu.org/licenses/.
19
Bibliography
[1] Free Software Foundation, Inc. GNU General Public License, 2007. [2] Andrew Makhorin. GNU Linear Programming Kit. GNU Software Foundation, 2010. [3] Sun Microsystems, Inc. Java Native Interface Specication v1.5, 2004. [4] SWIG.org. Simplied Wrapper and Interface Generator, 2010.
20
Index
abort, 13 callbacks, 11 class path, 8 classes, 8 classpath, 7 examples, 14 exceptions, 10 glp java error, 13 GlpkCallback, 11 GlpkCallbackListener, 11 GlpkException, 10, 13 GlpkTerminal, 13 GlpkTerminalListener, 13 JNI library, 6, 10 license, 19 Linux, 5, 6 output listener, 13 support, 4 SWIG, 8 threads, 13 Windows, 57
21