[SciPy-User] Saving Function Values in Optimize
Sebastian Berg
sebastian at sipsolutions.net
Thu May 8 19:04:32 EDT 2014
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140509/66aed5ef/attachment.sig>
More information about the SciPy-User
mailing list