# Supply chain simulation is not working. Any help please?

I am trying to simulate a supply chain scenario. Demand is random (e.g. between 100-400 units) and lead time vary 2 to 4 days for each supply. The holding cost(h), penalty cost (p) for lost_sales, fixed costs (f) for each order, and per unit purchase cost (z) are given. My goal is to minimize the cost and fulfill the service/demand (like.. order a quantity S when dropped to R) for each period (say 365 days is time horizon).

In the code, I am computing the "cost" before I find the order quantity and I was unable to fix it. Additionally, I think I am overlooking some conditions that I believe ought to be there. Any help you can offer to make it better would be greatly appreciated. Please be advised that I am an undergrad level and taking a course on it. Many thanks in advance.

library(ks)
library(rgl)
library(xtable)
rm(list=ls())

#Expected cost for
ExpectedCost = function(S,R,D,L){
h = 10.01
f = 120
z = 60
p = 75
cost = 0
lost_sales = 0
Inventory = S
Order = FALSE
OrderNumber = 0
CummInventory = 0

InventoryProfile <- c()

for(i in 1:length(D)){
if((Order == TRUE) & (Lead > 0)){
}

if((Order == TRUE) & (Lead == 0)){
Inventory = Inventory + S
Order = FALSE
}

if(Inventory >= D[i]){
Inventory = Inventory - D[i]
}
else if((Inventory >= 0) & (Inventory < D[i])){
Inventory = Inventory - Inventory
lost_sales = lost_sales + (D[i] - Inventory)
}
else{
Inventory = 0
}

if((Inventory <= R) & (Order == FALSE)){
Order = TRUE
Lead = sample(L, size = 1, replace = TRUE)
OrderNumber = OrderNumber + 1
}

CummInventory = CummInventory + Inventory
InventoryProfile <- c(InventoryProfile, Inventory)
InventoryProfile
}
cost = CummInventory*h + f*OrderNumber + z*S + p*lost_sales
Service = (sum(D) - lost_sales)/sum(D)

return(list(COST=cost, SERVICE = Service, INVENTORY = InventoryProfile))
}

#Create vectors for s and S
S = seq(500, 12000, 300)
R = seq(100, 6500, 100)

Cost = matrix(nrow=length(S), ncol=length(R))
Service = matrix(nrow=length(S), ncol=length(R))

#Bootstrapping
for(i in 1:length(S)){
for(j in 1:length(R)){
Output = ExpectedCost(S[i], R[j], d1$$Demand, d2$$Lead)
Cost[i,j] = Output$$COST Service[i,j] = Output$$SERVICE
}
}

Index1 = which(Service == 1, arr.ind = T)
Index2 = which(Cost == min(Cost[Index1]), arr.ind = T)

#Optimal (S,R) policy
S[Index2[1,1]]
R[Index2[1,2]]


N:B: I am just trying to implement a similar code to serve my purpose.

• Welcome to OR SE. I'm afraid that "I did messed" is not very meaningful. Can you edit the question to make it clearer what is wrong with the specified R code?
– prubin
Aug 5 at 15:59