/* nag_opt_lin_lsq (e04ncc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. * */ #include #include #include #include static int ex1(void); static int ex2(void); int main(void) { Integer exit_status_ex1=0; Integer exit_status_ex2=0; Vprintf("nag_opt_lin_lsq (e04ncc) Example Program Results.\n"); exit_status_ex1 = ex1(); exit_status_ex2 = ex2(); return exit_status_ex1 == 0 && exit_status_ex2 == 0 ? 0 : 1; } #define A(I,J) a[(I)*tda + J] #define H(I,J) h[(I)*tdh + J] static int ex1(void) { /* Local variables */ Integer exit_status=0, i, j, *kx=0, m, n, nbnd, nclin, tda, tdh; NagError fail; double *a=0, *b=0, *bl=0, *bu=0, *h=0, objf=0.0, *x=0; INIT_FAIL(fail); Vprintf("\nExample 1: default options\n"); Vscanf(" %*[^\n]"); /* Skip heading in data file */ Vscanf(" %*[^\n]"); /* Read problem dimensions */ Vscanf(" %*[^\n]"); Vscanf("%ld%ld%ld%*[^\n]", &m, &n, &nclin); if (m>0 && n>0 && nclin>=0) { nbnd = n + nclin; if ( !( a = NAG_ALLOC(nclin*n, double)) || !( b = NAG_ALLOC(m, double)) || !( bl = NAG_ALLOC(nbnd, double)) || !( bu = NAG_ALLOC(nbnd, double)) || !( h = NAG_ALLOC(m*n, double)) || !( x = NAG_ALLOC(n, double)) || !( kx = NAG_ALLOC(n, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdh = n; } else { Vprintf("Invalid m or n or nclin.\n"); exit_status = 1; return exit_status; } /* Read h, b, a, bl, bu and x from data file */ Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&H(i,j)); Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) Vscanf("%lf",&b[i]); if (nclin > 0) { Vscanf(" %*[^\n]"); for (i = 0; i < nclin; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&A(i,j)); } /* Read lower bounds */ Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf",&bl[i]); /* Read upper bounds */ Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf",&bu[i]); /* Read the initial point x */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf",&x[i]); /* nag_opt_lin_lsq (e04ncc). * Solves linear least-squares and convex quadratic * programming problems (non-sparse) */ nag_opt_lin_lsq(m, n, nclin, a, tda, bl, bu, (double*)0, b, h, tdh, kx, x, &objf, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_lin_lsq (e04ncc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (bl) NAG_FREE(bl); if (bu) NAG_FREE(bu); if (h) NAG_FREE(h); if (x) NAG_FREE(x); if (kx) NAG_FREE(kx); return exit_status; } /* ex1 */ static int ex2(void) { /* Local variables */ Integer exit_status=0, i, j, *kx=0, m, n, nbnd, nclin, tda, tdh; NagError fail; Nag_E04_Opt options; double *a=0, *bl=0, *bu=0, *cvec=0, *h=0, objf, *x=0; INIT_FAIL(fail); Vprintf("\nExample 2: some options are set\n"); Vscanf(" %*[^\n]"); /* Skip heading in data file */ /* Read problem dimensions */ Vscanf(" %*[^\n]"); Vscanf("%ld%ld%ld%*[^\n]", &m, &n, &nclin); if (m>0 && n>0 && nclin>=0) { nbnd = n + nclin; if ( !( a = NAG_ALLOC(nclin*n, double)) || !( bl = NAG_ALLOC(nbnd, double)) || !( bu = NAG_ALLOC(nbnd, double)) || !( cvec = NAG_ALLOC(n, double)) || !( h = NAG_ALLOC(m*n, double)) || !( x = NAG_ALLOC(n, double)) || !( kx = NAG_ALLOC(n, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdh = n; } else { Vprintf("Invalid m or n or nclin.\n"); exit_status = 1; return exit_status; } /* We solve a QP2 type problem in this example */ /* Read cvec, h, a, bl, bu and x from data file */ Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) Vscanf("%lf",&cvec[i]); Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&H(i,j)); if (nclin > 0) { Vscanf(" %*[^\n]"); for (i = 0; i < nclin; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&A(i,j)); } /* Read lower bounds */ Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf",&bl[i]); /* Read upper bounds */ Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf",&bu[i]); /* Read the initial point x */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf",&x[i]); /* Change the problem type */ /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); options.prob = Nag_QP2; /* nag_opt_lin_lsq (e04ncc), see above. */ nag_opt_lin_lsq(m, n, nclin, a, tda, bl, bu, cvec, (double*)0, h, tdh, kx, x, &objf, &options, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_lin_lsq (e04ncc).\n%s\n", fail.message); exit_status = 1; } /* Free options memory */ /* nag_opt_free (e04xzc). * Memory freeing function for use with option setting */ nag_opt_free(&options, "all", &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_free (e04xzc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (a) NAG_FREE(a); if (bl) NAG_FREE(bl); if (bu) NAG_FREE(bu); if (cvec) NAG_FREE(cvec); if (h) NAG_FREE(h); if (x) NAG_FREE(x); if (kx) NAG_FREE(kx); return exit_status; } /* ex2 */