[SciPy-User] Saving Function Values in Optimize

Kevin Bache kevin.bache at gmail.com
Thu May 8 21:57:43 EDT 2014


Thanks, Sebastian.  Good idea.
Kevin


On Thu, May 8, 2014 at 4:04 PM, Sebastian Berg
<sebastian at sipsolutions.net>wrote:

> On Do, 2014-05-08 at 12:56 -0700, Kevin Bache wrote:
> > Hi Everyone,
> >
> > Quick question: is there a preferred way to save function values in
> > optimize.minimize?  The callback function format only passes 'xk', the
> > current parameters for the optimization problem.  For some problem
> > types, it's computationally trivial to convert that to f(xk), but in
> > many, that process is expensive.  I'd like to save all function
> > evaluations as I progress through the optimization process, yielding
> > an object with the information:
> >
> >
> > (xk_1, f(xk_1)), (xk_2, f(xk_2)), ...  (xk_n, f(xk_n))
> >
> >
> > Does anyone have advice on how to go about this without hacking the
> > optimize code?
> >
> The sniplet below isn't perfect, but I think you should be able to adept
> it to your needs. Decorators can do this kind of magic pretty nicely
> (though of course nothing stops you from just implementing it into the
> function itself).
>
> - Sebastian
>
>
> def store_cost(func):
>     """Decorator for cost functions, as long as the cost function is only
>     called with the same arguments this works good. Defines func.x and
>     func.cost.
>
>     Example:
>     ========
>
>     from scipy.optimize import fmin
>
>     @store_cost
>     def func(x):
>         return (x - 10)**2
>     fmin(func, [0])
>
>     print func.x
>     print func.cost
>     """
>     x_list = []
>     cost_list = []
>     def new_func(x, *args):
>         x_list.append(x.copy())
>         e = func(x, *args)
>         cost_list.append(e)
>         return e
>     new_func.x = x_list
>     new_func.cost = cost_list
>     return new_func
>
>
> >
> > Thanks in advance!
> >
> >
> > Best Regards,
> > Kevin
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140508/2b637f25/attachment.html>


More information about the SciPy-User mailing list