GEKKO optimal control with control variables

Viewed 98

Hi Everyone, I wrote the code below and normally the variable "y" should converge to "z_obj" but it's not the case. Here's the code and the plot I get

enter image description here

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

m = GEKKO()

m.time = np.linspace(0,10,11)
z=np.repeat(20,11)
z_obj=m.Param(value=z)
y = m.Var(14)
u=m.CV(lb=0)
u.STATUS=1
u.FSTATUS=1
t = m.Param(value=m.time)
m.Equation(y.dt()==u)
m.Equation(u.dt()<=1)
m.Equation(u.dt()>=-5)
m.Obj((y-z_obj)**2)
m.options.IMODE=6
m.solve(disp=False)
plt.plot(m.time,y.value)
plt.xlabel('time')
plt.ylabel('y')
plt.show()

2 Answers

To enforce rate of change constraints on the Manipulated Variable use the DMAXHI and DMAXLO options of the m.MV() type.

Rate of Change Limit on the MV

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

m = GEKKO()

m.time = np.linspace(0,10,11)
z=20
y = m.Var(14)
u=m.MV(lb=0)
u.STATUS=1; u.DMAXHI=1; u.DMAXLO=-5
t = m.Param(value=m.time)
m.Equation(y.dt()==u)
m.Obj((y-z)**2)
m.options.IMODE=6
m.solve(disp=False)
plt.subplot(2,1,1)
plt.plot(m.time,y.value,'r--',label='y')
plt.ylabel('y'); plt.legend()
plt.subplot(2,1,2)
plt.plot(m.time,u.value,'b-',label='u')
plt.ylabel('u'); plt.legend()
plt.xlabel('time')
plt.show()

Your current method is to create another m.CV() so that you have the derivatives available. This also works but creates additional equations for the m.CV(). There is additional information on m.MV() and m.CV() types in the documentation, in the Quick Start guide, and #17 of 18 in the Gekko examples.

I manage to solve it .. i must get rid of u.STATUS =1 and just Keep u.FSTATUS=1 . but i don't know why ! any explication please !

Related