NAG Library Manual, Mark 31.1
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_opt_handle_write_file (e04sbc) Example Program.
 *
 * Copyright 2025 Numerical Algorithm Group.
 *
 * Mark 31.1
 */

#include <stdio.h>
#include <nag.h>

int main(void){

  Integer nclin, nvar, nnza, nnzc, nnzu, exit_status, i;
  Integer idlc;
  Integer *irowa = 0, *icola = 0;
  Integer iuser[2], pinfo[100];
  double *cvec = 0, *a = 0, *bla = 0, *bua = 0, *xl = 0, *xu = 0,
         *x = 0, *u = 0;
  double rinfo[100], stats[100];
  char fname[] = "data.dat";
  char ftype[] = "nagbin";
  void *handle = 0, *rehandle = 0;
  /* Nag Types */
  Nag_Comm comm;

  exit_status = 0;

  printf("nag_opt_handle_write_file (e04sbc) Example Program Results\n\n");
  printf("Setting up original handle...\n");
  fflush(stdout);

    /* Read the data file and allocate memory */
scanf(" %*[^\n]"); /* Skip heading in data file */
scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %*[^\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);

  /* Turn on monitoring */
  nag_opt_handle_opt_set(handle, "LPIPM Monitor Frequency = 1",
                         NAGERR_DEFAULT);

  comm.iuser = iuser;
  iuser[0] = 1;

  printf("Creating a Nag Binary data file to represent the original handle\n");
  fflush(stdout);
  
  /* nag_opt_handle_write_file (e04sbc) */
  nag_opt_handle_write_file(handle, fname, ftype, NAGERR_DEFAULT);

  /* nag_opt_handle_free (e04rzc).
   * Destroy the problem handle and deallocate all the memory. */
  if (handle)
    nag_opt_handle_free(&handle, NAGERR_DEFAULT);

  /* Create a handle from the data file
   * nag_opt_handle_read_file (e04sac)  */
  printf("\nCreate new handle from data file\n");
  nag_opt_handle_read_file(&rehandle, fname, ftype, pinfo, NAGERR_DEFAULT);

  /* Solve recreated handle using e04mtc*/
  printf("Solve new handle...\n");
  fflush(stdout);
  
  comm.iuser = iuser;
  iuser[0] = 1;
  nag_opt_handle_solve_lp_ipm(rehandle, nvar, x, nnzu, u, rinfo, stats, NULLFN,
                              &comm, NAGERR_DEFAULT);

  /* nag_opt_handle_free (e04rzc).
   * Destroy the new problem handle and deallocate all the memory. */
  if (rehandle)
    nag_opt_handle_free(&rehandle, NAGERR_DEFAULT);
                        
 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);
  
  return exit_status;
}