// e04nc Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.IO; namespace NagDotNetExamples { public class E04NCE { static bool defaultdata = true; static string datafile = ""; // as a command line argument. It defaults to the named file specified below otherwise. // static void Main(String[] args) { if (args.Length == 1) { defaultdata = false; datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = null; if(defaultdata) { sr = new DataReader("exampledata/e04nce.d"); } else { sr = new DataReader(datafile); } double obj = 0.0; int i, ifail, iter, j, m, n, nclin; Console.WriteLine("e04nc Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); m = int.Parse(sr.Next()); n = int.Parse(sr.Next()); nclin = int.Parse(sr.Next()); // // Read a, b, c, bl, bu and x from data file // double[,] a = new double[Math.Max(1,m), n]; double[] b = new double[m]; double[] bl = new double[n + nclin]; double[] bu = new double[n + nclin]; double[,] c = new double[nclin, n]; double[] clamda = new double[n + nclin]; double[] cvec = new double[n]; double[] work = new double[nclin]; double[] x = new double[n]; int[] istate = new int[n + nclin]; int[] kx = new int[n]; sr.Reset(); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { a[i - 1, j - 1] = double.Parse(sr.Next()); } } sr.Reset(); for (i = 1; i <= m; i++) { b[i - 1] = double.Parse(sr.Next()); } sr.Reset(); for (i = 1; i <= nclin; i++) { for (j = 1; j <= n; j++) { c[i - 1, j - 1] = double.Parse(sr.Next()); } } sr.Reset(); for (i = 1; i <= n + nclin; i++) { bl[i - 1] = double.Parse(sr.Next()); } sr.Reset(); for (i = 1; i <= n + nclin; i++) { bu[i - 1] = double.Parse(sr.Next()); } sr.Reset(); for (i = 1; i <= n; i++) { x[i - 1] = double.Parse(sr.Next()); } // // Initialise E04.e04nc and check for error exits // E04.e04ncOptions options = new E04.e04ncOptions(); // // Solve the problem // options.Set("Defaults"); options.Set("List"); options.Set("Print level = 10"); // E04.e04nc(m, n, nclin, c, bl, bu, cvec, istate, kx, x, a, b, out iter, out obj, clamda, options, out ifail); // // A valid licence was found: continue. // Check for error exits // Console.WriteLine(""); if (ifail == 6) { Console.WriteLine(" ** An input parameter is invalid"); } else if (ifail >= 0) { Console.WriteLine(" ** e04nc 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 C*X // (linear constraint values) and puts the result in // the first nclin locations of work. // F06.f06pa("N", nclin, n, 1.00e0, c, 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]); } } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Final objective value = {0,15:g7}", obj); } else { Console.WriteLine(" ** e04nc returned with ifail = {0, 3}", ifail); } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } } }