微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Simpy:存储匹配时间的放置/获取

我想模拟一个出租车-乘客系统。有C个出租车站,出租车和乘客在离开前可以在那里匹配。由于出租车和乘客都有队列,我在考虑使用容量 = C 的 Store,其中乘客产生“获取”请求,出租车产生“放置”请求。

然而,“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

解决方法

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

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

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

看看我的例子

"""
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,taxiQueue):
    """
    generates arriving passengers and kicks off their taxi use
    """

    while True:
        yield env.timeout(randint(1,15))
        env.process(getTaxi(env,Passenger(),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,taxiQueue))

# start generating passengers
env.process(genPassengers(env,taxiQueue))

# start sim
env.run(until = 100) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。