Implementation with Xpress Optimizer
The following C program foliolp.c shows how to input and solve this LP problem with Xpress Optimizer. We have also added printing of the solution. Before trying to access the solution, the LP problem status is checked (see the `Optimizer Reference Manual' for further explanation). To use Xpress Optimizer, we need to include the header file xprs.h.
To load a problem into Xpress Optimizer we need to perform the following steps:
- Initialize Xpress Optimizer.
- Create a new problem.
- Load the matrix data.
#include <stdio.h> #include <stdlib.h> #include "xprs.h" int main(int argc, char **argv) { XPRSprob prob; int s, status; double objval, *sol; /* Problem parameters */ int ncol = 10; int nrow = 3; /* Row data */ char rowtype[] = { 'L','G','E'}; double rhs[] = {1.0/3,0.5, 1}; /* Column data */ double obj[] = { 5, 17, 26, 12, 8, 9, 7, 6, 31, 21}; double lb[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; double ub[] = {0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3}; /* Matrix coefficient data */ int colbeg[] = {0, 2, 5, 8, 11,12,13,14,15, 17, 19}; int rowidx[] = {1,2,0,1,2,0,1,2,0,1,2, 2, 2, 2, 2, 0,2, 0,2}; double matval[] = {1,1,1,1,1,1,1,1,1,1,1, 1, 1, 1, 1, 1,1, 1,1}; XPRSinit(NULL); /* Initialize Xpress Optmizer */ XPRScreateprob(&prob); /* Create a new problem */ /* Load the problem matrix */ XPRSloadlp(prob, "FolioLP", ncol, nrow, rowtype, rhs, NULL, obj, colbeg, NULL, rowidx, matval, lb, ub); XPRSchgobjsense(prob, XPRS_OBJ_MAXIMIZE); /* Set sense to maximization */ XPRSlpoptimize(prob, ""); /* Solve the problem */ XPRSgetintattrib(prob, XPRS_LPSTATUS, &status); /* Get LP sol. status */ if(status == XPRS_LP_OPTIMAL) { XPRSgetdblattrib(prob, XPRS_LPOBJVAL, &objval); /* Get objective value */ printf("Total return: %g\n", objval); sol = (double *)malloc(ncol*sizeof(double)); XPRSgetlpsol(prob, sol, NULL, NULL, NULL); /* Get primal solution */ for(s=0;s<ncol;s++) printf("%d: %g%%\n", s+1, sol[s]*100); } XPRSdestroyprob(prob); /* Delete the problem */ XPRSfree(); /* Terminate Xpress */ return 0; }
Instead of defining colbeg with one extra entry for the last+1 column we may give the numbers of coefficients per column in the array nelem:
/* Matrix coefficient data */ int colbeg[] = {0, 2, 5, 8, 11,12,13,14,15, 17}; int nelem[] = {2, 3, 3, 3, 1, 1, 1, 1, 2, 2}; int rowidx[] = {1,2,0,1,2,0,1,2,0,1,2, 2, 2, 2, 2, 0,2, 0,2}; double matval[] = {1,1,1,1,1,1,1,1,1,1,1, 1, 1, 1, 1, 1,1, 1,1}; ... /* Load the problem matrix */ XPRSloadlp(prob, "FolioLP", ncol, nrow, rowtype, rhs, NULL, obj, colbeg, nelem, rowidx, matval, lb, ub);
The seventh argument of the function XPRSloadlp remains empty for our problem since it is reserved for range information on constraints.
The second argument of the optimization function XPRSlpoptimize indicates the algorithm to be used: an empty string stands for the default LP algorithm. After solving the problem we check whether the LP has been solved and if so, we retrieve the objective function value and the primal solution for the decision variables.