gridOptim: Global optimization by partial gridding

Description Usage Arguments Value Requirements for the optimizer function Details for progress handler function Author(s) See Also Examples

Description

Global optimization by partial gridding where the actual optimization is done transparently by calls to optim.

Usage

1
2
3
4
## Default S3 method:
gridOptim(theta, par, min=NA, max=NA, width=max - min, steps=1, depth=1, shrink=1/2,
  optimizer=stats::optim, ..., maximize=TRUE, .checkArgs=TRUE, progressHandler=NULL,
  verbose=FALSE)

Arguments

theta

A vector of parameters for which the grid search should be performed. Thes values are also used as the initial center of the grid.

par

A vector of "free" parameters for which the objective function is optimized over, given fix theta parameters. Passed to the optimizer function.

min,max

Two vectors of bounds for the grid search. No theta parameters will be search for outside these bounds. If NA, there are no bounds.

width

A vector of initial widths for each of the theta parameters.

steps

A vector of the number of steps each theta parameter should search over. The total number of theta parameter sets to be optimized over is prod(steps).

depth

The depth, or the number of "generations", to grid search over. When all theta parameters sets have been searched, the grid can be narrowed around the optimal theta and recursively search around that point. If depth==1, only the current generation is searched.

shrink

A vector of shrink factors in (0,1) specifying how much the grid widths should be shrunk at each generation.

...

Other arguments passed to the optimizer function.

optimizer

An optimizer function called at each grid point. For details see below. By default the optimizer is optim.

maximize

If TRUE, maximization is performed, otherwise minimization. Note that for instance the control argument used by optim is not interpreted here.

.checkArgs

If TRUE, all arguments are checked and modified/extended if necessary. If FALSE, all arguments are assumed to be of correct length, type etc. This argument is used by internal recursive calls.

progressHandler

A function that is called at certain steps of optimization.

verbose

If TRUE, detailed information while grid searching is printed.

Value

Return a list with information about the optimal settings:

theta

The grid point theta for which the objective function was optimized.

optim

The result returned by optim at the optimum.

gridCount

Total number of grid points tested.

callCount

Total number of calls to the objective (and the gradient) function.

Requirements for the optimizer function

The optimizer function specified by the optimizer argument must accept the argument par as the first argument, cf. optim, followed by ..., followed by the named argument theta at any position.

The optimizer function must return a list with the element value containing the optimal numerical value given the current gridpoint (passed to the function by an argument named theta). Optionally, it may return the number of internal optimization steps using the count element (a numeric scalar or vector), which is then summed up together over the whole grid (including recursive grid) by this function. For instance, the optim function returns the number of calls to the objective and the gradient functions.

Details for progress handler function

The progressHandler function must accept the following arguments:

Author(s)

Henrik Bengtsson

See Also

optim.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
objective <- function(par, ..., theta=theta, verbose=FALSE) {
  if (verbose) {
    args <- list(
      theta=theta,
      par=par,
      ...
    );
    str(args);
  }
 
  sum(theta);
}

theta <- c(1,1)
par <- 0
min <- theta - 2
max <- theta + 2
depth <- 2
steps <- 3

opt <- gridOptim(theta=theta, par=par, min=min, max=max, steps=steps,
                 depth=depth, fn=objective, maximize=FALSE)
print(opt)

HenrikBengtsson/R.basic documentation built on May 6, 2019, 11:51 p.m.