BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
580 stars 103 forks source link

Error: At line 1464 of file apm.f90 #82

Closed CaptainFerMag closed 4 years ago

CaptainFerMag commented 4 years ago

Hi, I am trying to solve ODE using Gekko in Python. I running Jupyter and Anaconda on WIn10 64bit, my gekko model arguments are as follows:

m = GEKKO(remote = False)
m.options.IMODE = 4 
m.solve(disp=True, debug=True)

and the output I receive is as follow. can you please help me with the issue?


APMonitor, Version 0.9.2 APMonitor Optimization Suite

--------- APM Model Size ------------ Each time step contains Objects : 3 Constants : 4 Variables : 175 Intermediates: 72 Connections : 6 Equations : 116 Residuals : 44

Error: At line 1464 of file apm.f90 Traceback: not available, compile with -ftrace=frame or -ftrace=full Fortran runtime error: Out of memory

Error: 'results.json' not found. Check above for additional error details

FileNotFoundError Traceback (most recent call last)

in 412 m.options.IMODE = 4 ##IMODE 7? 413 print("ZZZZZZZZZ") --> 414 m.solve(disp=True, debug=True) 415 print("XXXXXXXXXXX") 416 ##State vector C:\Anaconda3\lib\site-packages\gekko\gekko.py in solve(self, disp, debug, GUI, **kwargs) 2143 if timing == True: 2144 t = time.time() -> 2145 self.load_JSON() 2146 if timing == True: 2147 print('load JSON', time.time() - t) C:\Anaconda3\lib\site-packages\gekko\gk_post_solve.py in load_JSON(self) 11 ## options.JSON has all APM options 12 def load_JSON(self): ---> 13 f = open(os.path.join(self._path,'options.json')) 14 data = json.load(f) 15 f.close() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\17235779\\AppData\\Local\\Temp\\tmpfba3c5wogk_model1\\options.json'
dhill2522 commented 4 years ago

Thanks for letting us know about this and welcome to GEKKO.

It is throwing Fortran runtime error: Out of memory, so it looks like your system is running out of memory during the solve process. Is that likely with your system?

If an error crashes the Fortran backend during the solve, then no solution is returned to the Python process and you get a 'results.json' not found error.

APMonitor commented 4 years ago

@dhill2522 is correct on diagnosing the problem. Even though you may not be running out of RAM on your computer, it may be a restriction to <=4GB with the 32-bit Windows local process. The macOS and Linux versions of the local executable are 64-bit.

One way to get around this error is to try m.options.IMODE = 7. It should give the same results as m.options.IMODE=4 but processes the file sequentially instead of simultaneously. This is especially useful if you have large data files or a large model.

CaptainFerMag commented 4 years ago

Thank you very much @APMonitor & @dhill2522 . I am running the code on a windows 10 64bit 16GB RAM machine with latest Anaconda and Jupyter and Python and Gekko. I triedIMODE=7 but now it is saying the DOFshould be zero:


APMonitor, Version 0.9.2 APMonitor Optimization Suite

--------- APM Model Size ------------ Each time step contains Objects : 0 Constants : 4 Variables : 163 Intermediates: 72 Connections : 0 Equations : 107 Residuals : 35

Number of state variables: 98 Number of total equations: - 69 Number of slack variables: - 0

Degrees of freedom : 29

@error: Degrees of Freedom

  • Error: DOF must be zero for this mode STOPPING...

Exception Traceback (most recent call last)

in 416 m.options.SOLVER = 1 417 print("ZZZZZZZZZ") --> 418 m.solve(disp=True, debug=True) 419 print("XXXXXXXXXXX") 420 ##State vector C:\Anaconda3\lib\site-packages\gekko\gekko.py in solve(self, disp, debug, GUI, **kwargs) 2057 print("Error:", errs) 2058 if (debug >= 1) and record_error: -> 2059 raise Exception(apm_error) 2060 2061 else: #solve on APM server Exception: @error: Degrees of Freedom * Error: DOF must be zero for this mode STOPPING...
APMonitor commented 4 years ago

Gekko is correctly reporting that you need to have the same number of m.Var as m.Equation for simulation. If it hadn't run out of memory, IMODE=4 would have reported the same error. When you get to optimization then you can have more variables than equations. However, for simulation mode, you'll need to specify some of your variables so that you have a square system.

CaptainFerMag commented 4 years ago

Thank you again @APMonitor. Then I guess it is related to my intermediate equations. My ODE system has been implemented in MATLAB before and dynamic simulation and solvers work fine. My ODE system is a stiff model of biochemical reactions and my differential equations look like this:

state_n_initial, state_n, v1, v2, v3 = m.Var()
c1 , c2 = m.Const()
p1, p2 = m.Param()
I1 = m.Intermediate(c1 * v1+p1 )
I2 = m.Intermediate(c2 / v2 +I1 - p2 )
.
.
.
state_n.dt() = c3 + v3 * (state_n_initial - state_n) + I2 
.
.
m.options.IMODE = 4 
m.options.MAX_ITER = 1000
m.options.SOLVER = 1
m.solve(disp=True, debug=True)

state_n_initial is the initial value for state variable state_n What I was trying to point out was that I have nested intermediate equations because 1) differential equations are very long 2) there are different modes of my system that I can change by changing the intermediate equations Thanking you in advance for your help :)

APMonitor commented 4 years ago

Nested Intermediates shouldn't be a problem and don't contribute to the Degree of Freedom calculation. One way to diagnose your problem is to view the model file that Gekko creates with m.open_folder() somewhere before the m.solve() command. You can open the .apm file with a text editor to view your equations and variables. If you want to make it more readable then give your variables a name with x = m.Var(name='concentration'). You should have the same number of variables and equations.

APMonitor commented 4 years ago

Here are some references with:

Lewis, N.R., Hedengren, J.D., Haseltine, E.L., Hybrid Dynamic Optimization Methods for Systems Biology with Efficient Sensitivities, Special Issue on Algorithms and Applications in Dynamic Optimization, Processes, 2015, 3(3), 701-729; doi:10.3390/pr3030701.

Safdarnejad, S.M., Hedengren, J.D., Lewis, N.R., Haseltine, E., Initialization Strategies for Optimization of Dynamic Systems, Computers and Chemical Engineering, Vol. 78, pp. 39-50, DOI: 10.1016/j.compchemeng.2015.04.016.

that may be relevant to your problem.

CaptainFerMag commented 4 years ago

Thanks a million @APMonitor I will close the issue and come back with comments after I try your links.

CaptainFerMag commented 4 years ago

Thank you again for your help. The solver worked and is validated. I usedIMODE=7 and for DOF I had to set some m.Var as m.FV

APMonitor commented 4 years ago

That is great to hear! If you do find it useful, please consider including a citation to one of these two references if the work is for eventual publication.

CaptainFerMag commented 4 years ago

Absolutely!