BYU-PRISM / GEKKO

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

FileNotFoundError for options.json after setting MEAS_CHK to 0 #73

Closed Tastaturtaste closed 4 years ago

Tastaturtaste commented 4 years ago

I am currently in the process of learning how to use GEKKO and as an exercise want to use GEKKO as a MPC for a simulation game. Initially my script ran without error, even though it seemed to stop doing anything after a few seconds. But after setting the option MEAS_CHK to 0 the scripts stopped working and crashes on the solve command with the following error:

Error: Exception: Access Violation Traceback: not available, compile with -ftrace=frame or -ftrace=full

Error: 'results.json' not found. Check above for additional error details Traceback (most recent call last): File "i:/dev/Python/kRPC/hover_with_mpc.py", line 29, in mpc.cycle() File "i:\dev\Python\kRPC\MPC.py", line 77, in cycle self.m.solve(disp=False) File "I:\dev\Python\kRPC\virtual_env\lib\site-packages\gekko\gekko.py", line 2145, in solve self.load_JSON() File "I:\dev\Python\kRPC\virtual_env\lib\site-packages\gekko\gk_post_solve.py", line 13, in load_JSON f = open(os.path.join(self._path,'options.json')) FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\joshu\AppData\Local\Temp\tmpfs1za7e0gk_model0\options.json'

I verified that the specified file is not in the given directory.

As mentioned I am currently in the process of learning how to use GEKKO, so maybe I just do it wrong...

APMonitor commented 4 years ago

It appears that the bug is with MPC but not with other modes. Here is a simple IMODE=3 application:

from gekko import GEKKO
m = GEKKO()
x1 = m.Var(value=0 , lb=0 , ub=5 , name='x1') # Product 1
x2 = m.Var(value=0 , lb=0 , ub=4 , name='x2') # Product 2
profit = m.Var(value=1 , name='profit')
m.Obj(-profit)
m.Equation(profit==100*x1+125*x2)
m.Equation(3*x1+6*x2<=30)
m.Equation(8*x1+4*x2<=44)
m.options.MEAS_CHK = 0
m.solve()
print ('')
print ('--- Results of the Optimization Problem ---')
print ('Product 1 (x1): ' + str(x1[0]))
print ('Product 2 (x2): ' + str(x2[0]))
print ('Profit: ' + str(profit[0]))

There is no error and the model runs successfully. Here is an MPC case that produces the bug:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt  
m = GEKKO(remote=False)
m.time = np.linspace(0,20,41)
mass = 500
b = m.Param(value=50)
K = m.Param(value=0.8)
p = m.MV(value=0, lb=0, ub=100)
p.STATUS = 1  # allow optimizer to change
p.DCOST = 0.1 # smooth out gas pedal movement
p.DMAX = 20   # slow down change of gas pedal
v = m.CV(value=0)
v.STATUS = 1  # add the SP to the objective
m.options.CV_TYPE = 2 # squared error
v.SP = 40     # set point
v.TR_INIT = 1 # set point trajectory
v.TAU = 5     # time constant of trajectory
m.Equation(mass*v.dt() == -v*b + K*b*p)
m.options.IMODE = 6 # control
m.options.MEAS_CHK = 0
m.solve(disp=True)
APMonitor commented 4 years ago

Thanks for helping to find a bug. I fixed the problem with MEAS_CHK=0 for MPC. The new fix will be available in APM 0.9.3+ and Gekko v0.2.7+. You can get around this bug by using MEAS_CHK=0 (default option) and just keep the validity limits (VLLO, VLHI, VDVL) wide open for no measurement checking.

Tastaturtaste commented 4 years ago

Ok, glad to be of help. Just to clarify, The validity limits are defaulted wide open at 10**20, aren't they? Because my controller seems to ignore the measurements and just cranks up the BIAS...

APMonitor commented 4 years ago

Yes, the validity limits are open. If there is unexpected behavior for your application, you can look at the measurements that are inserted into the model by opening your run folder with m.open_folder(). Look for measurements.dbs and it will have all of the values that are inserted with x.MEAS when you specify the measurements. The Bias calculation is BIAS = MEAS - Unbiased Model Prediction.