estimateGradient: Approximate gradient of a single-objective function.

Description Usage Arguments Value Note Examples

Description

estimateGradient approximates the gradient of a single-objective function fn in position x based on small changes in one direction per dimension of x.

\nabla f(\mathbf{x}) = \frac{f(\mathbf{x} + \boldsymbol{\varepsilon}) - f(\mathbf{x})}{||\boldsymbol{\varepsilon}||}

The gradient approximation is performed separately for each dimension of the search space. That is, the i-th element of the gradient results from a slight step (step size: prec.grad) of the i-th element of x, while all remaining elements of x are not altered.

In contrast to the aforementioned function, estimateGradientBothDirections performs a small step in both directions (of the i-th element) of x.

\nabla f(\mathbf{x}) = \frac{f(\mathbf{x} + \boldsymbol{\varepsilon}) - f(\mathbf{x} - \boldsymbol{\varepsilon})}{2 \cdot ||\boldsymbol{\varepsilon}||}

Usage

1
2
3
4
5
estimateGradientSingleDirection(fn, ind, side = NULL,
  prec.grad = 1e-06, check.data = TRUE, lower, upper, ...)

estimateGradientBothDirections(fn, ind, prec.grad = 1e-04,
  check.data = TRUE, lower, upper, ...)

Arguments

fn

[function]
Single-objective function, whose gradient will be approximated.

ind

[numeric(d)]
d-dimensional individual.

side

[logical(d)]
Logical vector of the same length as x, stating per element whether to approximate the gradient into the positive (TRUE) or negative (FALSE) direction of x. The default is rep(TRUE, length(x)).

prec.grad

[numeric(1L)]
Precision value (= step size) used for approximating the gradient. The default is 1e-6.

check.data

[logical(1L)]
Should sanity checks be performed? The default is TRUE. Note that the checks should only be turned off (e.g., for a slight speed up), if you are sure that you provide the input data in the correct format.

lower

[numeric(d)]
Vector of lower bounds.

upper

[numeric(d)]
Vector of upper bounds.

...

Further arguments to be passed to fn.

Value

[numeric(d)]

Note

This function basically is a slightly modified version of numDeriv::grad(..., method = "simple") and was mainly developed for internal usage to have a speed up over the aforementioned version. However, this speed up will only have an effect, if you call this function very frequently and if you turn off the sanity checks.

ATTENTION: Only turn off the sanity checks (check.data = FALSE), if you can ensure that all input parameters are provided in the correct format.

Examples

1
2
3
4
fn = function(x) sum(x^2)
estimateGradientSingleDirection(fn, c(0.3, 0.5))
estimateGradientSingleDirection(fn, c(0.3, 0.5), side = rep(FALSE, 2L))
estimateGradientBothDirections(fn, c(0.3, 0.5))

kerschke/mogsa documentation built on July 11, 2019, 11:52 p.m.