/* nag_ip_bb (h02bbc) Example Program * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 6 revised, 2000. * Mark 7 revised, 2001. * */ #include #include #include #include #include static void ex1(void); static void ex2(void); #ifdef __cplusplus extern "C" { #endif static void qphess(Integer n, Integer jthcol, double h[], Integer tdh, double x[], double hx[], Nag_Comm *comm); #ifdef __cplusplus } #endif #define MAXN 10 #define MAXM 7 #define MAXBND MAXN+MAXM int main(void) { /* Two examples are called, ex1() uses the * default settings to solve a problem while * ex2() solves another problem with some * of the optional parameters set by the user. */ Vprintf("h02bbc Example Program Results.\n"); ex1(); ex2(); return EXIT_SUCCESS; } static void ex1(void) { /* Local variables */ double a[MAXM][MAXN], cvec[MAXN], bl[MAXBND], bu[MAXBND]; double x[MAXN]; double objf; Integer i, j, is_int; Integer m, n, nbnd, tda; Boolean intvar[MAXN]; static NagError fail; Vprintf("\nExample 1: default options used.\n"); Vscanf(" %*[^\n]"); /* Skip headings in data file */ Vscanf(" %*[^\n]"); fail.print = TRUE; tda = MAXN; /* Read the problem dimensions */ Vscanf(" %*[^\n]"); Vscanf("%ld%ld", &m, &n); /* Read objective coefficients */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf", &cvec[i]); /* Read the matrix coefficients */ Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&a[i][j]); /* Read the bounds */ nbnd = n+m; Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf", &bl[i]); Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf", &bu[i]); /* Read which variables are integer */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) { Vscanf("%ld", &is_int); /* is_int = 1 if integer variable, 0 if not */ intvar[i] = is_int ? TRUE : FALSE; } /* Read the initial estimate of x */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf", &x[i]); h02bbc(n, m, &a[0][0], tda, bl, bu, intvar, cvec, (double *)0, 0, NULLFN, x, &objf, H02_DEFAULT, NAGCOMM_NULL, &fail); } /* ex1 */ static void qphess(Integer n, Integer jthcol, double h[], Integer tdh, double x[], double hx[], Nag_Comm *comm) { Integer i; /* In this qphess function the Hessian is defined implicitly */ for (i = 0; i < n; ++i) hx[i] = 2.0*x[i]; } /* qphess */ static void ex2(void) { /* Local variables */ double a[MAXM][MAXN], cvec[MAXN], bl[MAXBND], bu[MAXBND]; double x[MAXN]; double objf, red_bnd; Integer i, j, is_int; Integer m, n, nbnd, tda; char *crnames[MAXBND]; char names[9*MAXBND]; Boolean intvar[MAXN], intvar2[MAXN]; static NagError fail; Nag_H02_Opt options; Vprintf("\nExample 2: some options set.\n"); Vscanf(" %*[^\n]"); /* Skip heading */ fail.print = TRUE; tda = MAXN; /* Read the problem dimensions */ Vscanf(" %*[^\n]"); Vscanf("%ld%ld", &m, &n); /* Read names */ Vscanf(" %*[^\n]"); nbnd = n+m; for (i = 0; i < nbnd; ++i) { Vscanf("%s", &names[9*i]); crnames[i] = &names[9*i]; } /* Read objective coefficients */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf", &cvec[i]); /* Read the matrix coefficients */ Vscanf(" %*[^\n]"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) Vscanf("%lf",&a[i][j]); /* Read the bounds */ Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf", &bl[i]); Vscanf(" %*[^\n]"); for (i = 0; i < nbnd; ++i) Vscanf("%lf", &bu[i]); /* Read which variables are integer */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) { Vscanf("%ld", &is_int); /* is_int = 1 if integer variable, 0 if not */ intvar[i] = is_int ? TRUE : FALSE; } /* Read the initial estimate of x */ Vscanf(" %*[^\n]"); for (i = 0; i < n; ++i) Vscanf("%lf", &x[i]); h02xxc(&options); /* Initialise options structure */ options.crnames = crnames; options.print_level = Nag_Soln; h02bbc(n, m, &a[0][0], tda, bl, bu, intvar, cvec, (double *)0, 0, NULLFN, x, &objf, &options, NAGCOMM_NULL, &fail); /* Now solve a related problem obtained by reducing lower bound on a constraint */ /* Read amount to reduce lower bound on constraint 1 by */ Vscanf(" %*[^\n]"); Vscanf("%lf", &red_bnd); bl[n] -= red_bnd; Vprintf("\nSolve modified problem - use different tree search.\n"); Vprintf("---------------------------------------------------\n"); options.list = FALSE; if (red_bnd > 0.0) { /* We have a valid bound for the objective since this problem is less constrained than first one */ options.int_obj_bound = objf + 1.0e-3; } options.nodsel = Nag_Deep_Search; options.print_level = Nag_Iter; Vprintf("***Set options.list = FALSE\n"); Vprintf("***Set options.int_obj_bound = %15.7e\n", options.int_obj_bound); Vprintf("***Set options.nodsel = Nag_Deep_Search\n"); Vprintf("***Set options.print_level = Nag_Iter\n"); h02bbc(n, m, &a[0][0], tda, bl, bu, intvar, cvec, (double *)0, 0, NULLFN, x, &objf, &options, NAGCOMM_NULL, &fail); Vprintf("\n***IP objective value = %15.7e\n", objf); Vprintf("\n\nIllustrate effect of supplying branching directions.\n"); Vprintf("----------------------------------------------------\n\n"); options.branch_dir = Nag_Branch_InitX; Vprintf("***Set options.branch_dir = Nag_Branch_InitX\n"); h02bbc(n, m, &a[0][0], tda, bl, bu, intvar, cvec, (double *)0, 0, NULLFN, x, &objf, &options, NAGCOMM_NULL, &fail); Vprintf("\n***IP objective value = %15.7e\n", objf); h02xzc(&options, "", NAGERR_DEFAULT); /* Finally, illustrate solution of an MIQP problem - we find the IP solution which is closest in least-squares sense to the root node LP solution of BB tree */ Vprintf("\n\nObtain solution of root LP problem.\n"); Vprintf("-----------------------------------\n\n"); /* Set all variables non-integer to obtain LP solution */ for (i = 0; i < n; ++i) intvar2[i] = 0; options.print_level = Nag_NoPrint; Vprintf("***Printout suppressed: options.print_level = Nag_NoPrint\n"); h02bbc(n, m, &a[0][0], tda, bl, bu, intvar2, cvec, (double *)0, 0, NULLFN, x, &objf, &options, NAGCOMM_NULL, &fail); Vprintf("***LP objective value = %15.7e\n", objf); /* Set linear part of solution */ for (i = 0; i < n; ++i) cvec[i] = -2.0*x[i]; /* Re-initialise options structure */ h02xzc(&options, "", NAGERR_DEFAULT); h02xxc(&options); options.crnames = crnames; options.list = TRUE; options.prob = Nag_MIQP2; Vprintf("\n\nFinally, solve a related MIQP problem.\n"); Vprintf("--------------------------------------\n"); h02bbc(n, m, &a[0][0], tda, bl, bu, intvar, cvec, (double *)0, 0, qphess, x, &objf, &options, NAGCOMM_NULL, &fail); h02xzc(&options, "", NAGERR_DEFAULT); } /* ex2 */