Python Forum

Full Version: how to specify options or tickle python to output points swept by the optimization?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need the list of points (x,y) python uses to reach the minimum. But the best I can find at the moment is to output the x coordinates of the list of points as shown below

>>>[xopt, fopt, iter, funcalls, warnflag, allvecs] = optimize.fmin(eng,[-1],disp=True,retall=True,full_output=True)

>>> print(xopt)
[8.8817842e-16]

>>> for items in allvecs:
... print(items)
...
[-1.]
[-0.9]
[-0.7]
[-0.3]
[0.1]
[-0.1]
[8.8817842e-16]

Is there a different optimization function which might output both x and y coordinate of the optimization path? If not, then would it be feasible to tickle the source code of scipy to achieve this feature?
Use callback parameter, something like this:

values_storage = list()
def my_callback(x):
    values_storage.append(eng(x))
    
 ... = optimize.fmin(eng,[-1],disp=True,retall=True,full_output=True, callback=my_callback)

for a, b in zip(allvecs, values_storage):
    print(a, b)
Thank you, scidam! Although not fully understand how callback works, I get your point requesting an extra function evaluation at given x instead of receiving it directly from fmin. Since my function evaluation can be expensive, I wonder whether there is a more straightforward way to get f(x) directly from the optimization procedure.
(Aug-02-2019, 05:56 AM)bsmile Wrote: [ -> ]Since my function evaluation can be expensive, I wonder whether there is a more straightforward way to get f(x) directly from the optimization procedure.
I don't think it is possible without intervention to the source code of fmin. Usually, optimization algorithms do a lot of function calculations at different points, so one additional calculation in the callback should not significantly impact on total time required for minimum finding.