I have a nonlinear system that has quadratic inputs (manipulated variables, [MV]). To do an MPC simulation of this system using GEKKO, I have defined the following function to handle the quadratic input:
def NTVin(T, u):
n = T.shape[0]
Tntv = zeros(n)
u2 = zeros_like(u, dtype = float64)
for i in range(len(u2)):
u2[i] = copy(u[i].VALUE)
for i in range(n):
Tntv[i] = dot(dot(u2, T[i,:,:]), u2)
return Tntv
I then define my system equations as follows:
from pylab import*
dt = .02 # sec = 20ms
m = GEKKO(remote = True)
ntg = int(tf/dt) + 1
m.time = linspace(0., tf, ntg)
m.options.CV_TYPE = 2 # 2 = squared error
x = m.Array(m.SV, Nnew, value = 0.)
y = m.Array(m.CV, Mm, value = 0.)
unb = m.Array(m.MV, B.shape[1], lb = 5.e5, ub = 2.25e6)
untv = m.Array(m.MV, Bntv.shape[1], value = 0.)
tau_sp = 3.e-2
for i in range(Bntv.shape[1]):
untv[i].value = ic[i]
untv[i].STATUS = 1
for i in range(B.shape[1]):
unb[i].value = 0.
unb[i].STATUS = 1
unb[i].DCOST = 0.
unb[i].COST = 0.
for i in range(N):
x[i].value = ans0[i]
for i in range(Nnew):
m.Equation(x[i].dt() == dot(A[i,:], x) + Bint[i] + dot(B[i,:], unb) - dot(Mnew[i,:], NTVin(Bntv, untv)))
for i in range(Mm):
m.Equation(y[i] == dot(Phim[i,:], x))
y[i].value = dot(Phim[i,:], ans0)
y[i].STATUS = 1
y[i].TR_INIT = 1
y[i].SP = omegaR0[i]
y[i].TAU = tau_sp
m.options.IMODE = 6
m.options.NODES = 2
m.solve(disp = True, GUI = False)
The simulation runs, but GEKKO never changes the MV untv
(whatever is set for untv[i].value
, remains at that value for the simulation), whereas the linear MV unb
is optimized during the simulation and does vary in time. I suspect that my function NTVin
is to blame (specifically in the line: u2[i] = copy(u[i].VALUE
). My question is then, can GEKKO handle nonlinear MVs, and if so, is there a way to handle this quadratic input when declaring the system equations?
Yes, Gekko and the solvers (APOPT, BPOPT, IPOPT) can solve nonlinear expressions. When you copy the u2[i]
value, it is creating a numeric value instead of a Gekko variable. One idea for your quadratic objective term is to try the pre-built qobj
function as described in the Model Building Functions of the documentation. You could also try the State Space object and set m.options.CV_TYPE=2
for a quadratic objective. There are options for continuous or discrete state space.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.