Adding rows to a problem: example lpex3.c
This application adds rows to a model.
This example illustrates how to develop your own solution algorithms
with routines from the Callable Library. It also shows you how to
add rows to a problem object. Here is the problem that lpex3 solves:
| Minimize | c^Tx | |||
| subject to | Hx = d Ax = b l ≤ x ≤ u |
|||
| where | H = | (-1 0 1 0 1 0 0 0) | d = | (-3) |
| (1 -1 0 1 0 0 0 0) | (1) | |||
| (0 1 -1 0 0 1 -1 0) | (4) | |||
| (0 0 0 -1 0 -1 0 1) | (3) | |||
| (0 0 0 0 -1 0 1 -1) | (-5) | |||
| A = | (2 1 -2 -1 2 -1 -2 -3) | b = | (4) | |
| (1 -3 2 3 -1 2 1 1) | (-2) | |||
| c = | (-9 1 4 2 -8 2 8 12) | |||
| l = | (0 0 0 0 0 0 0 0) | |||
| u = | (50 50 50 50 50 50 50 50) |
The constraints Hx=d represent the flow conservation constraints of a pure network flow problem. The example solves this problem in two steps:
The CPLEX network optimizer is used to solve
Minimize c^Tx subject to Hx = d
l ≤ x ≤ u
The constraints Ax=b are added to the problem, and the dual simplex optimizer is used to solve the new problem, starting at the optimal basis of the simpler network problem.
The data for this problem consists of the network portion (using variable names beginning with the letter H) and the complicating constraints (using variable names beginning with the letter A).
The example first calls CPXopenCPLEX to
create the environment and then turns on the CPLEX screen switch (CPX_PARAM_SCRIND ).
Next it sets the simplex display level (CPX_PARAM_SIMDISPLAY )
to 2 to indicate iteration-by-iteration output, so that the progress
of each iteration of the optimizer can be observed. Setting this parameter
to 2 is not generally recommended; the example does so only for illustrative
purposes.
The example creates a problem object by a call to CPXcreateprob.
Then the network data is copied via a call to CPXcopylp.
After the network data is copied, the parameter CPX_PARAM_LPMETHOD is
set to CPX_ALG_NET and the routine CPXlpopt is
called to solve the network part of the optimization problem using
the network optimizer. The objective value of this problem is retrieved
by CPXgetobjval.
Then the extra rows are added by CPXaddrows.
For convenience, the total number of nonzeros in the rows being added
is stored in an extra element of the array rmatbeg ,
and this element is passed for the parameter nzcnt .
The name arguments to CPXaddrows are NULL ,
since no variable or constraint names were defined for this problem.
After the CPXaddrows call,
the parameter CPX_PARAM_LPMETHOD is set to CPX_ALG_DUAL and
the routine CPXlpopt is
called to re-optimize the problem using the dual simplex optimizer.
After re-optimization, CPXsolution accesses
the solution status, the objective value, and the primal solution. NULL is
passed for the other solution values, since the information they provide
is not needed in this example.
At the end, the problem is written as a SAV file by CPXwriteprob.
This file can then be read into the Interactive Optimizer to analyze
whether the problem was correctly generated. Using a SAV file is recommended
over MPS and LP files, as SAV files preserve the full numeric precision
of the problem.
After the TERMINATE: label, CPXfreeprob releases
the problem object, and CPXcloseCPLEX releases
the CPLEX environment.
You can view the complete program online in the standard distribution
of the product at yourCPLEXinstallation/examples/src/lpex3.c.