// e04nq Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04NQE { static string datafile = "ExampleData/e04nqe.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.E04NQ_QPHX qphxE04NQ = new E04.E04NQ_QPHX(qphx); try { DataReader sr = new DataReader(datafile); double obj, objadd, sinf; int i, icol=0, ifail, iobj, j, jcol, lenc, m, n, ncolh, ne, ninf, nname, ns=0; string start = ""; string prob = ""; int lenrw = 600; Console.WriteLine("e04nq Example Program Results"); // Skip heading in data file. sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); double[] acol = new double[n*m]; double[] bl = new double[n + m]; double[] bu = new double[n + m]; double[] c = new double[1]; double[] pi = new double[m]; double[] rc = new double[n + m]; double[] ruser = new double[1]; double[] rw = new double[lenrw]; double[] x = new double[n + m]; int[] helast = new int[n + m]; int[] hs = new int[n + m]; int[] inda = new int[n*m]; int[] loca = new int[n + 1]; string[] names = new string[n + m]; // // Read ne, iobj, ncolh, start and nname from data file. sr.Reset(); ne = int.Parse(sr.Next()); iobj = int.Parse(sr.Next()); ncolh = int.Parse(sr.Next()); start = sr.Next(); nname = int.Parse(sr.Next()); // // Read names from data file. sr.Reset(); for (i = 1; i <= nname; i++) { names[i - 1] = sr.Next(); } // // Read the matrix acol from data file. Set up loca. jcol = 1; loca[jcol - 1] = 1; for (i = 1; i <= ne; i++) { // // Element ( inda[i-1], icol-1 ) is stored in acol[ i-1 ]. sr.Reset(); acol[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); inda[i - 1] = int.Parse(sr.Next()); icol = int.Parse(sr.Next()); // if (icol < jcol) { // Elements not ordered by increasing column index. Console.WriteLine(" {0}{1,5}{2}{3,5}{4}{5}", "Element in column", icol, " found after element in column", jcol, ". Problem", " abandoned."); goto L100; } else if (icol == jcol + 1) { // Index in acol of the start of the icol-th column equals i. loca[icol - 1] = i; jcol = (int)icol; } else if (icol > jcol + 1) { // Index in acol 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 loca to i. for (j = jcol + 1; j <= icol - 1; j++) { loca[j - 1] = i; } loca[icol - 1] = i; jcol = (int)icol; } } // loca[n + 1 - 1] = ne + 1; // if (n > icol) { // Columns n,n-1,...,icol+1 are empty. Set the corresponding // elements of loca accordingly. for (i = n; i <= icol + 1; i += -1) { loca[i - 1] = loca[i + 1 - 1]; } } // // Read bl, bu, hs and x 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++) { hs[i - 1] = int.Parse(sr.Next()); } } else if (start == "W") { sr.Reset(); for (i = 1; i <= n + m; i++) { hs[i - 1] = int.Parse(sr.Next()); } } sr.Reset(); for (i = 1; i <= n; i++) { x[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Initialise e04nq. E04.e04nqOptions options = new E04.e04nqOptions(); // options.Set("Print file = 6"); // // We have no explicit objective vector so set lenc = 0; the // objective vector is stored in row iobj of acol. lenc = 0; objadd = 0.00e0; prob = ""; // // Do not allow any elastic variables (i.e. they cannot be // infeasible). If we'd set optional argument "Elastic mode" to 0, // we wouldn't need to set the individual elements of array helast. for (i = 1; i <= n + m; i++) { helast[i - 1] = 0; } // // Solve the QP problem. start = "C"; E04.e04nq(start, qphxE04NQ, m, n, ne, nname, lenc, ncolh, iobj, objadd, prob, acol, inda, loca, bl, bu, c, names, helast, hs, x, pi, rc, ref ns, out ninf, out sinf, out obj, options, out ifail); // Console.WriteLine(""); if (ifail >= 0) { Console.WriteLine(" On exit from e04nq, ifail = {0,5}", ifail); if (ifail == 0) { Console.WriteLine(" Final objective value = {0,11:e3}\n", obj); Console.Write(" Optimal x = "); for (i = 1; i <= n; i++) { Console.Write(" " + " {0, 9:f2}", x[i - 1]); } Console.WriteLine(" "); } } else { Console.WriteLine(" ** e04nq returned with ifail = {0, 3}", ifail); } // L100: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void qphx(int ncolh, double[] x, double[] hx, int nstate) { // 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]; } } }