// e04vj Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class E04VJE { static int lenamx=300; static int lengmx=300; static int nfname=1; static int nxname=1; static string datafile = "ExampleData/e04vje.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() { try { DataReader sr = new DataReader(datafile); E04.E04VJ_USRFUN usrfunE04VJ = new E04.E04VJ_USRFUN(usrfun); E04.E04VH_USRFUN usrfunE04VH = new E04.E04VH_USRFUN(usrfun); double objadd=0.0, sinf=0.0; int i=0, lena=0, leng=0, n=0, nea=0, neg=0, nf=0, ninf=0, ns=0, objrow=0, start=0; string prob=""; int ifail ; Console.WriteLine("e04vj Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); n = int.Parse(sr.Next()); nf = int.Parse(sr.Next()); double[] a = new double[300]; double[] f = new double[100]; double[] flow = new double[100]; double[] fmul = new double[nf]; double[] fupp = new double[100]; double[] ruser = new double[1]; double[] x = new double[n]; double[] xlow = new double[n]; double[] xmul = new double[n]; double[] xupp = new double[n]; int[] fstate = new int[nf]; int[] iafun = new int[300]; int[] igfun = new int[300]; int[] iuser = new int[1]; int[] javar = new int[300]; int[] jgvar = new int[300]; int[] xstate = new int[100]; string[] fnames = new string[1]; string[] xnames = new string[1]; // if ( n > 0 && nf > 0) { // // Call e04vg to initialise E04VJF. E04.e04vhOptions options = new E04.e04vhOptions(); // lena = lenamx; leng = lengmx; // // Read the bounds on the variables. for (i = 1 ; i <= n ; i++) { sr.Reset(); xlow[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); xupp[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // for (i = 1 ; i <= n ; i++) { x[i - 1] = 0.00e0; } // // Determine the Jacobian structure. E04.e04vj(nf, n, usrfunE04VJ, iafun, javar, out nea, a, igfun, jgvar, out neg, x, xlow, xupp, options, out ifail ); // // Print the Jacobian structure. Console.WriteLine(""); Console.WriteLine(" NEA (the number of non-zero entries in A) = {0,3}",nea); Console.WriteLine(" I IAFUN(I) JAVAR(I) A(I)"); Console.WriteLine(" ---- -------- -------- -----------"); for (i = 1 ; i <= nea ; i++) { Console.WriteLine(" {0,3}{1,10}{2,10}{3,18:e4}",i,iafun[i - 1],javar[i - 1],a[i - 1]); } Console.WriteLine(""); Console.WriteLine(" NEG (the number of non-zero entries in G) = {0,3}",neg); Console.WriteLine(" I IGFUN(I) JGVAR(I)"); Console.WriteLine(" ---- -------- --------"); for (i = 1 ; i <= neg ; i++) { Console.WriteLine(" {0,3}{1,10}{2,10}",i,igfun[i - 1],jgvar[i - 1]); } // // Now that we have the determined the structure of the // Jacobian, set up the information necessary to solve // the optimization problem. start = 0; prob = "E04VJF"; objadd = 0.00e0; for (i = 1 ; i <= n ; i++) { x[i - 1] = 0.00e0; xstate[i - 1] = 0; xmul[i - 1] = 0.00e0; } for (i = 1 ; i <= nf ; i++) { f[i - 1] = 0.00e0; fstate[i - 1] = 0; fmul[i - 1] = 0.00e0; } // // The row containing the objective function. sr.Reset(); objrow = int.Parse(sr.Next()); // // Read the bounds on the functions. for (i = 1 ; i <= nf ; i++) { sr.Reset(); flow[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); fupp[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // By default e04vh does not print monitoring // information. Set the print file unit or the summary // file unit to get information. options.Set("Print file = 6"); // // Tell e04vh that we supply no derivatives in USRFUN. options.Set("Derivative option = 0"); // // Solve the problem. E04.e04vh(start, nf, n, nxname, nfname, objadd, objrow, prob, usrfunE04VH, iafun, javar, nea, a, igfun, jgvar, neg, xlow, xupp, xnames, flow, fupp, fnames, x, xstate, xmul, f, fstate, fmul, ref ns, out ninf, out sinf, options, out ifail); // Console.WriteLine(""); Console.WriteLine(" On exit from E04VHF, ifail = {0,5}",ifail); if ((ifail == 0) || (ifail == 4)) { Console.WriteLine(" Final objective value = {0,11:f1}", f[objrow - 1]); Console.WriteLine(""); Console.Write(" Optimal x = "); for (i = 1; i <= n; i++) { Console.Write(" " + " {0, 9:f2}", x[i - 1]); } Console.WriteLine(" "); } } else { Console.WriteLine(" At least one of N or NF is too large"); } // return; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } // public static void usrfun (ref int status, int n, double[] x, int needf, int nf, double[] f, int needg, double[] g) { if (needf > 0) { f[0] = 1000.00e+0 * Math.Sin( -x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin( -x[1] - 0.250e+0) - x[2]; f[1] = 1000.00e+0 * Math.Sin(x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[0] - x[1] - 0.250e+0) - x[3]; f[2] = 1000.00e+0 * Math.Sin(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[1] - 0.250e+0); f[3] = -x[0] + x[1]; f[4] = x[0] - x[1]; f[5] = 1.00e-6 * Math.Pow(x[2] ,3) + 2.00e-6 * Math.Pow(x[3] , 3) / 3.00e+0 + 3 * x[2] + 2 * x[3]; } // } } }