#! /usr/bin/env python # def bohach1 ( m, x ): #*****************************************************************************80 # ## BOHACH1 evaluates the Bohachevsky function #1. # # Discussion: # # The minimizer is # # X* = [ 0.0, 0.0 ] # F(X*) = 0.0 # # Suggested starting point: # # X = [ 0.5, 1.0 ] # # 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] - 0.3 * np.cos ( 3.0 * np.pi * x[0] ) \ + 2.0 * x[1] * x[1] - 0.4 * np.cos ( 4.0 * np.pi * x[1] ) \ + 0.7 return f def bohach1_test ( ): #*****************************************************************************80 # ## BOHACH1_TEST works with the Bohachevsky function #1. # # 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 ( 'BOHACH1_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the Bohachevsky function #1' ) m = 2 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ 0.5, 1.0 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( bohach1 ( m, x ) ) ) x, fx, k = compass_search ( bohach1, m, x, delta_tol, delta, k_max ) r8vec_print ( m, x, ' Estimated minimizer X[1]:' ) 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 ( '' ) print ( ' F(X*) = %g' % ( bohach1 ( m, x ) ) ) # # Terminate. # print ( '' ) print ( 'BOHACH1_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) bohach1_test ( ) timestamp ( )