/* nag_opt_bounds_deriv (e04kbc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <string.h>
#include <nag_stdlib.h>
#include <nage04.h>

#ifdef __cplusplus
extern "C"
{
#endif
  static void NAG_CALL objfun(Integer n, const double x[], double *f,
                              double g[], Nag_Comm *comm);
#ifdef __cplusplus
}
#endif

int main(void)
{
  const char *optionsfile = "e04kbce.opt";
  Nag_Boolean print;
  Integer exit_status = 0;
  Integer n;
  Nag_BoundType bound;
  Nag_E04_Opt options;
  double *bl = 0, *bu = 0, *g = 0, objf, *x = 0;
  Nag_Comm comm;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_opt_bounds_deriv (e04kbc) Example Program Results\n");
  fflush(stdout);

  n = 4;
  if (n >= 1) {
    if (!(x = NAG_ALLOC(n, double)) ||
        !(g = NAG_ALLOC(n, double)) ||
        !(bl = NAG_ALLOC(n, double)) || !(bu = NAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }

  x[0] = 3.0;
  x[1] = -0.9;
  x[2] = 0.13;
  x[3] = 1.1;

  /* Initialize options structure and read option values from file */
  print = Nag_TRUE;
  /* nag_opt_init (e04xxc).
   * Initialization function for option setting
   */
  nag_opt_init(&options);
  strcpy(options.outfile, "stdout");
  /* nag_opt_read (e04xyc).
   * Read options from a text file
   */
  nag_opt_read("e04kbc", optionsfile, &options, print, options.outfile,
               &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_read (e04xyc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  bound = Nag_Bounds;
  bl[0] = 1.0;
  bu[0] = 3.0;
  bl[1] = -2.0;
  bu[1] = 0.0;
  /* Third variable is not bounded, so third lower bound
   * is set to a large negative number and third upper
   * bound to a large positive number.
   */
  bl[2] = -1.0e10;
  bu[2] = 1.0e10;
  bl[3] = 1.0;
  bu[3] = 3.0;

  /* nag_opt_bounds_deriv (e04kbc), see above. */
  nag_opt_bounds_deriv(n, objfun, bound, bl, bu, x, &objf,
                       g, &options, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error/Warning from nag_opt_bounds_deriv (e04kbc).\n%s\n",
           fail.message);
    if (fail.code != NW_COND_MIN)
      exit_status = 1;
  }

  /* Free memory allocated by nag_opt_bounds_deriv (e04kbc) to pointers hesd,
   * hesl and state.
   */
  /* nag_opt_free (e04xzc).
   * Memory freeing function for use with option setting
   */
  nag_opt_free(&options, "all", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_opt_free (e04xzc).\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

END:
  NAG_FREE(x);
  NAG_FREE(g);
  NAG_FREE(bl);
  NAG_FREE(bu);

  return exit_status;
}

static void NAG_CALL objfun(Integer n, const double x[], double *objf,
                            double g[], Nag_Comm *comm)
{
  /* Routine to evaluate objective function. */

  double x1, x2, x3, x4;
  double tmp, tmp1, tmp2, tmp3, tmp4;

  x1 = x[0];
  x2 = x[1];
  x3 = x[2];
  x4 = x[3];

  /* Supply a single function value */
  tmp1 = x1 + 10.0 * x2;
  tmp2 = x3 - x4;
  tmp3 = x2 - 2.0 * x3, tmp3 *= tmp3;
  tmp4 = x1 - x4, tmp4 *= tmp4;
  *objf = tmp1 * tmp1 + 5.0 * tmp2 * tmp2 + tmp3 * tmp3 + 10.0 * tmp4 * tmp4;

  if (comm->flag != 0) {
    tmp = x1 - x4;
    g[0] = 2.0 * (x1 + 10.0 * x2) + 40.0 * tmp * tmp * tmp;
    tmp = x2 - 2.0 * x3;
    g[1] = 20.0 * (x1 + 10.0 * x2) + 4.0 * tmp * tmp * tmp;
    tmp = x2 - 2.0 * x3;
    g[2] = 10.0 * (x3 - x4) - 8.0 * tmp * tmp * tmp;
    tmp = x1 - x4;
    g[3] = 10.0 * (x4 - x3) - 40.0 * tmp * tmp * tmp;
  }
} /* objfun */