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

NAG CL Interface Introduction
Example description
/* nag_opt_handle_solve_lp_simplex (e04mkc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

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

int main(void) {

  Integer nclin, nvar, nnza, nnzc, nnzu, exit_status, i;
  Integer idlc, ioflag, liarr;
  Integer *irowa = 0, *icola = 0, *hs = 0;
  Integer iuser[2];
  double *cvec = 0, *a = 0, *bla = 0, *bua = 0, *xl = 0, *xu = 0, *x = 0,
         *u = 0;
  double rinfo[100], stats[100];
  const char *cmdstr = "Basis";
  void *handle = 0;
  /* Nag Types */
  Nag_Comm comm;

  exit_status = 0;

  printf("nag_opt_handle_solve_lp_simplex (e04mkc) Example Program Results\n\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)) ||
      !(hs = NAG_ALLOC(nvar+nclin, 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;
  }

  /* 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]");
  /* Read starting point */
  for (i = 0; i < nvar; i++) {
    scanf("%lf ", &x[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 off option printing */
  nag_opt_handle_opt_set(handle, "Print Options = No",
                         NAGERR_DEFAULT);
  /* Turn off printing of intermediate progress output */
  nag_opt_handle_opt_set(handle, "Print Level = 1", NAGERR_DEFAULT);

  /* nag_opt_handle_solve_lp_simplex (e04mkc)
   * Call LP Simplex algorithm */
  nag_opt_handle_solve_lp_simplex(handle, nvar, x, nnzu, u, rinfo, stats, NULLFN,
                              &comm, NAGERR_DEFAULT);

  /* nag_opt_handle_set_get_integer (e04rwc)
   * Extract Basis information */
  ioflag = 1;
  liarr = nvar + nclin;
  nag_opt_handle_set_get_integer(handle, cmdstr, ioflag, &liarr, hs, 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);
  NAG_FREE(hs);
  /* 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;
}