hillclimbing <- function ( f, x, h = 1.0, m = 1000 ) #*****************************************************************************80 # ## hillclimbing minimizes a multivariate function using hill climbing. # # Licensing: # # Copyright 2016 James P. Howard, II # # The computer code and data files on this web page are distributed under # https://opensource.org/licenses/BSD-2-Clause, the BSD-2-Clause license. # # Modified: # # 17 March 2020 # # Author: # # Original R code by James Howard; # Modifications by John Burkardt. # # Reference: # # James Howard, # Computational Methods for Numerical Analysis with R, # CRC Press, 2017, # ISBN13: 978-1-4987-2363-3. # { n <- length ( x ) xcurr <- x ycurr <- f ( x ) for ( j in 1 : m ) { xnext <- xcurr i <- ceiling ( runif ( 1, 0, n ) ) xnext[i] <- rnorm ( 1, xcurr[i], h ) ynext <- f ( xnext ) if ( ynext < ycurr ) { xcurr <- xnext ycurr <- ynext } } x <- xcurr return ( x ) }