get_primal_solution, get_ptr_primal_solution, get_var_primalresult

Returns the solution of the model.

unsigned char get_primal_solution(lprec *lp, REAL *pv);

unsigned char get_ptr_primal_solution(lprec *lp, REAL **ptr_pv);

REAL get_var_primalresult(lprec *lp, int index);

Return Value

get_primal_solution, get_ptr_primal_solution return TRUE (1) if the operation was successful. A return value of FALSE (0) indicates an error.
get_var_primalresult returns the value for index.

Parameters

lp

Pointer to previously created lp model. See return value of make_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI

pv

An array that will contain the value of the objective function (element 0), values of the constraints (elements 1 till Nrows), and the values of the variables (elements Nrows+1 till Nrows+NColumns).

ptr_pv

The address of a pointer that will point to an array that will contain the value of the objective function (element 0), values of the constraints (elements 1 till Nrows), and the values of the variables (elements Nrows+1 till Nrows+NColumns).

index

index of the constraint/variable.

Remarks

The get_primal_solution, get_ptr_primal_solution, get_var_primalresult functions retrieve the values of the objective function, constraints and variables.
These values are only valid after a successful solve or lag_solve. Function get_primal_solution needs an array that is already dimensioned with 1 + get_Nrows + get_Ncolumns elements. get_ptr_primal_solution returns a pointer to an array already dimensioned by lp_solve. Element 0 is the value of the objective function, elements 1 till Nrows the values of the constraints and elements Nrows+1 till Nrows+NColumns the values of the variables. get_var_primalresult requires no array. index is the array element number of the above functions and returns the value for this array element.
Special considerations when presolve was done. When set_presolve is called before solve, then presolve can have deleted both rows and columns from the model because they could be eliminated. This influences get_primal_solution and get_ptr_primal_solution. These functions only report the values of the remaining variables and constraints. However get_var_primalresult returns all values, also the deleted ones by presolve. So index is the original index number as known by the caller.

Note that get_ptr_primal_solution returns a pointer to memory allocated and maintained by lp_solve. Be careful what you do with it. Don't modify its contents or free the memory. Unexpected behaviour would occur. Also note that this memory pointer is only guaranteed to remain constant until a next lp_solve API call is done. You should call this function again to make sure you have again the correct pointer. Otherwise, this pointer could point to invalid memory. This should not be a problem since this call is very efficient.

Example

#include <stdio.h>
#include <stdlib.h>
#include "lp_lib.h"

int main(void)
{
  lprec *lp;
  REAL pv[1+2+3], *ptr_pv;

  /* Create a new LP model */
  lp = make_lp(3, 2);
  if(lp == NULL) {
    fprintf(stderr, "Unable to create new LP model\n");
    return(1);
  }

  solve(lp);

  get_primal_solution(lp, pv);
  get_ptr_primal_solution(lp, &ptr_pv);

  delete_lp(lp);
  return(0);
}

lp_solve API reference

See Also make_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI, is_feasible, get_objective, get_working_objective, get_variables, get_ptr_variables, get_constraints, get_ptr_constraints, get_sensitivity_rhs, get_ptr_sensitivity_rhs, get_dual_solution, get_ptr_dual_solution, get_var_dualresult, get_sensitivity_obj, get_ptr_sensitivity_obj, get_sensitivity_objex, get_ptr_sensitivity_objex