/* nag_opt_bounds_no_deriv (e04jbc) Example Program * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 7 revised, 2001. * */ #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void objfun(Integer n, double x[], double *f, double g[], Nag_Comm *comm); #ifdef __cplusplus } #endif static void ex1(void); static void ex2(void); #define NMAX 4 int main(void) { /* Two examples are called, ex1() which uses the * default settings to solve the problem and * ex2() which solves the same problem with * some optional parameters set by the user. */ Vprintf("e04jbc Example Program Results.\n"); ex1(); ex2(); return EXIT_SUCCESS; } static void objfun(Integer n, double x[], double *objf, double g[], Nag_Comm *comm) { /* Routine to evaluate objective function. */ double a, b, c, d, x1, x2, x3, x4; x1 = x[0]; x2 = x[1]; x3 = x[2]; x4 = x[3]; /* Supply a single function value */ a = x1 + 10.0*x2; b = x3 - x4; c = x2 - 2.0*x3, c *= c; d = x1 - x4, d *= d; *objf = a*a + 5.0*b*b + c*c + 10.0*d*d; } /* objfun */ static void ex1(void) { double x[NMAX], g[NMAX], bl[NMAX], bu[NMAX]; double objf; Integer n; Nag_BoundType bound; static NagError fail; Vprintf("\ne04jbc example 1: no option setting.\n"); fail.print = TRUE; n = NMAX; x[0] = 3.0; x[1] = -1.0; x[2] = 0.0; x[3] = 1.0; /* Set bounds on variables */ bound = Nag_Bounds; bl[0] = 1.0; bu[0] = 3.0; bl[1] = -2.0; bu[1] = 0.0; /* x[2] is not bounded, so we set bl[2] to a large negative * number and bu[2] to a large positive number */ bl[2] = -1.0e10; bu[2] = 1.0e10; bl[3] = 1.0; bu[3] = 3.0; /* Call optimization routine */ e04jbc(n, objfun, bound, bl, bu, x, &objf, g, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR && fail.code != NW_COND_MIN) exit(EXIT_FAILURE); } /* ex1 */ static void ex2(void) { double x[NMAX], g[NMAX], bl[NMAX], bu[NMAX]; Boolean print; double objf; Integer n; Nag_BoundType bound; Nag_E04_Opt options; static NagError fail, fail2; Vprintf("\n\ne04jbc example 2: using option setting.\n"); fail.print = TRUE; fail2.print = TRUE; n = NMAX; x[0] = 3.0; x[1] = -1.0; x[2] = 0.0; x[3] = 1.0; /* Initialise options structure */ e04xxc(&options); options.optim_tol = 100*sqrt(X02AJC); /* Specify accuracy of solution */ /* Read remaining option values from file */ print = TRUE; e04xyc("e04jbc", "stdin", &options, print, "stdout", &fail); if (fail.code == NE_NOERROR) { /* Set bounds on variables */ bound = Nag_Bounds; bl[0] = 1.0; bu[0] = 3.0; bl[1] = -2.0; bu[1] = 0.0; /* x[2] is not bounded, so we set bl[2] to a large negative * number and bu[2] to a large positive number */ bl[2] = -1.0e10; bu[2] = 1.0e10; bl[3] = 1.0; bu[3] = 3.0; /* Call optimization routine */ e04jbc(n, objfun, bound, bl, bu, x, &objf, g, &options, NAGCOMM_NULL, &fail); /* Free Nag allocated memory */ e04xzc(&options, "all", &fail2); } if ((fail.code != NE_NOERROR && fail.code != NW_COND_MIN) || fail2.code != NE_NOERROR) exit(EXIT_FAILURE); } /* ex2 */