# How to remove overhead due to nested optimization with Optim package

Consider minimizing f(x, y1, y2) using a nested procedure. In an inner loop y1, y2 are minimized as a function of x and in the outer loop f( x, y1(x), y2(x) ) is minimized where y1(x), y2(x) are the solutions to the inner optimization problems.

So during each iteration of the outer loop, there is overhead in creating new optimizers for y1 and y2. How can I avoid such overhead with the Optim package?

Thanks!!

The last I looked, there was no standard way to do it. I customized the Brent function to allow me to pass in the optim object and thus reuse it. I noticed very little speedup (even though almost all allocation was gone). YMMV…

1 Like

How did you reuse it? (Optim package)

The structure that Optim creates and returns with optimization results and info, I just made a version of the Brent function that accepted it (modified the one from Optim). I called it directly rather than using optimize. I overwrote the fields to prevent some from incorrectly passing from one run to the next. Like I said, there is not really a standard way to do it so this way was somewhat hacked together.

1 Like

Thanks! I feared I’d have to do something like that.

Can you post an example? Is the problem that the interior optimization problem is multivariate so it allocates? Also, why not just do a full optimization of all three variables at once?

Thanks @pkofod . The problem is of the form sum_j f_j(x,y_j) where there are many j’s, each f_j is convex in y_j but not in x. So to answer your second question, one would be converting say 100 20-dimensional convex optimization problems into one 2000-dimensional nonconvex one.

Right. Wrt to the problem, I was asking for the example because I wanted to show you how to avoid the allocations.