1

こんにちは、SimPy で比較的複雑な離散イベント シミュレーション モデルを構築しています。

yield ステートメントを関数内に配置しようとすると、プログラムが機能しないようです。以下に例を示します。

import SimPy.SimulationTrace as Sim
import random

## Model components ##
class Customer(Sim.Process):
    def visit(self):
        yield Sim.hold, self, 2.0
        if random.random()<0.5:
            self.holdLong()
        else:
            self.holdShort()

    def holdLong(self):
        yield Sim.hold, self, 1.0
        # more yeild statements to follow

    def holdShort(self):
        yield Sim.hold, self, 0.5
        # more yeild statements to follow

## Experiment data ##
maxTime = 10.0 #minutes

## Model/Experiment ##
#random.seed(12345)
Sim.initialize()
c = Customer(name = "Klaus") #customer object
Sim.activate(c, c.visit(), at = 1.0)
Sim.simulate(until=maxTime)

これを実行して得られる出力は次のとおりです。

0 activate <Klaus > at time: 1.0 prior: False
1.0 hold  < Klaus >  delay: 2.0
3.0 <Klaus > terminated

holdLong() および holdShort メソッドはまったく機能していないようです。どうすればこれを修正できますか? 前もって感謝します。

4

2 に答える 2

6

ジェネレーター関数を呼び出すと、反復可能なジェネレーター オブジェクトが返されます。この戻り値を無視しているだけなので、何も起こりません。代わりに、ジェネレーターを反復処理して、すべての値を再生成する必要があります。

class Customer(Sim.Process):
    def visit(self):
        yield Sim.hold, self, 2.0
        if random.random()<0.5:
            for x in self.holdLong():
                yield x
        else:
            for x in self.holdShort():
                yield x
于 2011-11-28T14:03:05.043 に答える
1

Python では、yield は関数呼び出しを介して上方に伝播できません。次のように変更visitします。

def visit(self):
    yield Sim.hold, self, 2.0
    if random.random()<0.5:
        for x in self.holdLong():
            yield x
    else:
        for x in self.holdShort():
            yield x
于 2011-11-28T14:03:40.147 に答える