// e04wd Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04WDE { static string datafile = "ExampleData/e04wde.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.E04WD_CONFUN confunE04WD = new E04.E04WD_CONFUN(confun); E04.E04WD_OBJFUN objfunE04WD = new E04.E04WD_OBJFUN(objfun); try { DataReader sr = new DataReader(datafile); double objf = 0.0; int i, ifail, j, majits, n, nclin, ncnln; Console.WriteLine("e04wd Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); nclin = int.Parse(sr.Next()); ncnln = int.Parse(sr.Next()); double[,] a = new double[nclin, n]; double[] bl = new double[nclin+ncnln+n]; double[] bu = new double[nclin+ncnln+n]; double[] ccon = new double[ncnln]; double[,] cjac = new double[ncnln, n]; double[] clamda = new double[nclin+ncnln+n]; double[] grad = new double[n]; double[,] h = new double[n, n]; double[] x = new double[n]; int[] istate = new int[nclin+ncnln+n]; // // Read a, bl, bu and x from data file if (nclin > 0) { sr.Reset(); for (i = 1; i <= nclin; i++) { for (j = 1; j <= n; j++) { a[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } sr.Reset(); for (i = 1; i <= n + nclin + ncnln; i++) { bl[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (i = 1; i <= n + nclin + ncnln; i++) { bu[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (i = 1; i <= n; i++) { x[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Initialise e04wd. E04.e04wdOptions options = new E04.e04wdOptions(); // // By default e04wd does not print monitoring // information. Set the print file unit or the summary // file unit to get information. options.Set("Print file = 6"); // // Solve the problem. E04.e04wd(n, nclin, ncnln, a, bl, bu, confunE04WD, objfunE04WD, out majits, istate, ccon, cjac, clamda, out objf, grad, h, x, options, out ifail); // Console.WriteLine(""); Console.WriteLine(" On exit from e04wd, ifail = {0,5}", ifail); if (ifail == 0) { Console.WriteLine(" Final objective value = {0,11:f3}", objf); Console.WriteLine(""); Console.Write(" Output x"); for (i = 1; i <= n; i++) { Console.Write(" " + " {0,11:f3}", x[i - 1]); } Console.WriteLine(" "); } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void objfun(ref int mode, int n, double[] x, ref double objf, double[] grad, int nstate) { // Routine to evaluate objective function and its 1st derivatives. double one = 1.00e0; double two = 2.00e0; if ((mode == 0) || (mode == 2)) { objf = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]; } // if ((mode == 1) || (mode == 2)) { grad[0] = x[3] * (two * x[0] + x[1] + x[2]); grad[1] = x[0] * x[3]; grad[2] = x[0] * x[3] + one; grad[3] = x[0] * (x[0] + x[1] + x[2]); } // } // public static void confun(ref int mode, int ncnln, int n, int[] needc, double[] x, double[] ccon, double[,] cjac, int nstate) { // Routine to evaluate the nonlinear constraints and their 1st // derivatives. double zero = 0.00e0; double two = 2.00e0; int i, j; if (nstate == 1) { // First call to confun. Set all Jacobian elements to zero. // Note that this will only work when 'Derivative Level = 3' for (j = 1; j <= n; j++) { for (i = 1; i <= ncnln; i++) { cjac[i - 1, j - 1] = zero; } } } // if (needc[0] > 0) { if ((mode == 0) || (mode == 2)) { ccon[0] = (x[0]) * (x[0]) + (x[1]) * (x[1]) + (x[2]) * (x[2]) + (x[3]) * (x[3]); } if ((mode == 1) || (mode == 2)) { cjac[0, 0] = two * x[0]; cjac[0, 1] = two * x[1]; cjac[0, 2] = two * x[2]; cjac[0, 3] = two * x[3]; } } // if (needc[1] > 0) { if ((mode == 0) || (mode == 2)) { ccon[1] = x[0] * x[1] * x[2] * x[3]; } if ((mode == 1) || (mode == 2)) { cjac[1, 0] = x[1] * x[2] * x[3]; cjac[1, 1] = x[0] * x[2] * x[3]; cjac[1, 2] = x[0] * x[1] * x[3]; cjac[1, 3] = x[0] * x[1] * x[2]; } } // } } }