2017-02-22 13 views
1

私はSymPy(3.0.8)を初めて使用しており、与えられた値(250)までデモシミュレーション 'conference attendee'を実行したい、つまりtalk 7(3 talks 、1ブレーク、3トーク、1ブレーク、1トーク)。特定の値までSimpyシミュレーションを実行する

3 * 30 + 15 + 3 * 30 + 15 + 1 * 30 = 240

しかし、シミュレーションは3つの会談、1つのブレーク、3回の講演や1つのブレーク(210)後に終了します。

「トーク7」までシミュレーションが実行されない理由と、これを修正する方法を知りたいと思います。

# import 
import simpy 
from random import randint 

# config 
TALKS_PER_SESSION = 3 
TALK_LENGTH = 30 
BREAK_LENGTH = 15 
ATTENDEES = 3 

# process function 
def attendee(env, name, knowledge=0, hunger=0): 
    talks = 0 
    breaks = 0 
    while True: 
     # Visit talks 
     for i in range(TALKS_PER_SESSION): 
      knowledge += randint(0, 3)/(1 + hunger) 
      hunger += randint(1, 4) 
      talks += 1 
      yield env.timeout(TALK_LENGTH) 

     print('Attendee %s finished %d talks with knowledge %.2f and hunger %.2f.' % (name, talks, knowledge, hunger)) 

     # Go to buffet 
     food = randint(3, 12) 
     hunger -= min(food, hunger) 
     breaks += 1 

     yield env.timeout(BREAK_LENGTH) 

     print('Attendee %s has finished break %d with hunger %.2f' % (name, breaks, hunger)) 

# setup environment and run simulation 
env = simpy.Environment() 
for i in range(ATTENDEES): 
    env.process(attendee(env, i)) 
env.run(until=250) 

このシミュレーションの出力は次のようになります。

Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00. 
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00. 
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00. 
Attendee 0 has finished break 1 with hunger 0.00 
Attendee 1 has finished break 1 with hunger 0.00 
Attendee 2 has finished break 1 with hunger 0.00 
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00. 
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00. 
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00. 
Attendee 0 has finished break 2 with hunger 0.00 
Attendee 1 has finished break 2 with hunger 0.00 
Attendee 2 has finished break 2 with hunger 0.00 

しかし、私はそれがあることが予想:ATTENDEES = 1を設定し、インナforループを変更してください

Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00. 
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00. 
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00. 
Attendee 0 has finished break 1 with hunger 0.00 
Attendee 1 has finished break 1 with hunger 0.00 
Attendee 2 has finished break 1 with hunger 0.00 
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00. 
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00. 
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00. 
Attendee 0 has finished break 2 with hunger 0.00 
Attendee 1 has finished break 2 with hunger 0.00 
Attendee 2 has finished break 2 with hunger 0.00 
Attendee 0 finished 7 talks with knowledge <...> and hunger <...>. 
Attendee 1 finished 7 talks with knowledge <...> and hunger <...>. 
Attendee 2 finished 7 talks with knowledge <...> and hunger <...>. 

答えて

0

for i in range(TALKS_PER_SESSION): 
     print(f'Talk {talks+1} begins at {env.now}') 
     knowledge += randint(0, 3)/(1 + hunger) 
     hunger += randint(1, 4) 
     talks += 1 
     yield env.timeout(TALK_LENGTH) 
     print(f'Talk {talks} ends at {env.now}') 

シミュレーションをもう一度実行して何が起こるかを考えるとき、得られる出力が正しいことを理解する必要があります。 :)

+0

ありがとう!あなたのコードがPythonの私のバージョン(3.5.3)で動くようにするには、printコマンドを '' print {'} {1}で始める' 'print {'{talk} + 1、env.now}' ' – Rene

関連する問題