NAG Library Function Document nag_opt_simplex_easy (e04cbc)

Similar documents
NAG Library Function Document nag_quad_md_gauss (d01fbc)

nag opt simplex (e04ccc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

NAG Library Function Document nag_matop_complex_gen_matrix_fun_num (f01flc)

NAG Library Function Document nag_matop_real_gen_matrix_fun_num (f01elc)

NAG Library Function Document. nag_matop_real_gen_matrix_cond_num

NAG Library Function Document. nag_matop_complex_gen_matrix_cond_num

NAG Library Function Document nag_outlier_peirce (g07gac)

NAG Library Function Document nag_quad_2d_fin (d01dac)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_zero_nonlin_eqns_rcomm (c05qdc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_legendre_p (s22aac)

NAG Library Function Document nag_tsa_cp_pelt_user (g13nbc)

NAG Library Function Document nag_ode_bvp_fd_nonlin_fixedbc (d02gac)

NAG Library Function Document nag_robust_m_corr_user_fn_no_derr (g02hmc)

NAG Library Function Document nag_4d_shep_eval (e01tlc)

NAG Library Function Document nag_stable_sort (m01ctc)

NAG Library Function Document nag_robust_m_corr_user_fn (g02hlc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG Library Function Document nag_sum_convcorr_real (c06fkc)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

NAG Library Function Document nag_summary_stats_onevar_combine (g01auc)

NAG Library Function Document nag_bsm_price (s30aac)

NAG Library Function Document nag_ode_ivp_rk_interp (d02pxc)

NAG Library Function Document nag_ode_bvp_fd_lin_gen (d02gbc)

NAG Library Function Document nag_ode_ivp_rkts_errass (d02puc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_full_step_regsn (g02efc)

NAG Library Function Document nag_dtpttr (f01vcc)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG Library Function Document nag_1d_quad_inf_1 (d01smc)

NAG Library Function Document nag_dtfttp (f01vlc)

NAG Library Function Document nag_sum_fft_cosine (c06rfc)

NAG Library Function Document nag_real_gen_matrix_exp (f01ecc)

NAG Library Function Document nag_2d_panel_sort (e02zac)

NAG Library Function Document nag_dtrttf (f01vec)

NAG Library Function Document nag_binomial_ci (g07aac)

NAG Library Function Document nag_frequency_table (g01aec)

NAG C Library Function Document nag_linf_fit (e02gcc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

NAG Library Function Document nag_ode_bvp_ps_lin_cheb_eval (d02uzc)

NAG Library Function Document nag_wfilt (c09aac)

NAG Library Function Document nag_1d_cheb_intg (e02ajc)

NAG Library Function Document nag_dger (f16pmc)

NAG Library Function Document nag_ztfttp (f01vmc)

NAG Library Function Document nag_nd_shep_eval (e01znc)

NAG Library Function Document nag_asian_geom_price (s30sac)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_zero_cont_func_brent_rcomm (c05azc)

NAG Library Function Document nag_zaxpby (f16gcc)

NAG Library Function Document nag_tsa_dickey_fuller_unit (g13awc)

NAG Library Function Document nag_all_regsn (g02eac)

NAG Library Function Document nag_sum_fft_real_3d (c06pyc)

NAG Library Function Document nag_tsa_cp_binary (g13ndc)

NAG Library Function Document nag_ztrttf (f01vfc)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_dwt (c09cac)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_2d_spline_deriv_rect (e02dhc)

NAG Library Function Document nag_dgetrf (f07adc)

NAG Library Function Document nag_dspr (f16pqc)

NAG Library Function Document nag_search_vector (m01fsc)

NAG Library Function Document nag_prob_lin_non_central_chi_sq (g01jcc)

NAG Library Function Document nag_1d_spline_fit (e02bec)

NAG Library Function Document nag_rand_copula_normal (g05rdc)

NAG Library Function Document nag_zhfrk (f16zqc)

NAG Library Function Document nag_zpbequ (f07htc)

NAG Library Function Document nag_1d_spline_evaluate (e02bbc)

NAG Library Function Document nag_superlu_lu_factorize (f11mec)

NAG Library Function Document nag_1d_spline_interpolant (e01bac)

NAG Library Function Document nag_pde_parab_1d_euler_exact (d03pxc)

NAG Library Function Document nag_zsymv (f16tac)

NAG Library Function Document nag_dgttrs (f07cec)

NAG Library Function Document nag_prob_non_central_f_dist (g01gdc)

NAG Library Function Document nag_zgelsy (f08bnc)

NAG Library Function Document nag_zhpev (f08gnc)

NAG Library Function Document nag_dsf_norm (f16rkc)

NAG Library Function Document nag_dtrmv (f16pfc)

NAG Library Function Document nag_1d_spline_deriv (e02bcc)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_dtpsv (f16plc)

NAG Library Function Document nag_mldwt_2d (c09ecc)

NAG Library Function Document nag_complex_bessel_j (s17dec)

NAG Library Function Document nag_dgelsd (f08kcc)

NAG Library Function Document nag_tsa_spectrum_univar_cov (g13cac)

NAG Library Function Document nag_complex_bessel_y (s17dcc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_sparse_nherm_jacobi (f11dxc)

NAG C Library Function Document nag_dgemv (f16pac)

NAG C Library Function Document nag_multid_quad_monte_carlo_1 (d01xbc)

NAG C Library Function Document nag_zgemv (f16sac)

NAG Library Function Document nag_complex_bessel_i (s18dec)

NAG Library Function Document nag_rngs_copula_students_t (g05rbc)

NAG Library Function Document nag_dgglse (f08zac)

NAG Library Function Document nag_1d_cheb_fit_constr (e02agc)

NAG Library Function Document nag_dgb_norm (f16rbc)

NAG C Library Function Document nag_rank_regsn (g08rac)

Transcription:

e04 Minimizing or Maximizing a Function NAG Library Function Document nag_opt_simplex_easy () 1 Purpose nag_opt_simplex_easy () minimizes a general function FðÞ x of n independent variables x ¼ ðx 1 ;x 2 ;...;x n Þ T by the Nelder and Mead simplex method (see Nelder and Mead (1965)). Derivatives of the function need not be supplied. 2 Specification #include <nag.h> #include <nage04.h> void nag_opt_simplex_easy (Integer n, double x[], double *f, double tolf, double tolx, void (*funct)(integer n, const double xc[], double *fc, Nag_Comm *comm), void (*monit)(double fmin, double fmax, const double sim[], Integer n, Integer ncall, double serror, double vratio, Nag_Comm *comm), Integer maxcal, Nag_Comm *comm, NagError *fail) 3 Description nag_opt_simplex_easy () finds an approximation to a minimum of a function F of n variables. You must supply a function to calculate the value of F for any set of values of the variables. The method is iterative. A simplex of n þ 1 points is set up in the n-dimensional space of the variables (for example, in 2 dimensions the simplex is a triangle) under the assumption that the problem has been scaled so that the values of the independent variables at the minimum are of order unity. The starting point you have provided is the first vertex of the simplex, the remaining n vertices are generated by nag_opt_simplex_easy (). The vertex of the simplex with the largest function value is reflected in the centre of gravity of the remaining vertices and the function value at this new point is compared with the remaining function values. Depending on the outcome of this test the new point is accepted or rejected, a further expansion move may be made, or a contraction may be carried out. See Nelder and Mead (1965) and Parkinson and Hutchinson (1972) for more details. When no further progress can be made the sides of the simplex are reduced in length and the method is repeated. The method can be slow, but computational bottlenecks have been reduced following Singer and Singer (2004). However, nag_opt_simplex_easy () is robust, and therefore very useful for functions that are subject to inaccuracies. There are the following options for successful termination of the method: based only on the function values at the vertices of the current simplex (see (1)); based only on a volume ratio between the current simplex and the initial one (see (2)); or based on which one of the previous two tests passes first. The volume test may be useful if F is discontinuous, while the function-value test should be sufficient on its own if F is continuous. 4 References Nelder J A and Mead R (1965) A simplex method for function minimization Comput. J. 7 308 313 Parkinson J M and Hutchinson D (1972) An investigation into the efficiency of variants of the simplex method Numerical Methods for Nonlinear Optimization (ed F A Lootsma) Academic Press Singer S and Singer S (2004) Efficient implementation of the Nelder Mead search algorithm Appl. Num. Anal. Comp. Math. 1(3) 524 534 Mark 25.1

NAG Library Manual 5 Arguments 1: n Integer Input On entry: n, the number of variables. Constraint: n 1. 2: x½nš double Input/Output On entry: a guess at the position of the minimum. Note that the problem should be scaled so that the values of the x½i 1Š are of order unity. On exit: the value of x corresponding to the function value in f. 3: f double * Output On exit: the lowest function value found. 4: tolf double Input On entry: the error tolerable in the function values, in the following sense. If f i, for i ¼ 1; 2;...;nþ 1, are the individual function values at the vertices of the current simplex, and if f m is the mean of these values, then you can request that nag_opt_simplex_easy () should terminate if vffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffi u 1 X t nþ1 ðf i f m Þ 2 < tolf: ð1þ n þ 1 i¼1 You may specify tolf ¼ 0 if you wish to use only the termination criterion (2) on the spatial values: see the description of tolx. Constraint: tolf must be greater than or equal to the machine precision (see Chapter x02), or if tolf equals zero then tolx must be greater than or equal to the machine precision. 5: tolx double Input On entry: the error tolerable in the spatial values, in the following sense. If LV denotes the linearized volume of the current simplex, and if LV init denotes the linearized volume of the initial simplex, then you can request that nag_opt_simplex_easy () should terminate if LV LV init < tolx: ð2þ You may specify tolx ¼ 0 if you wish to use only the termination criterion (1) on function values: see the description of tolf. Constraint: tolx must be greater than or equal to the machine precision (see Chapter x02), or if tolx equals zero then tolf must be greater than or equal to the machine precision. 6: funct function, supplied by the user External Function funct must evaluate the function F at a specified point. It should be tested separately before being used in conjunction with nag_opt_simplex_easy (). The specification of funct is: void funct (Integer n, const double xc[], double *fc, Nag_Comm *comm) 1: n Integer Input On entry: n, the number of variables..2 Mark 25

e04 Minimizing or Maximizing a Function 2: xc½nš const double Input On entry: the point at which the function value is required. 3: fc double * Output On exit: the value of the function F at the current point x. 4: comm Nag_Comm * Pointer to structure of type Nag_Comm; the following members are relevant to funct. user double * iuser Integer* p Pointer The type Pointer will be void *. Before calling nag_opt_simplex_easy () you may allocate memory and initialize these pointers with various quantities for use by funct when called from nag_opt_simplex_easy () (see Section 3.2.1.1 in the Essential Introduction). 7: monit function, supplied by the user External Function monit may be used to monitor the optimization process. It is invoked once every iteration. If no monitoring is required, monit maybespecifiedasnullfn. The specification of monit is: void monit (double fmin, double fmax, const double sim[], Integer n, Integer ncall, double serror, double vratio, Nag_Comm *comm) 1: fmin double Input On entry: the smallest function value in the current simplex. 2: fmax double Input On entry: the largest function value in the current simplex. 3: sim½ðn þ 1ÞnŠ const double Input On entry: the n þ 1 position vectors of the current simplex, where sim½ðj 1Þðn þ 1Þþi 1Š is the jth coordinate of the ith position vector. 4: n Integer Input On entry: n, the number of variables. 5: ncall Integer Input On entry: the number of times that funct has been called so far. 6: serror double Input On entry: the current value of the standard deviation in function values used in termination test (1). 7: vratio double Input On entry: the current value of the linearized volume ratio used in termination test (2). 8: comm Nag_Comm * Pointer to structure of type Nag_Comm; the following members are relevant to monit. Mark 25.3

NAG Library Manual user double * iuser Integer* p Pointer The type Pointer will be void *. Before calling nag_opt_simplex_easy () you may allocate memory and initialize these pointers with various quantities for use by monit when called from nag_opt_simplex_easy () (see Section 3.2.1.1 in the Essential Introduction). 8: maxcal Integer Input On entry: the maximum number of function evaluations to be allowed. Constraint: maxcal 1. 9: comm Nag_Comm * The NAG communication argument (see Section 3.2.1.1 in the Essential Introduction). 10: fail NagError* Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). 6 Error Indicators and Warnings NE_ALLOC_FAIL Dynamic memory allocation failed. See Section 3.2.1.2 in the Essential Introduction for further information. NE_BAD_PARAM On entry, argument NE_INT On entry, maxcal ¼ hvaluei. Constraint: maxcal 1. On entry, n ¼ hvaluei. Constraint: n 1. hvaluei had an illegal value. NE_INTERNAL_ERROR An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance. An unexpected error has been triggered by this function. Please contact NAG. See Section 3.6.6 in the Essential Introduction for further information. NE_NO_LICENCE Your licence key may have expired or may not have been installed correctly. See Section 3.6.5 in the Essential Introduction for further information. NE_REAL On entry, tolf ¼ 0:0 andtolx ¼ hvaluei. Constraint: if tolf ¼ 0:0 thentolx is greater than or equal to the machine precision. On entry, tolx ¼ 0:0 andtolf ¼ hvaluei. Constraint: if tolx ¼ 0:0 thentolf is greater than or equal to the machine precision..4 Mark 25

e04 Minimizing or Maximizing a Function NE_REAL_2 On entry, tolf ¼ hvaluei and tolx ¼ hvaluei. Constraint: if tolf 6¼ 0:0 andtolx 6¼ 0:0 then both should be greater than or equal to the machine precision. NW_TOO_MANY_FEVALS maxcal function evaluations have been completed without any other termination test passing. Check the coding of funct before increasing the value of maxcal. 7 Accuracy On a successful exit the accuracy will be as defined by tolf or tolx, depending on which criterion was satisfied first. 8 Parallelism and Performance Not applicable. 9 Further Comments Local workspace arrays of fixed lengths (depending on n) are allocated internally by nag_opt_simplex_easy (). The total size of these arrays amounts to n 2 þ 6n þ 2 double elements. The time taken by nag_opt_simplex_easy () depends on the number of variables, the behaviour of the function and the distance of the starting point from the minimum. Each iteration consists of 1 or 2 function evaluations unless the size of the simplex is reduced, in which case n þ 1 function evaluations are required. 10 Example This example finds a minimum of the function Fðx 1 ;x 2 Þ ¼ e x 1 4x 2 1 þ 2x2 2 þ 4x 1x 2 þ 2x 2 þ 1 : This example uses the initial point ð 1; 1Þ (see Section 10.3), and we expect to reach the minimum at ð0:5; 1Þ. 10.1 Program Text /* nag_opt_simplex_easy () Example Program. * * Copyright 2014 Numerical Algorithms Group. * * Mark 9 revised, 2009. */ #include <nag.h> #include <math.h> #include <stdio.h> #include <nag_stdlib.h> #include <nage04.h> #include <nagx02.h> #ifdef cplusplus extern "C" #endif static void NAG_CALL funct(const Integer n, const double *xc, double *fc, Nag_Comm *comm); static void NAG_CALL monit(const double fmin, const double fmax, const double sim[], const Integer n, const Integer ncall, const double serror, const double vratio, Nag_Comm *comm); Mark 25.5

NAG Library Manual #ifdef cplusplus #endif int main(void) /* Scalars */ double f, tolf, tolx; Integer exit_status, i, monitoring, maxcal = 100, n = 2; NagError fail; /* Arrays */ static double ruser[2] = -1.0, -1.0; double *x = 0; Nag_Comm comm; exit_status = 0; INIT_FAIL(fail); printf("nag_opt_simplex_easy () Example Program Results\n"); /* For communication with user-supplied functions: */ comm.user = ruser; /* Allocate memory */ if (!(x = NAG_ALLOC(n, double))) printf("allocation failure\n"); exit_status = -1; goto END; /* Set monitoring to a nonzero value to obtain monitoring information */ monitoring = 0; comm.p = (Pointer)&monitoring; /* Starting values */ x[0] = -1.0; x[1] = 1.0; tolf = sqrt(nag_machine_precision); tolx = sqrt(tolf); nag_opt_simplex_easy(n, x, &f, tolf, tolx, funct, monit, maxcal, &comm, &fail); if (fail.code!= NE_NOERROR) printf("error from nag_opt_simplex_easy ().\n%s\n", fail.message); exit_status = 1; goto END; printf("the final function value is %12.4f\n", f); printf("at the point"); for (i = 1; i <= n; ++i) printf(" %12.4f", x[i-1]); printf("\n"); END: NAG_FREE(x); return exit_status; static void NAG_CALL funct(const Integer n, const double *xc, double *fc, Nag_Comm *comm).6 Mark 25

e04 Minimizing or Maximizing a Function if (comm->user[0] == -1.0) printf("(user-supplied callback funct, first invocation.)\n"); comm->user[0] = 0.0; *fc = exp(xc[0])*(4.0*xc[0]*(xc[0]+xc[1])+2.0*xc[1]*(xc[1]+1.0)+1.0); static void NAG_CALL monit(const double fmin, const double fmax, const double sim[], const Integer n, const Integer ncall, const double serror, const double vratio, Nag_Comm *comm) #define SIM(I, J) sim[(j-1)*(n+1) + (I-1)] Integer i, j; Integer monitoring = *(Integer *)comm->p; if (comm->user[1] == -1.0) printf("(user-supplied callback monit, first invocation.)\n"); comm->user[1] = 0.0; if (monitoring!= 0) printf("\nthere have been %5"NAG_IFMT" function calls\n", ncall); printf("the smallest function value is %10.4f\n", fmin); printf("\nthe simplex is\n"); for (i = 1; i <= n+1; ++i) for (j = 1; j <= n; ++j) printf(" %13.4e", SIM(i, j)); printf("\n"); printf("\nthe standard deviation in function values at the" " vertices of the simplex is %10.4f\n", serror); printf("the linearized volume ratio of the current simplex" " to the starting one is %10.4f\n", vratio); 10.2 Program Data None. 10.3 Program Results nag_opt_simplex_easy () Example Program Results (User-supplied callback funct, first invocation.) (User-supplied callback monit, first invocation.) The final function value is 0.0000 at the point 0.5000-0.9999 Mark 25.7

NAG Library Manual 2 Example Program Contours of F Showing the Initial Point (X) and Local Minimum (*) 1.5 1 X 0.5 0-0.5-1 * -1.5-2 -1.5-1 -0.5 0 0.5 1 1.5.8 (last) Mark 25