#! /usr/bin/env python # def local ( m, x ): #*****************************************************************************80 # ## LOCAL computes the local function. # # Discussion: # # This function has a local minimizer: # # X* = ( 0.2858054412..., 0.2793263206...), F(X*) = 5.9225... # # and a global minimizer: # # X* = ( -21.02667179..., -36.75997872...), F(X*) = 0. # # Suggested starting point for local minimizer: # # X = ( 1, 1 ), F(X) = 3.33 * 10^6. # # Suggested starting point for global minimizer: # # X = ( -15, -35), F(X) = 1.49 * 10^8. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 January 2016 # # Author: # # John Burkardt # # Reference: # # David Himmelblau, # Applied Nonlinear Programming, # McGraw Hill, 1972, # ISBN13: 978-0070289215, # LC: T57.8.H55. # # Parameters: # # Input, integer M, the number of variables. # # Input, real X(M), the argument of the function. # # Output, real F, the value of the function at X. # f = ( x[0] ** 2 + 12.0 * x[1] - 1.0 ) ** 2 \ + ( 49.0 * x[0] ** 2 + 49.0 * x[1] ** 2 + 84.0 * x[0] \ + 2324.0 * x[1] - 681.0 ) ** 2 return f def local_test ( ): #*****************************************************************************80 # ## LOCAL_TEST works with the Local function. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 January 2016 # # Author: # # John Burkardt # import numpy as np import platform from compass_search import compass_search from r8vec_print import r8vec_print print ( '' ) print ( 'LOCAL_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the Local function.' ) m = 2 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ 1.0, 1.0 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( local ( m, x ) ) ) x, fx, k = compass_search ( local, m, x, delta_tol, delta, k_max ) r8vec_print ( m, x, ' Estimated minimizer X1:' ) print ( '' ) print ( ' F(X1) = %g, number of steps = %d' % ( fx, k ) ) # # Demonstrate local minimizer. # x = np.array ( [ 0.2858054412, 0.2793263206 ] ) r8vec_print ( m, x, ' Correct local minimizer X*:' ) print ( '' ) print ( ' F(X*) = %g' % ( local ( m, x ) ) ) # # Try for global minimizer. # x = np.array ( [ -15.0, -35.0 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( local ( m, x ) ) ) x, fx, k = compass_search ( local, m, x, delta_tol, delta, k_max ) r8vec_print ( m, x, ' Estimated minimizer X1:' ) print ( '' ) print ( ' F(X1) = %g, number of steps = %d' % ( fx, k ) ) # # Demonstrate global minimizer. # x = np.array ( [ -21.02667179, -36.75997872 ] ) r8vec_print ( m, x, ' Correct global minimizer X*:' ) print ( '' ) print ( ' F(X*) = %g' % ( local ( m, x ) ) ) # # Terminate. # print ( '' ) print ( 'LOCAL_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) local_test ( ) timestamp ( )