using System; using System.Text; using System.Runtime.InteropServices; using NagLibrary; public class NagE04Functions { public static void Main() { nag_declarations.nag_opt_nlin_lsq_objfun_DELEGATE OBJFUN = new nag_declarations.nag_opt_nlin_lsq_objfun_DELEGATE(objfun); nag_declarations.nag_opt_nlin_lsq_confun_DELEGATE CONFUN = new nag_declarations.nag_opt_nlin_lsq_confun_DELEGATE(confun); int m = 44; int n = 2; int nclin = 1; int ncnlin = 1; int tda = n; int tdfjac = n; double[] a = { 1.0, 1.0 }; double[] bl = { 0.4, -4.0, 1.0, 0.0 }; double[] bu = { 1.0e+25, 1.0e+25, 1.0e+25, 1.0e+25 }; double[] y = { 0.49, 0.49, 0.48, 0.47, 0.48, 0.47, 0.46, 0.46, 0.45, 0.43, 0.45, 0.43, 0.43, 0.44, 0.43, 0.43, 0.46, 0.45, 0.42, 0.42, 0.43, 0.41, 0.41, 0.40, 0.42, 0.40, 0.40, 0.41, 0.40, 0.41, 0.41, 0.40, 0.40, 0.40, 0.38, 0.41, 0.40, 0.40, 0.41, 0.38, 0.40, 0.40, 0.39, 0.39 }; double[] x = { 0.4, 0.0 }; double objf = 0.0; double[] f = new double[m]; double[] fjac = new double[m * 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); options.optim_tol = 1.0e-3; options.print_level = nag_declarations.Nag_PrintType.Nag_Soln_Iter; options.max_iter = 100; /* nag_opt_nlin_lsq (e04unc). * Minimization of a smooth nonlinear sum of squares function */ nag_declarations.nag_opt_nlin_lsq(m, n, nclin, ncnlin, a, tda, bl, bu, y, OBJFUN, CONFUN, x, ref objf, f, fjac, tdfjac, ref options, ref user_comm, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_opt_nlin_lsq (e04unc):"); Console.WriteLine(enc.GetString(fail.message)); } } public static void objfun(int m, int n, IntPtr x_ptr, IntPtr f_ptr, IntPtr fjac_ptr, int tdfjac, ref nag_declarations.Nag_Comm comm) { double[] a = { 8.0, 8.0, 10.0, 10.0, 10.0, 10.0, 12.0, 12.0, 12.0, 12.0, 14.0, 14.0, 14.0, 16.0, 16.0, 16.0, 18.0, 18.0, 20.0, 20.0, 20.0, 22.0, 22.0, 22.0, 24.0, 24.0, 24.0, 26.0, 26.0, 26.0, 28.0, 28.0, 30.0, 30.0, 30.0, 32.0, 32.0, 34.0, 36.0, 36.0, 38.0, 38.0, 40.0, 42.0 }; double temp; int i; double x0, x1, ai; double[] x = new double[n]; double[] f = new double[m]; double[] fjac = new double[m * n]; Marshal.Copy(x_ptr, x, 0, n); /* Evaluate the objective subfunctions and their 1st derivatives. */ x0 = x[0]; x1 = x[1]; for (i = 0; i < m; ++i) { ai = a[i]; temp = Math.Exp(-x1 * (ai - 8.0)); /* Evaluate objective subfunction f(i+1) only if required */ if (comm.needf == i + 1 || comm.needf == 0) f[i] = x0 + (.49 - x0) * temp; if (comm.flag == 2) { fjac[i * tdfjac + 0] = 1.0 - temp; fjac[i * tdfjac + 1] = -(.49 - x0) * (ai - 8.0) * temp; } } Marshal.Copy(f, 0, f_ptr, m); Marshal.Copy(fjac, 0, fjac_ptr, m * 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[] 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. */ conjac[0 * ncnlin + 0] = conjac[0 * ncnlin + 1] = 0.0; } if (needc[0] > 0) { conf[0] = -0.09 - x[0] * x[1] + 0.49 * x[1]; if (comm.flag == 2) { conjac[0 * ncnlin + 0] = -x[1]; conjac[0 * ncnlin + 1] = -x[0] + 0.49; } } Marshal.Copy(conf, 0, conf_ptr, ncnlin); Marshal.Copy(conjac, 0, conjac_ptr, ncnlin * n); } }