简体   繁体   English

在速度限制下使用 Gekko 进行轨迹优化

[英]Trajectory optimization with gekko under speed constraints

PSA: I am very new to gekko, thus I might be missing something very obvious here. PSA:我对壁虎很陌生,因此我可能在这里遗漏了一些非常明显的东西。

I have been trying to find the solution to an optimal control problem, namely trajectory optimization of a regular vehicle, under certain speed constraints at certain distances along their trip.我一直试图找到最优控制问题的解决方案,即常规车辆的轨迹优化,在一定的速度限制下,在他们的行程中的特定距离。 In order to do this, I tried using a pwl function based on the distance and speed constraint data and using v_max as a constraint to v. As a objective function, I use a Vehicle Specific Power (VSP) approximation.为了做到这一点,我尝试使用基于距离和速度约束数据的 pwl function 并使用v_max作为对 v 的约束。作为目标 function,我使用车辆比功率 (VSP) 近似值。

The computation keeps going until the maximum no.计算一直持续到最大值。 of iterations is reached and cancels.迭代次数达到并取消。 Is there maybe a way to discretize the search space of this problem to make it solvable in acceptable time trading off computation time for accuracy?是否有一种方法可以离散化这个问题的搜索空间,使其在可接受的时间内解决,权衡计算时间以获得准确性?

  • goal_dist = The distance that needs to be covered目标距离 = 需要覆盖的距离
  • max_accel = maximum possible acceleration of the vehicle max_accel = 车辆的最大可能加速度
  • max_decel = maximum possible deceleration of the vehicle max_decel = 车辆的最大可能减速度
  • max_velocity = maximum possible velocity of the vehicle max_velocity = 车辆的最大可能速度
  • min_velocity = minimum possible velocity of the vehicle min_velocity = 车辆的最小可能速度
  • trip_time = No. of discrete data points (1s apart) trip_time = 离散数据点的数量(相隔 1 秒)
  • distances = array of length trip_time of discrete distance values based on GPS data points of the desired trip距离 = 基于所需行程的 GPS 数据点的离散距离值的长度trip_time 数组
  • speed_limits = array of length trip_time of discrete speed limits based on GPS data points of the desired trip speed_limits = 基于所需行程的 GPS 数据点的离散速度限制的长度 trip_time 数组
  • slope = array of length trip_time of discrete angle values斜率=离散角度值的长度trip_time数组
def optimal_trip(goal_dist, max_accel, max_decel, max_velocity, min_velocity, trip_time, distances ,speed_limits, slope):

    model = GEKKO(remote=True)
    model.time = [i for i in range(trip_time)]

    x = model.Var(value=0.0)
    v = model.Var(value=0.0, lb = min_velocity, ub = max_velocity)

    v_max = model.Var()
    slope_var = model.Var()
    
    a = model.MV(value=0, lb=max_decel ,ub=max_accel)
    a.STATUS = 1
    
    #define vehicle movement
    model.Equation(x.dt()==v)
    model.Equation(v.dt()==a)
    # path constraint
    model.Equation(x >= 0)

    #aggregated velocity constraint
    model.pwl(x, v_max, distances, speed_limits)
    model.Equation(v_max>=v)

#slope is modeled as a piecewise linear function
    model.pwl(x, slope_var, distances, slope)

    #End state constraints
    model.fix(x, pos=trip_time-1,val=goal_dist) # vehicle must arrive at destination
    model.fix(v, pos=trip_time-1,val=0) # vehicle must be fully stopped
    #VSPI Objective function
    obj = (v * (1.1 * a + 9.81 * slope_var + 0.132) +0.0003002*pow(v, 3))
    model.Obj(obj)
    # solve
    model.options.IMODE = 6
    model.options.REDUCE = 3
    model.solve(disp=True)
    return x.value, v.value, obj.value

Could someone shed some light onto this?有人可以对此有所了解吗?

Here is a version of the model with sample values that solves successfully:这是 model 的一个版本,其中包含成功求解的示例值:

from gekko import GEKKO
import numpy as np

min_velocity =  0
max_velocity = 10
max_decel    = -1
max_accel    =  1
distances    = np.linspace(0,20,21)
goal_dist    = 200

trip_time    = 100

# set up PWL functions
distances    = np.linspace(0,1000,10)
speed_limits = np.ones(10)*5
speed_limits[5:]=7
slope        = np.zeros(10)
slope[3:5]=1; slope[7:9]=-1

model = GEKKO(remote=True)
model.time = [i for i in range(trip_time)]

x = model.Var(value=0.0)
v = model.Var(value=0.0, lb = min_velocity, ub = max_velocity)

v_max = model.Var()
slope_var = model.Var()

a = model.MV(value=0, lb=max_decel ,ub=max_accel)
a.STATUS = 1

#define vehicle movement
model.Equation(x.dt()==v)
model.Equation(v.dt()==a)
# path constraint
model.Equation(x >= 0)

#aggregated velocity constraint
model.pwl(x, v_max, distances, speed_limits)
model.Equation(v_max>=v)

#slope is modeled as a piecewise linear function
model.pwl(x, slope_var, distances, slope)

#End state constraints
model.fix(x, pos=trip_time-1,val=goal_dist) # vehicle must arrive at destination
model.fix(v, pos=trip_time-1,val=0) # vehicle must be fully stopped
#VSPI Objective function
obj = (v * (1.1 * a + 9.81 * slope_var + 0.132) +0.0003002*pow(v, 3))
model.Obj(obj)
# solve
model.options.IMODE = 6
model.options.REDUCE = 3
model.solve(disp=True)

It may be that the values you are using cause an infeasible solution.您使用的值可能会导致不可行的解决方案。 Here are some suggestions to help the model solve more reliably:以下是一些帮助 model 更可靠地解决问题的建议:

  1. Use upper bounds on variables instead of general inequality constraints when possible.尽可能使用变量的上限而不是一般的不等式约束。
# remove these lines
#model.Equation(x >= 0)
#x = model.Var(value=0.0)

# put lower bound on x
x = model.Var(value=0,lb=0)
  1. Use soft terminal constraints instead of hard terminal constraints.使用软终端约束而不是硬终端约束。
#End state constraints
# vehicle must arrive at destination
#model.fix(x, pos=trip_time-1,val=goal_dist)
# vehicle must be fully stopped
#model.fix(v, pos=trip_time-1,val=0) 
p = np.zeros_like(model.time); p[-1]=1
final = model.Param(p)
model.Minimize(1e4*final*(v**2))
model.Minimize(1e4*final*((x-goal_dist)**2))
  1. Increase maximum iterations.增加最大迭代次数。 Sometimes the solver needs more iterations to find a solution.有时求解器需要更多的迭代才能找到解决方案。
model.options.MAX_ITER=1000

The final version of the model has these changes. model 的最终版本具有这些更改。 I may help converge to a solution and avoid maximum iterations or an infeasible solution.我可以帮助收敛到一个解决方案并避免最大迭代或不可行的解决方案。

from gekko import GEKKO
import numpy as np

min_velocity =  0
max_velocity = 10
max_decel    = -1
max_accel    =  1
distances    = np.linspace(0,20,21)
goal_dist    = 200

trip_time    = 100

# set up PWL functions
distances    = np.linspace(0,1000,10)
speed_limits = np.ones(10)*5
speed_limits[5:]=7
slope        = np.zeros(10)
slope[3:5]=1; slope[7:9]=-1

model = GEKKO(remote=True)
model.time = [i for i in range(trip_time)]

x = model.Var(value=0.0, lb=0)
v = model.Var(value=0.0, lb = min_velocity, ub = max_velocity)

v_max = model.Var()
slope_var = model.Var()

a = model.MV(value=0, lb=max_decel ,ub=max_accel)
a.STATUS = 1

#define vehicle movement
model.Equation(x.dt()==v)
model.Equation(v.dt()==a)

#aggregated velocity constraint
model.pwl(x, v_max, distances, speed_limits)
model.Equation(v_max>=v)

#slope is modeled as a piecewise linear function
model.pwl(x, slope_var, distances, slope)

#End state constraints
# vehicle must arrive at destination
#model.fix(x, pos=trip_time-1,val=goal_dist)
# vehicle must be fully stopped
#model.fix(v, pos=trip_time-1,val=0) 
p = np.zeros_like(model.time); p[-1]=1
final = model.Param(p)
model.Minimize(1e4*final*(v**2))
model.Minimize(1e4*final*((x-goal_dist)**2))

#VSPI Objective function
obj = (v * (1.1 * a + 9.81 * slope_var + 0.132) +0.0003002*pow(v, 3))
model.Minimize(obj)
# solve
model.options.IMODE = 6
model.options.REDUCE = 3
model.options.MAX_ITER=1000
model.solve(disp=True)

If you ask another question on StackOverflow, don't forget to include a minimal and complete working example that replicates the problem.如果您在 StackOverflow 上提出另一个问题,请不要忘记包含一个最小且完整的工作示例来复制该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM