lp_solve is a free (see LGPL for the GNU lesser general public license) linear (integer) programming solver based on the revised simplex method and the Branch-and-bound method for the integers. lp_solve has its own community via the Yahoo group http://groups.yahoo.com/group/lp_solve. There you can find the latest sources, executables for the common platforms, examples, manuals and a message board where people can share their thoughts on lp_solve.
lp_solve is written in ANSI C and can be compiled on many different platforms like Linux and Windows. Basically, lp_solve is a library, a set of routines, that can be called easily from programming languages like C, C++, C# and VB. Unfortunately, there is no simple and straightforward way to use native C libraries like lp_solve in Java programs. This library (also called "Java wrapper") is designed to remedy this shortcoming. It consists of two main parts:
LpSolve class.This document should help you getting started using the Java wrapper and lp_solve in your
Java programs. Read it in addition to the documentation that comes with lp_solve.
Always refer to the lp_solve docs as ultimate reference for
using the routines of the optimization library.
Bug reports, succes stories and requests for changes concerning the Java wrapper are welcome
by email at juergen.ebert@web.de or in the lp_solve discussion group.
The current wrapper version was written to work with lp_solve 5.5.0.9 and was tested under Windows XP and Linux. As long as the API stays the same, other versions of lp_solve are likely to work as well. The wrapper requires a Java Runtime Environment 1.3 or later.
The latest version of the Java wrapper can be found in the files section of the lp_solve group. The wrapper is released under the same LGPL license conditions as lp_solve. A copy of the LGPL text is contained in the distribution archive.
lp_solve_5.5_dev.(zip or tar.gz)
and lp_solve_5.5_exe.(zip or tar.gz) to a standard library directory for your target platform.
On Windows, a typical place would be \WINDOWS or \WINDOWS\SYSTEM32.
On Linux, a typical place would be the directory /usr/local/lib.
lpsolve55j.dll
to the directory that already contains lpsolve55.dll.
liblpsolve55j.so
to the directory that already contains liblpsolve55.so. Run ldconfig to include
the library in the shared libray cache.
To create a Java application that uses lp_solve routines, you must perform the following steps:
lpsolve55j.jar from the Java wrapper distribution to a
directory that is included in the CLASSPATH of your java program.lpsolve.* at the beginning of your
source file.LpSolve.makeLp(...) or one of the other static factory methods of the LpSolve
class to create a LpSolve instance. Each LpSolve instance represents an optimization
problem.LpSolve instance to define the problem and obtain the solution.
Use the examples and implementation notes later in this documentation for further
information.lpsolve55j.jar in the CLASSPATH.
Also, on Windows, if you installed the native stub library in a directory that is not included
in the PATH variable, you have to define the Java system variable java.library.path
which must point to the installation directory. On Linux, the equivalent of the Windows PATH
variable is called LD_LIBRARY_PATH.The following program is a very simple example that shows how to program with lp_solve in Java.
import lpsolve.*;
public class Demo {
public static void main(String[] args) {
try {
// Create a problem with 4 variables and 0 constraints
LpSolve solver = LpSolve.makeLp(0, 4);
// add constraints
solver.strAddConstraint("3 2 2 1", LpSolve.LE, 4);
solver.strAddConstraint("0 4 3 1", LpSolve.GE, 3);
// set objective function
solver.strSetObjFn("2 3 -2 3");
// solve the problem
solver.solve();
// print solution
System.out.println("Value of objective function: " + solver.getObjective());
double[] var = solver.getPtrVariables();
for (int i = 0; i < var.length; i++) {
System.out.println("Value of var[" + i + "] = " + var[i]);
}
// delete the problem and free memory
solver.deleteLp();
}
catch (LpSolveException e) {
e.printStackTrace();
}
}
}
The following code fragment shows you how to use callbacks in Java.
The example defines an anonymous inner class that implements the AbortListener
interface which is then passed to the putAbortfunc method.
LpSolve solver = LpSolve.makeLp(0, 4);
AbortListener abortfunc = new AbortListener() {
public boolean abortfunc(LpSolve problem, Object handle) {
System.out.println("Java abortfunc called, handle = " + handle);
return false;
}
};
solver.putAbortfunc(abortfunc, new Integer(123));
Follow these steps to run the demo application, which is a port of the C demo program that comes with lp_solve to the Java language. You will need a Java Runtime Environment (JRE) on your machine in order to run the demo. You can download the latest JRE from http://java.sun.com
demo directory and
start the batch script "run_demo.bat".demo directory of the wrapper distribution and
run "sh run_demo".In the demo directory you will find
the file LpSolveTest.java which contains more than 100
JUnit test cases (see http://www.junit.org for
details about this highly useful software)
to strengthen the faith in the Java wrapper implementation. The test cases
may also seve as examples of basic lp_solve usage in Java. You will need
the library junit.jar in your CLASSPATH to run the test cases.
junit.jar is included in the lib directory of the Java wrapper. You
can run the test cases directly by starting the batch script "run_unittests.bat" on Windows
or "sh run_unittests" on Linux.
str_add_constraint
becomes strAddConstraint in Java.lprec* argument taken by almost all lp_solve API routines is hidden
completely inside the LpSolve class. All methods that create new lprec
structures were made static methods of the LpSolve class.set_row_name returns FALSE if an error has occured. In Java,
setRowName is of type void and throws a LpSolveException.boolean. Example:
set_debug(lprec *lp, unsigned char debug) is setDebug(boolean debug) in Java.
LpSolve object, which represents a problem, is only used by one thread at a
time.get_ptr_sensitivity_rhs, get_ptr_reduced_costs,
get_ptr_sensitivity_obj, and get_ptr_sensitivity_objex are not implemented,
because it is not possible in Java to pass pointers by reference to a method. Use the corresponding
methods without the Ptr part in the method name instead, which require allocation
of the resulting arrays by the caller.reference.html for details on how the lp_solve API functions
are mapped to the Java methods.The Java wrapper archive contains precompiled binary libraries for Windows and Linux. If you just want to use the wrapper there should be no need to build the libs from the sources. But if you absolutely have to, follow the guidelines in this chapter.
lp_solve_5.5_dev.zip unpackedlib directory and edit the file build.bat.
Change the path to the directory where you unpacked the lp_solve Windows
archive. Run the script to build lpsolve55j.dll.
The dll will be created in directory win32 or win64 depending on the OS.
lp_solve_5.5_dev.tar.gz unpackedlib directory and edit the file build.
Change the paths to the directory where you unpacked the lp_solve linux archive
and where the JDK is installed.
Run sh build to build liblpsolve55j.so.
The dll will be created in directory ux32 or ux64 depending on the OS.
Change to the lib/mac directory and edit the file build_osx.
Change the directory paths as indicated in the comments.
Thanks to Sean P. Kane (spkane@genomatica.com) who provided this build script.
Jython (http://www.jython.org) is a 100% Java implementation of the popular scripting language Python. One of the most remarkable features of Jython is the seamless interaction between Python and Java. Java programmers can add the Jython libraries to their system to allow end users to write scripts that add functionality to the application. On the other hand, Python/Jython programs can interact with Java packages or with running Java applications.
lp_solve functions can be called via the Java wrapper from Python/Jython programs.
See the file demo.py in the demo directory of the Java wrapper distribution
for an example program.
To run this program, you must install lp_solve, the Java wrapper, and Jython. Don't forget
to include lpsolve55j.jar in the Java CLASSPATH when you run Jython.