我正在尝试在单个车道上创建交通拥堵的离散事件模拟。车道分为10个段,宽度为#34;每辆车经过。这些段都需要1个单位时间才能通过。我目前的设置是以相同的速度通过汽车,但我会改变它以允许更慢的移动汽车需要更长的时间并造成交通堵塞。如果当前正在使用,则每个路段都是一个受request()函数约束的资源。
我在python中使用simpy这样做。请记住,我几乎没有使用python的经验。这是我的代码:
import simpy
import numpy
inter_ArrivalTime = 2
car_Speed = 1
c = 1
simpy.seg1 = simpy.Resource(env, 1)
simpy.seg2 = simpy.Resource(env, 1)
simpy.seg3 = simpy.Resource(env, 1)
simpy.seg4 = simpy.Resource(env, 1)
simpy.seg5 = simpy.Resource(env, 1)
simpy.seg6 = simpy.Resource(env, 1)
simpy.seg7 = simpy.Resource(env, 1)
simpy.seg8 = simpy.Resource(env, 1)
simpy.seg9 = simpy.Resource(env, 1)
simpy.seg10 = simpy.Resource(env, 1)
SIM_LOG = []
class Street(object):
def __init__(self, env, carSpeed):
self.env = env
self.carSpeed = carSpeed
def seg1(self, car):
yield self.env.timeout(car_Speed)
def seg2(self, car):
yield self.env.timeout(car_Speed)
def seg3(self, car):
yield self.env.timeout(car_Speed)
def seg4(self, car):
yield self.env.timeout(car_Speed)
def seg5(self, car):
yield self.env.timeout(car_Speed)
def seg6(self, car):
yield self.env.timeout(car_Speed)
def seg7(self, car):
yield self.env.timeout(car_Speed)
def seg8(self, car):
yield self.env.timeout(car_Speed)
def seg9(self, car):
yield self.env.timeout(car_Speed)
def seg10(self, car):
yield self.env.timeout(car_Speed)
def car(env, name, Street):
with Street.seg1.request() as req:
yield req
print("%s enters road at %.2f" % (name, env.now))
SIM_LOG.append([name, "enters road", env.now])
yield env.process(Street.seg1(name))
with Street.seg2.request() as req:
yield req
SIM_LOG.append([name, "segment 2", env.now])
yield env.process(Street.seg2(name))
with Street.seg3.request() as req:
yield req
SIM_LOG.append([name, "segment 3", env.now])
yield env.process(Street.seg3(name))
with Street.seg4.request() as req:
yield req
SIM_LOG.append([name, "segment 4", env.now])
yield env.process(Street.seg4(name))
with Street.seg5.request() as req:
yield req
SIM_LOG.append([name, "segment 5", env.now])
yield env.process(Street.seg5(name))
with Street.seg6.request() as req:
yield req
SIM_LOG.append([name, "segment 6", env.now])
yield env.process(Street.seg6(name))
with Street.seg7.request() as req:
yield req
SIM_LOG.append([name, "segment 7", env.now])
yield env.process(Street.seg7(name))
with Street.seg8.request() as req:
yield req
SIM_LOG.append([name, "segment 8", env.now])
yield env.process(Street.seg8(name))
with Street.seg9.request() as req:
yield req
SIM_LOG.append([name, "segment 9", env.now])
yield env.process(Street.seg9(name))
with Street.seg10.request() as req:
yield req
print("%s left the road at %.2f" % (name, env.now))
SIM_LOG.append([name, "segment 10", env.now])
yield env.process(Street.seg10(name))
def setup(env, carSpeed, interArrivalTime, c):
street = Street(env, carSpeed)
yield env.timeout(interArrivalTime)
c += 1
env.process(car(env, c, street))
#random.seed(30)
env = simpy.Environment()
env.process(setup(env, car_Speed, inter_ArrivalTime, c))
env.run(until=60)
运行此命令会出现以下错误:
AttributeError: 'function' object has no attribute 'request'
我有两个问题: 1)这个错误的原因是什么?修复这个原因会使我的代码工作吗? 2)有没有更简单的方式来编写这种简单的模拟? - 尤其是一段又一遍地重写了大量相同代码的道路。
答案 0 :(得分:0)
Street.seg1是你在Strett类中定义的函数seg1,而不是你在上面定义的资源(为什么你甚至在simpy-Namespace中你的资源实例?)。
是的,有更好的方法。
我认为你应该首先使用SimPy文档中提供的示例,然后在继续进行实际模拟之前,先熟悉Python和SimPy。