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

NAG CL Interface Introduction
Example description
/* nag_glopt_handle_solve_mcs (e05kbc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.3, 2023.
 */

#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) {
  
  /* Compute the objective function value*/
  
  *f = 3.0 * pow(1.0 - x[0], 2) * exp(-pow(x[0], 2) -
       pow(x[1] + 1.0, 2)) - 10.0 * (x[0] / 5.0 -
       pow(x[0], 3) - pow(x[1], 5)) * exp(-pow(x[0], 2) -
       pow(x[1], 2)) - 1.0 / 3.0 * exp(-pow(x[0] + 1.0, 2) -
       pow(x[1], 2));
}

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

int main(void) {

  Integer nvar, nnzfd;
  Integer idxfd[2] = {1, 2};
  double x[2];
  double rinfo[100], stats[100];
  double ux[2] = {3.0, 3.0};
  double lx[2] = {-3.0, -3.0};
  void *handle;
  Integer exit_status = 0;

  /* Nag Types */
  Nag_Comm comm;
  NagError fail;

  printf("nag_glopt_handle_solve_mcs (e05kbc) Example Program Results\n\n");
  fflush(stdout);

  /* Fill the problem data structure */
  nvar = 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
   */
  /* Reduce the log details */
  nag_opt_handle_opt_set(handle, "Print Level = 1", 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_glopt_handle_solve_mcs (e05kbc)
   * Call the solver
   */
  INIT_FAIL(fail);
  nag_glopt_handle_solve_mcs(handle, objfun, monit, nvar, x, rinfo, stats,
                             &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_glopt_handle_solve_mcs (e05kbc).\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;
}