[SciPy-user] scipy.optimize.anneal with multiple parameters

Robert Kern robert.kern at gmail.com
Sun Mar 5 22:10:34 CST 2006


Tim Leslie wrote:
> Hi all,
> 
> Before I dive too deeply into the internals of this code I thought I'd
> check here to see what people know.
> 
> I'm trying to use simulated annealing to fit a quadratic to some data
> (I'll be using a more complex function later, the quadratic is just to
> get me started). I'm doing the following:
> 
> func = lambda p: sum(abs(dat - array([p[0]*x*x + p[1]*x + p[2] for x in
> range(150, 450)])))
> print anneal(func,[0.001, 0.001, 0.001],full_output=1,upper=[3.0, 3.0,
> 3.0],lower=[-3.0, -3.0, -3.0],feps=1e-6,maxiter=1000,schedule='fast')
> 
> timl at mercury:~/thesis/src% python fit.py
> Traceback (most recent call last):
>   File "fit.py", line 17, in ?
>     print anneal(func,[0.001, 0.001, 0.001],full_output=1,upper=[3.0,
> 3.0, 3.0],lower=[- 3.0, -3.0, -3.0],feps=1e-6,maxiter=1000,schedule='fast')
>   File "/usr/lib/python2.4/site-packages/scipy/optimize/anneal.py", line
> 215, in anneal
>     xnew = schedule.update_guess(x0)
>   File "/usr/lib/python2.4/site-packages/scipy/optimize/anneal.py", line
> 92, in update_guess
>     xc = y*(self.upper - self.lower)
> TypeError: unsupported operand type(s) for -: 'list' and 'list'
> 
> I stole the syntax from the __main__ section of anneal.py and modified
> it to use a list of size 3, but I'm not sure if this is the correct way
> to do multiple parameters, the docs leave plenty to the imagination
> 
> So, am I doing something wrong, in which case could someone show me the
> light, or is anneal a bit broken, in which case I'm happy to dive in and
> take a stab at fixing it.

It looks like anneal()'s argument handling is not very robust. If you pass in
arrays instead of lists, it should probably work. The way to fix it would be to
call numpy.asarray() on the appropriate inputs.

-- 
Robert Kern
robert.kern at gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco



More information about the SciPy-user mailing list