0

I simulate a warehouse in R, where supplies (o.in) arrive with random lead times and demand (d) is served. The stock level (stockHistory) is calculated based on the difference between supply and demand.

for(i in 1:periods){

  stockHistory[i] = stockHistory[i] - d[i] + o.in[i] # o.in has been determined in previous looping, d is known before loop
  openOrders = openOrders - o.in[i]

  missing_packages = ceiling((reorderPoint - stockHistory[i] - openOrders)/q) # stockHistory + openOrders are based on o.in and calculated in loop

  if (missing_packages > 0 ) {
    openOrders = openOrders + missing_packages * q
    o.in[leadtimes[i]+i] = o.in[leadtimes[i]+i] + missing_packages * q # leadtimes is known before looping
  }
}

How can I avoid the for loop by vectorization? My problem is that I do not know how to handle the

o.in[leadtimes[i]+i] = o.in[leadtimes[i]+i] + missing_packages * q

in a vectorizedd way so that the offset leadtimes[i]+i in the vector is accounted for elementwise in the calculation. I checked diff(), but it doesn't seem apt for my use case.

The second problem is that I cannot determine the right order for the operations since they are based one on another.

Any help would be highly appreciated. Thanks in advance!

mondano
  • 827
  • 10
  • 29
  • sample input and output (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be quite helpful here to clarify your question. – Marat Talipov Jan 29 '15 at 15:08

1 Answers1

1

Well, let's look at it line by line. For example:

 stockHistory[i] = stockHistory[i] - d[i] + o.in[i] 

Since there's no "history dependence," just write

stockHistory <- stockHistory -d +o.in 

  openOrders = openOrders - o.in[i]

Here, make openOrders a vector of same length as everything else.

openOrders <- -o.in # and later modify each i-th element in some way

The problem you're going to have with missing_packages is that you don't appear to have made it a vector of values which align with your i-th time periods. When you've better defined your situation I can suggest further vectorized code. I will point out that your current setup is naive, since adding zero doesn't change a value :-) . To point:

if ( foo > 0 )  bar <- bar + 5*foo

is a waste of effort. bar<-bar + 5*foo does exactly the same thing whether foo is zero or not.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • My problem is the order of calculations. I guess, the vectorization approach you suggest would probably lead to: stockHistory <- stockHistory -d +o.in openOrders = openOrders - o.in missing_packages = ceiling((reorderPoint - stockHistory[i] - openOrders)/q) openOrders = openOrders + missing_packages * q o.in[leadtimes] = o.in[leadtimes] + missing_packages * q # does this approach work with the indices? Now, my problem is, that the sequence of operations is flawed, since I would need the o.in before I calculate it. Do you see, what I mean? – mondano Jan 30 '15 at 14:10
  • Sadly, not in the least, because you're still claiming `openOrders` is a scalar, which means you can calculate all i-th instances vectorially. – Carl Witthoft Jan 30 '15 at 19:41