1

Relatively new to Python and struggling with a compatibility issue. Originally developed some code in 3.6 on my desktop but need to run it on a server which has 2.7 (and I can't upgrade the server - it's complicated!). The code runs on each platform with no errors. However, it runs correctly in 3.6 and the results in 2.7 are completely off. The problem seems to be in the scipy.optimize - the minimize function. In 3.6, the minimize function converges on a minimum and returns the expected result. In 2.7, the exact same code and inputs, it is clear that the minimize function does not converge and ultimately uses the upper bounds. That causes the returned results to be completely unrealistic. I am assuming that this is a syntax issue between 3.6 and 2.7, but with repeated searches, I have yet to find anything that points to the potential problem. Below is my minimize function:

from scipy.optimize import minimize

...
##############################################################################
#Minimize function
##############################################################################
def optimize(beta, eta, ht, age_day, age):
    initial_guess = [beta, eta]
    global cur_clust
    result = minimize(Renewal, initial_guess, args=[ht,age_day,age],method = 'SLSQP', bounds=((0.4,8.0),(0.05,1000.0)),options = {'disp': False})
    if result.success:
        fitted_params = result.x
        #print(cur_clust)
        #print(fitted_params)
        B_opt = fitted_params[0]
        E_opt = fitted_params[1]
        SSE = result.fun
    else:
        B_opt = 9999
        E_opt = 9999
        SSE = result.fun

    return B_opt, E_opt, SSE;

Any suggestions?

Thanks!

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
FredG
  • 303
  • 1
  • 4
  • 9
  • I would recommend installing a 2.7 environment on your desktop and debugging the code in `2.7` directly to see what is occurring. – PixelEinstein Aug 30 '18 at 21:29
  • In addition to what PixelEinstein says, I'd check the scipy version (https://stackoverflow.com/a/21385217/4092569) on the server and compare to what's working locally. You might be hitting a bug in an older version of scipy. – Jesse Scherer Aug 30 '18 at 21:35
  • I did check and scipy version is 1.1.0 in both instances of Python. And I am a bit stuck as to debugging something that doesn't return any errors or warnings. The code works in both versions of Python, but just returns the wrong results in 2.7. Plus is there a way to return intermediate results from a function within a package (minimize function in scipy.optimize)? – FredG Sep 06 '18 at 12:28
  • Ok, I did find the problem. Trying to run the code written in 3.6 under 2.7, 2.7 does integer division and 3.6 if you divide two integers you get a float. So to run properly under 2.7, I had to add: from __future__ import division at the beginning of my program. – FredG Sep 06 '18 at 17:25

0 Answers0