// e04lb Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04LBE { static void Main(String[] args) { StartExample(); } public static void StartExample() { E04.E04HC_FUNCT functE04HC = new E04.E04HC_FUNCT(funct); E04.E04HD_FUNCT functE04HD = new E04.E04HD_FUNCT(funct); E04.E04HD_H hE04HD = new E04.E04HD_H(h); E04.E04LB_FUNCT functE04LB = new E04.E04LB_FUNCT(funct); E04.E04LB_H hE04LB = new E04.E04LB_H(h); E04.E04LB_MONIT monitE04LB = new E04.E04LB_MONIT(monit); try { double eta, f, stepmx, xtol; int ibound, ifail, iprint, j, maxcal, nz; int n = 4; int lh = n * (n - 1) / 2; int liw = 2; int lw = 7 * n + n * (n - 1) / 2; double[] bl = new double[n]; double[] bu = new double[n]; double[] g = new double[n]; double[] hesd = new double[n]; double[] hesl = new double[lh]; double[] w = new double[lw]; double[] x = new double[n]; int[] istate = new int[n]; int[] iw = new int[liw]; Console.WriteLine("e04lb Example Program Results"); // Set up an arbitrary point at which to check the derivatives x[0] = 1.460e0; x[1] = -0.820e0; x[2] = 0.570e0; x[3] = 1.210e0; // Check the 1st derivatives E04.e04hc(n, functE04HC, x, out f, g, out ifail); // if (ifail == 0) { // // Check the 2nd derivatives E04.e04hd(n, functE04HD, hE04HD, x, g, hesl, hesd, out ifail); } if (ifail != 0) { Console.WriteLine(""); Console.WriteLine(" ** e04hc returned with ifail = {0, 3}", ifail); goto L40; } // // Continue setting parameters for e04lb // set iprint to 1 to obtain output from monit at each iteration * iprint = -1; maxcal = 50 * n; eta = 0.90e0; // Set xtol to zero so that e04lb will use the default tolerance xtol = 0.00e0; // We estimate that the minimum will be within 4 units of the // starting point stepmx = 4.00e0; ibound = 0; bl[0] = 1.00e0; bu[0] = 3.00e0; bl[1] = -2.00e0; bu[1] = 0.00e0; // 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.00e6; bu[2] = 1.00e6; bl[3] = 1.00e0; bu[3] = 3.00e0; // Set up starting point x[0] = 3.00e0; x[1] = -1.00e0; x[2] = 0.00e0; x[3] = 1.00e0; // E04.e04lb(n, functE04LB, hE04LB, monitE04LB, iprint, maxcal, eta, xtol, stepmx, ibound, bl, bu, x, hesl, hesd, istate, out f, g, out ifail); // if (ifail >= 0) { if (ifail != 0) { Console.WriteLine(""); Console.WriteLine(" {0}{1,3}{2}", "Error exit type", ifail, " - see method document"); } if (ifail != 1) { Console.WriteLine(""); Console.WriteLine(" {0}{1,9:f4}", "Function value on exit is ", f); Console.Write(" " + " {0}", "at the point"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 9:f4}", x[j - 1]); } Console.WriteLine(" "); Console.WriteLine(" {0}", "The corresponding (machine dependent) gradient is"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 12:e3}", g[j - 1]); } Console.WriteLine(" "); Console.Write(" " + " {0}", "ISTATE contains"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 5}", istate[j - 1]); } Console.WriteLine(" "); nz = 0; for (j = 1; j <= n; j++) { if (istate[j - 1] > 0) { nz = nz + 1; } } Console.Write(" " + " {0}", "and HESD contains"); for (j = 1; j <= nz; j++) { Console.Write(" " + " {0, 12:e4}", hesd[j - 1]); } Console.WriteLine(" "); } } else { Console.WriteLine(""); Console.WriteLine(" ** e04lb returned with ifail = {0, 3}", ifail); } L40: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void funct(ref int iflag, int n, double[] xc, ref double fc, double[] gc) { // Routine to evaluate objective function and its 1st derivatives. fc = ((xc[0] + 10.00e0 * xc[1])) * ((xc[0] + 10.00e0 * xc[1])) + 5.00e0 * ((xc[2] - xc[3])) * ((xc[2] - xc[3])) + Math.Pow((xc[1] - 2.00e0 * xc[2]), 4) + 10.00e0 * Math.Pow((xc[0] - xc[3]), 4); gc[0] = 2.00e0 * (xc[0] + 10.00e0 * xc[1]) + 40.00e0 * Math.Pow((xc[0] - xc[3]), 3); gc[1] = 20.00e0 * (xc[0] + 10.00e0 * xc[1]) + 4.00e0 * Math.Pow((xc[1] - 2.00e0 * xc[2]), 3); gc[2] = 10.00e0 * (xc[2] - xc[3]) - 8.00e0 * Math.Pow((xc[1] - 2.00e0 * xc[2]), 3); gc[3] = 10.00e0 * (xc[3] - xc[2]) - 40.00e0 * Math.Pow((xc[0] - xc[3]), 3); } // public static void h(ref int iflag, int n, double[] xc, double[] fhesl, double[] fhesd) { // Routine to evaluate 2nd derivatives fhesd[0] = 2.00e0 + 120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesd[1] = 200.00e0 + 12.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesd[2] = 10.00e0 + 48.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesd[3] = 10.00e0 + 120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesl[0] = 20.00e0; fhesl[1] = 0.00e0; fhesl[2] = -24.00e0 * ((xc[1] - 2.00e0 * xc[2])) * ((xc[1] - 2.00e0 * xc[2])); fhesl[3] = -120.00e0 * ((xc[0] - xc[3])) * ((xc[0] - xc[3])); fhesl[4] = 0.00e0; fhesl[5] = -10.00e0; } // public static void monit(int n, double[] xc, double fc, double[] gc, int[] istate, double gpjnrm, double cond, bool posdef, int niter, int nf) { // Monitoring method int isj, j; Console.WriteLine(""); Console.WriteLine(" {0}", " Itn Fn evals Fn value Norm of proj gradient"); Console.WriteLine(" {0,3} {1,5} {2,20:e4} {3,20:e4}", niter, nf, fc, gpjnrm); Console.WriteLine(""); Console.WriteLine(" {0}", " J X(J) G(J) Status"); Console.WriteLine(""); Console.WriteLine(" {0}", " Itn Fn evals Fn value Norm of proj gradient"); Console.WriteLine(" {0,3} {1,5} {2,20:e4} {3,20:e4}", niter, nf, fc, gpjnrm); Console.WriteLine(""); Console.WriteLine(" {0}", " J X(J) G(J) Status"); for (j = 1; j <= n; j++) { isj = istate[j - 1]; if (isj > 0) { Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Free"); Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Free"); } else if (isj == -1) { Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Upper Bound"); Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Upper Bound"); } else if (isj == -2) { Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Lower Bound"); Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " Lower Bound"); } else if (isj == -3) { Console.WriteLine(" {0,2} {1,20:e4}{2,20:e4}{3}", j, xc[j - 1], gc[j - 1], " ant"); } } if (cond != 0.00e0) { if (cond > 1.00e6) { Console.WriteLine(""); Console.WriteLine(" {0}", "Estimated condition number of projected Hessian is more than 1.0E+6"); } else { Console.WriteLine(""); Console.WriteLine(" {0}{1,10:e2}", "Estimated condition number of projected Hessian = ", cond); } if (!(posdef)) { // The following statement is included so that this monit // can also be used in conjunction with e04kd Console.WriteLine(""); Console.WriteLine(" {0}", "Projected Hessian matrix is not positive definite"); } } // } } }