问题描述
我在 SimPy 4 中使用 python 3.7。我有 4 个 Resources
(比如“第一级”),容量为 5,每个 Resource
都有一个关联的 Resource
(比如“二级”),容量为 1(因此,总共有 4 个“一级”资源和 4 个“二级”资源)。当代理到达时,它会从“第一级”的任何 Resource
请求 Resource
,当它获得访问权限时,它会请求“第二级”的关联 Resource
。
我正在使用 AnyOf
选择任何“第一级”资源。它有效,但我需要知道哪个代理选择了哪个资源。我该怎么做?
以下是我目前所做的工作:
from simpy.events import AnyOf,Event
num_FL_Resources = 4
capacity_FL_Resources = 5
FL_Resources = [simpy.Resource(env,capacity = capacity_FL_Resources ) for i in range(num_FL_Resources)]
events = [FirstLevelResource.request() for FirstLevelResource in FL_Resources]
yield Anyof(env,events)
注意 1:我没有在“第一级”中使用 Store
或 FilterStore
,而是将代理随机放置到一个可用的 Store
因为代理不断来,所有的 Stores
可能都在使用中。他们需要排队。另外,请告诉我这里是否有使用 Store
的好方法。
注意 2:Resource.users 给了我 <Request() object at 0x...>
所以它没有帮助。
注意 3::我对“第一级”和“第二级”资源使用嵌套字典,如下所示。但是,为了方便起见,我没有在此处添加更长的代码。
{'Resource1': {'FirstLevel1': <simpy.resources.resource.Resource at 0x121f45690>,'SecondLevel1': <simpy.resources.resource.Resource at 0x121f45710>},'Resource2': {'FirstLevel2': <simpy.resources.resource.Resource at 0x121f457d0>,'SecondLevel2': <simpy.resources.resource.Resource at 0x121f458d0>},'Resource3': {'FirstLevel3': <simpy.resources.resource.Resource at 0x121f459d0>,'SecondLevel3': <simpy.resources.resource.Resource at 0x121f45a90>},'Resource4': {'FirstLevel4': <simpy.resources.resource.Resource at 0x121f47750>,'SecondLevel4': <simpy.resources.resource.Resource at 0x121f476d0>}}
解决方法
所以我是在一家商店里做的。在商店中,我有一组具有共同二级资源的一级对象。这是代码
"""
example of a two stage resource grab using a store and resouces
A agent will queue up to get a first level resource object
and then use this object to get a second level rescource
However groups of the frist level resouce have one common second level resource
so there will also be a queue for the second level resource.
programer: Michael R. Gibbs
"""
import simpy
import random
class FirstLevel():
"""
A frist level object,a group of these objects will make a type of resource
each object in the group will have the same second level resource
"""
def __init__(self,env,groupId,secondLevel):
self.env = env
self.groupId = groupId
self.secondLevel = secondLevel
def agent(env,agentId,firstLevelStore):
"""
sims a agent/entity that will first grab a first level resource
then a second level resource
"""
print(f'agent {agentId} requesting from store with {len(firstLevelStore.items)} and queue {len(firstLevelStore.get_queue)}')
# queue and get first level resouce
firstLevel = yield firstLevelStore.get()
print(f"agent {agentId} got first level resource {firstLevel.groupId} at {env.now}")
# use the first level resource to queue and get the second level resource
with firstLevel.secondLevel.request() as req:
yield req
print(f"agent {agentId} got second level resource {firstLevel.groupId} at {env.now}")
yield env.timeout(random.randrange(3,10))
print(f"agent {agentId} done second level resource {firstLevel.groupId} at {env.now}")
# put the first level resource back into the store
yield firstLevelStore.put(firstLevel)
print(f"agent {agentId} done first level resource {firstLevel.groupId} at {env.now}")
def agentGen(env,firstLevelStore):
"""
creates a sequence of agents
"""
id = 1
while True:
yield env.timeout(random.randrange(1,2))
print(f"agent {id} arrives {env.now}")
env.process(agent(env,id,firstLevelStore))
id += 1
if __name__ == '__main__':
print("start")
num_FL_Resources = 4 # number of first level groups/pools
capacity_FL_Resources = 5 # number of first level in each group/pool
env = simpy.Environment()
# store of all first level,all mixed togethers
store = simpy.Store(env,capacity=(num_FL_Resources * capacity_FL_Resources))
for groupId in range(num_FL_Resources):
# create the second level resource for each group os first level resources
secondLevel = simpy.Resource(env,1)
for cap in range(capacity_FL_Resources):
# create the individual first level objects for the group
firstLevel = FirstLevel(env,secondLevel)
store.items.append(firstLevel)
env.process(agentGen(env,store))
env.run(200)
print("done")