using System; using System.Text; using System.Runtime.InteropServices; using NagLibrary; public class NagE04Functions { public static void Main() { nag_declarations.nag_opt_nlp_objfun_DELEGATE OBJFUN = new nag_declarations.nag_opt_nlp_objfun_DELEGATE(objfun); nag_declarations.nag_opt_nlp_confun_DELEGATE CONFUN = new nag_declarations.nag_opt_nlp_confun_DELEGATE(confun); int n = 4; int nclin = 1; int ncnlin = 2; int tda = n; 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_declarations.Nag_E04_Opt options = new nag_declarations.Nag_E04_Opt(); nag_declarations.Nag_Comm user_comm = new nag_declarations.Nag_Comm(); nag_declarations.NagError fail = new nag_declarations.NagError(); fail.message = new byte[512]; Encoding enc = Encoding.ASCII; /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_declarations.nag_opt_init(ref options); /* nag_opt_nlp (e04ucc). * Minimization of a smooth nonlinear function */ nag_declarations.nag_opt_nlp(n, nclin, ncnlin, a, n, bl, bu, OBJFUN, CONFUN, x, ref objf, g, ref options, ref user_comm, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_opt_nlp (e04ucc):"); Console.WriteLine(enc.GetString(fail.message)); } } public static void objfun(int n, IntPtr x_ptr, ref double objf, IntPtr g_ptr, ref nag_declarations.Nag_Comm comm) { double[] x = new double[n]; double[] g = new double[n]; Marshal.Copy(x_ptr, x, 0, n); /* Evaluate objective function and its 1st derivatives. */ 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]); } Marshal.Copy(g, 0, g_ptr, n); } public static void confun(int n, int ncnlin, IntPtr needc_ptr, IntPtr x_ptr, IntPtr conf_ptr, IntPtr conjac_ptr, ref nag_declarations.Nag_Comm comm) { int i, j; int[] needc = new int[ncnlin]; double[] x = new double[n]; double[] conjac = new double[ncnlin * n]; double[] conf = new double[ncnlin]; Marshal.Copy(needc_ptr, needc, 0, ncnlin); Marshal.Copy(x_ptr, x, 0, n); /* Evaluate the nonlinear constraints and their 1st derivatives. */ if (comm.first == nag_declarations.Nag_Boolean.Nag_TRUE) { /* First call to confun. Set all Jacobian elements to zero. */ 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); } }