我正在研究 python 2 中的排队模拟模型,该模型有工作进入系统并请求多个资源。每个到达的工作都需要不同数量的资源(而不是特定的资源!),并在不同的时间内使用这些资源。
我发现的任何此类示例都请求特定资源,例如 res[1] 和 res[2]。我只需要请求 2 个资源。
此外,我的作业仅在第一个作业完成后才运行。我知道我的 for 循环存在问题,但我不确定如何正确修复它。在这种情况下,因为有 2 个资源 a 和 b 应该能够在时间 1 运行。但是 b 等到 a 完成。奇怪的。
我将不胜感激有关请求多种资源并在适当的时间运行作业的帮助。
到目前为止,这是我的代码:
import simpy
#resource
class SuperComputer:
def __init__(self, env):
self.nodes = simpy.Resource(env, capacity = 2)
#users of resource
class Job:
#enter: time the job enters the system
#timeout is how long the job occupies a resource for
#resources is how many resources a job needs in order to run
def __init__(self, env, name, enter,timeout, resources):
self.env = env
self.name = name
self.enter = enter
self.timeout = timeout
self.resources = resources
#system
def system(env, jobs, super_computer):
with super_computer.nodes.request() as req:
for job in jobs:
print('%s arrives at %s' % (job.name, job.enter))
yield req
yield env.timeout(job.enter)
print('%s starts running with %s resources at %s' % (job.name, job.resources, env.now))
yield env.timeout(job.timeout)
print('%s completed job at %s' % (job.name, env.now))
env = simpy.Environment()
super_computer = SuperComputer(env)
jobs = [
Job(env, 'a', 1, 4, 1),
Job(env, 'b', 1, 4, 1),
Job(env, 'c', 1, 4, 1),
Job(env, 'd', 1, 4, 1),
]
env.process(system(env, jobs, super_computer))
env.run(50)
输出:
a arrives at 1
a starts running with 1 resources at 1
a completed job at 5
b arrives at 1
b starts running with 1 resources at 6
b completed job at 10
c arrives at 1
c starts running with 1 resources at 11
c completed job at 15
d arrives at 1
d starts running with 1 resources at 16
d completed job at 20