GEKKO-使用模块操作添加不等式约束 我想尽量减少整个行程的行程时间,但我必须考虑交叉路口红灯持续时间的限制。我选择离散的是距离,因此m.实际上表示距离,x表示主体车辆在其行驶区域内行驶的时间cp表示信号循环时钟中车辆通过交叉口的时间,c0表示车辆离开原点的时钟时间,Tc表示时钟周期,Tr表示红灯持续时间
代码如下:GEKKO-使用模块操作添加不等式约束 我想尽量减少整个行程的行程时间,但我必须考虑交叉路口红灯持续时间的限制。我选择离散的是距离,因此m.实际上表示距离,x表示主体车辆在其行驶区域内行驶的时间cp表示信号循环时钟中车辆通过交叉口的时间,c0表示车辆离开原点的时钟时间,Tc表示时钟周期,Tr表示红灯持续时间,gekko,Gekko,代码如下: import numpy as np import matplotlib.pyplot as plt from gekko import GEKKO #constant lambda = 1 vmin = 0.001; vmax =60/3.6 tf = 40 amax = 5; amin =-3 # Intersection information Tr = 30 Tc = 60 interloc = 150 c0 = 1 # Call gekko for optimizat
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
#constant
lambda = 1
vmin = 0.001; vmax =60/3.6
tf = 40
amax = 5; amin =-3
# Intersection information
Tr = 30
Tc = 60
interloc = 150
c0 = 1
# Call gekko for optimization
m = GEKKO(remote=False)
nt = 301; m.time = np.linspace(0,300,nt) #represent distance
# Variables
x = m.Var(value=0,ub=tf ) #represent time
v = m.Var(value=vmin,ub=vmax)
u = m.Var(value=5,ub=amax,lb=amin)
cp = m.Var(value = np.mod(x.VALUE+c0,Tc))
p = np.zeros(nt); p[-1] = 1.0
final = m.Param(value=p)
p2 = np.zeros(nt);p2[interloc] = 1.0
enforce = m.Param(p2)
# Equations
m.Equation(x.dt()==1/v)
m.Equation(v.dt()==u/v)
m.Equation(enforce*(cp-Tr)>=0)
# Final conditions
soft = True
if soft:
# soft terminal constraint
m.Minimize(final*1e5*x)
m.Minimize(final*1e5*(v+1)**2)
else:
# hard terminal constraint
xf = m.Param(); vf = m.Param()
m.free(xf); m.free(vf)
m.fix_final(xf,0); m.fix_final(vf,vmin)
# connect endpoint parameters to x and v
m.Equations([xf==x,vf==v])
# Objective Function
obj = x
m.Minimize(final*obj)
m.options.IMODE = 6
m.options.NODES = 2
m.solve()
print(cp.Value)
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.plot([0,30],[150,150],'r:',label=r'$red light$')
plt.plot(x.value,m.time,'k-',lw=2,label=r'$t$')
plt.ylabel('Distance')
plt.legend(loc='best')
plt.xlabel('Time')
plt.subplot(1,2,2)
plt.plot(x.value,v.value,'b--',lw=2,label=r'$v$')
plt.ylabel('Velocity')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()
当soft=True时,它可以找到最优解
但结果似乎不受截面限制,它在红色持续时间内穿过交叉口。
因此,问题是我应该如何正确地添加红色持续时间约束以获得最佳速度序列?以及为什么此模型中的硬约束无法找到解决方案?
非常感谢你抽出时间