QSopt QSget_solution QSopt > Callable Library > Function List
  QSopt
  Downloads
  LP Info
  Software
  Problem Formats
  Callable Library
  Overview
Function List
  Rational Solver
  Beta
  Contact Info
Purpose
Copy various solution data into arrays.
Synopsis
int QSget_solution (QSprob p, double *value, double *x, double *pi,
    double *slack, double *rc)
Arguments
p a handle to an initialized problem.
value returns the value of the objective function (this field can be NULL).
x returns the solution vector as an array (this field can be NULL, if it is not NULL then it should point to an array of length at least ncols, the number of columns in the problem).
pi returns the values of the dual variables as an array (this field can be NULL, if it is not NULL then it should point to an array of length at least nrows, the number of rows in the problem).
slack returns the values of the slack variables associated with the constraints (this field can be NULL, if it is not NULL then it should point to an array of length at least nrows).
rc returns the reduced costs of the variables (this field can be NULL, if it is not NULL then it should point to an array of length at least ncols).
Returns
A zero value if the function terminated correctly, and a non-zero value if an error occurred. Note that QSget_solution returns an error code if it is called with a specified problem that has not been solved with one of the optimization functions ( QSopt_dual or QSopt_primal) since the last time the problem was loaded or modified.
Description

This function allows the user to obtain different components of the solution in one call (if some component is not needed, the field can be given as NULL). To obtain individual components, it is easier to use the specialized functions listed in the Accessing an LP Solution section in the Callable Function List.

Example
/* p is an initialized QSprob, a handle to an existing LP problem  */
/* Assume that p was solved with QSopt_dual() or QSopt_primal, and */
/* that the status code indicates that an optimal solution was     */
/* obtained.  We will grab and print all solution components.      */

int rval, ncols, nrows, i, j;
double value;
double *x, *pi, *slack, *rc;
char **colnames, **rownames;

ncols = QSget_colcount (p);
nrows = QSget_rowcount (p);

/* Get the variable names and the constraint names, for printing */

colnames = (char **) malloc (ncols * sizeof (char *));
rownames = (char **) malloc (nrows * sizeof (char *));
rval = QSget_colnames (p, colnames);
if (rval) {
    fprintf (stderr, 
             "Could not get column names, error code %d\n", rval);
    /* free the memory for colnames and rownames                 */
    /* return an error code                                      */
}
rval = QSget_rownames (p, rownames);
if (rval) {
    fprintf (stderr, 
             "Could not get row names, error code %d\n", rval);
    /* free the memory for colnames and rownames                 */
    /* return an error code                                      */
}

/* Allocate memory for the solution components.                  */

x  = (double *) malloc (ncols * sizeof (double));
rc = (double *) malloc (ncols * sizeof (double));
pi = (double *) malloc (nrows * sizeof (double));
slack = (double *) malloc (nrows * sizeof (double));

rval = QSget_solution (p, &value, x, pi, slack, rc);
if (rval) {
    fprintf (stderr, 
             "Could not get solution, error code %d\n", rval);
    /* free the memory for x, rc, pi, slack, colnames, rownames  */
    /* return an error code                                      */
}

printf ("Objective Value = %.6f\n", value);
for (j = 0; j < ncols; j++) {
    printf ("%s = %.6f, reduced cost = %.6f\n", 
            colnames[j], x[j], rc[j]);
}
for (i = 0; i < nrows; i++) {
    printf ("dual %s = %.6f, slack = %.6f\n", 
            rownames[i], pi[i], slack[i]);
}

/* Free the memory, including the individual names of cols and rows */

free (x);  free (rc);  free (pi);  free (slack);
for (j = 0; j < ncols; j++) {
    /* Use QSfree for memory from QSopt functs  */
    QSfree (colnames[j]);
}
free (colnames);
for (i = 0; i < nrows; i++) {
    /* Use QSfree for memory from QSopt functs  */
    QSfree (rownames[i]);   
}
free (rownames);
 
QSopt | Problem Formats | Downloads Back
Last Updated: November 2003