// e04nk Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04NKE { static string datafile = "ExampleData/e04nke.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() { E04.E04NK_QPHX qphxE04NK = new E04.E04NK_QPHX(qphx); try { DataReader sr = new DataReader(datafile); double obj, sinf; int i, icol=0, ifail, iobj, j, jcol, m, miniz, minz, n, ncolh, ninf, nname, nnz, ns=0; string start = ""; int idummy = -11111; string[] names = new string[5]; Console.WriteLine("e04nk Example Program Results"); // Skip heading in data file. sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); // // Read nnz, iobj, ncolh, start and nname from data file. // sr.Reset(); nnz = int.Parse(sr.Next()); iobj = int.Parse(sr.Next()); ncolh = int.Parse(sr.Next()); start = sr.Next(); nname = int.Parse(sr.Next()); // // Read names and crname from data file. // double[] a = new double[nnz]; double[] bl = new double[n + m]; double[] bu = new double[n + m]; double[] clamda = new double[n + m]; double[] xs = new double[n + m]; int[] ha = new int[nnz]; int[] istate = new int[n + m]; int[] ka = new int[n + 1]; string[] crname = new string[n + m]; sr.Reset(); for (i = 1; i <= 5; i++) { names[i - 1] = sr.Next(); } sr.Reset(); for (i = 1; i <= nname; i++) { crname[i - 1] = sr.Next(); } // // read the matrix a from data file. Set up ka. // jcol = 1; ka[jcol - 1] = 1; for (i = 1; i <= nnz; i++) { // // Element ( ha( i-1 ), icol ) is stored in a[ i-1]. // sr.Reset(); a[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); ha[i - 1] = int.Parse(sr.Next()); icol = int.Parse(sr.Next()); // if (icol < jcol) { // // Elements not ordered by increasing column index. // Console.WriteLine("\n {0}{1,5}{2}{3,5}{4}{5}", "Element in column", icol, " found after element in column", jcol, ". Problem", " abandoned."); goto L120; } else if (icol == jcol + 1) { // // Index in a of the start of the icol-th column equals I. // ka[icol - 1] = i; jcol = (int)icol; } else if (icol > jcol + 1) { // // Index in a of the start of the icol-th column equals i, // but columns jcol+1,jcol+2,...,icol-1 are empty. Set the // corresponding elements of ka to i. // for (j = jcol + 1; j <= icol - 1; j++) { ka[j - 1] = i; } ka[icol - 1] = i; jcol = (int)icol; } } // ka[n + 1 - 1] = nnz + 1; // if (n > icol) { // // Columns n,n-1,...,icol+1 are empty. Set the corresponding // elements of ka accordingly. // for (i = n; i <= icol + 1; i += -1) { if (ka[i - 1] == idummy) { ka[i - 1] = ka[i + 1 - 1]; } } } // // Read bl, bu, istate and xs from data file. // sr.Reset(); for (i = 1; i <= n + m; i++) { bl[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (i = 1; i <= n + m; i++) { bu[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } if (start == "C") { sr.Reset(); for (i = 1; i <= n; i++) { istate[i - 1] = int.Parse(sr.Next()); } } else if (start == "W") { sr.Reset(); for (i = 1; i <= n + m; i++) { istate[i - 1] = int.Parse(sr.Next()); } } sr.Reset(); for (i = 1; i <= n; i++) { xs[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Initialise E04.e04nk // // Switch off all output. PrintManager.Warning = new PrintManager.MessageLogger(discardmessage); PrintManager.Message = new PrintManager.MessageLogger(discardmessage); E04.e04nkOptions options = new E04.e04nkOptions(); // // Solve the QP problem. // // start = "C"; // First get appropriate minimum lengths of arrays, z and iz int lenz = 1; int leniz = 1; double[] z = new double[lenz]; int[] iz = new int[leniz]; E04.e04nk(n, m, nnz, iobj, ncolh, qphxE04NK, a, ha, ka, bl, bu, start, names, nname, crname, ref ns, xs, istate, out miniz, out minz, out ninf, out sinf, out obj, clamda, iz, z, options, out ifail); Console.WriteLine("Computed values of the size of z and iz are {0}, {1} respectively.", minz, miniz); lenz = 358; leniz = 428; z = new double[lenz]; iz = new int[leniz]; // Initialise E04.e04nk again and check for error exits PrintManager.Warning = new PrintManager.MessageLogger(printmessage); PrintManager.Message = new PrintManager.MessageLogger(printmessage); options = new E04.e04nkOptions(); options.Set("List"); options.Set("Print level = 10"); E04.e04nk(n, m, nnz, iobj, ncolh, qphxE04NK, a, ha, ka, bl, bu, start, names, nname, crname, ref ns, xs, istate, out miniz, out minz, out ninf, out sinf, out obj, clamda, iz, z, options, out ifail); // // Check for error exits // Console.WriteLine(""); if (ifail == 7) { Console.WriteLine(" ** An input parameter is invalid"); } else if (ifail >= 0) { Console.WriteLine(" ** e04nk returned with ifail = {0, 3}", ifail); Console.WriteLine(""); Console.WriteLine(" Variable Istate Value Lagr Mult"); Console.WriteLine(""); for (i = 1; i <= n; i++) { Console.WriteLine(" {0} {1,3} {2,14:g6} {3,12:g4}", crname[i - 1], istate[i - 1], xs[i - 1], clamda[i - 1]); } if (m > 0) { // Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Constrnt Istate Value Lagr Mult"); Console.WriteLine(""); for (i = n + 1; i <= n + m; i++) { j = i - n; Console.WriteLine(" {0} {1,3} {2,14:g6} {3,12:g4}", crname[i - 1], istate[i - 1], xs[i - 1], clamda[i - 1]); } } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Final objective value = {0,15:g7}", obj); } else { Console.WriteLine(" ** e04nk returned with ifail = {0, 3}", ifail); } L120: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void qphx(int nstate, int ncolh, double[] x, double[] hx) { // // Routine to compute H*x. (In this version of QPHX, the Hessian // matrix h is not referenced explicitly.) // double two = 2.00e+0; hx[0] = two * x[0]; hx[1] = two * x[1]; hx[2] = two * (x[2] + x[3]); hx[3] = hx[2]; hx[4] = two * x[4]; hx[5] = two * (x[5] + x[6]); hx[6] = hx[5]; // } static void discardmessage(String message) { } static void printmessage(String message) { Console.WriteLine(message); } } }