/* nag_opt_sparse_mps_read (e04mzc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 7 revised, 2001. * Mark 8 revised, 2004. * */ #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void qphess(Integer ncolh, double x[], double hx[], 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; /* Two examples are called: ex1() uses the * default settings to read an MPSX file and * solve the problem, while ex2() uses the * options structure to obtain the column * and row names. */ Vprintf("nag_opt_sparse_mps_read (e04mzc) Example Program Results.\n"); 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, *ha, iobj, *ka, m, n, ncolh, ninf, nnz; NagError fail; double *a, *bl, *bu, obj, sinf, *xs; INIT_FAIL(fail); Vprintf("\nExample 1: default options used.\n"); /* Read the MPSX file */ /* nag_opt_sparse_mps_read (e04mzc). * Read MPSX data for sparse LP or QP problem from a file */ nag_opt_sparse_mps_read((char*)0, &n, &m, &nnz, &iobj, &a, &ha, &ka, &bl, &bu, &xs, E04_DEFAULT, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_sparse_mps_read (e04mzc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Solve the problem */ ncolh = 5; /* nag_opt_sparse_convex_qp (e04nkc). * Solves sparse linear programming or convex quadratic * programming problems */ nag_opt_sparse_convex_qp(n, m, nnz, iobj, ncolh, qphess, a, ha, ka, bl, bu, xs, &ninf, &sinf, &obj, E04_DEFAULT, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_sparse_convex_qp (e04nkc).\n%s\n", fail.message); exit_status = 1; } /* Free the memory allocated by nag_opt_sparse_mps_read (e04mzc) */ /* nag_opt_sparse_mps_free (e04myc). * Free memory allocated by nag_opt_sparse_mps_read (e04mzc) */ nag_opt_sparse_mps_free(&a, &ha, &ka, &bl, &bu, &xs); END: return exit_status; } static void qphess(Integer ncolh, double x[], double hx[], Nag_Comm *comm) { /* Function to compute H*x. */ hx[0] = 2.0*x[0] + x[1] + x[2] + x[3] + x[4]; hx[1] = x[0] + 2.0*x[1] + x[2] + x[3] + x[4]; hx[2] = x[0] + x[1] + 2.0*x[2] + x[3] + x[4]; hx[3] = x[0] + x[1] + x[2] + 2.0*x[3] + x[4]; hx[4] = x[0] + x[1] + x[2] + x[3] + 2.0*x[4]; } /* qphess */ /* Example 2 */ static int ex2(void) { Integer exit_status=0, *ha, iobj, *ka, m, n, ncolh, ninf, nnz; NagError fail; Nag_E04_Opt options; double *a, *bl, *bu, obj, sinf, *xs; INIT_FAIL(fail); Vprintf("\nExample 2: use of options structure.\n"); /* Initialise the options structure and read MPSX data */ /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); Vstrcpy(options.prob_name, "..QP 2.."); Vstrcpy(options.obj_name, "..COST.."); /* nag_opt_sparse_mps_read (e04mzc), see above. */ nag_opt_sparse_mps_read((char*)0, &n, &m, &nnz, &iobj, &a, &ha, &ka, &bl, &bu, &xs, &options, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_sparse_mps_read (e04mzc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Column and row names are now available via options */ ncolh = 5; /* nag_opt_sparse_convex_qp (e04nkc), see above. */ nag_opt_sparse_convex_qp(n, m, nnz, iobj, ncolh, qphess, a, ha, ka, bl, bu, xs, &ninf, &sinf, &obj, &options, NAGCOMM_NULL, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_opt_sparse_convex_qp (e04nkc).\n%s\n", fail.message); exit_status = 1; } /* Free memory returned by nag_opt_sparse_mps_read (e04mzc) */ /* nag_opt_sparse_mps_free (e04myc), see above. */ nag_opt_sparse_mps_free(&a, &ha, &ka, &bl, &bu, &xs); /* Free memory in options (including column & row names) */ /* 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: return exit_status; }