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}; /* Initialize Xpress */ if (XPRSinit(NULL)) { printf("Failed to initialize Xpress.\n"); return -1; } 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)); XPRSgetsolution(prob, NULL, sol, 0, ncol-1); /* 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.
© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.