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

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

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

#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL objfun(Integer nvar, const double x[], double *f,
                            Integer *inform, Nag_Comm *comm);
static void NAG_CALL monit(Integer nvar, const double x[], Integer *inform,
                           const double rinfo[], const double stats[],
                           Nag_Comm *comm);
#ifdef __cplusplus
}
#endif

static void NAG_CALL objfun(Integer nvar, const double x[], double *f,
                            Integer *inform, Nag_Comm *comm) {
  *f = pow(x[0] + 10.0 * x[1], 2) + 5.0 * pow(x[2] - x[3], 2) +
       pow(x[1] - 2.0 * x[2], 4) + 10.0 * pow(x[0] - x[3], 4);
}

static void NAG_CALL monit(Integer nvar, const double x[], Integer *inform,
                           const double rinfo[], const double stats[],
                           Nag_Comm *comm) {}

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

  Integer nvar, nnzfd;
  Integer idxfd[4] = {1, 2, 3, 4};
  double x[4] = {3.0, -1.0, 0.0, 1.0};
  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 */
  Nag_Comm comm;
  NagError fail;

  printf("nag_opt_handle_solve_dfno (e04jdc) Example Program Results\n\n");
  fflush(stdout);

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

  /* 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);

  /* 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 (e04jdc)
   * Call the solver
   */
  INIT_FAIL(fail);
  nag_opt_handle_solve_dfno(handle, objfun, monit, nvar, x, rinfo, stats, &comm,
                            &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_handle_solve_dfno (e04jdc).\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;
}