Reputation: 485
I created a function with an if flow control, I am wondering a few things about it.
class Supplier(Process):
def weeklySupply(self):
global D1Vals
xMeet = D1Vals[0]
lead = 0
capac = 150.0
supply = 0
if stocked.amount >= 15000:
supply = 0
yield put, self, stocked, supply
S1Vals.append(0)
elif supply >= capac:
supply = capac
yield put, self, stocked, supply
S1Vals.append(capac)
else:
supply = random.triangular(.70 * xMeet , xMeet , xMeet * 1.05)
yield put, self, stocked, supply
print ('Week:'+'%6.0f:Supplied: %6.0f units. CurSupply = %6.0f' %
(now() + 1., supply, stocked.amount))
yield hold, self, lead
S1Vals.append(supply)
del D1Vals[0]
What I though I coded was:
If the amount in the stocked object was >= 15000, then the object generates zero supply or if a supply value was generated which was greater than a predefined 'capac' amount, then the supply value = 'capac'.
If non of the above were true then the supply value was generated as a random distribution. When I get my output though, the supply value does exceed the 'capac' value of 150.
Have I misunderstood how to do the flow?
Upvotes: 2
Views: 283
Reputation: 485
Seems to work a treat, and some nice things for me to think about. Only issue I can see is now my S1Vals list has stopped having the zero supply appended to the list. I can see this on my output graph and also when i do a len(S1Vals). The final code is with your suggestions is:
if stocked.amount >= 15000:
if supply >= capac:
supply = capac
S1Vals.append(capac)
yield put, self, stocked, supply
else:
supply = 0.0
yield put, self, stocked, supply
S1Vals.append(supply)
else:
supply = min(capac, random.triangular(.70 * xMeet , xMeet , xMeet * 1.05))
yield put, self, stocked, supply
print ('Week:'+'%6.0f:Supplied: %6.0f units. CurSupply = %6.0f' %
(now() + 1., supply, stocked.amount))
yield hold, self, lead
S1Vals.append(supply)
del D1Vals[0]
To get here I used the flow suggestion above from MaxPower, also min(supply,capac)
, and declared a global supply
. The last issue seems that supply = 0.0
never kicks in, but it should really?
Upvotes: 0
Reputation: 5486
Do not mixup the if elif
construct with some kind of or
as you did in your question. The elif
condition will only be evaluated if the if
condition is False
.
I think, what you really want is:
if stocked.amount >= 15000:
if supply >= capac:
supply = capac
yield put, self, stocked, supply
S1Vals.append(capac)
else:
supply = 0
yield put, self, stocked, supply
S1Vals.append(0)
else:
supply = random.triangular(.70 * xMeet , xMeet , xMeet * 1.05)
...
But this would only work, if supply
is not initialised with 0 at the beginning of the function.
Upvotes: 1