首頁  >  文章  >  後端開發  >  時間最優控制範例 GEKKO

時間最優控制範例 GEKKO

WBOY
WBOY轉載
2024-02-10 20:54:031148瀏覽

时间最优控制示例 GEKKO

問題內容

我正在嘗試在 gekko 中實現時間最優控制問題。特別是,我複製了這個簡短的程式碼片段。 為了實用性也在這裡報告:

from gekko import GEKKO
import matplotlib.pyplot as plt
import numpy as np
 
# set up the gekko model
m = GEKKO()
 
# set up the time (minimize the time with time scaling)
m.time = np.linspace(0, 1, 100)
 
# 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)
 
# set up the value we modify over the horizon
tf = m.FV(value=500, lb=0.1)
tf.STATUS = 1
 
# set up the MV
u = m.MV(integer=True, lb=-2, ub=1)
u.STATUS = 1
 
# set up the equations
m.Equation(POSITION.dt() / tf == VELOCITY)
m.Equation(VELOCITY.dt() / tf == u)
 
# set the objective
m.Obj(tf)
 
# set up the options
m.options.IMODE = 6     # optimal control
m.options.SOLVER = 3    # IPOPT
 
# solve
m.solve(disp=False)
 
# print the time
print("Total time taken: " + str(tf.NEWVAL))
 
# 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()

照原樣,它運作得很好,但是當我想刪除對 velocity 最終值的限制時。

如果我註解 m.fix_final(velocity, 0) 行,結果不會改變。無論如何,它似乎假設最終速度應該為零。此外,如果我將最終速度從零更改為任何其他數字,我會從 gekko 收到錯誤: exception: @error: solution not found

該解決方案應該很容易找到,特別是如果對最終速度沒有施加任何約束,則最佳控制將是在整個時間內保持加速()。

任何幫助將不勝感激! :)


正確答案


將最終約束從m.fix_final(velocity, 0)m.fix_final(position, 300) 更改為:

p = np.zeros(100); p[-1] = 1
last = m.Param(p)
m.Equation(last*(POSITION-300)>=0)

這在最後一個節點應用了不等式約束,以便 position>=300,但它也可以是等式約束。如果不可行的解決方案阻止求解器實現最終條件,我們有時也會使用軟約束,例如 m.minimize(last*(position-300)**2) 。相反,它會嘗試使解決方案盡可能接近最終約束。當使用 m.fix_final() 固定最終值時,導數也固定為零,因為不再計算該變數。這是 gekko 的已知限制,如此處所述。

以上是時間最優控制範例 GEKKO的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除