// e04uf Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04UFE { static string datafile = "ExampleData/e04ufe.d"; // as a command line argument. It defaults to the named file specified below otherwise. // static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); double objf = 0.0; int i, irevcm, iter=0, j, n, nclin, ncnln; double zero = 0.00e+0; double one = 1.00e+0; double two = 2.00e+0; int ifail; Console.WriteLine("e04uf Example Program Results"); // Skip heading in data file. sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); nclin = int.Parse(sr.Next()); ncnln = int.Parse(sr.Next()); double[,] a = new double[nclin, n]; double[] bl = new double[n + nclin + ncnln]; double[] bu = new double[n + nclin + ncnln]; double[] c = new double[ncnln]; double[,] cjac = new double[ncnln, n]; double[] clamda = new double[n + nclin + ncnln]; double[] objgrd = new double[n]; double[,] r = new double[n, n]; double[] work = new double[1000]; double[] x = new double[n]; int[] istate = new int[n + nclin + ncnln]; int[] iwork = new int[100]; int[] needc = new int[10]; // // Read a, bl, bu and x from data file. // if (nclin > 0) { sr.Reset(); for (i = 1; i <= nclin; i++) { for (j = 1; j <= n; j++) { a[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } sr.Reset(); for (i = 1; i <= n + nclin + ncnln; i++) { bl[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (i = 1; i <= n + nclin + ncnln; i++) { bu[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (i = 1; i <= n; i++) { x[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Set all constraint Jacobian elements to zero. // Note that this will only work when 'Derivative Level = 3' // (the default; see Section 11.2). // for (j = 1; j <= n; j++) { for (i = 1; i <= ncnln; i++) { cjac[i - 1, j - 1] = zero; } } // // Initialise E04.e04uf and check for error exits // E04.e04ufOptions options = new E04.e04ufOptions(); options.Set("List"); options.Set("Print level = 10"); // // Solve the problem. // irevcm = 0; // L60: ; // E04.e04uf(ref irevcm, n, nclin, ncnln, a, bl, bu, ref iter, istate, c, cjac, clamda, ref objf, objgrd, r, x, needc, iwork, work, options, out ifail); // // On intermediate exit ifail should not have been changed // and irevcm should be > 0. // if (irevcm > 0) { if ((irevcm == 1) || (irevcm == 3)) { // Evaluate the objective function. objf = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]; } if ((irevcm == 2) || (irevcm == 3)) { // Evaluate the objective gradient. objgrd[0] = x[3] * (two * x[0] + x[1] + x[2]); objgrd[1] = x[0] * x[3]; objgrd[2] = x[0] * x[3] + one; objgrd[3] = x[0] * (x[0] + x[1] + x[2]); } // if ((irevcm == 4) || (irevcm == 6)) { // Evaluate the nonlinear constraint functions. if (needc[0] > 0) { c[0] = (x[0]) * (x[0]) + (x[1]) * (x[1]) + (x[2]) * (x[2]) + (x[3]) * (x[3]); } if (needc[1] > 0) { c[1] = x[0] * x[1] * x[2] * x[3]; } } if ((irevcm == 5) || (irevcm == 6)) { // Evaluate the constraint Jacobian. if (needc[0] > 0) { cjac[0, 0] = two * x[0]; cjac[0, 1] = two * x[1]; cjac[0, 2] = two * x[2]; cjac[0, 3] = two * x[3]; } if (needc[1] > 0) { cjac[1, 0] = x[1] * x[2] * x[3]; cjac[1, 1] = x[0] * x[2] * x[3]; cjac[1, 2] = x[0] * x[1] * x[3]; cjac[1, 3] = x[0] * x[1] * x[2]; } } goto L60; } else if (ifail >= (9)) { Console.WriteLine(" ** An input parameter is invalid"); } else if (ifail == 7) { Console.WriteLine(" ** User supplied derivatives are incorrect"); } else if (ifail >= 0) { Console.WriteLine(" ** e04uf returned with ifail = {0, 3}", ifail); Console.WriteLine(""); Console.WriteLine(" Varbl Istate Value Lagr Mult"); Console.WriteLine(""); for (i = 1; i <= n; i++) { Console.WriteLine(" V {0,3} {1,3} {2,14:g6} {3,12:g4}", i, istate[i - 1], x[i - 1], clamda[i - 1]); } if (nclin > 0) { // // This performs the matrix vector multiplication A*X // (linear constraint values) and puts the result in // the first nclin locations of work. // F06.f06pa("N", nclin, n, 1.00e0, a, x, 1, 0.00e0, work, 1, out ifail); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" L Con Istate Value Lagr Mult"); Console.WriteLine(""); for (i = n + 1; i <= n + nclin; i++) { j = i - n; Console.WriteLine(" L {0,3} {1,3} {2,14:g6} {3,12:g4}", j, istate[i - 1], work[j - 1], clamda[i - 1]); } } if (ncnln > 0) { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" N Con Istate Value Lagr Mult"); Console.WriteLine(""); for (i = n + nclin + 1; i <= n + nclin + ncnln; i++) { j = i - n - nclin; Console.WriteLine(" N {0,3} {1,3} {2,14:g6} {3,12:g4}", j, istate[i - 1], c[j - 1], clamda[i - 1]); } } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Final objective value = {0,15:g7}", objf); } else { Console.WriteLine(" ** e04uf returned with ifail = {0, 3}", ifail); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }