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を使用して、「customer」クラスに「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()、ドアを開けたいときに毎回行うことができます。次に、ドアオープナーがそのイベントをトリガーします。

別の解決策は、a PriorityResource(またはおそらく `PreemptiveResource) をドアとして使用することです。ドアオープナーは顧客よりも高い優先度を使用するため、ドアを「ブロック」することができます (== ドアが閉じられます)。ドアオープナーがリソースを解放すると、次の顧客がドアを「開く」ことができます。

于 2016-05-24T08:12:54.833 に答える