/* nag_opt_nlp_option_set_file (e04wec) Example Program. * * Copyright 2004 Numerical Algorithms Group. * * Mark 8, 2004. */ #include #include #include #include #include static void confun(Integer *mode, Integer ncnln, Integer n, Integer ldcj, const Integer needc[], const double x[], double ccon[], double cjac[], Integer nstate, Nag_Comm *comm); static void objfun(Integer *mode, Integer n, const double x[], double *objf, double grad[], Integer nstate, Nag_Comm *comm); int main(void) { /* Scalars */ double bndinf, featol, objf; Integer elmode, exit_status, i, j, majits, n, nclin, ncnln, nctotal, pda; Integer pdcj, pdh; /* Arrays */ double *a=0, *bl=0, *bu=0, *ccon=0, *cjac=0, *clamda=0, *grad=0, *hess=0; double *ruser=0, *x=0; Integer *istate=0, *iuser=0; /*Nag Types*/ Nag_E04State state; NagError fail; Nag_Comm comm; Nag_FileID fileid; #define A(I,J) a[(I-1)*pda + J - 1] exit_status = 0; INIT_FAIL(fail); Vprintf("%s", "nag_opt_sparse_nlp_option_set_file (e04wec) Example Program" " Results"); Vprintf("\n"); /* This program demonstrates the use of routines to set and get values of * optional parameters associated with nag_opt_nlp_solve (e04wdc). */ /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld %ld %ld ", &n, &nclin, &ncnln); Vscanf("%*[^\n] "); if (n > 0 && nclin >= 0 && ncnln >= 0) { /* Allocate memory */ nctotal = n + nclin + ncnln; if ( !(a = NAG_ALLOC(ncnln*n, double)) || !(bl = NAG_ALLOC(nctotal, double)) || !(bu = NAG_ALLOC(nctotal, double)) || !(ccon = NAG_ALLOC(ncnln, double)) || !(cjac = NAG_ALLOC(ncnln*n, double)) || !(clamda = NAG_ALLOC(nctotal, double)) || !(grad = NAG_ALLOC(n, double)) || !(hess = NAG_ALLOC(n*n, double)) || !(ruser = NAG_ALLOC(1, double)) || !(x = NAG_ALLOC(n, double)) || !(istate = NAG_ALLOC(nctotal, Integer)) || !(iuser = NAG_ALLOC(1, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } pda = n; pdcj = n; pdh = n; } /* Read A, BL, BU and X from data file */ if (nclin > 0) { for (i = 1; i <= nclin; ++i) { for (j = 1; j <= n; ++j) { Vscanf("%lf", &A(i,j)); } } Vscanf("%*[^\n] "); } for (i = 1; i <= n + nclin + ncnln; ++i) { Vscanf("%lf", &bl[i - 1]); } Vscanf("%*[^\n] "); for (i = 1; i <= n + nclin + ncnln; ++i) { Vscanf("%lf", &bu[i - 1]); } Vscanf("%*[^\n] "); for (i = 1; i <= n; ++i) { Vscanf("%lf", &x[i - 1]); } Vscanf("%*[^\n] "); /* Call nag_opt_nlp_init (e04wcc) to initialise e04wdc. */ /* nag_opt_nlp_init (e04wcc). * Initialization function for nag_opt_nlp_solve (e04wdc) */ nag_opt_nlp_init(&state, &fail); if (fail.code != NE_NOERROR) { Vprintf("Initialisation of nag_opt_nlp_init (e04wcc) failed.\n"); exit_status = 1; goto END; } /* By default nag_opt_nlp_solve (e04wdc) does not print monitoring * information. Call nag_open_file (x04acc) to set the print file fileid. */ /* nag_open_file (x04acc). * Open unit number for reading, writing or appending, and * associate unit with named file */ nag_open_file("", 2, &fileid, &fail); if (fail.code != NE_NOERROR) { Vprintf("Fileid could not be obtained.\n"); exit_status = 1; goto END; } /* Use nag_opt_nlp_option_set_integer (e04wgc) to set the Integer-valued * option 'Print file' */ /* nag_opt_nlp_option_set_integer (e04wgc). * Set a single option for nag_opt_nlp_solve (e04wdc) from * an integer argument */ nag_opt_nlp_option_set_integer("Print file", fileid, &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_nlp_option_set_integer (e04wgc) failed to set Print" " File\n"); exit_status = 1; goto END; } /* Use nag_opt_sparse_nlp_option_set_file (e04wec) to read some options from * the end of the input data file. Call nag_open_file (x04acc) to set the * stdin fileid. */ /* nag_open_file (x04acc), see above. */ nag_open_file("", 0, &fileid, &fail); if (fail.code != NE_NOERROR) { Vprintf("Fileid could not be obtained.\n"); exit_status = 1; goto END; } /* nag_opt_nlp_option_set_file (e04wec). * Supply optional parameter values for nag_opt_nlp_solve * (e04wdc) from external file */ nag_opt_nlp_option_set_file(fileid, &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_sparse_nlp_option_set_file (e04wec) could not read input" " File\n"); exit_status = 1; goto END; } Vprintf("\n"); /* Use nag_opt_nlp_option_get_integer (e04wkc) to find the value of * Integer-valued option 'Elastic mode'. */ /* nag_opt_nlp_option_get_integer (e04wkc). * Get the setting of an integer valued option of * nag_opt_nlp_solve (e04wdc) */ nag_opt_nlp_option_get_integer("Elastic mode", &elmode, &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_nlp_option_get_integer (e04wkc) failed to find the value" " of Elastic Mode\n"); exit_status = 1; goto END; } Vprintf("%1s", ""); Vprintf("Option 'Elastic mode' has the value "); Vprintf("%3ld", elmode); Vprintf("."); Vprintf("\n"); /* Use nag_opt_nlp_option_set_double (e04whc) to set the value of real-valued * option 'Infinite bound size'. */ bndinf = 1e10; /* nag_opt_nlp_option_set_double (e04whc). * Set a single option for nag_opt_nlp_solve (e04wdc) from a * double argument */ nag_opt_nlp_option_set_double("Infinite bound size", bndinf, &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_nlp_option_set_double (e04whc) failed to set Infinite" " bound size\n"); exit_status = 1; goto END; } /* Use nag_opt_nlp_option_get_double (e04wlc) to find the value of real-valued * option 'Feasibility tolerance'. */ /* nag_opt_nlp_option_get_double (e04wlc). * Get the setting of a double valued option of * nag_opt_nlp_solve (e04wdc) */ nag_opt_nlp_option_get_double("Feasibility tolerance", &featol, &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_nlp_option_get_double (e04wlc) failed to find the value" " of a real-valued option\n"); exit_status = 1; goto END; } Vprintf("Option 'Feasibility tolerance' has the value %13.5e.\n", featol); /* Use nag_opt_nlp_option_set_string (e04wfc) to set the option 'Major * iterations limit'. */ /* nag_opt_nlp_option_set_string (e04wfc). * Set a single option for nag_opt_nlp_solve (e04wdc) from a * character string */ nag_opt_nlp_option_set_string("Major iterations limit 50", &state, &fail); if (fail.code != NE_NOERROR) { Vprintf("nag_opt_nlp_option_set_string (e04wfc) failed to set Major" " iterations limit\n"); exit_status = 1; goto END; } /* Solve the problem. */ /* nag_opt_nlp_solve (e04wdc). * Solves the nonlinear programming (NP) problem */ nag_opt_nlp_solve(n, nclin, ncnln, n, n, n, a, bl, bu, confun, objfun, &majits, istate, ccon, cjac, clamda, & objf, grad, hess, x, &state, &comm, &fail); if (fail.code == NE_NOERROR) { Vprintf("\nFinal objective value = %11.3f\n", objf); Vprintf("Optimal X = "); for (i = 1; i <= n; ++i) { Vprintf("%9.2f%s", x[i - 1], i%7 == 0 || i == n ?"\n":" "); } } else { Vprintf ("Error message from nag_opt_nlp_solve (e04wdc) %s\n", fail.message); } END: if (a) NAG_FREE(a); if (bl) NAG_FREE(bl); if (bu) NAG_FREE(bu); if (ccon) NAG_FREE(ccon); if (cjac) NAG_FREE(cjac); if (clamda) NAG_FREE(clamda); if (grad) NAG_FREE(grad); if (hess) NAG_FREE(hess); if (ruser) NAG_FREE(ruser); if (x) NAG_FREE(x); if (istate) NAG_FREE(istate); if (iuser) NAG_FREE(iuser); return exit_status; } static void objfun(Integer *mode, Integer n, const double x[], double *objf, double grad[], Integer nstate, Nag_Comm *comm) { /* Routine to evaluate objective function and its 1st derivatives. */ /* Function Body */ if (*mode == 0 || *mode == 2) { *objf = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]; } if (*mode == 1 || *mode == 2) { grad[0] = x[3] * (x[0] * 2. + x[1] + x[2]); grad[1] = x[0] * x[3]; grad[2] = x[0] * x[3] + 1.; grad[3] = x[0] * (x[0] + x[1] + x[2]); } return; } /* objfun */ static void confun(Integer *mode, Integer ncnln, Integer n, Integer ldcj, const Integer needc[], const double x[], double ccon[], double cjac[], Integer nstate, Nag_Comm *comm) { /* Scalars */ Integer i, j; #define CJAC(I,J) cjac[(I-1)*ldcj + J-1] /* Routine to evaluate the nonlinear constraints and their 1st */ /* derivatives. */ /* Function Body */ if (nstate == 1) { /* First call to CONFUN. Set all Jacobian elements to zero. */ /* Note that this will only work when 'Derivative Level = 3' */ /* (the default; see Section 11.2). */ for (j = 1; j <= n; ++j) { for (i = 1; i <= ncnln; ++i) { CJAC(i, j) = 0.; } } } if (needc[0] > 0) { if (*mode == 0 || *mode == 2) { ccon[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3]; } if (*mode == 1 || *mode == 2) { CJAC(1, 1) = x[0] * 2.; CJAC(1, 2) = x[1] * 2.; CJAC(1, 3) = x[2] * 2.; CJAC(1, 4) = x[3] * 2.; } } if (needc[1] > 0) { if (*mode == 0 || *mode == 2) { ccon[1] = x[0] * x[1] * x[2] * x[3]; } if (*mode == 1 || *mode == 2) { CJAC(2, 1) = x[1] * x[2] * x[3]; CJAC(2, 2) = x[0] * x[2] * x[3]; CJAC(2, 3) = x[0] * x[1] * x[3]; CJAC(2, 4) = x[0] * x[1] * x[2]; } } return; } /* confun */