创建一个全局字典来跟踪迭代

Create a global dictionary to keep track of iterations

我已经在simpy中创建了一个火车模拟,但是为了跟踪前面的火车,我打算使用字典,其中键值可以作为'Signal'状态。这些键基本上是信号编号。前面的火车可以检查下一个信号灯是否为绿色,反之亦然。但是,对于其余信号,代码似乎工作正常,但我想检查信号 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}

我得到了这个工作,而不是 doubly-linked 列表,我只是创建了一个全局词典并保留了火车的标志,用于前面的信号和后面的信号。这样,每列火车都会检查下一个信号灯,如果是红色,就会减速到下一个信号灯。整个项目的 link 可以在这里找到 -

https://github.com/abhiray92/train_simulation_simpy/blob/main/Simulation.ipynb