【问题标题】:Optional resources within a SimPySimPy 中的可选资源
【发布时间】:2021-03-10 23:47:34
【问题描述】:

我正在使用 Simpy 进行离散事件模拟,但对于我最能描述为可选资源的内容存在问题。上下文是我正在模拟将要执行的任务,并且需要一些资产(资源)来执行该任务。与大多数 SimPy 实现不同,任务需要在分配的时间开始,否则会失败,并且可能会接受较少的资源来启动任务。

例如,一个任务在时间 = t 需要四辆车。在时间 t 只有三辆车可用,因此任务以三辆车开始,但结果较少。如果只有两辆或更少的车辆可用,任务将不会继续,将被视为失败。

抱歉,此示例中缺少代码,我很难理解它。任何帮助将不胜感激。

【问题讨论】:

    标签: python simpy


    【解决方案1】:

    我使用容器来跟踪资源,但请看一下,让我知道这是否是您想要的

    """
    Demos checking the availability of resources befor taking resouces
    
    Each agent has a first choice and second choice for seizing resources
    if neither choice is avalale will not wait and skip getting resources
    
    programmer: Michael R. Gibbs
    """
    
    import simpy
    import random
    
    def eval(choice, containers):
        """
        steps through each requirement and checks if there is enough resouces is available
    
        returns True if there is enough, else returns False
        """
        for k,v in choice.items():
            if containers[k].level < v:
                # not enough
                return False
        return True
    
    def mission(env, id, firstChoice, secondChoice, startDelay, missingTime, containers):
        """
        Sims agent checking if first or second choice of resouces is available
        If so, will seize the resources, hold for a time, and then release the resouces
        """
    
        choice = None   # which choice to use
    
        # wait for mission start
        yield env.timeout(startDelay)
    
        print()
        print(f'{env.now} agent {id} is starting a mission')
        x = [(k, v.level) for k, v in containers.items()]
        print(f'available: {x}')
        print(f'first choice {firstChoice}')
        print(f'second choice {secondChoice}')
    
        # evaluate the choices and see if either will work
        if eval(firstChoice, containers):
            choice = firstChoice
            print(f'{env.now} agent {id} is going with first Choice')
        else:
            if eval(secondChoice,containers):
                choice = secondChoice
                print(f'{env.now} agent {id} is going with second Choice')
            else:
                 print(f'{env.now} agent {id} is abort because not enough resources')
    
        if choice is not None:
            # found a choice that works
            # seize the resouces, we checked that they are availale, so there should not be any queue time
            for k,v in choice.items():
                containers[k].get(v)
            
            yield env.timeout(missingTime)
    
            # return resouces
            for k,v in choice.items():
                containers[k].put(v)
    
            print(f'{env.now} agent {id} has finished the mission ')
    
    def build_choice():
        """
        quick helper to generate random first and second choice requirements
        (may not use all three resouces)
    
        returns a dictionary of the requirements where key=type, value=how much
        """
        choice = {}
        while len(choice) == 0:
            for k in ['A','B','C']:
                v = random.randint(0,3)
                if v > 0:
                    choice[k] = v
        return choice
    
    
    def gen_missions(env, containers):
        """
        Generate a series of agents to seize the resources
        """
    
        i = 0
        while True:
            i += 1 # id generator
            firstChoice = build_choice()
            secondChoice = build_choice()
    
            env.process(mission(env,i,firstChoice,secondChoice,random.randint(1,4),random.randint(2,8),containers))
    
            # put some time between agents
            yield env.timeout(1)
    
    env = simpy.Environment()
    
    # load the resouces
    containers = {}
    containers['A'] = simpy.Container(env, init=10, capacity=10)
    containers['B'] = simpy.Container(env, init=10, capacity=10)
    containers['C'] = simpy.Container(env, init=10, capacity=10)
    
    # start generating agents
    env.process(gen_missions(env,containers))
    
    env.run(100)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 2014-09-22
      • 2014-07-02
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多