ILOG CPLEX 11.0 Getting Started > Tutorials > Callable Library Tutorial > Building and Solving a Small LP Model in C |
Building and Solving a Small LP Model in C |
INDEX PREVIOUS NEXT |
The example lpex1.c
shows you how to use problem modification routines from the ILOG CPLEX Callable Library in three different ways to build a model. The application in the example takes a single command line argument that indicates whether to build the constraint matrix by rows, columns, or nonzeros. After building the problem, the application optimizes it and displays the solution. Here is the problem that the example optimizes:
Maximize |
x1 + 2x2 + 3x3 |
subject to |
-x1 + x2 + x3 20
x1 - 3x2 + x3 30 |
with these bounds |
0 x1 40
0 x2 +
0 x3 + |
Before any ILOG CPLEX Callable Library routine can be called, your application must call the routine CPXopenCPLEX
to get a pointer (called env
) to the ILOG CPLEX environment. Your application will then pass this pointer to every Callable Library routine. If this routine fails, it returns an error code. This error code can be translated to a string by the routine CPXgeterrorstring
.
After the ILOG CPLEX environment is initialized, the ILOG CPLEX screen indicator parameter (CPX_PARAM_SCRIND
) is turned on by the routine CPXsetintparam
. This causes all default ILOG CPLEX output to appear on the screen. If this parameter is not set, then ILOG CPLEX will generate no viewable output on the screen or in a file.
At this point, the routine CPXcreateprob
is called to create an empty problem object. Based on the problem-building method selected by the command-line argument, the application then calls a routine to build the matrix by rows, by columns, or by nonzeros. The routine populatebyrow
first calls CPXnewcols
to specify the column-based problem data, such as the objective, bounds, and variables names. The routine CPXaddrows
is then called to supply the constraints. The routine populatebycolumn
first calls CPXnewrows
to specify the row-based problem data, such as the righthand side values and sense of constraints. The routine CPXaddcols
is then called to supply the columns of the matrix and the associated column bounds, names, and objective coefficients. The routine populatebynonzero
calls both CPXnewrows
and CPXnewcols
to supply all the problem data except the actual constraint matrix. At this point, the rows and columns are well defined, but the constraint matrix remains empty. The routine CPXchgcoeflist
is then called to fill in the nonzero entries in the matrix.
After the problem has been specified, the application optimizes it by calling the routine CPXlpopt
. Its default behavior is to use the ILOG CPLEX Dual Simplex Optimizer. If this routine returns a nonzero result, then an error occurred. If no error occurred, the application allocates arrays for solution values of the primal variables, dual variables, slack variables, and reduced costs; then it obtains the solution information by calling the routine CPXsolution
. This routine returns the status of the problem (whether optimal, infeasible, or unbounded, and whether a time limit or iteration limit was reached), the objective value and the solution vectors. The application then displays this information on the screen.
As a debugging aid, the application writes the problem to a ILOG CPLEX LP file (named lpex1.lp
) by calling the routine CPXwriteprob
. This file can be examined to detect whether any errors occurred in the routines creating the problem. CPXwriteprob
can be called at any time after CPXcreateprob
has created the lp
pointer.
The label TERMINATE
: is used as a place for the program to exit if any type of failure occurs, or if everything succeeds. In either case, the problem object represented by lp
is released by the call to CPXfreeprob
, and any memory allocated for solution arrays is freed. The application then calls CPXcloseCPLEX
; it tells ILOG CPLEX that all calls to the Callable Library are complete. If an error occurs when this routine is called, then a call to CPXgeterrorstring
is needed to retrieve the error message, since CPXcloseCPLEX
causes no screen output.
You can view the complete program online in the standard distribution of the product at yourCPLEXinstallation/examples/src/lpex1.c
.
Copyright © 1987-2007 ILOG S.A. All rights reserved. Legal terms. | PREVIOUS NEXT |