Simpy:阻止资源超出其服务用户

时间:2017-04-10 15:20:24

标签: python-3.x simpy

考虑Simpy示例列表中描述的carwash。现在假设每次维修汽车时,相应的清洗单元必须自己维修,然后才能清洗下一辆汽车。同时,服务车可以离开。

我如何才能最好地模仿上述内容?我目前的解决方案是让具有高优先级的“鬼车”在再生时阻止洗车。我发现这个解决方案并不优雅,并且猜测有更好的方法。

在下面的例子中,代表上述教程的不良副本,服务车在再生期间不能离开泵。我怎么能修复它以模仿预期的行为?我想解决方案很简单;我只是没有看到它。

import random
import simpy

RANDOM_SEED = 42
NUM_MACHINES = 2  # Number of machines in the carwash
WASHTIME = 5      # Minutes it takes to clean a car
REGENTIME = 3
T_INTER = 7       # Create a car every ~7 minutes
SIM_TIME = 20     # Simulation time in minutes


class Carwash(object):
    def __init__(self, env, num_machines, washtime):
        self.env = env
        self.machine = simpy.Resource(env, num_machines)
        self.washtime = washtime

    def wash(self, car):
        yield self.env.timeout(WASHTIME)
        print("Carwash removed %s's dirt at %.2f." % (car, env.now))

    def regenerateUnit(self):
        yield self.env.timeout(REGENTIME)
        print("Carwash's pump regenerated for next user at %.2f." % (env.now))


def car(env, name, cw):
    print('%s arrives at the carwash at %.2f.' % (name, env.now))
    with cw.machine.request() as request:
        yield request

        print('%s enters the carwash at %.2f.' % (name, env.now))
        yield env.process(cw.wash(name))

        yield env.process(cw.regenerateUnit())

        print('%s leaves the carwash at %.2f.' % (name, env.now))


def setup(env, num_machines, washtime, t_inter):
   # Create the carwash
    carwash = Carwash(env, num_machines, washtime)

    # Create 4 initial cars
    for i in range(4):
        env.process(car(env, 'Car %d' % i, carwash))

    # Create more cars while the simulation is running
    while True:
        yield env.timeout(random.randint(t_inter - 2, t_inter + 2))
        i += 1
        env.process(car(env, 'Car %d' % i, carwash))


# Setup and start the simulation
random.seed(RANDOM_SEED)  # This helps reproducing the results

# Create an environment and start the setup process
env = simpy.Environment()
env.process(setup(env, NUM_MACHINES, WASHTIME, T_INTER))

# Execute!
env.run(until=SIM_TIME)

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

您想要的是对使用具有非常高优先级的资源的实体进行建模,以便普通实体同时无法使用它。所以你的鬼车"实际上并不是一个坏主意。