Search code examples
gekko

how to formulate the problem of finding the optimal PID paramters in gekko?


I have defined a first order process model and would like to find the optimal PID parameters for this process. The optimization objective is to minimize the IAE ( Integral of absolute error between the setpoint and process value) for set point change over a horizon of 5 times the process time constant. It is neither a dynamic optimization ( IMODE =6) problem , nor a pure steady state optimization problem (IMODE=3) as it involves the derivatives. How to formulate the above problem in gekko?

m = GEKKO(remote=False)


# Controller model
Kc = m.Var(1.0,lb=0.01,ub=10)                   # controller gain
tauI = m.Var(2.0,lb=0.01,ub=1000)               # controller reset time
tauD = m.Var(1.0,lb=0.0,ub=100)                 # derivative constant
OP = m.Var(value=0.0,lb=0.0,ub=100)       # controller output
PV = m.Var(value=0.0)       # process variable
SP = 1.0   # set point
Intgl = m.Var(value=0.0)    # integral of the error
err = m.Intermediate(SP-PV) # set point error
m.Equation(Intgl.dt()==err) # integral of the error
m.Equation(OP == Kc*(err + (1/tauI)*Intgl + tauD*PV.dt()))

# Process model
Kp = 2                    # process gain
tauP = 10.0                 # process time constant
m.Equation(tauP*PV.dt() + PV == Kp*OP)

m.Obj((SP-PV)**2)  # how to define the objective to minimize the error over a horizon
m.options.IMODE=3
m.solve(disp=False)


print(str(Kc.VALUE))
print(str(tauI.VALUE))
print(str(tauD.VALUE))
print(str(m.options.OBJFCNVAL))

Solution

  • There is a video tutorial on simulating (00:00-17:00) and optimizing (17:00-23:41) PID tuning parameters with GEKKO. There is starting code as problem #14 in this list of tutorials.

    Video tutorial

    The main points from the video are to switch to IMODE=6 and set the STATUS=1 for the parameters that should be adjusted to minimize the error: (SP-PV)**2.