BYU-PRISM / GEKKO

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

Warning: there is insufficient data in CSV file 103.238.130.134_gk_model0.csv #183

Closed lyhybrid closed 1 month ago

lyhybrid commented 1 month ago

I am using an example from gekko document to do a dynamic simulation:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

m = GEKKO()
m.time = np.linspace(0,20,100)
k = 10
y = m.Var(value=5.0)
t = m.Param(value=m.time)
m.Equation(k*y.dt()==-t*y)
m.options.IMODE = 4
m.solve(disp=False)

plt.plot(m.time,y.value)
plt.xlabel('time')
plt.ylabel('y')

when I change from m.time = np.linspace(0,20,100) to m.time = np.linspace(0,20,2000000), i.e. increase the time scale, I will encounter a warning as below, and the dimension of the variable y will become 2 which does not match with m.time. I am wondering why is it happening? ,Sincerely

 **Warning: there is insufficient data in CSV file 103.238.130.134_gk_model0.csv**

 --------- APM Model Size ------------
 Each time step contains
   Objects      :            0
   Constants    :            0
   Variables    :            2
   Intermediates:            0
   Connections  :            0
   Equations    :            1
   Residuals    :            1

 Number of state variables:              2
 Number of total equations: -            2
 Number of slack variables: -            0
 ---------------------------------------
 Degrees of freedom       :              0

 **********************************************
 Dynamic Simulation with Interior Point Solver
 **********************************************
 Info: Exact Hessian

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

This is Ipopt version 3.12.10, running with linear solver ma57.

Number of nonzeros in equality constraint Jacobian...:        4
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        0

Total number of variables............................:        2
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        2
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  0.0000000e+00 5.00e+00 0.00e+00   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  0.0000000e+00 4.44e-16 0.00e+00 -11.0 8.57e-01    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 1

                                   (scaled)                 (unscaled)
Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   4.4408920985006262e-16    4.4408920985006262e-16
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   4.4408920985006262e-16    4.4408920985006262e-16

Number of objective function evaluations             = 2
Number of objective gradient evaluations             = 2
Number of equality constraint evaluations            = 2
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 2
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 1
Total CPU secs in IPOPT (w/o function evaluations)   =      0.001
Total CPU secs in NLP function evaluations           =      0.000

EXIT: Optimal Solution Found.

 The solution was found.

 The final value of the objective function is   0.000000000000000E+000

With error:

 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   4.899999999906868E-003 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------

Traceback (most recent call last):

  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 499, in _plot_args
    **raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (2000000,) and (2,)**

Process finished with exit code 1
APMonitor commented 1 month ago

Adding additional time points improves accuracy, but 2,000,000 time points is generally not required. If a large number of time points are required, consider switching to IMODE=7 (sequential simulation). Otherwise the problem may take a while to solve with IMODE=4 (simultaneous simulation). The issue that you've encountered is that this number of time points is not allowed on the remote server. Try switching to a local solve with remote=False.

m = GEKKO(remote=False)

Here is a complete script that adds additional points and uses NODES=3 to increase accuracy.

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

m = GEKKO(remote=False)
m.time = np.linspace(0,20,2000)
k = 10
y = m.Var(value=5.0)
t = m.Param(value=m.time)
m.Equation(k*y.dt()==-t*y)
m.options.NODES = 3 # 2-6, 2=default
m.options.IMODE = 4
m.solve(disp=False)

plt.plot(m.time,y.value)
plt.xlabel('time')
plt.ylabel('y')
plt.show()

Please ask future questions on StackOverflow: https://stackoverflow.com/questions/tagged/gekko

lyhybrid commented 1 month ago

Thank you so much for your help!