我使用Python2.7用的SimPy模塊,第一次在這裏發帖。 我只是在學習他們所以我希望我解釋正確。 我的程序目標: 創建一個Demand對象並每週生成一個數字。 將它存儲在一個列表中。 根據需求對象創建的編號,創建一個Supply對象並每週生成一個數字。 我似乎能夠創建我的52號碼,並將它們追加到列表中,但我無法成功獲取Supply對象來讀取列表。 我的代碼如下:Python 2.7版:功能訪問列表
from SimPy.Simulation import *
import pylab as pyl
from random import Random
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# Model components
runLength = 51
## Lists for examination
D1Vals = []
S1Vals = []
.... other code lines
class Demander(Process):
# This object creates the demand, and stores the values in the 'D1Vals' list above
def weeklyDemand(self): # Demand Weekly
while True:
lead = 1.0 # time between demand requests
demand = random.triangular(20,110,370) # amount demanded each increment
#yield put, self, orderBook, delivery
print('Week'+'%6.0f: Need %6.0f units: Total Demand = %6.0f' %
(now(), demand, orderBook.amount))
yield hold, self, lead
yield put, self, orderBook, demand
D1Vals.append(demand)
# This object is trying to read each value iteratively in D1Vals,
and create a supply value and store in a list 'S1Vals'
class Supplier(Process):
def supply_rate(self):
lead = 1.0
for x in D1Vals:
supply = random.triangular(x - 30, x , x + 30)
yield put, self, stocked, supply
print('Week'+'%6.0f: Gave %6.0f units: Inv. Created = %6.0f' %
(now(), supply,stocked.amount))
yield hold, self, lead
S1Vals.append(stocked.amount)
..... other misc coding .....
# Model
demand_1 = Demander()
activate(demand_1, demand_1.weeklyDemand())
supply_1 = Supplier()
activate(supply_1, supply_1.supply_rate())
simulate(until=runLength)
當我運行我的程序,它創建了我的需求,每週和累計值輸出到控制檯,還打印D1Vals名單給我看看,這是不是空。
誰能請指引我正確的路徑,成功地讀取來自供應對象和功能列表。 謝謝,請原諒我對python的'新鮮'外觀;)
您的supply_rate()和weeklyDemand()方法壓痕都搞砸 – 2012-07-25 19:55:10