gekko在速度约束下的轨迹优化



PSA:我是gekko的新手,因此我可能在这里遗漏了一些非常明显的东西。

我一直在试图找到一个最优控制问题的解决方案,即常规车辆在行驶途中一定距离、一定速度约束下的轨迹优化。为了做到这一点,我尝试使用基于距离和速度约束数据的pwl函数,并使用v_max作为v的约束。作为目标函数,我使用车辆比功率(VSP(近似。

计算一直进行,直到达到最大迭代次数并取消。是否有一种方法可以离散化这个问题的搜索空间,使其在可接受的时间内可解,以牺牲计算时间的准确性?

  • goal_dist=需要覆盖的距离
  • max_accel=车辆可能的最大加速度
  • max_decel=车辆可能的最大减速度
  • max_velocity=车辆的最大可能速度
  • min_velocity=车辆的最小可能速度
  • trip_time=离散数据点的数量(间隔1s(
  • distance=基于期望行程的GPS数据点的离散距离值的长度trip_time的阵列
  • speed_limits=基于所需行程的GPS数据点的离散速度限制的长度行程时间数组
  • 斜率=离散角度值的长度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

有人能解释一下吗?

以下是一个具有成功求解的样本值的模型版本:

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)

可能是您使用的值导致了不可行的解决方案。以下是一些帮助模型更可靠地解决问题的建议:

  1. 尽可能使用变量的上限,而不是一般的不等式约束
# 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. 使用软终端约束,而不是硬终端约束
#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. 增加最大迭代次数。有时,解算器需要更多的迭代才能找到解决方案
model.options.MAX_ITER=1000

模型的最终版本有这些更改。我可以帮助收敛到一个解决方案,并避免最大迭代或不可行的解决方案。

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)

如果你在StackOverflow上问另一个问题,不要忘记包括一个复制问题的最小且完整的工作示例。

相关内容

  • 没有找到相关文章

最新更新