using System; using System.Runtime.InteropServices; using NagCFunctionsAPI; public class NagE04Functions { public static void nag_opt_nlp (int n, int nclin, int ncnlin, double [,] a, double [] bl, double [] bu, NAG_E04UCC_OBJFUN objfun, NAG_E04UCC_CONFUN confun, double [] x, ref double objf, double [] g, ref Nag_E04_Opt options, ref CommStruct user_comm, ref NagError fail) { NagFunctions.e04xxc(ref options); /* 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'; */ int tda = n; NagFunctions.e04ucc(n, nclin, ncnlin, a, tda, bl, bu, objfun, confun, x, ref objf, g, ref options, ref user_comm, ref fail); } public static void Main() { NAG_E04UCC_OBJFUN OBJFUN = new NAG_E04UCC_OBJFUN (objfun); NAG_E04UCC_CONFUN CONFUN = new NAG_E04UCC_CONFUN (confun); int n = 4; int nclin = 1; int ncnlin = 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}; double [] x = {1.0, 5.0, 5.0, 1.0}; double objf = 0.0; double [] g = new double[n]; Nag_E04_Opt options = new Nag_E04_Opt(); CommStruct user_comm = new CommStruct(); NagError fail = new NagError(); nag_opt_nlp(n, nclin, ncnlin, a, bl, bu, OBJFUN, CONFUN, x, ref objf, g, ref options, ref user_comm, ref fail); if (fail.code != 0) Console.WriteLine(fail.char_array); } public static void objfun(int n, IntPtr x_ptr, ref double objf, [In, Out] IntPtr g_ptr, ref CommStruct comm) { double [] x = new double[n]; Marshal.Copy( x_ptr, x, 0, n ); double [] g = new double[n]; if (comm.flag == 0 || comm.flag == 2) { objf = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]; } if (comm.flag == 2) { g[0] = x[3] * (2.0*x[0] + x[1] + x[2]); g[1] = x[0] * x[3]; g[2] = x[0] * x[3] + 1.0; g[3] = x[0] * (x[0] + x[1] + x[2]); } // if (x[1] > 2.0 ) // comm.flag = -1; Marshal.Copy( g, 0, g_ptr, n); } public static void confun(int n, int ncnlin, IntPtr needc_ptr, IntPtr x_ptr, [In, Out] IntPtr conf_ptr, [In, Out] IntPtr conjac_ptr, ref CommStruct 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 (comm.flag == 0 || comm.flag == 2) conf[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3]; if (comm.flag == 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 (comm.flag == 0 || comm.flag == 2) conf[1] = x[0] * x[1] * x[2] * x[3]; if (comm.flag == 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 */ }