0

我一直在尝试使用 Simpy 进行此模拟,但我无法弄清楚它是如何工作的。如果您有任何关于如何从示例代码中学习它的提示(从底部开始,通过函数向上,或者相反?),或者任何已经有很大帮助的好的资源。

我要模拟的内容:在 t=0 时具有 S 个租赁站和 T 辆自行车的自行车租赁服务。客户的到来和租赁时间呈指数分布。租用自行车时,有给定的概率去任何一个租用站。例如,当 S=2 时,概率为 [[0.9,0.1],[0.5,0.5]]。

我试图在没有简单的情况下做到这一点,但我不知道如何管理车站的自行车数量并在租赁发生时管理到达。

任何帮助都非常受欢迎,因为我开始有点绝望了。谢谢!

4

1 回答 1

0

这是一种方法

"""
Simple simulation of several bike rental stations

Stations are modeled with containers so bikes can be returned
to a station different from where it was rented from

programer:  Michael R. Gibbs
"""

import simpy
import random

# scenario attributes
station_names = ['A','B']
rent_probs = [.9,.1]
return_probs = [.5,.5]
bikes_per_station = 5

def rent_proc(env, id, station_names, rent_probs, return_probs, station_map):
    """
    Models the process of:
    selecting a station
    renting a bike
    using a bike
    returning a bike (can be different station)
    """

    #select a station
    name = random.choices(station_names,weights=rent_probs)
    name = name[0]
    station = station_map[name]

    print(f'{env.now}: id:{id} has arrived at station {name} q-len:{len(station.get_queue)} and {station.level} bikes')

    # get a bike
    yield station.get(1)

    print(f'{env.now}: id:{id} has rented bike at station {name} q-len:{len(station.get_queue)} and {station.level} bikes')

    # use bike
    yield env.timeout(random.triangular(1,5,3))

    # return bike
    name = random.choices(station_names,weights=return_probs)
    name = name[0]
    station = station_map[name]

    yield station.put(1)

    print(f'{env.now}: id:{id} has returned bike at station {name} q-len:{len(station.get_queue)} and {station.level} bikes')

def gen_arrivals(env, station_names, rent_probs, return_probs, station_map):
    """
    Generates arrivales to the rental stations
    """

    cnt = 0

    while True:
        yield env.timeout(random.expovariate(2.5))
        cnt += 1
        env.process(rent_proc(env,cnt,station_names,rent_probs,return_probs, station_map))

# set up
env = simpy.Environment()

# create station based on name list
cap = len(station_names) * bikes_per_station
station_map = {
    name: simpy.Container(env, init=bikes_per_station, capacity=cap)
    for name in station_names
}

# start generation arrivals
env.process(gen_arrivals(env, station_names, rent_probs, return_probs, station_map))

# start sim
env.run(100)
于 2021-12-07T00:58:09.293 回答