【问题标题】:Simpy: Store put/get with matching timesSimpy:使用匹配时间存储 put/get
【发布时间】:2023-03-22 02:30:01
【问题描述】:

我想模拟一个出租车乘客系统。有C个出租车站,出租车和乘客可以在出发前匹配那里。由于出租车和乘客都有排队,我正在考虑使用容量 = C 的 Store,其中乘客产生“get”请求,出租车产生“put”请求。

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

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

【问题讨论】:

  • 不确定我是否理解这个问题,你能添加一个更详细的例子吗?

标签: python simpy


【解决方案1】:

我的猜测是您希望出租车一次只消耗一辆。但是,如果资源池有资源,它会立即填满所有请求。所以如果三个乘客同时请求打车,而资源池中有三辆出租车,那么这三个请求会同时被填满。

如果您希望出租车一次只消耗一辆,那么您需要添加一个看门人。在这种情况下,它将是出租车站,通常是销售柜台。

出租车站是一种资源的资源池。资源代表队列的头部。当乘客到达时,他们请求队列的头部,如果其他人已经在队列的头部,乘客排队等待轮到他们。一旦乘客排在队伍的前列,他们就会从您的出租车商店请求出租车。如果出租车店是空的,那么将有另一个等待出租车返回。请注意,乘客在他们的出租车请求实际被填满之前不会释放出租车站队列的头部。另请注意,乘客在打完出租车后需要将出租车还给出租车店

看看我的例子

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

【讨论】:

  • 对于这么晚的回复,我真的很抱歉。我所说的“出租车和乘客都有排队”的意思是这个排队是双面的。乘客和出租车都到达系统并相互匹配,然后离开系统,而不考虑出租车的返回。这就是为什么我认为它就像一个带有 get/put 的 Store。
  • 我认为您可以执行 :yield store1.get() 和 store2.get() ,这将等待两者都完成,然后再移动到下一行代码。这是你要找的吗?
  • 查看了您的原始帖子。那么出租车 2 和乘客 2 什么时候匹配呢?
  • 出租车 2 和乘客 2 将在出租车 0-乘客 0 或出租车 1-乘客 1 中的一对完成匹配并离开出租车站后立即开始匹配。您能否在之前的评论中详细说明这个想法?我想我还没有得到它...
  • 将taxiStand = simpy.Resource(env, capacity=1) 改为taxStand = simpy.Resource(env, capacity=2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2018-07-27
  • 2015-10-23
  • 2015-03-03
相关资源
最近更新 更多