Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-10 Thread Greg Snow
Another option that is similar to Enrico's is to use object oriented programming with R6 or reference objects. I prefer the R6 package (which will still use an environment like Enrico, but with some different syntax and a little easier if you want to do this multiple times. Here is some example

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-08 Thread J C Nash
But why time methods that the author (me!) has been telling the community for years have updates? Especially as optimx::optimr() uses same syntax as optim() and gives access to a number of solvers, both production and didactic. This set of solvers is being improved or added to regularly, with a

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-08 Thread Sami Tuomivaara
Thank you all very much for the suggestions, after testing, each of them would be a viable solution in certain contexts. Code for benchmarking: # preliminaries install.packages("microbenchmark") library(microbenchmark) data <- new.env() data$ans2 <- 0 data$ans3 <- 0 data$i <- 0 data$fun.value

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-06 Thread Ott Toomet
I have done this using attributes: fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 attr(ans, "extra1") <- 1:10 attr(ans, "extra2") <- letters ans } Not sure if this works in your case though. Cheers, Ott On

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-05 Thread Martin Becker
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:

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-04 Thread Duncan Murdoch
Enrico gave you a workaround that stores the extra values in an environment. Another possible workaround is an optional argument to myfun() that asks it to return more information, e.g. fr <- function(x, data, extraInfo = FALSE) { ## Rosenbrock Banana function x1 <- x[1]

Re: [Rd] feature request: optim() iteration of functions that return multiple values

2023-08-04 Thread Enrico Schumann
On Thu, 03 Aug 2023, Sami Tuomivaara writes: > Dear all, > > I have used optim a lot in contexts where it would > useful to be able to iterate function myfun that, in > addition to the primary objective to be minimized > ('minimize.me'), could return other values such as > alternative metrics of

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

2023-08-04 Thread Sami Tuomivaara
Dear all, I have used optim a lot in contexts where it would useful to be able to iterate function myfun that, in addition to the primary objective to be minimized ('minimize.me'), could return other values such as alternative metrics of the minimization, informative intermediate values from