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

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

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

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

  Integer nvar, nnzfd, irevcm, neval, maxeval, i;
  Integer idxfd[4] = {1, 2, 3, 4};
  double x[8], f[2];
  double rinfo[100], stats[100];
  double ux[4] = {3.0, 0.0, infbnd, 3.0};
  double lx[4] = {1.0, -2.0, -infbnd, 1.0};
  void *handle;
  Integer exit_status = 0;

  /* Nag Types */
  NagError fail;

  printf("nag_opt_handle_solve_dfno_rcomm (e04jec) "
         "Example Program Results\n\n");
  fflush(stdout);

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

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

  /* nag_opt_handle_set_nlnobj (e04rgc)
   * Define nonlinear objective
   */
  nnzfd = nvar;
  nag_opt_handle_set_nlnobj(handle, nnzfd, idxfd, 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);
  /* Set starting trust region (default was 0.1) */
  nag_opt_handle_opt_set(handle, "DFO Starting trust Region = 0.2",
                         NAGERR_DEFAULT);

  /* Optionally define bounds for the variables
   * nag_opt_handle_set_simplebounds (e04rhc)
   */
  nag_opt_handle_set_simplebounds(handle, nvar, lx, ux, NAGERR_DEFAULT);

  /* nag_opt_handle_solve_dfno_rcomm (e04jec)
   * Call the solver
   */
  INIT_FAIL(fail);
  x[0] = 3.0;
  x[1] = -1.0;
  x[2] = 0.0;
  x[3] = 1.0;
  irevcm = 1;
  while (irevcm != 0) {
    nag_opt_handle_solve_dfno_rcomm(handle, &irevcm, &neval, maxeval, nvar, x,
                                    f, rinfo, stats, &fail);
    if (fail.code != NE_NOERROR)
      break;
    if (irevcm == 1) {
      /* Compute the objective function on the requested points */
      for (i = 0; i < neval; i++) {
        f[i] = pow(x[i * nvar + 0] + 10.0 * x[i * nvar + 1], 2) +
               5.0 * pow(x[i * nvar + 2] - x[i * nvar + 3], 2) +
               pow(x[i * nvar + 1] - 2.0 * x[i * nvar + 2], 4) +
               10.0 * pow(x[i * nvar + 0] - x[i * nvar + 3], 4);
      }
    }
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_handle_solve_dfno_rcomm (e04jec).\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);

  return exit_status;
}