BYU-PRISM / GEKKO

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

System Identification #44

Closed APMonitor closed 5 years ago

APMonitor commented 5 years ago

A linear time invariant (LTI) system identification function (ARX, State Space, etc) is not currently available in GEKKO. There are functions in APM MATLAB (ARX, Continuous State Space, and Discrete State Space) and APM Python (ARX only). I'd like to request a function similar to apm_id (APM MATLAB) or sysid (APM Python) to facilitate creation of LTI models. I'd be glad to a assist if someone wants to take the lead.

APM MATLAB The apm_id function in APM MATLAB does this for you. Please see the source at:

https://github.com/APMonitor/apm_matlab/blob/master/apm/apm_id.m

In particular, lines 216-241 converts the ARX model (for identification) into a continuous (sysc) and discrete (sysd) state space form. There are examples on the github site such as:

https://github.com/APMonitor/apm_matlab/blob/master/example_id/myTest.m
https://github.com/APMonitor/apm_matlab/blob/master/example_lti_regression/lti_regression.m

APM Python You can install APMonitor (previously used before GEKKO) with pip install APMonitor. The apm_id function in APM MATLAB is more capable than the sysid function available in APM Python. The APM Python sysid function is here:

https://github.com/APMonitor/apm_python/blob/master/apm.py (See starting at line 705)

Unlike the APM MATLAB function, the APM Python function does not do the final step of converting to state space. Here is an example:

https://github.com/APMonitor/apm_python/tree/master/example_lti_regression
APMonitor commented 5 years ago

With 0.1rc6, added system identification of time series models. Here is example usage:

import numpy as np
from gekko import GEKKO

######################################################
# Configuration
######################################################
# number of terms
na = 3 # output coefficients
nb = 4 # input coefficients
# number of inputs
nu = 2
# number of outputs
ny = 2
# load data and parse into columns
data = np.loadtxt('data_no_headers.csv',delimiter=',') # 2 by 2 data
t = data[:,0]
u = data[:,1:3]
y = data[:,3:5]

######################################################

# generate time-series model
m = GEKKO(remote=False)

yp,a,b,c = m.sysid(t,u,y,na,nb,shift=0)

yd,ud = m.arx(a,b,c)

print(a)
print(b)
print(c)

import matplotlib.pyplot as plt
plt.plot(t,u)
plt.plot(t,y)
plt.plot(t,yp)
plt.show()
APMonitor commented 5 years ago

System identification and ARX models are now fully tested and included with the latest release (0.1rc7).