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

NAG CL Interface Introduction
Example description
/* nag_opt_handle_solve_dfls_rcomm (e04fgc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

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

int main(void) {
  const double infbnd = 1.0e20;

  Integer nvar, nres, isparse, nnzrd, irevcm, neval, maxeval, i;
  double x[4] = {-1.2, 1.0, 0.0, 0.0};
  double rinfo[100], stats[100];
  double *rx, *lx, *ux;
  void *handle;
  Integer exit_status = 0;

  /* Nag Types */
  NagError fail;

  printf("nag_opt_handle_solve_dfls_rcomm (e04fgc) "
         "Example Program Results\n\n");
  fflush(stdout);

  /* Fill the problem data structure */
  nvar = 2;
  nres = 2;
  maxeval = 2;

  /* Allocate memory */
  if (!(lx = NAG_ALLOC(nvar, double)) || !(ux = NAG_ALLOC(nvar, double)) ||
      !(rx = NAG_ALLOC(nres * maxeval, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_opt_handle_init (e04rac).
   * Initialize the handle
   */
  nag_opt_handle_init(&handle, nvar, NAGERR_DEFAULT);

  /* nag_opt_handle_set_nlnls (e04rmc)
   * Define residuals structure, isparse=0 means the residual structure is
   * dense => irowrd and icolrd arguments can be NULL
   */
  isparse = 0;
  nnzrd = 1;
  nag_opt_handle_set_nlnls(handle, nres, isparse, nnzrd, NULL, NULL,
                           NAGERR_DEFAULT);

  /* nag_opt_handle_opt_set (e04zmc)
   * Set options
   */
  /* Relax the main convergence criteria a bit */
  nag_opt_handle_opt_set(handle, "DFO Trust Region Tolerance = 5.0e-6",
                         NAGERR_DEFAULT);
  /* Print the solution */
  nag_opt_handle_opt_set(handle, "Print Solution = YES", NAGERR_DEFAULT);

  /* Define bounds for the variables */
  lx[0] = -1.5;
  ux[0] = 2.0;
  lx[1] = -2.0;
  ux[1] = infbnd;
  /* nag_opt_handle_set_simplebounds (e04rhc) */
  nag_opt_handle_set_simplebounds(handle, nvar, lx, ux, NAGERR_DEFAULT);

  /* nag_opt_handle_solve_dfls_rcomm (e04fgc)
   * Call the solver
   */
  INIT_FAIL(fail);
  irevcm = 1;
  while (irevcm != 0) {
    nag_opt_handle_solve_dfls_rcomm(handle, &irevcm, &neval, maxeval, nvar, x,
                                    nres, rx, rinfo, stats, &fail);
    if (fail.code != NE_NOERROR)
      break;
    if (irevcm == 1) {
      /* Compute the Rosenbrock function on the requested points */
      for (i = 0; i < neval; i++) {
        rx[i * nres] = 1.0 - x[i * nvar];
        rx[i * nres + 1] = 10.0 * (x[i * nvar + 1] - pow(x[i * nvar], 2));
      }
    }
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_handle_solve_dfls_rcomm (e04fgc).\n%s\n",
           fail.message);
    exit_status = 1;
  }

END:
  /* Clean data */
  if (handle)
    /* nag_opt_handle_free (e04rzc).
     * Destroy the problem handle and deallocate all the memory used
     */
    nag_opt_handle_free(&handle, NAGERR_DEFAULT);
  NAG_FREE(rx);
  NAG_FREE(lx);
  NAG_FREE(ux);

  return exit_status;
}