Skip to content
Prev 61773 / 63424 Next

feature request: optim() iteration of functions that return multiple values

For a solution that does not require any change to the original function 
being optimized, the following one-liner could be used, which converts 
existing functions to functions that return only the first element:

returnFirst <- function(fun) function(...) do.call(fun,list(...))[[1]]

Example:

fr <- function(x) {   ## Rosenbrock Banana function
   x1 <- x[1]
   x2 <- x[2]
   ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
   list(ans=ans, extra1 = 1:10, extra2 = letters)
}

fr2 <- returnFirst(fr)
tmp <- optim(c(-1.2,1), fr2)
fr(tmp$par)


Am 03.08.23 um 22:21 schrieb Sami Tuomivaara: