/* nag_opt_bounds_no_deriv (e04jbc) Example Program * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 7 revised, 2001. * Mark 8 revised, 2004. * */ #include #include #include #include #include #include #ifdef E04JBC static void objfun(Integer n, double x[], double *f, double g[], Nag_Comm *comm); #else static void objfun(Integer *mode, Integer n, const double x[], double *objf, double grad[], Integer nstate, Nag_Comm *comm); #endif int main(void) { Integer exit_status=0, n; Integer nclin, ncnln; Integer majits; NagError fail; Nag_BoundType bound; Nag_E04State state; Nag_FileID fileid; double *bl=0, *bu=0, *g=0, objf, *x=0; double *hess, *clamda; Integer i, *istate; Nag_Comm comm; double a[1], ccon[1], cjac[1]; INIT_FAIL(fail); Vprintf("e04jbc Example Program Results.\n"); n = 4; if (n>=1) { if ( !( x = NAG_ALLOC(n, double)) || !( g = NAG_ALLOC(n, double)) || !( bl = NAG_ALLOC(n, double)) || !(clamda = NAG_ALLOC(n, double)) || !(hess = NAG_ALLOC(n*n, double)) || !(istate = NAG_ALLOC(n, Integer))|| !( bu = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } 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 */ #ifdef E04JBC e04jbc(n, objfun, bound, bl, bu, x, &objf, g, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error/Warning from e04jbc.\n%s\n", fail.message); if (fail.code != NW_COND_MIN) exit_status = 1; goto END; } #else e04wcc(&state, &fail); if (fail.code != NE_NOERROR) { Vprintf("Initialisation of e04wcc failed.\n"); exit_status = 1; goto END; } x04acc("", 2, &fileid, &fail); if (fail.code != NE_NOERROR) { Vprintf("Fileid could not be obtained.\n"); exit_status = 1; goto END; } e04wgc("Print file", fileid, &state, &fail); /* Solve the problem. */ nclin=0; ncnln=0; e04wdc(n, nclin, ncnln, n, n, n, a, bl, bu, 0, objfun, &majits, istate, ccon, cjac, clamda, &objf, g, hess, x, &state, &comm, &fail); if (fail.code != NE_NOERROR) { Vprintf ("Error message from e04wdc %s\n", fail.message); } #endif END: if (x) NAG_FREE(x); if (g) NAG_FREE(g); if (bl) NAG_FREE(bl); if (bu) NAG_FREE(bu); if (clamda) NAG_FREE(clamda); if (hess) NAG_FREE(hess); if (istate) NAG_FREE(istate); return exit_status; } #ifdef E04JBC static void objfun(Integer n, double x[], double *objf, double g[], Nag_Comm *comm) #else static void objfun(Integer *mode, Integer n, const double x[], double *objf, double grad[], Integer nstate, Nag_Comm *comm) #endif { /* 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 */