using System; using System.Text; using System.Runtime.InteropServices; using NagLibrary; public class NagE04Functions { public static void Main() { nag_declarations.nag_opt_nlp_solve_objfun_DELEGATE OBJFUN = new nag_declarations.nag_opt_nlp_solve_objfun_DELEGATE(objfun); nag_declarations.nag_opt_nlp_solve_confun_DELEGATE CONFUN = new nag_declarations.nag_opt_nlp_solve_confun_DELEGATE(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_declarations.Nag_Comm comm = new nag_declarations.Nag_Comm(); nag_declarations.Nag_E04State e04state = new nag_declarations.Nag_E04State(); nag_declarations.NagError fail = new nag_declarations.NagError(); fail.message = new byte[512]; Encoding enc = Encoding.ASCII; /* nag_opt_nlp_init (e04wcc). * Initialization function for nag_opt_nlp_solve (e04wdc) */ nag_declarations.nag_opt_nlp_init(ref e04state, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_opt_nlp_init (e04wcc):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } int writemode = 2; int fileid = 0; /* nag_open_file (x04acc). * By default nag_opt_nlp_solve (e04wdc) does not print monitoring * information. Call nag_open_file (x04acc) to set the print file fileid. */ nag_declarations.nag_open_file("", writemode, ref fileid, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_open_file (x04acc):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } /* nag_opt_nlp_option_set_integer (e04wgc). * Set the print file fileid as an option for nag_opt_nlp_solve (e04wdc) */ nag_declarations.nag_opt_nlp_option_set_integer("Print file", fileid, ref e04state, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_opt_nlp_option_set_integer (e04wgc):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } /* nag_opt_nlp_solve (e04wdc). * Solve the nonlinear programming (NP) problem */ nag_declarations.nag_opt_nlp_solve(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); if (fail.code != 0) { Console.WriteLine("Error from nag_opt_nlp_solve (e04wdc):"); Console.WriteLine(enc.GetString(fail.message)); Environment.Exit(fail.code); } Console.WriteLine("\nFinal value of the objective function = {0}", objf); Console.WriteLine("At the point:"); for (int i = 0; i < x.Length; i++) { Console.Write("{0} ", x[i]); } Console.WriteLine(); } public static void objfun(ref int mode, int n, IntPtr x_ptr, ref double objf, IntPtr g_ptr, int nstate, 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 (mode == 0 || mode == 2) { objf = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]; } if (mode == 1 || mode == 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(ref int mode, int ncnlin, int n, int pdcj, IntPtr needc_ptr, IntPtr x_ptr, IntPtr conf_ptr, IntPtr conjac_ptr, int nstate, 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 (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); } }