0

我正在尝试研究 simpy 2.2 的一些示例(请参阅:https ://pythonhosted.org/SimPy/Tutorials/TheBank2OO.html )并使用 simpy 3.0 词典重写它们(请参阅:http://simpy.readthedocs。 io/en/latest/about/history.html)。有没有人遇到过为 3.0 重写的以下示例(银行门打开)?我不完全确定如何使用 simpy 3.0 将“客户”类中的“self.sim.door_open”编写为事件。

from SimPy.Simulation import     Simulation,Process,Resource,hold,waituntil,request,release
from random import expovariate,seed

##Model components ------------------------

class Doorman(Process):                                       
    """ Doorman opens the door"""
    def openthedoor(self):
        """ He will open the door when he arrives"""
        yield hold,self,expovariate(1.0/10.0)                 
        self.sim.door = 'Open'                                           
        print "%7.4f Doorman: Ladies and "\
              "Gentlemen! You may all enter."%(self.sim.now(),)

class Source(Process):                                        
    """ Source generates customers randomly"""
    def generate(self,number,rate):       
        for i in range(number):
            c = Customer(name = "Customer%02d"%(i,),sim=self.sim)
            self.sim.activate(c,c.visit(timeInBank=12.0))
            yield hold,self,expovariate(rate)

class Customer(Process):                                      
    """ Customer arrives, is served and leaves """
    def visit(self,timeInBank=10):       
        arrive = self.sim.now()

        if self.sim.dooropen():
            msg = ' and the door is open.'         
        else:
            msg = ' but the door is shut.'
        print "%7.4f %s: Here I am%s"%(self.sim.now(),self.name,msg)

        yield waituntil,self,self.sim.dooropen                         

        print "%7.4f %s: I can  go in!"%(self.sim.now(),self.name)     
        wait = self.sim.now()-arrive
        print "%7.4f %s: Waited %6.3f"%(self.sim.now(),self.name,wait)

        yield request,self,self.sim.counter
        tib = expovariate(1.0/timeInBank)
        yield hold,self,tib
        yield release,self,self.sim.counter

        print "%7.4f %s: Finished    "%(self.sim.now(),self.name)                                 

## Model  ----------------------------------

class BankModel(Simulation):
    def dooropen(self):                                               
        return self.door=='Open'

    def run(self,aseed):
        self.initialize()
        seed(aseed)
        self.counter = Resource(capacity=1,name="Clerk",sim=self)
        self.door = 'Shut'
        doorman=Doorman(sim=self)                                          
        self.activate(doorman,doorman.openthedoor())                    
        source = Source(sim=self)                                                         
        self.activate(source,
             source.generate(number=5,rate=0.1),at=0.0)    
        self.simulate(until=400.0)

## Experiment data -------------------------

maxTime = 2000.0   # minutes    
seedVal = 393939

## Experiment  ----------------------------------

BankModel().run(aseed=seedVal)

到目前为止我有什么,但我收到错误“环境对象没有属性'door_open'”

import simpy
import random

忍者编辑:我设法让模拟运行,但是我无法让门初始化为“关闭”然后在某个时候打开。

def openthedoor(self):
    yield self.timeout(random.expovariate(1.0 /10.0))
    print('%7.4f Doorman: ladies and gentleman you may enter' %(self.now))



def source(env, name, counter):

     for i in range(500):
        env.process(customer(env, 'Customer%02d' % i, counter,      time_in_queue=30.0))
        t = random.expovariate(1.0 / 20.0)
        yield env.timeout(t)

def customer(env, name, counter, time_in_queue):
    arrive = env.now

    if env.process.openthedoor(env):
        msg = 'and the door is open'
    else:
        msg = 'but the door is sht'
        print('%7.4f %s: Customer has entered queue and' % (arrive, name, msg))

    yield env.process(openthedoor(env))
    print('%7.4f %s: I can go in' % (env.now, name))
    wait = env.now - arrive
    print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))

    with counter.request() as req:
        # Wait for the counter or abort at the end of our tether
        yield req 
        waited = env.now - arrive
        tib = random.expovariate(1.0 / 20.0)
        yield env.timeout(tib)
        print('%7.4f %s: Waited %6.3f' % (env.now, name, waited))
        print('%7.4f %s: Finished' % (env.now, name))

print('Batch Record Review Simulation')
random.seed(RANDOM_SEED)
env = simpy.Environment()
data = []
counter = simpy.Resource(env, capacity=1)
env.process(source(env, CUSTOMERS, counter))
# Start processes and run


env.run(until=SIM_TIME)
4

1 回答 1

0

在 SimPy 2 中,waituntil使用“忙等待”,这意味着它在每个步骤中检查条件是否为True。这不是很好的做法,可能会消耗大量 CPU 时间。这就是 SimPy 3 中不再有此功能的原因。

在 SimPy 3 中,您将使用法线Event(由 创建Environment.event())。例如,您可以创建一个Door类并将门实例的引用传递给开门器和客户。客户door.open = env.event()每次想开门时都可以这样做。然后开门器将触发该事件。

另一种解决方案可能是使用PriorityResource(或可能是`PreemptiveResource)作为门。开门器使用比客户更高的优先级,因此能够“阻止”门(== 门关闭)。一旦开门器释放资源,下一个客户就可以“打开”门。

于 2016-05-24T08:12:54.833 回答