#! /usr/bin/env python # def goldstein_price ( m, x ): #*****************************************************************************80 # ## GOLDSTEIN_PRICE evaluates the Goldstein-Price polynomial. # # Discussion: # # The minimizer is # # X* = [ 0.0, -1.0 ] # F(X*) = 3.0 # # Suggested starting point: # # X = [ -0.5, 0.25 ] (easy convergence) # X = [ -4.0, 5.00 ] (harder convergence) # # 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. # a = x[0] + x[1] + 1.0 b = 19.0 - 14.0 * x[0] + 3.0 * x[0] * x[0] - 14.0 * x[1] \ + 6.0 * x[0] * x[1] + 3.0 * x[1] * x[1] c = 2.0 * x[0] - 3.0 * x[1] d = 18.0 - 32.0 * x[0] + 12.0 * x[0] * x[0] + 48.0 * x[1] \ - 36.0 * x[0] * x[1] + 27.0 * x[1] * x[1] f = ( 1.0 + a * a * b ) * ( 30.0 + c * c * d ) return f def goldstein_price_test ( ): #*****************************************************************************80 # ## GOLDSTEIN_PRICE_TEST works with the Goldstein-Price 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 ( 'GOLDSTEIN_PRICE_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the Goldstein-Price function.' ) m = 2 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ -0.5, 0.25 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( goldstein_price ( m, x ) ) ) x, fx, k = compass_search ( goldstein_price, 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 ) ) # # Repeat with more difficult start. # x = np.array ( [ -4.0, 5.0 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( goldstein_price ( m, x ) ) ) x, fx, k = compass_search ( goldstein_price, 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, -1.0 ] ) r8vec_print ( m, x, ' Correct minimizer X*:' ) print ( '' ) print ( ' F(X*) = %g' % ( goldstein_price ( m, x ) ) ) # # Terminate. # print ( '' ) print ( 'GOLDSTEIN_PRICE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) goldstein_price_test ( ) timestamp ( )