/* nag_opt_simplex (e04ccc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 6 revised, 2000. * Mark 7 revised, 2001. * Mark 7b revised, 2004. * Mark 8 revised, 2004. */ #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void funct(Integer n, double *xc, double *fc, Nag_Comm *comm); static void monit(const Nag_Search_State *st, Nag_Comm *comm); #ifdef __cplusplus } #endif static int ex1(void); static int ex2(void); int main(void) { Integer exit_status_ex1=0; Integer exit_status_ex2=0; Vprintf("nag_opt_simplex (e04ccc) Example Program Results.\n"); /* 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. */ exit_status_ex1 = ex1(); exit_status_ex2 = ex2(); return exit_status_ex1 == 0 && exit_status_ex2 == 0 ? 0 : 1; } static int ex1(void) { Integer exit_status=0, n; NagError fail; double objf, *x=0; INIT_FAIL(fail); Vprintf("\nnag_opt_simplex (e04ccc) example 1: no option setting.\n"); n = 2; if (n>=1) { if ( !( x = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } /* Set up the starting point */ x[0] = 0.4; x[1] = -0.8; /* nag_opt_simplex (e04ccc). * Unconstrained minimization using simplex algorithm */ nag_opt_simplex(n, funct, x, &objf, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_simplex (e04ccc).\n%s\n", fail.message); exit_status = 1; } END: if (x) NAG_FREE(x); return exit_status; } static void funct(Integer n, double *xc, double *objf, Nag_Comm *comm) { *objf = exp(xc[0]) * (xc[0] * 4.0 * (xc[0] + xc[1]) + xc[1] * 2.0 * (xc[1] + 1.0) + 1.0); } static int ex2(void) { Nag_Boolean print; Integer exit_status=0, monit_freq, n; NagError fail; Nag_Comm comm; Nag_E04_Opt options; double objf, *x=0; INIT_FAIL(fail); Vprintf("\n\nnag_opt_simplex (e04ccc) example 2: using option setting.\n"); n = 2; if (n>=1) { if ( !( x = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } monit_freq = 20; /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); options.print_fun = monit; /* Read remaining option value from file */ print = Nag_TRUE; /* nag_opt_read (e04xyc). * Read options from a text file */ nag_opt_read("e04ccc", "stdin", &options, print, "stdout", &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_read (e04xyc).\n%s\n", fail.message); exit_status = 1; goto END; } comm.p = (Pointer)&monit_freq; /* Starting values */ x[0] = -1.0; x[1] = 1.0; /* nag_opt_simplex (e04ccc), see above. */ nag_opt_simplex(n, funct, x, &objf, &options, &comm, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_simplex (e04ccc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (x) NAG_FREE(x); return exit_status; } static void monit(const Nag_Search_State *st, Nag_Comm *comm) { #define SIM(I,J) sim[((I)-1)*n + (J)-1] double *sim; Integer i, j; Integer n, ncall, iter; double fmin; Integer *monit_freq=(Integer *)comm->p; fmin = st->fmin; sim=st->simplex; ncall = st->nf; iter = st->iter; n = st->n; if (iter % *monit_freq == 0) { Vprintf("\nAfter %1ld iteration and %1ld function calls," " the function value is %10.4e\n", iter, ncall, fmin); Vprintf("The simplex is\n"); for (i = 1; i <= n+1; ++i) { for (j = 1; j <= n; ++j) { Vprintf(" %12.4e", SIM(i,j)); } Vprintf("\n"); } } if (comm->sol_prt) { Vprintf("The final solution is\n"); for (i = 0; i x[i]); Vprintf("After %1ld iterations and %1ld function calls " "the function \nvalue at the current solution point is %12.4e.\n", iter, ncall, fmin); } } /* monit */