grid_search: Carry out the grid search algorithm with a zoom.

View source: R/algorithms.R

grid_searchR Documentation

Description

This function carries out the grid search algorithm with a zoom.

Usage

grid_search(
  FUN,
  grid,
  MoreArgs = NULL,
  zoom = 0,
  decay = 0.5,
  num = 1,
  parallel = FALSE,
  cores = NULL,
  silent = TRUE
)

Arguments

FUN

the target function to be minimized.

grid

an object of class GRID created by build_grid.

MoreArgs

a named list of additional arguments to FUN, see mapply.

zoom

number of (additional) zoom-in layers, 0 by default.

decay

a number in (0,1) controlling the decay of subgrid sizes.

num

number of points to return at each grid search, 1 by default.

parallel

a logical; if TRUE, parallel computation is used.

cores

an integer specifying the requested number of workers when parallel = TRUE. If NULL, the function uses 2 workers by default (subject to future::availableCores()).

silent

a logical indicating whether progress information is printed.

Details

The target function FUN to be minimized is a scalar real-valued function. Maximization can be achieved by multiplying -1 to the original function and then passing the new function to FUN.

The grid must be created by build_grid.

Any other invariant arguments to FUN can be specified in MoreArgs using a named list, see mapply.

The common grid search first builds a grid within a bounded region, evaluates FUN at each grid point, and returns the num points that yield the smallest values.

zoom = 0 implies no zoom-in (a single grid search). Any integer zoom > 0 applies additional zoom-in layers. With zoom > 0, the algorithm performs

n^{0} + n^{1} + n^{2} + \cdots + n^{z}

grid searches, where n is num and z is zoom. Consequently, the total number of returned points is

n^{1} + n^{2} + n^{3} + \cdots + n^{z+1}

.

At each zoom-in layer, the algorithm builds subgrids around the best points found in the previous layer. To limit the computational burden, the subgrid size is reduced by the decay rate decay. For each parameter, the number of points in the subgrid is max(Int(decay * N), 3), where N is the number of points in the original grid for that parameter.

Parallel computation can be enabled by setting parallel = TRUE. In that case, the function uses the future framework with future::multisession (cross-platform). The number of workers is determined as follows:

  1. Let n be the value returned by future::availableCores().

  2. Let m be the user input cores. If cores = NULL, set m = 2.

  3. The number of workers is \min(m, n).

If parallel = TRUE, the packages future and future.apply must be installed.

The boolean silent controls whether progress information is printed to the console.

Value

a list with components:

par

the approximate global minimizer

points

all candidate points found by the grid search with zoom-in layers

Author(s)

Yukai Yang, yukai.yang@statistik.uu.se

See Also

build_grid, grid_search_check

Examples

# Rastrigin function
ndim = 2
nA = 10
Rastrigin <- function(vx) nA * ndim + sum(vx * vx - nA * cos(2 * pi * vx))

# Build a grid
bin = c(from = -5.12, to = 5.12, by = .5)
grid = build_grid(bin, bin)

# Serial computation
ret0 = grid_search(Rastrigin, grid, silent = FALSE)
ret0$par


# Finer grid
bin = c(from = -5.12, to = 5.12, by = .1)
grid = build_grid(bin, bin)

# Serial computation
ret1 = grid_search(Rastrigin, grid, silent = FALSE)
ret1$par

# Parallel computation (requires future and future.apply)
ret2 = grid_search(Rastrigin, grid, num = 2, parallel = TRUE, cores = 2, silent = FALSE)
ret2$par

# Grid search with zoom-in layers
ret3 = grid_search(Rastrigin, grid, zoom = 2, num = 2, parallel = TRUE, cores = 2, silent = FALSE)
ret3$par



zoomgrid documentation built on March 1, 2026, 1:07 a.m.