Jump to content

GLPK/Java

From Wikibooks, open books for an open world

Java is an object-oriented application programming language.

GLPK for Java

[edit | edit source]
GLPK for Java comes with a bunch of examples. GmplSwing.java demonstrates the use of callbacks and terminal output redirection.

GLPK for Java uses SWIG to generate code for its Java language binding. This binding is published under the GNU General Public License.

Installation

[edit | edit source]

GLPK for Java is available through the Debian package libglpk-java, which can also be used with Ubuntu. Windows binaries are provided as part of the GLPK for Windows project.

Makefiles for Windows and POSIX-compliant systems (which includes all Linux distros) are available for manual builds on other systems. You will need to install GLPK and SWIG beforehand if you choose this option.

Documentation

[edit | edit source]

The GLPK for Java file doc/glpk-java.pdf contains a short description. Practical examples can be found in the examples/java directory. For the usage of the individual methods refer to doc/glpk.pdf of the GLPK source distribution.

Compiling and running

[edit | edit source]

Below is a minimal Java class. Save it as file Test.java.

import org.gnu.glpk.GLPK;
public class Test {
  public static void main(String[] args) {
    System.out.println( GLPK.glp_version());
  }
}

Then compile this class under 64-bit Windows:

"%JAVA_HOME%\bin\javac" -classpath "C:\Program Files\GLPK\glpk-4.47\w64\glpk-java.jar" Test.java

Or compile it under Linux:

$JAVA_HOME/bin/javac -classpath /usr/local/share/java/glpk-java.jar Test.java

Run the resulting file on 64-bit Windows:

java -Djava.library.path="C:\Program Files\GLPK\glpk-4.47\w64" -classpath "C:\Program Files\GLPK\glpk-4.47\w64\glpk-java.jar";. Test

Or run the file on Linux (the file paths may need adjustment to match your installation):

java -Djava.library.path=/usr/local/lib/jni \
-classpath /usr/local/share/java/glpk-java.jar:. \
Test

The output will be your GLPK version number, for example: 4.47.

Usage with Eclipse

[edit | edit source]
Project property settings needed in Eclipse

To use GLPK for Java with Eclipse the GLPK for Java jar library has to be added to the project properties. Furthermore the path to the native DLL library has to be set here. (See screenshot).

Linear Optimization Wrapper for Java

[edit | edit source]

The Linear Optimization Wrapper for Java provides a more intuitive interface to the GLPK for Java API. Columns and rows can be directly accessed via names and indices.

Columns are created like this:

Problem p = new Problem().
    setName("Cutting Stock");
// x(i,j) : x pieces of product j are cut from stock i
for (int i = 0; i < stock.length; i++) {
    for (int j = 0; j < product.length; j++) {
    p.column("x", i, j).
        type(Problem.ColumnType.INTEGER).
        bounds(0.0, null);
    }
}

Rows can be created and populated like this:

// demand(j) = sum( x(i,j) )
for (int j = 0; j < product.length; j++) {
    p.row("demand", j).bounds(demand[j], demand[j]);
    for (int i = 0; i < stock.length; i++) {
        p.row("demand", j).
            add(1.0, "x", i, j);
    }
}

You may download the code using subversion:

svn checkout http://www.xypron.de/svn/linopt/ linopt

For using this library in your Maven project enter the following repository and dependency in your pom.xml (adjust the version number as needed).

    <repositories>
        <repository>
            <id>XypronRelease</id>
            <name>Xypron Release</name>
            <url>http://rsync.xypron.de/repository</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>de.xypron.linopt</groupId>
            <artifactId>linopt</artifactId>
            <version>1.10</version>
        </dependency>
    </dependencies>

When testing with Maven it may be necessary to indicate the installation path of the GLPK for Java shared library (.so or .dll).

mvn clean install -DargLine='-Djava.library.path=/usr/local/lib/jni:/usr/lib/jni'

The exec:java target may require to indicate the installation path of the GLPK for Java shared library in MAVEN_OPTS, e.g.

export MAVEN_OPTS="-Djava.library.path=/usr/local/lib/jni:/usr/lib/jni"
mvn exec:java

Java ILP

[edit | edit source]

Java ILP provides a java interface to several linear programming solvers including GLPK. It is licences under the GNU Lesser General Public License (LGPL). The link to GLPK uses GLPK for Java.

The homepage http://javailp.sourceforge.net/ provides an example code. According to the homepage GLPK ≥ 4.43 is supported.

Development seems to have stopped. The last code update was on 2012-08-02.

OptimJ

[edit | edit source]

OptimJ is a Java-based modeling language and optimization environment. OptimJ is available with several commercial and non-commercial solvers to select from, including GLPK, and is offered under a variety of licensing, free (of charge) download, and purchase arrangements. OptimJ originates from Ateji, a software company based in Paris, France.

Development seems to have stopped. The latest release dates back to 2011.

Java Native Access

[edit | edit source]

Java Native Access (JNA) can be used to call shared native libraries from Java. The project home is https://github.com/twall/jna.

Using JNA has the following drawbacks:

  • Errors occurring in the GLPK library will terminate the main process.
  • Callbacks cannot be used.

The following example uses JNA to display the GLPK library version number.

// file GlpkVersion.java

import com.sun.jna.Library;
import com.sun.jna.Native;

public class GlpkVersion {
        /**
         * Interface of the GLPK library.
         */
        public interface Glpk extends Library {
                Glpk INSTANCE = (Glpk) Native.loadLibrary("glpk", Glpk.class);
                String glp_version();
        }

        public static void main(String[] args) {
                System.out.println(Glpk.INSTANCE.glp_version());
        }
}

Compile and run with

javac -classpath /usr/share/java/jna.jar GlpkVersion.java
java -classpath .:/usr/share/java/jna.jar GlpkVersion

Adjust the classpath according to your JNA installation path.

Obsolete interfaces

[edit | edit source]

GLPK 3.3 offered a Java language binding, written by Yuri Victorovich. This binding was removed from official GLPK distribution in version 4.7, because, at that time, no GNU GPL-compliant Java implementation was available. Third party projects now provide Java bindings for GLPK.

The GLPK 4.8 Java Interface was published by Björn Frank. It is no longer maintained and cannot be used with current versions of GLPK. In particular, users have reported faulty floating point arithmetic when deployed on 64-bit Linux systems.