// Mark 24 Release. NAG Copyright 2012. // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class E05UCE { public static void schwefel_obj(ref int mode, int n, double[] x, ref double objf, double[] objgrd, int nstate) { if ((mode == 0) || (mode == 2)) { // Evaluate the objective function. objf = 0.0; for(int j=0; j< n ;j++){ objf = objf + x[j] * Math.Sin(Math.Sqrt(Math.Abs(x[j ]))); } } if ((mode == 1) || (mode == 2)) { for(int j=0; j< n ;j++){ // Calculate the gradient of the objective function. objgrd[j] = Math.Sin(Math.Sqrt(Math.Abs(x[j]))) + 0.5 * Math.Sqrt(Math.Abs(x[j])) * Math.Cos(Math.Sqrt(Math.Abs(x[j]))); } } } public static void schwefel_confun(ref int mode, int ncnln, int n, int[] needc, double[] x, double[] c, double[,] cjsl, int nstate) { double t1, t2; int k = 0; if ((mode == 0) || (mode == 2)) { // Constraint values are required. // Only those for which needc is non-zero need be set. for (k = 1; k <= ncnln; k++) { if (needc[k - 1] > 0) { switch (k) { case 1: c[k - 1] = (x[0]) * (x[0]) - (x[1]) * (x[1]) + 3.0 * x[0] * x[1]; break; case 2: c[k - 1] = Math.Cos(((x[0] / 200.0)) * ((x[0] / 200.0)) + (x[1] / 100.0)); break; } } } } if ((mode == 1) || (mode == 2)) { // Constraint derivatives are required. for (k = 1; k <= ncnln; k++) { switch (k) { case 1: cjsl[k - 1, 0] = 2.0 * x[0] + 3.0 * x[1]; cjsl[k - 1, 1] = -2.0 * x[1] + 3.0 * x[0]; break; case 2: t1 = x[0] / 200.0; t2 = x[1] / 100.0; cjsl[k - 1, 0] = -Math.Sin((t1) * (t1) + t2) * 2.0 * t1 / 200.0; cjsl[k - 1, 1] = -Math.Sin((t1) * (t1) + t2) / 100.0; break; } } } } public static void mystart(ref int npts, double[,] quas, ref int n, ref bool repeat, double[] bl, double[] bu, int[] iuser, double[] ruser, ref int mode) { // Sets an initial point. // quas(1:n,1:npts) is pre-assigned to zero. for (int j = 0; j < n; j++) { quas[j, 0] = 420.9687; } } static string datafile = "ExampleData/e05uce.d"; // Specify data file 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{ E05.E05UC_CONFUN schwefel_confunE05UCF = new E05.E05UC_CONFUN(schwefel_confun); E05.E05UC_OBJFUN schwefel_objE05UCF = new E05.E05UC_OBJFUN(schwefel_obj); E05.E05UC_START e05uczE05UCF = new E05.E05UC_START(E05.e05ucz); DataReader sr = new DataReader(datafile); // e05uc Example Main Program int n = 2, nclin = 1, ncnln = 2; int i = 0, ifail = 0, j = 0, k = 0, l = 0, lda = 0, ldc = 0, ldcjac = 0, ldclda = 0, ldobjd = 0, ldr = 0, ldx = 0, listat = 0, nb = 0, npts = 0, sda = 0, sdcjac = 0, sdr = 0; bool repeat = false; double[,] a; double[] bl = { -500.0, -500.0, -10000.0, -1.0, -0.9 }; double[] bu = { 500.0, 500.0, 10.0, 500000.0, 0.9 }; double[,] c; double[,,] cjac; double[,] clamda; double[] objf; double[,] objgrd; double[,,] r; double[] work; double[,] x; double[] ruser = new double[1]; int[] info; int[,] istate; int[] iter; int[] iuser = new int[1]; Console.WriteLine(" {0}", "e05uc Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); nb = int.Parse(sr.Next()); npts = int.Parse(sr.Next()); sr.Reset(); repeat = bool.Parse(sr.Next()); lda = (int)nclin; if (nclin > 0) { sda = (int)n; } else { sda = 1; } ldx = (int)n; ldobjd = (int)n; ldc = (int)ncnln; ldcjac = (int)ncnln; if (ncnln > 0) { sdcjac = (int)n; } else { sdcjac = 0; } ldr = (int)n; sdr = (int)n; ldclda = n + nclin + ncnln; listat = n + nclin + ncnln; // allocate a= new double[lda, sda]; x = new double[ldx, nb]; objf= new double[nb]; objgrd= new double[ldobjd, nb]; iter= new int[nb]; c= new double[ldc, nb]; cjac= new double[ldcjac,sdcjac, nb ]; r = new double[ldr, sdr, nb]; clamda= new double[ldclda, nb]; istate= new int[listat, nb]; info=new int[nb]; work= new double[nclin]; a[0,0] = 3.0; a[0,1] = -2.0; // Initialize the solver. ifail = 0; E05.e05ucOptions options = new E05.e05ucOptions(); // Solve the problem. // USE mystart and pass this instead of e05ucz to e05uc // to set your own start points. ifail = -1; E05.e05uc(n, nclin, ncnln, a, bl, bu, schwefel_confunE05UCF, schwefel_objE05UCF, npts, x, e05uczE05UCF, repeat, nb, objf, objgrd, iter, c, cjac, sdcjac, r, sdr, clamda, istate, options, info, out ifail); switch (ifail) { case 0: l = (int)nb; break; case 8: l = info[nb - 1]; Console.WriteLine(" {0} {1}", iter[nb - 1], " starting points converged"); break; default: break; } for (i = 1; i <= l; i++) { Console.WriteLine("\n Solution number{0,16}", i); Console.WriteLine("\n Local minimization exited with code{0,5}", info[i - 1]); Console.WriteLine("\n {0} Istate Value Lagr Mult\n", "Varbl"); for (j = 1; j <= n; j++) { Console.WriteLine(" {0} {1,3} {2,3} {3,14:f6} {4,12:f4}", "V", j, istate[j - 1, i - 1], x[j - 1, i - 1], clamda[j - 1, i - 1]); } if (nclin > 0) { Console.WriteLine("\n {0} Istate Value Lagr Mult\n", "L Con"); // 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.0, a, ArraySlice.ArraySlice1(x, 1, i), 1, 0.0, work, 1); // dgemv('N',nclin,n,1.0_nag_wp,a,lda,x(1,i),1,0.0_nag_wp,work,1) double[] intx = new double[n]; for (int lindex = 0; lindex < n; lindex++) { intx[lindex] = x[lindex,i-1 ]; } F06.f06pa( "N" , nclin , n , 1.0 , a , intx , 1 , 0.0 , work , 1 , out ifail ); for (k = n + 1; k <= n + nclin; k++) { j = k - n; Console.WriteLine(" {0} {1,3} {2,3} {3,14:f6} {4,12:f4}", "L", j, istate[k - 1, i - 1], work[j - 1], clamda[k - 1, i - 1]); } } if (ncnln > 0) { Console.WriteLine("\n {0} Istate Value Lagr Mult\n", "N Con"); for (k = n + nclin + 1; k <= n + nclin + ncnln; k++) { j = k - n - nclin; Console.WriteLine(" {0} {1,3} {2,3} {3,14:f6} {4,12:f4}", "N", j, istate[k - 1, i - 1], c[j - 1, i - 1], clamda[k - 1, i - 1]); } } Console.WriteLine("\n Final objective value = {0,15:f7}", objf[i - 1]); Console.WriteLine("\n QP multipliers"); for (k = 1; k <= n + nclin + ncnln; k++) { Console.WriteLine(" {0,12:f4}", clamda[k-1,i-1]); } Console.WriteLine(); Console.WriteLine(""); Console.WriteLine(" {0}", " ------------------------------------------------------ "); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }