# 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. Please be advised that I am an undergrad level and taking a course on it.

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, 2022 at 15:59
• I made some edits. Please help me to get it workout. Thanks Aug 5, 2022 at 20:46
• (1) Is the R code yours or someone else's? If not yours, what is the source (and relevance) of it? (2) Do demand and lead time follow known distributions (e.g., normal and Poisson) with known parameters, or are you estimating them from observed data? (3) You mentioned both simulation and optimization. Are you trying to build a discrete event simulation, or are you trying to solve an optimization problem (cost minimization?) using either known distributions or known demand/lead time data?
– prubin
Aug 5, 2022 at 22:18
• @Roger, why not try to calculate $q^*$ and reorder-point directly by the given data instead of using an optimization problem? Dec 9, 2022 at 17:43