Maison  >  Article  >  développement back-end  >  Exemple de contrôle optimal du temps GEKKO

Exemple de contrôle optimal du temps GEKKO

WBOY
WBOYavant
2024-02-10 20:54:031148parcourir

时间最优控制示例 GEKKO

Contenu de la question

J'essaie d'implémenter un problème de contrôle optimal en temps dans gekko. En particulier, j'ai copié cet extrait de code court. Également signalé ici pour des raisons pratiques :

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()

En l'état, cela fonctionne bien, mais quand je veux supprimer la contrainte sur la valeur finale de la vitesse.

Si j'annote m.fix_final(velocity, 0) 行,结果不会改变。无论如何,它似乎假设最终速度应该为零。此外,如果我将最终速度从零更改为任何其他数字,我会从 gekko 收到错误: exception: @error: solution not found.

La solution devrait être facile à trouver, surtout si aucune contrainte n'est imposée sur la vitesse finale, le contrôle optimal serait de continuer à accélérer() tout au long du temps.

Toute aide serait grandement appréciée ! :)


Bonne réponse


Changer la contrainte finale de 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)

Cela applique une contrainte d'inégalité au dernier nœud de sorte que lorsque position>=300,但它也可以是等式约束。如果不可行的解决方案阻止求解器实现最终条件,我们有时也会使用软约束,例如 m.minimize(last*(position-300)**2) 。相反,它会尝试使解决方案尽可能接近最终约束。当使用 m.fix_final() la valeur finale est fixée, la dérivée est également fixée à zéro puisque cette variable n'est plus calculée. Il s'agit d'une limitation connue du gekko, comme décrit ici.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer