HTTP error when I run the APMonitor nlc example in python

Viewed 56

I've been interested in learning MPC control, and I wanted to try to nlc python example found here:

http://apmonitor.com/wiki/index.php/Main/PythonApp

When I ran the initial demo example, I got an HTTP error. I was able to run the demo example by changing the instances of "http" to "https" in the apm.py file, similar to the problem found here:

https://github.com/olivierhagolle/LANDSAT-Download/issues/33

I've been trying to run the nlc example now and I'm getting the same kind of error (shown below). However, changing the instances of "http" to "https" no longer seem to be helping.

*Traceback (most recent call last): File "C:\Users\veli95839\Documents\Python\Scripts\example_nlc\nlc.py", line 88, in response = apm_meas(server,app,x,value) File "C:\Users\veli95839\Documents\Python\Scripts\example_nlc\apm.py", line 607, in load_meas f = urllib.request.urlopen(url_base,params_en) File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 640, in http_response response = self.parent.error( File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 569, in error return self._call_chain(*args) File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 502, in _call_chain result = func(args) File "C:\Users\veli95839\Documents\Python\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 503: Service Unavailable

Please let me know if anyone has experienced similar issues!

Thanks,

Claire

1 Answers

You may have received that error if your computer was not connected to the Internet or the server was unavailable at the time you ran the test. You can install a local APM server (for Windows or Linux) to avoid any disruptions. Another option is to switch to Python gekko that uses the same underlying APM engine but can run locally with remote=False. Here is the same MPC example in Python gekko.

Python gekko MPC

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

m = GEKKO(remote=False)
m.time = np.linspace(0,20,41)

# Parameters
mass = 500
b = m.Param(value=50)
K = m.Param(value=0.8)

# Manipulated variable
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

# Controlled Variable
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

# Process model
m.Equation(mass*v.dt() == -v*b + K*b*p)

m.options.IMODE = 6 # control
m.solve(disp=False,GUI=True)

# get additional solution information
import json
with open(m.path+'//results.json') as f:
    results = json.load(f)

plt.figure()
plt.subplot(2,1,1)
plt.plot(m.time,p.value,'b-',label='MV Optimized')
plt.legend()
plt.ylabel('Input')
plt.subplot(2,1,2)
plt.plot(m.time,results['v1.tr'],'k-',label='Reference Trajectory')
plt.plot(m.time,v.value,'r--',label='CV Response')
plt.ylabel('Output')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()

If you'd like to get more information on either APM Python or gekko for Nonlinear or Linear Model Predictive Control, there are a series of tutorials with the Temperature Control Lab (TCLab).

Advanced Control Labs with Solutions

  1. Digital Twin Model Development
    • Lab A - Single Heater Modeling
    • Lab B - Dual Heater Modeling
  2. Machine Learning with Parameter and State Estimation
    • Lab C - Parameter Estimation
    • Lab D - Empirical Model Estimation
    • Lab E - Hybrid Model Estimation
  3. Model Predictive Control
    • Lab F - Linear Model Predictive Control
    • Lab G - Nonlinear Model Predictive Control
    • Lab H - Moving Horizon Estimation with MPC

These exercises teach how to do first principles or empirical modeling, state estimation, and predictive control. Here is an example of combined Moving Horizon Estimation and Model Predictive Control from Lab H.

MHE and MPC for the TCLab

Related