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

NAG CL Interface Introduction
Example description
/* nag_opt_handle_read_file (e04sac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

int main(void) {

  Integer nvar, nnzu, nnzuc, exit_status;
  Integer i, x_idx;
  Integer *idx = 0;
  Integer verbose_output;
  double *x = 0, *u = 0, *uc = 0;
  double rinfo[100], stats[100];
  char fname[] = "e04sace.opt";
  char ftype[] = "mps", ptype[] = "CONT";
  Integer pinfo[100];
  void *handle = 0;
  /* Nag Types */
  Nag_Comm comm;
  NagError fail;

  exit_status = 0;

  printf("nag_opt_handle_read_file (e04sac) Example Program Results\n\n");
  fflush(stdout);

  /* Read mps file and generate the handle */
  nag_opt_handle_read_file(&handle, fname, ftype, pinfo, NAGERR_DEFAULT);

  /* Allocate memory */
  nvar = pinfo[0];
  nnzu = pinfo[10];
  nnzuc = pinfo[11];
  if (!(x = NAG_ALLOC(nvar, double)) || !(u = NAG_ALLOC(nnzu, double)) ||
      !(uc = NAG_ALLOC(nnzuc, double)) || !(idx = NAG_ALLOC(nvar, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < nvar; i++) {
    x[i] = 0.0;
    idx[i] = i + 1;
  }

  /* Set all variables as continuous */
  nag_opt_handle_set_property(handle, ptype, nvar, idx, NAGERR_DEFAULT);

  /* Set this to 1 to cause nag_opt_handle_solve_socp_ipm (e04ptc)
   * to produce intermediate progress output */
  verbose_output = 0;

  if (verbose_output) {
    /* nag_opt_handle_opt_set (e04zmc) */
    /* Require printing of primal and dual solutions at the end of the solve */
    nag_opt_handle_opt_set(handle, "Print Solution = Yes", NAGERR_DEFAULT);
  } else {
    /* Turn off printing of intermediate progress output */
    nag_opt_handle_opt_set(handle, "Print Level = 1", NAGERR_DEFAULT);
  }

  /* nag_opt_handle_solve_socp_ipm (e04ptc) */
  INIT_FAIL(fail);
  nag_opt_handle_solve_socp_ipm(handle, nvar, x, nnzu, u, nnzuc, uc, rinfo,
                                stats, NULLFN, &comm, &fail);
  if (fail.code != NE_NOERROR && fail.code != NW_NOT_CONVERGED) {
    printf("nag_opt_handle_solve_socp_ipm (e04ptc) failed.\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print solution if optimal or suboptimal solution found */
  printf(" Optimal X:\n");
  printf("  x_idx       Value\n");
  for (x_idx = 0; x_idx < nvar; x_idx++) {
    printf("  %5" NAG_IFMT "   %12.5E\n", x_idx + 1, x[x_idx]);
  }

END:
  NAG_FREE(x);
  NAG_FREE(u);
  NAG_FREE(uc);
  NAG_FREE(idx);
  /* 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;
}