> Tutorials > Concert Technology Tutorial for Java Users > Building and Solving a Small LP Model in Java |
Building and Solving a Small LP Model in Java |
INDEX
![]() |
The example LPex1.java
, part of the standard distribution of ILOG CPLEX, is a program that builds a specific small LP model and then solves it. This example follows the general structure found in many ILOG CPLEX Concert Technology applications, and demonstrates three main ways to construct a model:
Example LPex1.java
is an extension of the example presented in Entering the Example Problem:
Maximize |
x1 + 2x2 + 3x3 |
subject to |
-x1 + x2 + x3 ![]()
x1 - 3x2 + x3 ![]() |
with these bounds |
0 ![]() ![]()
0 ![]() ![]() ![]()
0 ![]() ![]() ![]() |
After an initial check that a valid option string was provided as a calling argument, the program begins by enclosing all executable statements that follow in a try/catch
pair of statements. In case of an error ILOG CPLEX Concert Technology will throw an exception of type IloException
, which the catch statement then processes. In this simple example, an exception triggers the printing of a line stating Concert exception `e' caught
, where `e' is the specific exception.
First create the model object cplex
by executing the following statement:
IloCplex cplex = new IloCplex();
At this point, the cplex object represents an empty model, that is a model with no variables, constraints or other content. The model is then populated in one of several ways depending on the command line argument. The possible choices are implemented in the methods
All these methods pass the same three arguments. The first argument is the cplex
object to be populated. The second and third arguments correspond to the variables (var
) and range constraints (rng
) respectively; the methods will write to var[0]
and rng[0]
an array of all the variables and constraints in the model, for later access.
After the model has been created in the cplex
object, it is ready to be solved by calling cplex.solve
. The solution log will be output to the screen; this is because IloCplex
prints all logging information to the OutputStream cplex.out
, which by default is initialized to System.out
. You can change this by calling the method cplex.setOut
. In particular, you can turn off logging by setting the output stream to null
, that is, by calling cplex.setOut(null)
. Similarly, IloCplex
issues warning messages to cplex.warning
, and cplex.setWarning
can be used to change (or turn off) the OutputStream
that will be used.
If the solve
method finds a solution for the active model, it returns true
. The next section of code accesses the solution. The method cplex.getValues(var[0])
returns an array of primal solution values for all the variables. This array is stored as double[] x
. The values in x
are ordered such that x[j]
is the primal solution value for variable var[0][j]
. Similarly, the reduced costs, duals, and slack values are queried and stored in arrays dj
, pi
, and slack
, respectively. Finally, the solution status of the active model and the objective value of the solution are queried with the methods IloCplex.getStatus
and IloCplex.getObjValue
, respectively. The program then concludes by printing the values that have been obtained in the previous steps, and terminates after calling cplex.end
to free the memory used by the model object; the catch
method of IloException
provides screen output in case of any error conditions along the way.
The remainder of the example source code is devoted to the details of populating the model object, mentioned above, and the following three sections provide details on how the methods work.
Copyright © 1987-2003 ILOG, S.A. All rights reserved. Legal terms. | PREVIOUS NEXT |