Simpy:存储具有匹配时间的put/get

2024-06-07 01:43:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想模拟一个出租车乘客系统。这里有C个出租车站,出租车和乘客在离开前可以在那里配对。因为出租车和乘客都在排队,所以我考虑使用容量为C的商店,乘客会发出“get”请求,出租车会发出“put”请求

但是,“get”请求似乎会立即从商店获取资源(出租车)。在这种情况下,我如何处理匹配时间

例如,如果有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

Tags: andthegetis系统with商店排队
1条回答
网友
1楼 · 发布于 2024-06-07 01:43:12

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

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

出租车停靠站是一个资源池,包含一种资源。资源表示队列的头。当乘客到达时,他们要求排队等候,如果其他人已经在排队等候,乘客将排队等待轮到他们。一旦乘客排在队列的最前面,他们就会向您的出租车店请求出租车。如果出租车店是空的,那么将有另一个等待,直到出租车返回。请注意,在乘客的出租车请求实际被填满之前,乘客不会释放出租车招呼站队列的负责人。还请注意,乘客需要在完成出租车后将出租车送回出租车商店

看看我的例子

"""
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) 

相关问题 更多 >