#! /usr/bin/env python # def bohach2 ( m, x ): #*****************************************************************************80 # ## BOHACH2 evaluates the Bohachevsky function #2. # # Discussion: # # The minimizer is # # X* = [ 0.0, 0.0 ] # F(X*) = 0.0 # # Suggested starting point: # # X = [ 0.6, 1.3 ] # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 January 2016 # # Author: # # John Burkardt # # Reference: # # Zbigniew Michalewicz, # Genetic Algorithms + Data Structures = Evolution Programs, # Third Edition, # Springer Verlag, 1996, # ISBN: 3-540-60676-9, # LC: QA76.618.M53. # # 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. # import numpy as np f = x[0] * x[0] \ + 2.0 * x[1] * x[1] \ - 0.3 * np.cos ( 3.0 * np.pi * x[0] ) * np.cos ( 4.0 * np.pi * x[1] ) \ + 0.3 return f def bohach2_test ( ): #*****************************************************************************80 # ## BOHACH2_TEST works with the Bohachevsky function #2. # # 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 ( 'BOHACH2_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the Bohachevsky function #2.' ) m = 2 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ 0.6, 1.3 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) f = bohach2 ( m, x ) print ( ' F(X0) = %g' % ( f ) ) x, fx, k = compass_search ( bohach2, 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 correct minimizer. # x = np.array ( [ 0.0, 0.0 ] ) r8vec_print ( m, x, ' Correct minimizer X*:' ) print ( '' ) f = bohach2 ( m, x ) print ( ' F(X*) = %g' % ( f ) ) # # Terminate. # print ( '' ) print ( 'BOHACH2_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) bohach2_test ( ) timestamp ( )