G: Extract Gradient information

View source: R/gradient.R

GR Documentation

Extract Gradient information

Description

Extract the gradient from its argument (typically a ROI object of class "objective").

Usage

G(x, ...)

Arguments

x

an object used to select the method.

...

further arguments passed down to the grad() function for calculating gradients (only for "F_objective").

Details

By default ROI uses the "grad" function from the numDeriv package to derive the gradient information. An alternative function can be provided via "ROI_options". For example ROI_options("gradient", myGrad) would tell ROI to use the function "myGrad" for the gradient calculation. The only requirement to the function "myGrad" is that it has the argument "func" which takes a function with a scalar real result.

Value

a "function".

Examples

## Not run: 
   f <- function(x) {
       return( 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2 )
   }
   x <- OP( objective = F_objective(f, n=2L), 
            bounds = V_bound(li=1:2, ui=1:2, lb=c(-3, -3), ub=c(3, 3)) )
   G(objective(x))(c(0, 0)) ## gradient numerically approximated by numDeriv


   f.gradient <- function(x) {
       return( c( -400 * x[1] * (x[2] - x[1] * x[1]) - 2 * (1 - x[1]),
                   200 * (x[2] - x[1] * x[1])) )
   }
   x <- OP( objective = F_objective(f, n=2L, G=f.gradient), 
            bounds = V_bound(li=1:2, ui=1:2, lb=c(-3, -3), ub=c(3, 3)) )
   G(objective(x))(c(0, 0)) ## gradient calculated by f.gradient

## End(Not run)

ROI documentation built on April 21, 2023, 1:11 a.m.

Related to G in ROI...