6

I am using scipy.optimize.minimize to find the optimum value from a function. Here is the simplest example, using the built-in Rosenbrock function:

>>> from scipy.optimize import minimize, rosen
>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
>>> # Minimize returns a scipy.optimize.OptimizeResult object...
>>> res = minimize(rosen, x0, method='Nelder-Mead') 
>>> print res
  status: 0
    nfev: 243
 success: True
     fun: 6.6174817088845322e-05
       x: array([ 0.99910115,  0.99820923,  0.99646346,  0.99297555,  0.98600385])
 message: 'Optimization terminated successfully.'
     nit: 141

x is just the final, optimum input vector. ​Can I get a list for all iterations (i.e. an objective function with corresponding input vector) from the returned scipy.optimize.OptimizeResult object?

13
  • 1
    stackoverflow.com/questions/16739065/…
    – cel
    Commented Dec 15, 2015 at 10:10
  • 1
    @cel Good answer, but the print command in the callbackF() function is re-evaluating the rosen() objective function. In this case it would be OK, but for an expensive objective it would be unacceptable to calculate the result a second time just to see it's value! Is there a way to get the result directly from the callback?
    – feedMe
    Commented Dec 15, 2015 at 10:26
  • Usually you do not need any of those intermediate results. computing/storing them would just slow the algorithm down a lot. Fast algorithms cannot give you that kind of information for the sake of performance.
    – cel
    Commented Dec 15, 2015 at 10:32
  • @cel OK well in most situations where the objective is expensive, say an engineering simulation, the speed of the algorithm is negligible compared to the simulation time. So I am not interested in the optimization algorithm being fast, I am only interested in it deciding which input vector to use for the next iteration. And I want all of the information at the end of the run without having to re-run those same simulations again! I don't want intermediate results, I just want them all at the end!
    – feedMe
    Commented Dec 15, 2015 at 10:41
  • 1
    Somewhat of a kludge, but you could incorporate it in to your function to minimize - either print out at each call (console of file), or append data from each call on to a (global) list to be perused later. It is kind of neat to see (once or twice) how the algorithm goes about its business.
    – Jon Custer
    Commented Dec 15, 2015 at 14:48

1 Answer 1

0

Yes, you could add the optional argument 'return_all'.

Example:

from scipy.optimize import minimize

def f(params):
    x1, x2 = params
    f = 4 * ((x1**2+(10-x2)**2)**0.5 - 10)**2 \
    + (1/2)*((x1**2+(10+x2)**2)**0.5-10)**2 \
    -5*(x1+x2)
    return f

x0 = [-4, 4]

res = minimize(f, x0, method='CG', options={'return_all':True})

# This example returns all iteration.
1
  • Note: the options does not works for all optimization method. For example, the methaheuristic differential_evolution does not have this argument.
    – rafael
    Commented Feb 12, 2023 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.