using System; using System.Runtime.InteropServices; using NagCFunctionsAPI; public class NagE04Functions { public static void nag_opt_nlp_solve (double [,] a, double [] bl, double [] bu, NAG_E04WDC_CONFUN confun, NAG_E04WDC_OBJFUN objfun, ref int majits, int [] state, double [] ccon, double [,] cjac, double [] clamda, ref double objf, double [] grad, double [,] hess, double [] x, ref Nag_E04State e04state, ref Nag_Comm comm, ref NagError fail) { /* options.list=0; options.optim_tol=1.0e-3; options.print_level=1135; options.max_iter=100; */ /* options.outfile[0] = 'd'; options.outfile[1] = '.'; options.outfile[2] = 'o'; */ // String name = new String( // options.outfile = name.ToCharArray(); int n=x.GetLength(0); int nclin=a.GetLength(0); int ncnln=cjac.GetLength(0); NagFunctions.e04wdc(n, nclin, ncnln, n, n, n, a, bl, bu, confun, objfun, ref majits, state, ccon, cjac, clamda, ref objf, grad, hess, x, ref e04state, ref comm, ref fail); } public static void Main() { NAG_E04WDC_OBJFUN OBJFUN = new NAG_E04WDC_OBJFUN (objfun); NAG_E04WDC_CONFUN CONFUN = new NAG_E04WDC_CONFUN (confun); int n = 4; int nclin = 1; int ncnln = 2; double [,] a = {{1.0, 1.0, 1.0, 1.0}}; double [] bl = {1.0, 1.0, 1.0, 1.0, -1.0e25, -1.0e25, 25.0}; double [] bu = {5.0, 5.0, 5.0, 5.0, 20.0, 40.0, 1.0e25}; int majits = 0; int [] state = new int[n+nclin+ncnln]; double [] ccon = new double[ncnln]; double [,] cjac = new double[ncnln,n]; double [] clamda = new double[n+nclin+ncnln]; double objf = 0.0; double [] grad = new double[n]; double [,] hess = new double[n,n]; double [] x = {1.0, 5.0, 5.0, 1.0}; Nag_Comm comm = new Nag_Comm(); Nag_E04State e04state = new Nag_E04State(); NagError fail = new NagError(); NagFunctions.e04wcc(ref e04state, ref fail); int writemode=2; int fileid=0; NagFunctions.x04acc("", writemode, ref fileid, ref fail); NagFunctions.e04wgc("Print file", fileid, ref e04state, ref fail); nag_opt_nlp_solve(a, bl, bu, CONFUN, OBJFUN, ref majits, state, ccon, cjac, clamda, ref objf, grad, hess, x, ref e04state, ref comm, ref fail); Console.WriteLine("\nFinal value of the objective function = {0}", objf); Console.WriteLine("At the point:"); for (int i=0; i 2.0 ) // comm.flag = -1; Marshal.Copy( g, 0, g_ptr, n); } public static void confun(ref int mode, int ncnlin, int n, int pdcj, IntPtr needc_ptr, IntPtr x_ptr, [In, Out] IntPtr conf_ptr, [In, Out] IntPtr conjac_ptr, int nstate, ref Nag_Comm comm) { int i, j; int [] needc = new int[ncnlin]; Marshal.Copy( needc_ptr, needc, 0, ncnlin ); // double [] x = new double[n]; Marshal.Copy( x_ptr, x, 0, n ); double [] conjac = new double[ncnlin*n]; double [] conf = new double[ncnlin]; /* Routine to evaluate the nonlinear constraints and * their 1st derivatives. */ /* Function Body */ if (comm.first == 1) { /* First call to confun. Set all Jacobian elements to zero. * Note that this will only work when con_deriv = TRUE * (the default; see Section 4 (confun) and 8.2 (con_deriv)). */ for (j = 1; j <= n; ++j) for (i = 1; i <= ncnlin; ++i) conjac[(i-1)*n + j-1] = 0.0; } if (needc[0] > 0) { if (mode == 0 || mode == 2) conf[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3]; if (mode == 0 || mode == 2) { conjac[(1-1)*n + 1-1] = x[0] * 2.0; conjac[(1-1)*n + 2-1] = x[1] * 2.0; conjac[(1-1)*n + 3-1] = x[2] * 2.0; conjac[(1-1)*n + 4-1] = x[3] * 2.0; } } if (needc[1] > 0) { if (mode == 0 || mode == 2) conf[1] = x[0] * x[1] * x[2] * x[3]; if (mode == 0 || mode == 2) { conjac[(2-1)*n + 1-1] = x[1] * x[2] * x[3]; conjac[(2-1)*n + 2-1] = x[0] * x[2] * x[3]; conjac[(2-1)*n + 3-1] = x[0] * x[1] * x[3]; conjac[(2-1)*n + 4-1] = x[0] * x[1] * x[2]; } } Marshal.Copy( conf, 0, conf_ptr, ncnlin ); Marshal.Copy( conjac, 0, conjac_ptr, ncnlin*n ); } /* confun */ }