// e04ug Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04UGE { static string datafile = "ExampleData/e04uge.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.E04UG_CONFUN confunE04UG = new E04.E04UG_CONFUN(confun); E04.E04UG_OBJFUN objfunE04UG = new E04.E04UG_OBJFUN(objfun); try { DataReader sr = new DataReader(datafile); double obj, sinf; int i, icol=0, iobj, j, jcol, m, miniz, minz, n, ncnln, ninf, njnln, nname, nnz, nonln, ns=0; string start=""; int idummy = -11111; string[] names = new string[200]; int ifail; Console.WriteLine("e04ug Example Program Results"); // Skip heading in data file. sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); // // Read ncnln, nonln and njnln from data file. // sr.Reset(); ncnln = int.Parse(sr.Next()); nonln = int.Parse(sr.Next()); njnln = int.Parse(sr.Next()); // // Read nnz, iobj, start, nname and names from data file. // sr.Reset(); nnz = int.Parse(sr.Next()); iobj = int.Parse(sr.Next()); start = sr.Next(); nname = int.Parse(sr.Next()); double[] a = new double[nnz]; double[] bl = new double[n+m]; double[] bu = new double[n+m]; double[] clamda = new double[n+m]; double[] user = new double[1]; double[] xs = new double[n+m]; int[] ha = new int[300]; int[] istate = new int[n+m]; int[] iuser = new int[1]; int[] ka = new int[n+1]; if (nname == n + m) { sr.Reset(); for (i = 1 ; i <= n + m ; i++) { names[i - 1] = sr.Next(); } } // // Initialize ka. // for (i = 1 ; i <= n + 1 ; i++) { ka[i - 1] = idummy; } // // 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-1 ) 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 L160; } 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, xs and clamda 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); } if (ncnln > 0) { sr.Reset(); for (i = n + 1 ; i <= n + ncnln ; i++) { clamda[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } // Switch off all output. PrintManager.Warning = new PrintManager.MessageLogger(discardmessage); PrintManager.Message = new PrintManager.MessageLogger(discardmessage); // // Initialise E04.e04ug // E04.e04ugOptions options = new E04.e04ugOptions(); // // Solve the problem. // start="C"; // First get appropriate minimum lengths of arrays, z and iz int leniz = 500; int lenz = 500; double[] z = new double[lenz]; int[] iz = new int[leniz]; E04.e04ug(confunE04UG, objfunE04UG, n, m, ncnln, nonln, njnln, iobj, nnz, a, ha, ka, bl, bu, start, nname, names, ref ns, xs, istate, clamda, out miniz, out minz, out ninf, out sinf, out obj, iz, z, options, out ifail); Console.WriteLine("Computed values of the size of z and iz are {0}, {1} respectively.", minz, miniz); // Initialise E04.e04ug again and check for error exits PrintManager.Warning = new PrintManager.MessageLogger(printmessage); PrintManager.Message = new PrintManager.MessageLogger(printmessage); options = new E04.e04ugOptions(); options.Set("List"); options.Set("Print level = 10"); // // Solve the problem. // start="C"; // The minimum values of lenz and leniz found above are only a preliminary estimate. // In practice, larger values need to be chosen frequently, we have therefore specified // each of leniz and lenz as 1000. leniz = 1000; lenz = 1000; z = new double[lenz]; iz = new int[leniz]; E04.e04ug(confunE04UG, objfunE04UG, n, m, ncnln, nonln, njnln, iobj, nnz, a, ha, ka, bl, bu, start, nname, names, ref ns, xs, istate, clamda, out miniz, out minz, out ninf, out sinf, out obj, iz, z, options, out ifail); // // Check for error exits // Console.WriteLine(""); if (ifail >= (7)) { Console.WriteLine(" ** e04ug returned with ifail = {0, 3}", ifail); } else if (ifail >= 0) { Console.WriteLine(" ** e04ug 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(" Varble {0,2} {1,3} {2,14:g6} {3,12:g4}",i,istate[i - 1],xs[i - 1],clamda[i - 1]); } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Constrnt Istate Value Lagr Mult"); Console.WriteLine(""); if (ncnln > 0) { for (i = n + 1 ; i <= n + ncnln ; i++) { j = i - n; Console.WriteLine(" NlnCon {0,2} {1,3} {2,14:g6} {3,12:g4}",j,istate[i - 1],xs[i - 1],clamda[i - 1]); } } if ((((ncnln == 0)) && ((m == 1))) && ((a[0] == 0.00e0))) { Console.WriteLine(" DummyRow {0,3} {1,14:g6} {2,12:g4}",istate[n + 1 - 1],xs[n + 1 - 1],clamda[n + 1 - 1]); } else if (m > ncnln) { for (i = n + ncnln + 1 ; i <= n + m ; i++) { j = i - n - ncnln; if (i - n == iobj) { Console.WriteLine(" Free Row {0,3} {1,14:g6} {2,12:g4}",istate[i - 1],xs[i - 1],clamda[i - 1]); } else { Console.WriteLine(" LinCon {0,2} {1,3} {2,14:g6} {3,12:g4}",j,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(" ** e04ug returned with ifail = {0, 3}", ifail); } // L160: ; // }catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void confun (ref int mode, int ncnln, int njnln, int nnzjac, double[] x, double[] f, double[] fjac, int nstate) { // Computes the nonlinear constraint functions and their Jacobian. if ((mode == 0) || (mode == 2)) { f[0] = 1000.00e+0 * Math.Sin( -x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin( -x[1] - 0.250e+0); f[1] = 1000.00e+0 * Math.Sin(x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[0] - x[1] - 0.250e+0); f[2] = 1000.00e+0 * Math.Sin(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[1] - 0.250e+0); } // if ((mode == 1) || (mode == 2)) { // // Nonlinear Jacobian elements for column 1. // fjac[0] = -1000.00e+0 * Math.Cos( -x[0] - 0.250e+0); fjac[1] = 1000.00e+0 * Math.Cos(x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0); fjac[2] = -1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0); // // Nonlinear Jacobian elements for column 2. // fjac[3] = -1000.00e+0 * Math.Cos( -x[1] - 0.250e+0); fjac[4] = -1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0); fjac[5] = 1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[1] - 0.250e+0); } // } // public static void objfun (ref int mode, int nonln, double[] x, ref double objf, double[] objgrd, int nstate) { // Computes the nonlinear part of the objective function and its // gradient if ((mode == 0) || (mode == 2)) { objf = 1.00e-6 * Math.Pow(x[2] ,3) + 2.00e-6 * Math.Pow(x[3] ,3) / 3.00e+0; } // if ((mode == 1) || (mode == 2)) { objgrd[0] = 0.00e+0; objgrd[1] = 0.00e+0; objgrd[2] = 3.00e-6 * (x[2])*(x[2]); objgrd[3] = 2.00e-6 * (x[3])*(x[3]); } // } static void discardmessage(String message) { } static void printmessage(String message) { Console.WriteLine(message); } } }