Python 如何知道使用SimPy'时使用的是哪种资源;s Simpy.events.AnyOf

Python 如何知道使用SimPy'时使用的是哪种资源;s Simpy.events.AnyOf,python,simulation,simpy,Python,Simulation,Simpy,我正在使用python 3.7和SimPy 4。我有4个资源(称为“第一级”)的容量为5,每个资源都有一个关联的资源(称为“第二级”)的容量为1(因此,总共有4个“第一级”资源和4个“第二级”资源)。当代理到达时,它从“第一级”的任何资源请求资源,当它访问该资源时,它将请求“第二级”的相关资源 我正在使用AnyOf选择任何“第一级”资源。它可以工作,但我需要知道哪个代理选择了哪个资源。我该怎么做 以下是我目前正在做的工作: 从simpy.events导入任意事件 num\u FL\u资源=4 容

我正在使用python 3.7和SimPy 4。我有4个
资源
(称为“第一级”)的容量为5,每个
资源
都有一个关联的
资源
(称为“第二级”)的容量为1(因此,总共有4个“第一级”资源和4个“第二级”资源)。当代理到达时,它从“第一级”的任何
资源
请求
资源
,当它访问该资源时,它将请求“第二级”的相关
资源

我正在使用
AnyOf
选择任何“第一级”资源。它可以工作,但我需要知道哪个代理选择了哪个资源。我该怎么做

以下是我目前正在做的工作:

从simpy.events导入任意事件
num\u FL\u资源=4
容量资源=5
FL\u Resources=[simpy.Resource(env,capacity=capacity\u FL\u Resources)用于范围内的i(num\u FL\u Resources)]
events=[FirstLevelResource.request()用于FL_资源中的FirstLevelResource]
产生任何(环境、事件)
注1:我没有在“第一级”中使用
Store
FilterStore
,而是随机将代理放在一个可用的
Store
中,因为代理不断出现,而且所有
存储区都可能正在使用中。他们需要排队。另外,请告诉我这里是否有使用
Store
的好方法

注意2:Resource.users给了我
,所以它没有帮助

注3::我使用嵌套字典来表示“一级”和“二级”资源,如下所示。然而,为了方便起见,我没有在这里添加更长的代码

{'Resource1':{'FirstLevel1':,
“SecondLevel1”:},
'Resource2':{'FirstLevel2':,
'SecondLevel2':},
'Resource3':{'FirstLevel3':,
'第二级3':},
'Resource4':{'FirstLevel4':,
“SecondLevel4”:}

所以我在一家商店做了这件事。在商店里,我有一组一级对象,它们有一个公共的二级资源。这是密码

"""
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,groupId,secondLevel)
            store.items.append(firstLevel)

    env.process(agentGen(env, store))
    env.run(200)

    print("done")

我真是太感谢你了!你花了宝贵的时间写了一个很棒的程序。这也帮助我纠正了对request()的误解。