我正在研究python 2中的排队模拟模型,该模型具有进入系统并请求多个资源的作业。到达的每个作业都会请求不同数量的资源(而不是特定资源!)并将资源用于不同的时间。
我找到的任何这样的例子都会请求特定的资源,比如res [1]和res [2]。我只需要2个资源。
此外,我的作业仅在第一个作业完成后才会运行。我知道我的for循环存在问题,但我不确定如何正确修复它。 在这种情况下,因为有2个资源a和b应该能够在时间1运行。但是b等待直到完成。奇。
我很感激请求多个资源并在适当的时间运行作业。
到目前为止,这是我的代码:
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
答案 0 :(得分:0)
问题是你的代码需要完成一轮for循环才能继续下一个。
如果for循环放在流程之外,您的代码就可以工作,而是为每个作业创建一个允许它们并行运行的新流程。
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, job, super_computer):
with super_computer.nodes.request() as req:
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),
]
for job in jobs:
env.process(system(env, job, super_computer))
env.run(50)