// e05us Example Program Text // Mark 24 Release. NAG Copyright 2012. // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class E05USE { public static void objfun(ref int mode, int m, int n, int needfi, double[] x, double[] f, double[,] fjac, int nstate) { // Routine to evaluate the subfunctions and their 1st derivatives. double[] a = { 8.0, 8.0, 10.0, 10.0, 10.0, 10.0, 12.0, 12.0, 12.0, 12.0, 14.0, 14.0, 14.0, 16.0, 16.0, 16.0, 18.0, 18.0, 20.0, 20.0, 20.0, 22.0, 22.0, 22.0, 24.0, 24.0, 24.0, 26.0, 26.0, 26.0, 28.0, 28.0, 30.0, 30.0, 30.0, 32.0, 32.0, 34.0, 36.0, 36.0, 38.0, 38.0, 40.0, 42.0 }; double ai, temp, x1, x2; int i = 0; bool mode02 = false, mode12 = false; x1 = x[0]; x2 = x[1]; mode02 = ((mode == 0) || (mode == 2)); mode12 = ((mode == 1) || (mode == 2)); for (i = 1; i <= m; i++) { if (needfi == i) { f[i - 1] = x1 + (0.49 - x1) * Math.Exp(-x2 * (a[i - 1] - 8.0)); } ai = a[i - 1]; temp = Math.Exp(-x2 * (ai - 8.0)); if (mode02) { f[i - 1] = x1 + (0.49 - x1) * temp; } if (mode12) { fjac[i - 1, 0] = 1 - temp; fjac[i - 1, 1] = -(0.49 - x1) * (ai - 8.0) * temp; } } } public static void confun(ref int mode, int ncnln, int n, int[] needc, double[] x, double[] c, double[,] cjac, int nstate) { // Routine to evaluate the nonlinear constraint and its 1st // derivatives. if (nstate == 1) { // First call to CONFUN. Set all Jacobian elements to zero. // Note that this will only work when 'Derivative Level = 3' // (the default; see Section 11.2). for (int i = 1; i <= ncnln; i++) { for (int j = 1; j <= n; j++) { cjac[i- 1, j - 1] = 0; } } } if (needc[0] > 0) { if ((mode == 0) || (mode == 2)) { c[0] = -0.09 - x[0] * x[1] + 0.49 * x[1]; } if ((mode == 1) || (mode == 2)) { cjac[0, 0] = -x[1]; cjac[0, 1] = -x[0] + 0.49; } } } public static void mystart(int lnpts, double[,] quas, int n, bool repeat, double[] bl, double[] bu, ref int mode) { // All elements of quas(1:n,1:lnpts) are pre-assigned to zero // so we only need to set non-zero elements quas[0, 0] = 0.4; } // Specify data file as a command line argument. // It defaults to the named file specified below otherwise. static string datafile = "ExampleData/e05use.d"; static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { E05.E05US_CONFUN confunE05USF = new E05.E05US_CONFUN(confun); E05.E05US_OBJFUN objfunE05USF = new E05.E05US_OBJFUN(objfun); E05.E05US_START mystartE05USF = new E05.E05US_START(mystart); try { DataReader sr = new DataReader(datafile); // e05us Example Main Program int mmax = 44, nb = 1, nclin = 1, nclmax = 1, ncnln = 1, ncnmax = 2, nmax = 2, npts = 1; int sda = nmax; int sdcjac = nmax; int sdfjac = nmax; int sdr = nmax; int lbjgrd = nmax; int lda = nclmax; int ldc = ncnmax; int ldcjac = ncnmax; int ldclda = nmax + nclmax + ncnmax; int ldfjac = mmax; int ldr = nmax; int ldx = nmax; int listat = nmax + nclmax + ncnmax; bool repeat = true; int i = 0, ifail = 0, j = 0, k = 0, l = 0, m = 0, n = 0; m = 44; n = 2; double[,] a = new double[lda,sda]; double[] bl = new double[n+ nclin+ncnln]; double[] bu= new double[n + nclin + ncnln]; double[,] c = new double [ncnmax, nb]; double[,,] cjac = new double[ldcjac, sdcjac, nb]; double[,] clamda = new double[ldclda, nb]; double[,] f = new double[m, nb]; double[,,] fjac = new double[ldfjac, sdfjac, nb]; double[] work = new double[nclin]; double[,] x = new double[n, nb]; double[] y = new double[m]; double[] objf = new double[nb]; int lrwsav = 485; int liwsav = 740; double[] rwsav = new double[lrwsav]; double[] user = new double[1]; int[] info = new int[nb]; int[,] istate = new int[listat, nb]; int[] iter = new int[nb]; int[] iuser = new int[1]; int[] iwsav = new int[liwsav]; Console.WriteLine(" {0}", "e05us Example Program Results"); // Skip heading in data file sr.Reset(); // allocate if (nclin > 0) { sr.Reset(); for (i = 0; i < nclin; i++) { for (j = 0; j < sda; j++) { a[i , j] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } sr.Reset(); for (j = 0; j < m; j++) { y[j ] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (j = 0; j < n + nclin + ncnln; j++) { bl[j] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } sr.Reset(); for (j = 0; j < n + nclin + ncnln; j++) { bu[j ] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // Initialise e05us and check for error exits ifail = 0; E05.e05usOptions options = new E05.e05usOptions(); options.Set("List"); options.Set("print level = 10"); // Solve the problem ifail = -1; E05.e05us(m, n, nclin, ncnln, a, bl, bu, y, confunE05USF, objfunE05USF, npts, x, mystartE05USF, repeat, nb, objf, f, fjac, sdfjac, iter, c, cjac, sdcjac, clamda, istate, options, info, out ifail); // Check for error exits if (ifail==0) { l = (int)nb; } else if (ifail ==2) { l=0; } else if (ifail==3){ l=0;} else if (ifail==8){ l = info[nb - 1]; Console.WriteLine(" {0} {1} {2}", "ONLY ", l, " SOLUTIONS FOUND"); Console.WriteLine(" {0} {1}", iter[nb - 1], " starting points converged"); } else if (ifail==9){ Console.WriteLine(" ** An input parameter is invalid"); l = 0; } else if (ifail==7){ Console.WriteLine(" User supplied derivatives are incorrect"); l = 0;} else if (ifail ==-399) { Console.WriteLine(" ** e05us returned with ifail = {0,5}", ifail); l = 0; } else{ Console.WriteLine(" Unexpected error exit, ifail = {0,5}", ifail); l = 0; } for (i = 1; i <= l; i++) { Console.WriteLine(" {0} {1}", "Solution number ", i); Console.WriteLine(""); Console.WriteLine(" e05us returned with ifail = {0,4}", info[i - 1]); Console.WriteLine(""); Console.WriteLine(" Varbl Istate Value Lagr Mult"); Console.WriteLine(""); for (j = 1; j <= n; j++) { Console.WriteLine(" V {0,3} {1,3} {2,14:f6} {3,12:f4}", j, istate[j - 1, i - 1], x[j - 1, i - 1], clamda[j - 1, i - 1]); } if (nclin > 0) { // Below is a call to the level 2 BLAS routine DGEMV. // This performs the matrix vector multiplication A*X // (linear constraint values) and puts the result in // the first NCLIN locations of WORK.F06.dgemv("N", nclin, n, 1.00e0, a, ArraySlice.ArraySlice1(x, 1, i), 1, 0.00e0, work, 1); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" L Con Istate Value Lagr Mult"); Console.WriteLine(""); for (k = n + 1; k <= n + nclin; k++) { j = k - n; Console.WriteLine(" L {0,3} {1,3} {2,14:f6} {3,12:f4}", j, istate[k - 1, i - 1], work[j - 1], clamda[k - 1, i - 1]); } } if (ncnln > 0) { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" N Con Istate Value Lagr Mult"); Console.WriteLine(""); for (k = n + nclin + 1; k <= n + nclin + ncnln; k++) { j = k - n - nclin; Console.WriteLine(" N {0,3} {1,3} {2,14:f6} {3,12:f4}", j, istate[k - 1, i - 1], c[j - 1, i - 1], clamda[k - 1, i - 1]); } } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" Final objective value = {0,15:f7}", objf[i - 1]); Console.WriteLine(""); Console.WriteLine(" {0}", "QP multipliers"); for (k = 0; k < n + nclin + ncnln; k++) { Console.WriteLine(" {0,12:f4}", clamda[k,i-1]); } Console.WriteLine(); Console.WriteLine(""); Console.WriteLine(" {0}", " ------------------------------------------------------ "); Console.WriteLine(""); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }