时间最优控制示例 GEKKO
问题内容
我正在尝试在 gekko 中实现时间最优控制问题。特别是,我复制了这个简短的代码片段。 为了实用性也在这里报告:
from gekko import GEKKO import matplotlib.pyplot as plt import numpy as np 1. set up the gekko model m = GEKKO() 1. set up the time (minimize the time with time scaling) m.time = np.linspace(0, 1, 100) 1. set up the variables POSITION = m.Var(value=0, ub=330, lb=0) VELOCITY = m.Var(value=0, ub=33, lb=0) m.fix_final(VELOCITY, 0) m.fix_final(POSITION, 300) 1. set up the value we modify over the horizon tf = m.FV(value=500, lb=0.1) tf.STATUS = 1 1. set up the MV u = m.MV(integer=True, lb=-2, ub=1) u.STATUS = 1 1. set up the equations m.Equation(POSITION.dt() / tf == VELOCITY) m.Equation(VELOCITY.dt() / tf == u) 1. set the objective m.Obj(tf) 1. set up the options m.options.IMODE = 6 # optimal control m.options.SOLVER = 3 # IPOPT 1. solve m.solve(disp=False) 1. print the time print("Total time taken: " + str(tf.NEWVAL)) 1. plot the results plt.figure() plt.subplot(211) plt.plot(np.linspace(0,1,100)*tf.NEWVAL, POSITION, label='Position') plt.plot(np.linspace(0,1,100)*tf.NEWVAL, VELOCITY, label='Velocity') plt.ylabel('Z') plt.legend() plt.subplot(212) plt.plot(np.linspace(0,1,100)*tf.NEWVAL, u, label=r'$u$') plt.ylabel('u') plt.xlabel('Time') plt.legend() plt.show() 登录后复制