我的猜测是您希望出租车一次只消耗一辆。但是,如果资源池有资源,它会立即填满所有请求。所以如果三个乘客同时请求打车,而资源池中有三辆出租车,那么这三个请求会同时被填满。
如果您希望出租车一次只消耗一辆,那么您需要添加一个看门人。在这种情况下,它将是出租车站,通常是销售柜台。
出租车站是一种资源的资源池。资源代表队列的头部。当乘客到达时,他们请求队列的头部,如果其他人已经在队列的头部,乘客排队等待轮到他们。一旦乘客排在队伍的前列,他们就会从您的出租车商店请求出租车。如果出租车店是空的,那么将有另一个等待出租车返回。请注意,乘客在他们的出租车请求实际被填满之前不会释放出租车站队列的头部。另请注意,乘客在打完出租车后需要将出租车还给出租车店
看看我的例子
"""
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)