GEKKO-通过模块操作添加不等式约束

问题描述

我正在尝试最小化整个行程的旅行时间,但是我必须考虑十字路口红灯持续时间的限制。我选择离散的是距离,所以 m。实际上表示距离,而 x 表示目标车辆在其行驶范围内的行驶时间。 cp 表示信号循环时钟中车辆通过交叉路口的时间, c0 是车辆偏离其原点 Tc 的时钟时间。 strong>代表时钟周期, Tr 是红灯持续时间。

这是代码

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)
plt.plot(x.value,v.value,'b--',label=r'$v$')
plt.ylabel('VeLocity')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()

soft = True 时,它可以找到最佳解决方https://i.stack.imgur.com/Gqb4j.png 但结果似乎不受积分的约束,它在红色持续时间内穿过交叉点。 那么问题是我应该如何正确添加红色持续时间约束以获得最佳速度序列?为什么这个模型中的硬约束找不到解决方案? 非常感谢您的宝贵时间。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)