[SciPy-User] optimize.minimize - help me understand arrays as variables (Andrew Nelson)
KURT PETERS
peterskurt at msn.com
Mon Jan 12 18:26:31 EST 2015
> Date: Sun, 11 Jan 2015 17:55:32 -0700
> From: KURT PETERS <peterskurt at msn.com>
> Subject: [SciPy-User] optimize.minimize - help me understand arrays as
> variables
> To: "scipy-user at scipy.org" <scipy-user at scipy.org>
> Message-ID: <BLU172-W37473350444550BD9CF90DD8430 at phx.gbl>
> Content-Type: text/plain; charset="iso-8859-1"
>
> I'm trying to use scipy.optimize.minimize.
> I've tried multiple "multivariate" methods that don't seem to actually take multivariate data and derivatives. Can someone tell me how I can make the multivariate part of the solver actually work?
>
> Here's an example:
> My main function the following (typical length for N is 3):
>
> input guess is a x0=np.array([1,2,3])
> the optimization function returns:
> def calc_f3d(...):
> f3d = np.ones((np.max([3,N]),1)
> .... do some assignments to f3d[row,0] ....
> return np.linalg.norm(f3d) # numpy.array that's 3x1
>
> The jacobian returns a Nx3 matrix:
> def jacob3d(...):
> df = np.ones((np.max([3,N]),3))
> ... do some assignments to df[row,col]
> return df # note numpy.array that's 3x3
>
> The optimize call is:
> OptimizeResult = optimize.minimize(
> fun=tdcalc.calc_f3d,
> x0=ract,
> jac=tdcalc.jacob3d,
> method='BFGS',
> args=(operdata,),
> tol=1.0e-8,
> options={'maxiter': 40000, 'xtol':1e-8}) <--- ops change based on whether using Newton-CG or BFGS
>
> When I use BFGS, I get:
> Traceback (most recent call last):
> File "./tdoa_calc.py", line 664, in <module>
> options={'maxiter': 40000, 'gtol':1e-8})
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 348, in minimize
> return _minimize_bfgs(fun, x0, args, jac, callback, **options)
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 779, in _minimize_bfgs
> old_fval, old_old_fval)
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 95, in line_search_wolfe1
> c1=c1, c2=c2, amax=amax, amin=amin, xtol=xtol)
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 147, in scalar_search_wolfe1
> alpha1 = min(1.0, 1.01*2*(phi0 - old_phi0)/derphi0)
> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>
> When I use Newton-CG, I get:
> Traceback (most recent call last):
> File "./tdoa_calc.py", line 655, in <module>
> options={'maxiter': 40000, 'xtol':1e-8})
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 351, in minimize
> **options)
> File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 1320, in _minimize_newtoncg
> eta = numpy.min([0.5, numpy.sqrt(maggrad)])
> File "/usr/lib64/python2.7/site-packages/numpy/core/fromnumeric.py", line 1982, in amin
> out=out, keepdims=keepdims)
> File "/usr/lib64/python2.7/site-packages/numpy/core/_methods.py", line 14, in _amin
> out=out, keepdims=keepdims)
> ValueError: setting an array element with a sequence.
>
========================================================================
> Date: Mon, 12 Jan 2015 11:58:31 +1100
> From: Andrew Nelson <andyfaff at gmail.com>
> Subject: Re: [SciPy-User] optimize.minimize - help me understand
> arrays as variables
> To: SciPy Users List <scipy-user at scipy.org>
> Message-ID:
> <CAAbtOZdLeL8j4=3Pc1smLd39g98ppwC8Ua44aWnk_3L4xqp7vw at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> `calc_f3d` needs to return a single number, the overall 'cost'.
>
the "return np.linalg.norm(f3d)" DOES return a scalar ( a single number).
numpy.linalg.norm([]) returns a single number.
Best,
Kurt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150112/004d62dd/attachment.html>
More information about the SciPy-User
mailing list