#! /usr/bin/env python # def extended_rosenbrock ( m, x ): #*****************************************************************************80 # ## EXTENDED_ROSENBROCK computes the extended Rosenbrock function. # # Discussion: # # The number of dimensions is arbitrary, except that it must be even. # # There is a global minimum at X* = (1,1,...), F(X*) = 0. # # The contours are sharply twisted. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 January 2016 # # Author: # # John Burkardt # # Reference: # # Howard Rosenbrock, # An Automatic Method for Finding the Greatest or Least Value of a Function, # Computer Journal, # Volume 3, 1960, pages 175-184. # # 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 r = np.zeros ( m ) for i in range ( 0, m, 2 ): r[i] = 1.0 - x[i] r[i+1] = 10.0 * ( x[i+1] - x[i] ** 2 ) f = np.dot ( r, r ) return f def extended_rosenbrock_test ( ): #*****************************************************************************80 # ## EXTENDED_ROSENBROCK_TEST works with the extended Rosenbrock 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 ( 'EXTENDED_ROSENBROCK_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the extended Rosenbrock function.' ) m = 4 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ - 1.2, 1.0, -1.5, 1.2 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( extended_rosenbrock ( m, x ) ) ) x, fx, k = compass_search ( extended_rosenbrock, 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 ( [ 1.0, 1.0, 1.0, 1.0 ] ) r8vec_print ( m, x, ' Correct minimizer X*:' ) print ( '' ) print ( ' F(X*) = %g' % ( extended_rosenbrock ( m, x ) ) ) # # Terminate. # print ( '' ) print ( 'EXTENDED_ROSENBROCK_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) extended_rosenbrock_test ( ) timestamp ( )