// e04kd Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04KDE { static void Main(String[] args) { StartExample(); } public static void StartExample() { E04.E04HC_FUNCT functE04HC = new E04.E04HC_FUNCT(funct); E04.E04KD_FUNCT functE04KD = new E04.E04KD_FUNCT(funct); E04.E04KD_MONIT monitE04KD = new E04.E04KD_MONIT(monit); try { double delta, eta, f, stepmx, xtol; int ibound, ifail, iprint, j, maxcal; int n = 4; int lh = n * (n - 1) / 2; int lw = 7 * n + n * (n - 1) / 2; int liw = 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.Out.Flush(); Console.WriteLine("e04kd Example Program Results"); // Check functE04HC by calling e04hc at an arbitrary point. Since e04hc // only checks the derivatives calculated when iflag = 2, a separate // program should be run before using e04hc or e04kd to check that // functE04HC gives the same values for the gc[j-1] when iflag is set to 1 // as when iflag is set to 2. x[0] = 1.460e0; x[1] = -0.820e0; x[2] = 0.570e0; x[3] = 1.210e0; // E04.e04hc(n, functE04HC, x, out f, g, out ifail); // if (ifail != 0) { Console.WriteLine(""); Console.WriteLine(" ** e04hc returned with ifail = {0, 3}", ifail); return; } // // Continue setting parameters for e04kd // Set iprint to 1 to obtain output from monitE04KD at each iteration * iprint = -1; maxcal = 50 * n; eta = 0.50e0; // Set xtol and delta to zero so that e04kd will use the default // values xtol = 0.00e0; delta = 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.e04kd(n, functE04KD, monitE04KD, iprint, maxcal, eta, xtol, delta, stepmx, ibound, bl, bu, x, hesl, hesd, istate, out f, g, out ifail); Console.Out.Flush(); // if (ifail == 0 || ifail == 3 || ifail == 5) { Console.WriteLine(""); Console.WriteLine(" {0}{1,12:f4}", "Function value on exit is ", f); Console.Write(" " + " {0}", "at the point"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 12: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(" "); Console.Write(" " + " {0}", "and hesd contains"); for (j = 1; j <= n; j++) { Console.Write(" " + " {0, 12:e4}", hesd[j - 1]); } Console.WriteLine(" "); } } // 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. if (iflag != 1) { 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 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-1] g[J-1] 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"); } else if (isj == -1) { 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"); } 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 be used in conjunction with either of the method // E04.e04kd or E04.e04lb Console.WriteLine(""); Console.WriteLine(" {0}", "Projected Hessian matrix is not positive definite"); } } // } } }