【问题标题】:How to know which Resource is being used when using SimPy's Simpy.events.AnyOf使用 SimPy 的 Simpy.events.AnyOf 时如何知道正在使用哪个资源
【发布时间】:2021-05-30 00:26:00
【问题描述】:

我正在使用带有 SimPy 4 的 python 3.7。我有 4 个容量为 5 的 Resources(比如“一级”),每个 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:我没有在“一级”中使用StoreFilterStore,而是随机将代理放到可用的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>}}

【问题讨论】:

    标签: python simulation simpy


    【解决方案1】:

    所以我在商店里做到了。在商店中,我有一组具有公共二级资源的一级对象。这是代码

    """
    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() 的误解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多