nos*_*rck 2 python simulation model traffic-simulation simpy
我需要帮助在SimPy中为火车系统(如地铁系统)建模,问题是我的"模拟"老师要我使用Python + SimPy并且我不知道如何使用它,经过一周的阅读后SimPy我设法理解银行的例子,这很容易,但现在我仍然不知道如何模拟老师给我的问题......
问题是大而广泛的,但我只需要一点帮助,而不是整个项目完成,所以,如果有人可以提供帮助,我将非常感激.
减少的问题将帮助我开始是这样的:
想象你有3个站点(A,B,C)相隔一段距离(比方说100米)我有2个列车,一个在A上,一个在C上(train1从A到C,tran2从C到A)每列火车都有最大速度(假设两者都是50米/秒)和加速度(比如5米/秒^ 5和-5米/秒^ 2),他们所要做的就是停在每个火车站上秒(比方说24秒)并继续到下一站,当他们到达终点时,他们等待并额外20秒(改变铁轨),然后重新开始.
车站有乘客限制(他们不需要模拟)唯一的事情是每个车站有随机数量的乘客,当火车到达时有些人起床而其他人下车...
所以,基本上我需要一个小型火车和这三个车站的车站...如果有人可以帮我请...
真正的问题有22个站点,2个额外的车站改变车道,随机时间等待,42个列车具有不同的最大速度和容量,不同的乘客数量取决于车站和时间等...所有这些东西我可以管理以后,但火车站系统的建模逻辑我似乎无法弄清楚...感谢您的帮助!
我绝不是SimPy的专家,但这应该让你开始:
"""
Simulation of a train network
"""
from SimPy.Simulation import *
from math import sqrt
from random import randint
from itertools import cycle
def timeTo(A, maxV, d):
"""
Given a trapezoidal velocity envelope defined by
A constant acceleration, m/s/s
maxV maximumum velocity, m/s
return time in seconds required to travel
d distance, m
"""
tA = float(maxV)/A # time to accelerate to maxV
dA = A*tA*tA # distance traveled during acceleration from 0 to maxV and back to 0
if (d < dA): # train never reaches full speed?
return sqrt(4.0*d/A) # time needed to accelerate to half-way point then decelerate to destination
else:
return 2*tA + (d-dA)/maxV # time to accelerate to maxV plus travel at maxV plus decelerate to destination
class Train(Process):
def __init__(self, name, sim, accel=1.0, maxV=50.0, passengers=0, maxPassengers=400):
super(Train, self).__init__(name, sim)
self.accel = accel
self.maxV = maxV
self.p = passengers
self.maxP = maxPassengers
def roll(self, route):
here = route.next() # starting location
for dest in route:
# travel to next station
print "{:.1f}s: {} leaving {} for {}".format(self.sim.now(), self.name, here, dest)
yield hold, self, timeTo(self.accel, self.maxV, here.distanceTo[dest])
# arrive at next station
here = dest
print "{:.1f}s: {} at {}".format(self.sim.now(), self.name, here)
yield hold, self, here.arrive(self)
def getOff(self, num):
if self.p >= num:
print " {} passengers got off".format(num)
self.p -= num
else:
num = self.p
print " train is empty - only {} passengers got off".format(num)
self.p = 0
def getOn(self, num):
if (self.maxP is None) or (self.p + num <= self.maxP):
print " {} passengers got on".format(num)
self.p += num
else:
num = self.maxp - self.p
print " train is full - only {} passengers got on".format(num)
self.p = self.maxp
class TrackNode(object):
def __init__(self, name, delay=5.0):
self.name = name
self.delay = delay
self.distanceTo = {}
def arrive(self, train):
pass
def __str__(self):
return self.name
class Station(TrackNode):
def arrive(self, train):
train.getOff(randint(1,15))
train.getOn(randint(1,15))
return self.delay
class Switch(TrackNode):
def arrive(self, train):
print(" switching tracks")
return self.delay
class SampleRailroad(Simulation):
def run(self, maxTime=100.0):
self.initialize()
# create places
x = Switch("switch x", 20.0)
A = Station("Station A", 24.0)
B = Station("Station B", 27.0)
C = Station("Station C", 25.0)
y = Switch("switch y", 18.0)
# distances between places
x.distanceTo[A] = 50.0
A.distanceTo[B] = 5000.0
B.distanceTo[C] = 2000.0
C.distanceTo[y] = 80.0
y.distanceTo[C] = 80.0
C.distanceTo[B] = 2000.0
B.distanceTo[A] = 5000.0
A.distanceTo[x] = 50.0
# set up first train
sf = Train("Santa Fe 219", self)
self.activate(sf, sf.roll(cycle([A,B,C,y,C,B,A,x])), at=0.0)
# set up second train
cn = Train("Canadian National 41", self, maxPassengers=200)
self.activate(cn, cn.roll(cycle([C,B,A,x,A,B,C,y])), at=5.0)
# start simulating!
self.simulate(until=maxTime)
def main():
rr = SampleRailroad()
rr.run(800.0)
if __name__=="__main__":
main()
Run Code Online (Sandbox Code Playgroud)