我在 simpy 中创建了一个火车模拟,但为了跟踪前面的火车,我计划使用字典,其中键值可以充当“信号”状态。键基本上是信号编号。前面的列车可以检查下一个信号是否为绿色,反之亦然。但是,对于其余信号,代码似乎工作正常,但我想检查信号 0 和信号 1,以便火车不会生成或不离开。
我没有输入整个代码,因为它只会让它变得冗长。这只是为了让我了解我正在尝试做的事情。
这是示例代码 -
signal_dict={}
def switchSignal(self,signal):
if signal == 1 or signal == 0:
signal_dict[signal]= False
return signal_dict
else:
s[signal-1] = False
return signal_dict
def switchSignal_2(self,signal):
if signal == 1 or signal == 0:
signal_dict[signal]= True
return signal_dict
else:
signal_dict[signal-1] = True
return signal_dict
class Train(object):
def __init__(self,xxxx):
xxxxxxx
xxxxxxx
def engagelock(self, car, drivetime,signals):
with self.machine.request() as request:
yield request
for signal in range(0,signals):
switchSignal(self,signal)
while signal_dict.get(signal+1) is False :
print(f"{now():s} {self.name:s} is waiting for Signal {signal+1} to turn GREEN")
yield env.timeout(60)
else:
if isdelay()[0] == True:
switchSignal(self,signal)
time_delay=round(delay()[0],2)
print(f"\n{now():s} {self.name:s} is experiencing a delay of {round((time_delay*10)/60,2)} min at Signal {signal}")
print(signal_dict)
yield env.timeout(round(time_delay*10,2))
switchSignal_2(self,signal)
def process(self,k):
here = 'London Old Oak Commons'
dest = 'Birmingham Interchange'
t1=env.now
print(f"{now():s} {self.name:s} Departed from {here:s}")
drivetime=timeTo(self.accel, self.maxV, d)
yield env.process(self.engagelock(self.name,drivetime,k))
yield env.process(self.releaselock(self.name))
yield env.timeout(drivetime)
print(f"{now():s} {self.name:s} has arrived at {dest:s}, Travelling time {round((env.now-t1)/60,2)} mins")
for i in range(int((stop-start)/timing)):
print(signal_dict)
while signal_dict.get(0) is False:
print(f"Train waiting to depart, congestion ahead!")
yield env.timeout(60)
else:
t = Train(i)
env.process(t.process(k,timing))
yield env.timeout(timing)
env = simpy.Environment()
env.process(trainGenerator(start=8*3600, stop=12*3600, timing=1500))
env.run()
当我试图跟踪全局字典 signal_dict 时,火车生成器似乎无法看到其中的更新值。
Total number of Signal Blocks 6
Distance Between signalling blocks is 25.17km
{0: True, 1: True, 2: True, 3: True, 4: True, 5: True}
08:00:00 [Train 0] Departed from London Old Oak Commons
08:00:00 [Train 0] is experiencing a delay of 3.37 min at Signal 5
{0: True, 1: True, 2: True, 3: True, 4: False}
{0: True, 1: True, 2: True, 3: True, 4: True, 5: True}
08:05:00 [Train 1] Departed from London Old Oak Commons
{0: True, 1: True, 2: True, 3: True, 4: True, 5: True}
08:10:00 [Train 2] Departed from London Old Oak Commons
08:10:00 [Train 2] is experiencing a delay of 14.46 min at Signal 3
{0: True, 1: True, 2: False, 3: True, 4: True}
{0: True, 1: True, 2: True, 3: True, 4: True, 5: True}