using System; using System.Runtime.InteropServices; using NagCFunctionsAPI; public class NagE04Functions { public static void nag_opt_nlin_lsq (int m, int n, int nclin, int ncnlin, double [,] a, double [] bl, double [] bu, double [] y, NAG_E04UNC_OBJFUN objfun, NAG_E04UNC_CONFUN confun, double [] x, ref double objf, double [] f, double [,] fjac, 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=Nag_PrintType.Nag_Soln_Iter; options.max_iter=100; int tda = n; int tdfjac = n; NagFunctions.e04unc(m, n, nclin, ncnlin, a, tda, bl, bu, y, objfun, confun, x, ref objf, f, fjac, tdfjac, ref options, ref user_comm, ref fail); } public static void Main() { NAG_E04UNC_OBJFUN OBJFUN = new NAG_E04UNC_OBJFUN (objfun); NAG_E04UNC_CONFUN CONFUN = new NAG_E04UNC_CONFUN (confun); int m = 44; int n = 2; int nclin = 1; int ncnlin = 1; 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_E04_Opt options = new Nag_E04_Opt(); CommStruct user_comm = new CommStruct(); NagError fail = new NagError(); nag_opt_nlin_lsq(m, n, nclin, ncnlin, a, bl, bu, y, OBJFUN, CONFUN, x, ref objf, f, fjac, ref options, ref user_comm, ref fail); } public static void objfun(int m, int n, IntPtr x_ptr, [In, Out] IntPtr f_ptr, [In, Out] IntPtr fjac_ptr, int tdfjac, ref CommStruct 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]; Marshal.Copy( x_ptr, x, 0, n ); double [] f = new double[m]; double [] fjac = new double[m*n]; /* Function to 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, [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. */ if (comm.first == 1) { /* First call to confun. Set all Jacobian elements to zero. * Note that this will only work when options.obj_deriv = TRUE * (the default). */ 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 ); } /* confun */ }