Simpy:用匹配的时间存储放置/获取



我想模拟一个出租车乘客系统。有C出租车站,出租车和乘客可以在离开前在那里配对。由于出租车和乘客都在排队,我考虑使用容量为C的商店,在那里乘客会让座;得到";请求和出租车让步;放入";请求。

然而;得到";请求似乎立即从商店拿走了资源(出租车(。在这种情况下,我如何处理匹配时间?

例如,如果有2个接入点,该过程将类似于

0.00 Passenger 0 arrives
0.10 Passenger 1 arrives
0.11 Taxi 0 arrives
0.11 Passenger 0 is matching with Taxi 0
0.15 Passenger 2 arrives
0.16 Taxi 1 arrives
0.16 Passenger 1 is matching with Taxi 1
0.17 Taxi 2 arrives (and wait in the queue because 2 access points are occupied)
0.20 Passenger 0 and Taxi 0 finish and leave the system
0.20 Passenger 2 is matching with Taxi 2

我的猜测是,您希望出租车一次消耗一辆。但是,如果资源池具有资源,它将立即填充所有请求。因此,如果三名乘客同时请求一辆出租车,并且资源池中有三辆出租车,那么这三个请求将同时被填满。

如果你想一次消耗一辆出租车,那么你需要增加一名门卫。在这种情况下,它将是出租车站,通常是销售柜台。

出租车站是一个资源库。资源表示队列的头。当乘客到达时,他们要求排在队伍的最前面,如果其他人已经排在队伍最前面,乘客就会排队等候。一旦乘客排在队伍的最前面,他们就会向你的出租车商店要求打车。如果出租车商店是空的,那么就要再等一次,直到出租车回来。请注意,乘客只有在他们的出租车请求被实际填满后,才会放开出租车站的排队队伍。还要注意,乘客在完成出租车后,需要将出租车送回出租车商店

查看我的示例

"""
simulation of passengers waiting for taxis at a taxi stand
Passengers first wait to be first in line to seize the taxi stand
Then they wait for the next taxi
There is a short delay as the passenger gets into to taxi
before the passenger releases the the taxi stand, allowing 
the next passenger to wait for the next taxi
When the sim start, the taxi stand will have a queue of taxi
that will be eventualy depleted by the arriving passengers
as the passengers arrive faster then the taxi can server
programmer Michael R. Gibbs
"""
import simpy
from random import randint
class IdClass():
"""
quick class that generates unique id's 
for each object created
"""
nextId = 1
def __init__(self):
self.id = type(self).nextId
type(self).nextId +=1
class Passenger(IdClass):
"""
Passenger waiting for a taxi
"""

class Taxi(IdClass):
"""
Taxi is the resource passengers are competing for
"""

def getTaxi(env, passenger, taxiStand, taxiQueue):
"""
passenger waits for cab
then takes cap for a trip
Taxi returns after trip
"""

# get in line at the taxi stand
with taxiStand.request() as turnReq:
print(f'{env.now} passenger {passenger.id} has entered taxi stand queue')
yield turnReq
# first in line, now wait for taxi
print(f'{env.now} passenger {passenger.id} is waiting for taxi')
taxi = yield taxiQueue.get()
# got taxi, tine to get into cab
print(f'{env.now} passenger {passenger.id} has taken taxi {taxi.id}')
yield env.timeout(2)
# now cab leaves and taxi stand is free for next passenger
# leave stand and use taxit
print(f'{env.now} taxi {taxi.id} has left')
yield env.timeout(randint(10,60))
# taxi returns for next passenger
yield taxiQueue.put(taxi)
print(f'{env.now} taxi {taxi.id} has return')

def genPassengers(env, taxiStand, taxiQueue):
"""
generates arriving passengers and kicks off their taxi use
"""
while True:
yield env.timeout(randint(1,15))
env.process(getTaxi(env,Passenger(), taxiStand, taxiQueue))

# create simulation
env = simpy.Environment()
# start taxiStand
taxiStand = simpy.Resource(env, capacity=1)
# start taxi queue with 5 taxies
taxiQueue = simpy.Store(env) # unlimited capacity
taxiQueue.items = [Taxi() for _ in range(5)]
# start taxi stand with a queue of 3 passengers
for _ in range(3):
env.process(getTaxi(env, Passenger(),taxiStand,taxiQueue))
# start generating passengers
env.process(genPassengers(env,taxiStand,taxiQueue))
# start sim
env.run(until = 100) 

最新更新