2
$\begingroup$

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())

d1 = read.csv("C:/Users/Data.csv", header = T)
d2 = read.csv("C:/Users/Lead.csv", header = T)

#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
  Lead = 0
  OrderNumber = 0
  CummInventory = 0
 
  InventoryProfile <- c()
  
  for(i in 1:length(D)){
    if((Order == TRUE) & (Lead > 0)){
      Lead = Lead - 1
    }
    
    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.

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – prubin
    Aug 5, 2022 at 15:59
  • $\begingroup$ I made some edits. Please help me to get it workout. Thanks $\endgroup$
    – Roger
    Aug 5, 2022 at 20:46
  • 2
    $\begingroup$ (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? $\endgroup$
    – prubin
    Aug 5, 2022 at 22:18
  • $\begingroup$ @Roger, why not try to calculate $q^*$ and reorder-point directly by the given data instead of using an optimization problem? $\endgroup$
    – A.Omidi
    Dec 9, 2022 at 17:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.