Example description
/* nag_opt_handle_solve_dfls_rcomm (e04fgc) Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 *
 * Mark 27.0, 2019.
 */

#include <stdio.h>
#include <nag.h>
#include <assert.h>
#include <math.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;

  /* 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 = NAG_ALLOC(nvar, double); assert(lx);
  ux = NAG_ALLOC(nvar, double); assert(ux);
  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);
  rx = NAG_ALLOC(nres*maxeval, double); assert(rx);
  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;
  }
    
  /* 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);
  if (rx)
    NAG_FREE(rx);
  if (lx)
    NAG_FREE(lx);
  if (ux)
    NAG_FREE(ux);

  return exit_status;
}