I am using Python2.7 with the SimPy module, first time posting here.
I am just learning both of them so I hope I explain this correctly.
The aim of my program:
Create a Demand object and generate a number weekly.
Store it in a list.
Create a Supply object and generate a number weekly, based on the number created by the demand object.
I seem to be able to create my 52 numbers, and append them to a list, but I cant succesfully get the Supply object to read through the list.
My code is as follows:
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)
When I run my program, it creates my demand and outputs the weekly and cumulative values to the console, it also prints the D1Vals list for me to see that it is not empty.
Can anyone please guide me to the correct path for succesfully reading the list from the Supplier Object and function.
Thanks and please excuse my obvious ‘fresh’ outlook on python 😉
D1ValsandS1Valsare defined at the module scope;You should be able to write expressions like
x=S1Vals[-7:]anywhere in that module without a problem.This works for accessing the value of and mutating the value of these variables,
so that
should work.
To assign to them however, you need to declare them as global
If the
global S1Valsline is omitted, the result will be that the function-localvariable
S1Valsis defined by the assignment statement, shadowing the module-levelvariable with the same name.
A way to avoid using the global statement is to use the actual module name
to refer to these variables. I’ll assume that your code is defined in
SupplyAndDemandModel.py.At the top of this file you can put
and then refer to those module-scoped variables using the module name:
This provides a way of unambiguously indicating that you are accessing/modifying a module-level variable.