/* nag_opt_handle_set_get_real (e04rxc) Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.2, 2017.
*/
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nage04.h>
#include <nagx04.h>
#include <assert.h>
#ifdef __cplusplus
extern "C"
{
#endif
static void NAG_CALL monit(void *handle, const double rinfo[],
const double stats[], Nag_Comm *comm,
Integer *inform);
#ifdef __cplusplus
}
#endif
int main(void){
Integer nclin, nvar, nnza, nnzc, nnzu, exit_status, i, idlc;
Integer *irowa = 0, *icola = 0;
Integer iuser[1];
double *cvec = 0, *a = 0, *bla = 0, *bua = 0, *xl = 0, *xu = 0,
*x = 0, *u = 0;
double rinfo[100], stats[100];
void *handle = 0;
/* Nag Types */
Nag_Comm comm;
NagError fail;
exit_status = 0;
printf("nag_opt_handle_set_get_real (e04rxc) Example Program Results\n\n");
fflush(stdout);
/* Read the data file and allocate memory */
scanf(" %*[^\n]"); /* Skip heading in data file */
scanf("%ld %ld %ld %ld %*[^\n]",&nclin,&nvar,&nnza,&nnzc);
/* Allocate memory */
nnzu = 2*nvar + 2*nclin;
if (!(irowa = NAG_ALLOC(nnza, Integer)) ||
!(icola = NAG_ALLOC(nnza, Integer)) ||
!(cvec = NAG_ALLOC(nnzc,double)) ||
!(a = NAG_ALLOC(nnza,double)) ||
!(bla = NAG_ALLOC(nclin,double)) ||
!(bua = NAG_ALLOC(nclin,double)) ||
!(xl = NAG_ALLOC(nvar,double)) ||
!(xu = NAG_ALLOC(nvar,double)) ||
!(x = NAG_ALLOC(nvar,double)) ||
!(u = NAG_ALLOC(nnzu,double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i=0; i< nvar; i++){
x[i] = 0.0;
}
/* Read objective */
for (i=0; i<nnzc; i++){
scanf("%lf",&cvec[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix row indices */
for (i=0; i<nnza; i++){
scanf("%"NAG_IFMT,&irowa[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix col indices */
for (i=0; i<nnza; i++){
scanf("%"NAG_IFMT,&icola[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix values */
for (i=0; i<nnza; i++){
scanf("%lf",&a[i]);
}
scanf("%*[^\n]");
/* Read linear constraints lower bounds */
for (i=0; i<nclin; i++){
scanf("%lf ",&bla[i]);
}
scanf("%*[^\n]");
/* Read linear constraints upper bounds */
for (i=0; i<nclin; i++){
scanf("%lf ",&bua[i]);
}
scanf("%*[^\n]");
/* Read variables lower bounds */
for (i=0; i<nvar; i++){
scanf("%lf ",&xl[i]);
}
scanf("%*[^\n]");
/* Read variables upper bounds */
for (i=0; i<nvar; i++){
scanf("%lf ",&xu[i]);
}
scanf("%*[^\n]");
/* Create the problem handle */
/* nag_opt_handle_init (e04rac).
* Initialize an empty problem handle with NVAR variables. */
nag_opt_handle_init(&handle, nvar, NAGERR_DEFAULT);
/* nag_opt_handle_set_linobj (e04rec)
* Define a linear objective */
nag_opt_handle_set_linobj(handle,nvar,cvec,NAGERR_DEFAULT);
/* nag_opt_handle_set_simplebounds (e04rhc)
* Define bounds on the variables */
nag_opt_handle_set_simplebounds(handle,nvar,xl,xu,NAGERR_DEFAULT);
/* nag_opt_handle_set_linconstr (e04rjc)
* Define linear constraints */
idlc = 0;
nag_opt_handle_set_linconstr(handle,nclin,bla,bua,nnza,irowa,
icola,a,&idlc,NAGERR_DEFAULT);
/* nag_opt_handle_opt_set (e04zmc)
* Require printing of the solution at the end of the solve
*/
nag_opt_handle_opt_set(handle, "Print Solution = Yes",
NAGERR_DEFAULT);
/* Deactivate option printing */
nag_opt_handle_opt_set(handle, "Print Options = No",
NAGERR_DEFAULT);
/* Use a constant number of centrality correctors steps */
nag_opt_handle_opt_set(handle, "LPIPM Centrality Correctors = -6",
NAGERR_DEFAULT);
/* Turn on monitoring */
nag_opt_handle_opt_set(handle, "LPIPM Monitor Frequency = 1",
NAGERR_DEFAULT);
/* Print the solution at the end of the solve */
nag_opt_handle_opt_set(handle, "Print Solution = X",
NAGERR_DEFAULT);
comm.iuser = iuser;
iuser[0] = nvar;
INIT_FAIL(fail);
/* nag_opt_handle_solve_lp_ipm (e04mtc)
* Call LP interior point solver with the primal-dual algorithm */
nag_opt_handle_solve_lp_ipm(handle,nvar,x,nnzu,u,rinfo,stats,monit,
&comm,&fail);
END:
NAG_FREE(cvec);
NAG_FREE(irowa);
NAG_FREE(icola);
NAG_FREE(a);
NAG_FREE(bla);
NAG_FREE(bua);
NAG_FREE(xl);
NAG_FREE(xu);
NAG_FREE(x);
NAG_FREE(u);
/* nag_opt_handle_free (e04rzc).
* Destroy the problem handle and deallocate all the memory. */
if (handle)
nag_opt_handle_free(&handle, NAGERR_DEFAULT);
return exit_status;
}
static void NAG_CALL monit(void *handle, const double rinfo[],
const double stats[], Nag_Comm *comm,
Integer *inform){
/* Monitoring function */
double tol = 1.0e-03;
Integer nvar, i;
double *x = 0;
if (!comm || !comm->iuser){
/* The communication structure is not correctly allocated, abort solve */
*inform = -1;
return;
}
nvar = comm->iuser[0];
x = NAG_ALLOC(nvar,double); assert(x);
/* x is close to the solution, extract the values with
* nag_opt_handle_set_get_real (e04rxc) and print it
*/
if (rinfo[4]<tol && rinfo[5]<tol &&rinfo[6]<tol){
nag_opt_handle_set_get_real(handle,"Primal Variables",1,&nvar,x,
NAGERR_DEFAULT);
printf("\n");
printf(" monit() reports good approximate solution "
"(tol =, %8.2e):\n",tol);
for (i=0; i<nvar; i++){
printf(" X%1ld: %9.2e\n",i+1,x[i]);
}
printf(" end of monit()\n");
}
fflush(stdout);
if (x)
NAG_FREE(x);
}