mize_step {mize} | R Documentation |
One Step of Optimization
Description
Performs one iteration of optimization using a specified optimizer.
Usage
mize_step(opt, par, fg)
Arguments
opt |
Optimizer, created by |
par |
Vector of initial values for the function to be optimized over. |
fg |
Function and gradient list. See the documentation of
|
Details
This function returns both the (hopefully) optimized vector of parameters, and
an updated version of the optimizer itself. This is intended to be used when
you want more control over the optimization process compared to the more black
box approach of the mize
function. In return for having to
manually call this function every time you want the next iteration of
optimization, you gain the ability to do your own checks for convergence,
logging and so on, as well as take other action between iterations, e.g.
visualization.
Normally calling this function should return a more optimized vector of
parameters than the input, or at least leave the parameters unchanged if no
improvement was found, although this is determined by how the optimizer was
configured by make_mize
. It is very possible to create an
optimizer that can cause a solution to diverge. It is the responsibility of
the caller to check that the result of the optimization step has actually
reduced the value returned from function being optimized.
Details of the fg
list can be found in the 'Details' section of
mize
.
Value
Result of the current optimization step, a list with components:
opt
. Updated version of the optimizer passed to theopt
argument Should be passed as theopt
argument in the next iteration.par
. Updated version of the parameters passed to thepar
argument. Should be passed as thepar
argument in the next iteration.nf
. Running total number of function evaluations carried out since iteration 1.ng
. Running total number of gradient evaluations carried out since iteration 1.f
. Optional. The new value of the function, evaluated at the returned value ofpar
. Only present if calculated as part of the optimization step (e.g. during a line search calculation).g
. Optional. The gradient vector, evaluated at the returned value ofpar
. Only present if the gradient was calculated as part of the optimization step (e.g. during a line search calculation.)
See Also
make_mize
to create a value to pass to opt
,
mize_init
to initialize opt
before passing it to this
function for the first time. mize
creates an optimizer and
carries out a full optimization with it.
Examples
rosenbrock_fg <- list(
fn = function(x) {
100 * (x[2] - x[1] * x[1])^2 + (1 - x[1])^2
},
gr = function(x) {
c(
-400 * x[1] * (x[2] - x[1] * x[1]) - 2 * (1 - x[1]),
200 * (x[2] - x[1] * x[1])
)
}
)
rb0 <- c(-1.2, 1)
opt <- make_mize(
method = "SD", line_search = "const", step0 = 0.0001,
par = rb0, fg = rosenbrock_fg
)
par <- rb0
for (iter in 1:3) {
res <- mize_step(opt, par, rosenbrock_fg)
par <- res$par
opt <- res$opt
}