#! /usr/bin/env python # def rosenbrock ( m, x ): #*****************************************************************************80 # ## ROSENBROCK computes the Rosenbrock function. # # Discussion: # # There is a global minimum at X* = (1,1), F(X*) = 0. # # The starting point X = [ -1.2, 1.0 ] is suggested. # # 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. # f = ( 1.0 - x[0] ) ** 2 + \ 100.0 * ( x[1] - x[0] * x[0] ) ** 2 return f def rosenbrock_test ( ): #*****************************************************************************80 # ## ROSENBROCK_TEST works with the 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 ( 'ROSENBROCK_TEST:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test COMPASS_SEARCH with the Rosenbrock function.' ) m = 2 delta_tol = 0.00001 delta = 0.3 k_max = 20000 x = np.array ( [ - 1.2, 1.0 ] ) r8vec_print ( m, x, ' Initial point X0:' ) print ( '' ) print ( ' F(X0) = %g' % ( rosenbrock ( m, x ) ) ) x, fx, k = compass_search ( 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 ] ) r8vec_print ( m, x, ' Correct minimizer X*:' ) print ( '' ) print ( ' F(X*) = %g' % ( rosenbrock ( m, x ) ) ) # # Terminate. # print ( '' ) print ( 'ROSENBROCK_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) rosenbrock_test ( ) timestamp ( )