3

In the following script:

import numpy as np
from scipy.optimize import minimise

a=np.array(range(4))
b=np.array(range(4,8))

def sm(x,a,b):
      sm=np.zeros(1)
      a=a*np.exp(x)
      sm += sum(b-a)
      return sm

 x0=np.zeros(4)
 print sm(x0,a,b) #checking my function

 opt = minimize(sm,x0,args=(a,b),method='nelder-mead', 
 options={'xtol': 1e-8,     'disp': True})        

I am trying to optimise for x but I am having the following message:

Warning: Maximum number of function evaluations has been exceeded.

And the result is:

array([-524.92769674, 276.6657959 , 185.98604937, 729.5822923 ])

Which is not the optimal. My question is am I having this message and result because my starting points are not correct?

DimKoim
  • 1,024
  • 6
  • 20
  • 33
  • Can anyone explain to me about the result? – DimKoim Mar 24 '15 at 10:26
  • There is more to the error message than you have posted: `/usr/local/lib/python2.7/site-packages/IPython/kernel/__main__.py:3: RuntimeWarning: overflow encountered in exp app.launch_new_instance() /usr/local/lib/python2.7/site-packages/scipy/optimize/optimize.py:456: RuntimeWarning: invalid value encountered in subtract numpy.max(numpy.abs(fsim[0] - fsim[1:])) <= ftol):` – AGS Mar 24 '15 at 11:46
  • @AGS Indeed. Running it on Spyder IDE produces only the afore mentioned error. Running it in Canopy (after your comment) produces the error message that you have posted. A possible explanation to your opinion? – DimKoim Mar 24 '15 at 12:34

2 Answers2

1

Your function sm appears to be unbounded. As you increase x, sm will get ever more negative, hence the fact that it is going to -inf.

Re: comment - if you want to make sm() as close to zero as possible, modify the last line in your function definition to read return abs(sm).

This minimised the absolute value of the function, bringing it close to zero.

Result for your example:

>>> opt = minimize(sm,x0,args=(a,b),method='nelder-mead', options={'xtol': 1e-8,     'disp': True})
Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 153
         Function evaluations: 272
>>> opt
  status: 0
    nfev: 272
 success: True
     fun: 2.8573836630130245e-09
       x: array([-1.24676625,  0.65786454,  0.44383101,  1.73177358])
 message: 'Optimization terminated successfully.'
     nit: 153
FuzzyDuck
  • 1,492
  • 12
  • 14
  • What I would like to achieve is to min((b[0]-a[0]*exp(x0))+(b[1]-a[1]*exp(x1))+(b[2]-a[2]*exp(x2)+(b[3]-a[3]*exp(x3)). What I am expecting is to have as a result 4 different **x** for each a. I will give you an example: for the pair (b[2]-a[2]*exp(x2))=6-2*exp(x2) I expect a result that gives exp(x2)=3. I change from exp(x) to exp(-x) but same warning. – DimKoim Mar 24 '15 at 12:56
  • Maybe I am misunderstanding your problem, but as you state it, `min((b[0]-a[0]*exp(x0))+(b[1]-a[1]*exp(x1))+(b[2]-a[2]*exp(x2)+(b[3]-a[3]*exp(x3‌​))` will be minimised for `x = [-inf, -inf, -inf, -inf]` – FuzzyDuck Mar 24 '15 at 13:23
  • First of all, thank you. `x0=-inf,x1=-inf,x2=-inf, x3=-inf ` will minimise my sm to inf. Probably my implementation is wrong. How can I have as a result from the optimization `x0,x1,x2,x3 ` so to minimum of sm will be close to zero? – DimKoim Mar 24 '15 at 13:39
  • 1
    See edited answer - you need to minimise the absolute value of the function. – FuzzyDuck Mar 24 '15 at 13:45
  • Yes with that way I bound my function. However, so as to have the expected result that I want I replace: sm += sum((b-a)**2) – DimKoim Mar 26 '15 at 15:43
1

Modifying the proposal of FuzzyDuck, I replace sm +=((b-a)**2) which return me the desired result.

DimKoim
  • 1,024
  • 6
  • 20
  • 33