// h02ce Example Program Text // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class H02CEE { // Specify data file as a command line argument. // It defaults to the named file specified below otherwise. static string datafile = "ExampleData/h02cee.d"; const int nmax = 100; const int mmax = 100; const int nnzmax = 100; const int leniz = 100000; const int lenz = 100000; const int lintvr = 10; const int mdepth = 2000; static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { H.H02CE_QPHX qphxH02CEF = new H.H02CE_QPHX(qphx); H.H02CE_MONIT monitH02CEF = new H.H02CE_MONIT(monit); DataReader sr = new DataReader(datafile); double obj = 0.0; int i = 0, icol = 0, iobj = 0, j = 0, jcol = 0, m = 0, miniz = 0, minz = 0, n = 0, ncolh = 0, nname = 0, nnz = 0, ns = 0, strtgy = 0; string start = ""; double[] a = new double[100]; double[] bl = new double[200]; double[] bu = new double[200]; double[] clamda = new double[200]; double[] xs = new double[200]; double[] z = new double[100000]; int[] ha = new int[100]; int[] intvar = new int[10]; int[] istate = new int[200]; int[] iz = new int[100000]; int[] ka = new int[101]; string[] crname = new string[200]; string[] names = new string[5]; Console.WriteLine("h02ce Example Program Results"); // Skip heading in data file. sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); int ifail = 0; if ((n <= nmax) && (m <= mmax)) { // // 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. // 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 ), ICOL ) is stored in A( I ). // 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 + 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; // // 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); } sr.Reset(); for (i = 1; i <= n; 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); } // strtgy = 3; intvar[0] = 2; intvar[1] = 3; intvar[2] = 4; intvar[3] = 5; intvar[4] = 6; intvar[5] = 7; intvar[6] = -1; // H.h02ceOptions options = new H.h02ceOptions(); options.Set("NoList"); options.Set("Print Level = 0"); ; // // Solve the QP problem. // // H.h02ce(n, m, nnz, iobj, ncolh, qphxH02CEF, a, ha, ka, bl, bu, start, names, nname, crname, ref ns, xs, intvar, lintvr, mdepth, istate, out miniz, out minz, out obj, clamda, strtgy, iz, z, monitH02CEF, options, out ifail); // // Print out the best integer solution found // Console.WriteLine(); Console.WriteLine("Optimal Integer Value is {0:G10}", obj); Console.WriteLine(); Console.WriteLine("Components are:"); for (i = 0; i < n; i++) { Console.WriteLine("x[{0}]={1:G10}", i, xs[i]); } } } // 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.) // const 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]; // } // public static void monit(int intfnd, int nodes, int depth, double obj, double[] x, ref double bstval, double[] bstsol, double[] bl, double[] bu, int n, ref bool halt, ref int count) { const double cutoff = -1847510.00e+0; if (intfnd == 0) { bstval = cutoff; } // } } }