Home  >  Article  >  Backend Development  >  Time optimal control example GEKKO

Time optimal control example GEKKO

WBOY
WBOYforward
2024-02-10 20:54:031181browse

时间最优控制示例 GEKKO

Question content

I am trying to implement a time optimal control problem in gekko. In particular, I copied this short code snippet. Also reported here for practicality:

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

As is, it works fine, but when I want to remove the constraint on the final value of velocity.

If I comment out the m.fix_final(velocity, 0) line, the result does not change. Regardless, it seems to assume that the final velocity should be zero. Also, if I change the final velocity from zero to any other number, I get the error from gekko: exception: @error: solution not found.

The solution should be easy to find, especially if no constraints are imposed on the final speed, the optimal control would be to keep accelerating() throughout the time.

Any help would be greatly appreciated! :)


Correct answer


Change the final constraints from m.fix_final(velocity, 0) and m.fix_final(position, 300) changed to:

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

This applies an inequality constraint at the last node so that position>=300, but it could also be an equality constraint. We also sometimes use soft constraints, such as m.minimize(last*(position-300)**2) if an infeasible solution prevents the solver from achieving the final condition. Instead, it tries to get the solution as close as possible to the final constraints. When the final value is fixed using m.fix_final(), the derivative is also fixed to zero because that variable is no longer evaluated. This is a known limitation of gekko, as described here.

The above is the detailed content of Time optimal control example GEKKO. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete