我正在使用带有 SimPy 模块的 Python2.7,第一次在这里发帖。我只是在学习他们两个,所以我希望我能正确解释这一点。我的程序的目标:创建一个需求对象并每周生成一个数字。将其存储在列表中。根据需求对象创建的数字,创建一个 Supply 对象并每周生成一个数字。我似乎能够创建我的 52 个数字,并将它们附加到一个列表中,但我无法成功地让 Supply 对象通读该列表。我的代码如下:
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 的明显“新鲜”展望;)