【问题标题】:Create a global dictionary to keep track of iterations创建一个全局字典来跟踪迭代
【发布时间】:2020-12-11 06:19:51
【问题描述】:

我在 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}

【问题讨论】:

  • 您好,您能否让我们更轻松一些并突出显示您遇到问题的代码部分?
  • @RichieV 代码运行良好,但是,我需要添加额外的火车功能,以获取前方的免费信号块。
  • 没关系,你能分享一下你所做的尝试吗?在代码/伪代码方面
  • @RichieV 我正在尝试使用修改后的双向链表作为堆栈来实现这一点,每列火车都会将其前一个节点设置为红色,并检查下一个节点是否为绿色。但是我在将整个链表作为一组信号进行管理时遇到了一些麻烦。虽然我正在努力,但欢迎任何意见
  • @RichieV 我搞定了。答案已标记。

标签: python simulation modeling simpy traffic-simulation


【解决方案1】:

我得到了这个工作,而不是一个双向链表,我只是创建了一个全球字典,并为前面的信号和后面的信号保留了一个火车标志。这样,每列火车都会检查下一个信号,如果它是红色的,它会减速到下一个信号。整个项目的链接可以在这里找到 -

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多