Matrix input
In this chapter we show how to
- initialize Xpress Optimizer,
- load matrices in MPS or LP format into the Optimizer,
- solve a problem, and
- write out the solution to a file.
Matrix files
With Xpress, the user has the choice between two matrix formats: extended MPS and extended LP format, the latter being in general more easily human-readable since constraints are printed in algebraic form. Such matrices may be written out by Xpress Optimizer, but more likely they will have been generated by some other tool.
If the optimization process with Xpress Optimizer is started from within a Mosel or BCL program, then the problem matrix is loaded in memory into the solver without writing it out to a file (which would be expensive in terms of running time). However, both tools may also be used to produce matrix files (see Chapter 9 for matrix generation with Mosel and Chapter 10 for BCL).
Implementation
To load a matrix into Xpress Optimizer we need to perform the following steps:
- Initialize Xpress Optimizer.
- Create a new problem.
- Read the matrix file.
The following C program folioinput.c (similar interfaces exist for Java and C#) shows how to load a matrix file, solve it, and write out the results. For clarity's sake we have omitted all error checking in this program. In general it is recommended to test the return value of the initialization function and also whether the problem has been created and read correctly.
To use Xpress Optimizer, we need to include the header file xprs.h.
#include <stdio.h> #include "xprs.h" int main(int argc, char **argv) { XPRSprob prob; XPRSinit(NULL); /* Initialize Xpress Optimizer */ XPRScreateprob(&prob); /* Create a new problem */ XPRSreadprob(prob, "Folio", ""); /* Read the problem matrix */ XPRSchgobjsense(prob, XPRS_OBJ_MAXIMIZE); /* Set sense to maximization */ XPRSlpoptimize(prob, ""); /* Solve the problem */ XPRSwriteprtsol(prob, "Folio.prt", ""); /* Write results to `Folio.prt' */ XPRSdestroyprob(prob); /* Delete the problem */ XPRSfree(); /* Terminate Xpress */ return 0; }
Compilation and program execution
If you have followed the standard installation procedure of Xpress Optimizer, you may compile this file with the following command under Windows:
cl /MD /I%XPRESSDIR%\include %XPRESSDIR%\lib\xprs.lib folioinput.c
For Linux or Solaris use
cc -D_REENTRANT -I${XPRESSDIR}/include -L${XPRESSDIR}/lib folioinput.c -o folioinput -lxprs
For other systems please refer to the example makefile provided with the corresponding distribution.
If we run this program with the matrix Folio.mat produced by BCL for the LP example problem of Chapter 2, then we obtain an output file Folio.prt with the following contents:
Problem Statistics Matrix FolioLP Objective *OBJ* RHS *RHS* Problem has 4 rows and 10 structural columns Solution Statistics Maximization performed Optimal solution found after 5 iterations Objective function value is 14.066659 Rows Section Number Row At Value Slack Value Dual Value RHS N 1 *OBJ* BS 14.066659 -14.066659 .000000 .000000 E 2 Cap EQ 1.000000 .000000 8.000000 1.000000 G 3 NA LL .500000 .000000 -5.000000 .500000 L 4 Risk UL .333333 .000000 23.000000 .333333 Columns Section Number Column At Value Input Cost Reduced Cost C 5 frac UL .300000 5.000000 2.000000 C 6 frac_1 LL .000000 17.000000 -9.000000 C 7 frac_2 BS .200000 26.000000 .000000 C 8 frac_3 LL .000000 12.000000 -14.000000 C 9 frac_4 BS .066667 8.000000 .000000 C 10 frac_5 UL .300000 9.000000 1.000000 C 11 frac_6 LL .000000 7.000000 -1.000000 C 12 frac_7 LL .000000 6.000000 -2.000000 C 13 frac_8 BS .133333 31.000000 .000000 C 14 frac_9 LL .000000 21.000000 -10.000000
The upper half contains some statistics concerning the problem size and the solution algorithm: the optimal LP solution found has a value of 14.066659. The Rows Section gives detailed solution information for the constraints in the problem. The solution values for the decision variables are located in the column labeled Value of the Columns Section.