Initializing help system before first use

Creating Xpress Solver Applications in Java

Topics covered in this chapter:

In this chapter we illustrate how to create a Java application that uses the Xpress Solver. We use a very simple application that only consists of this simple App.java file:

import static com.dashoptimization.XPRSconstants.PLUSINFINITY;
import static com.dashoptimization.XPRSconstants.NAMES_COLUMN;

import com.dashoptimization.XPRSprob;

/* A very simple Xpress application that solves this optimization problem:
 * <pre>
 *    Minimize
 *      3x1 + 5x2
 *    Subject To
 *      2x1 +  x2 >= 3
 *      2x1 + 2x2 >= 5
 *       x1 + 4x2 >= 4
 *      x1,x2 >= 0
 * </pre>
 * and displays the results.
 * For more detailed examples please see the examples/solver/optimizer/java
 * directory in your Xpress installation.
 */
public class App {
    public static void main(String[] args) {
        try (XPRSprob prob = new XPRSprob("")) {
            prob.loadLp("problem", 2, 3,
                    new char[]{ 'G', 'G', 'G' },                // rowtype
                    new double[] { 3, 5, 4 },                   // rhs
                    null,
                    new double[] { 3, 5 },                      // obj
                    new int[] {0, 3, 6 },                       // start
                    null,
                    new int[] { 0, 1, 2, 0, 1, 2 },             // rowind
                    new double[] { 2, 2, 1, 1, 2, 4 },          // rowcoef
                    new double[] { 0, 0 },                      // lb
                    new double[] { PLUSINFINITY, PLUSINFINITY } // ub
                    );
            prob.addNames(NAMES_COLUMN, new String[] { "x1", "x2"}, 0, 1);
            prob.optimize();
            double[] x = prob.getLpSolX();
            System.out.printf("x1 = %f%n", x[0]);
            System.out.printf("x2 = %f%n", x[1]);
        }
    }
}

This solves the very simple optimization problem \begin{array}{lrcl} \text{minimize} \\ & 3 x_1 + 5 x_2 \\ \text{subject to} \\ & 2 x_1 + x_2 & \geq & 3 \\ & 2 x_1 + 2x_2 & \geq & 5 \\ & x_1 + 4x_2 & \geq & 4 \\ & x_1,\,x_2 & \geq & 0 \end{array} and displays the results.

Note that there are much more convenient ways to create models (see the examples shipped with Xpress). Here we focus on how creating the application binaries and not the application content.

Compiling source files that use the Xpress Solver

In order to compile a Java source file that references the Xpress Solver you have to add the xprs.jar file to the classpath. This is done using the -cp argument for the Java compiler:

javac -cp /xpressmp/lib/xprs.jar App.java

There are also other ways to set the classpath, for example by setting the CLASSPATH environment variables.

Xpress also ships example makefiles in examples/solver/optimizer/java/makefile and examples/solver/nonlinear/java/makefile that illustrate how to compile Java source files that use the Xpress Solver classes. You can do

cd /xpressmp/examples/solver/optimizer/java
make TSP.class

or

cp /xpressmp/examples/solver/optimizer/java/TSP.java .
make -f /xpressmp/examples/solver/optimizer/java/makefile TSP.class

to run these makefiles either in your installation or locally.

Running an application that uses the Xpress Solver

After creating the class files for the application (see the previous section), the application can be run. We must tell the Java runtime environment where to find the Xpress Solver classes and the Solver libraries. This is done by the -cp and -Djava.library.path options respectively:

java -cp /xpressmp/lib/xprs.jar:. -Djava.library.path=/xpressmp/lib App

Notes for Windows:

  • The classpath separator character is ';' (semicolon), not ':' (colon). So we have to adjust the above command appropriately.
  • On Windows the java.library.path is /xpressmp/bin (note the bin instead of lib). Also, on Windows you must set the PATH environment variable to contain /xpressmp/bin since otherwise the Java runtime environment cannot find dependent Xpress libraries.

Setting up Eclipse for Xpress

In order to create an application with the above example file in Eclipse follow these steps:

  1. Create a new Java project called "XpressApplication"
  2. Select your projet and choose "Project -> Properties"
  3. In the dialog that opens select "Java Build Path" and go to the "Libraries" tab.
  4. On the "Libraries" tab click "Add External JARs..." and find and select the /xpressmp/lib/xprs.jar JAR.
  5. Click "Apply and Close"
  6. Add the App.java file listed above to your project.
  7. Create a new run configuration for your project:
    1. Choose "Run -> Run Configurations ..."
    2. In the dialog create a new "Java Application" with the following settings
      • Set the "Name" to something reasonable
      • On the "Main" tab select the App class as main class.
      • On the "Arguments" tab add -Djava.library.path=/xpressmp/bin for Windows and -Djava.library.path=/xpressmp/lib for other platforms
      • On the "Environment" tab add a new variable XPAUTH_PATH that points to your xpauth.xpr file.
      • On Windows also create a PATH variable with value /xpressmp/bin.
      • On the "Dependencies" tab add /xpressmp/lib/xprs.jar as external JAR to the "Classpath Entries" element.
      • Click "Apply".
    3. Execute the run configuration (run your application) by clicking on "Run".

It is also possible to link the Xpress javadoc with your application in Eclipse. This way you will get documentation if you hover over classes, functions, etc. that are defined in the Xpress JAR. In order to link Xpress javadoc perform the following:

  1. Locate xprs.jar in the project explorer.
  2. Right-click and choose "Properties".
  3. Go to "Javadoc location".
  4. Check "Javadoc in archive".
  5. Set the "Archive path" to /xpressmp/lib/xprs-javadoc.jar.
  6. Click "Apply and Close".

If you now hover for example over "XPRSprob" in the source code, you should see a short description of the XPRSprob class.


© 2001-2023 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.