[Scipy-tickets] [SciPy] #598: Function parameters overwritten and state not set in anneal.py, if T0=None

SciPy scipy-tickets@scipy....
Thu Jan 31 12:30:52 CST 2008


#598: Function parameters overwritten and state not set in anneal.py, if T0=None
----------------------------+-----------------------------------------------
 Reporter:  mythsmith       |       Owner:  somebody                   
     Type:  defect          |      Status:  new                        
 Priority:  high            |   Milestone:  0.7                        
Component:  scipy.optimize  |     Version:  devel                      
 Severity:  blocker         |    Keywords:  anneal, tipeerror, nonetype
----------------------------+-----------------------------------------------
 Encountered using very complex function (not found running tests or simple
 functions).
 My function works without errors with other minimizers, so i think it is
 not my fault...

 {{{
 Traceback (most recent call last):
   File "./model.py", line 154, in <module>
     exec(minimizers[i])
   File "<string>", line 1, in <module>
   File "/usr/lib/python2.5/site-packages/scipy/optimize/anneal.py", line
 235, in anneal
     current_state.x = schedule.update_guess(last_state.x)
   File "/usr/lib/python2.5/site-packages/scipy/optimize/anneal.py", line
 91, in update_guess
     xnew = x0 + xc
 TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
 }}}

 I found that x0 is erroneously overwritten at line 217 of anneal.py,
 instead T0:
 {{{
 216    if T0 is None:
 217          x0 = schedule.getstart_temp(best_state)
 }}}

 It should be:

 {{{
 216    if T0 is None:
 217          T0 = schedule.getstart_temp(best_state)
 }}}


 And that also reveal an error in function getstart_temp, at line 54:

 {{{
 54        return best_state.x
 }}}

 Should be:

 {{{
 54        return self.T0
 }}}


 The same function leaves best_state.cost and best_state.x undefined:

 {{{
 41      fmax = -300e8
         fmin = 300e8
         for _ in range(self.Ninit):
             x0 = random.uniform(size=self.dims)*(urange-lrange) + lrange
             fval = self.func(x0, *self.args)
             self.feval += 1
             if fval > fmax:
                 fmax = fval
             if fval < fmin:
                 fmin = fval
                 best_state.cost = fval
                 best_state.x = array(x0)
 }}}


 If for some reason fval is never < than fmin, best_state.cost and
 best_state.x are never set and will rise an error:


 {{{
 Traceback (most recent call last):
   File "./model.py", line 151, in <module>
     exec(minimizers[i])
   File "<string>", line 1, in <module>
   File "/usr/lib/python2.5/site-packages/scipy/optimize/anneal.py", line
 261, in anneal
     if abs(af[-1]-best_state.cost) > feps*10:
 TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
 }}}


 So there are two ways: rise fmin to my needs (the function starts with
 very high values, 10**13
  and fmin is set to 300e8...), or put fmin as the value of the function in
 the start point if it is > than fmin (the starting point is supposed to be
 reasonable, after all...).
 I corrected it the second way, but I had to include the x0 information in
 the _state() template class, so it can be referenced as best_state.x.

 But I'm not a simulated annealing guru so maybe this is not the best.

 These errors - and other i did not find - persist in the svn version.

-- 
Ticket URL: <http://www.scipy.org/scipy/scipy/ticket/598>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list