SCM

Forum: help

Monitor Forum | Start New Thread Start New Thread
Need help with nested for loop [ Reply ]
By: Prasenjit Datta on 2021-02-16 19:22
[forum:48761]
Hello Guys,

I need to modify a R code to do a simulation for 1000 times and return some metrics in data frame or vector form. I have put the code below. At the end of the loop, I want the data frame of average_inventory and service_level


base_stock <- 50
Demand <- abs(round(rnorm(45,Mean_demand,std_Dev)))
Ship <- abs(round(rnorm(45,Mean_demand,std_Dev)))
simulated_Stock <- list(Period = numeric(),
Cust_Order = numeric(),
Cust_ship = numeric(),
Sale = numeric(),
Backorder = numeric(),
Start_Inventory_Level = numeric(),
End_Inventory_Level = numeric(),
Order_given = numeric(),
Order_received = numeric(),
Order_incoming = numeric(),
Net_incoming_order = numeric(),
Available_Inv_Sale = numeric())


for(i in 1:45){
simulated_Stock$Period[i] <- Receive_data$Sim_week[i]
simulated_Stock$Cust_Order[i] <- Demand[i]
simulated_Stock$Cust_ship[i] <- Ship[i]
simulated_Stock$Order_given[i] <- simulated_Stock$Cust_Order[i]
simulated_Stock$Order_incoming[i] <- Receive_data$Qty[i]
if(i < 14){
simulated_Stock$Order_received[i] <- 0
}else{
simulated_Stock$Order_received[i] <- simulated_Stock$Cust_Order[i-13]
}
simulated_Stock$Net_incoming_order[i] <- simulated_Stock$Order_incoming[i] + simulated_Stock$Order_received[i]
if(i == 1){
simulated_Stock$Start_Inventory_Level[i] <- base_stock
simulated_Stock$Available_Inv_Sale[i] <- simulated_Stock$Start_Inventory_Level[i] + simulated_Stock$Net_incoming_order[i]
simulated_Stock$Sale[i] <- min(simulated_Stock$Cust_ship[i],simulated_Stock$Available_Inv_Sale[i])
simulated_Stock$Backorder[i] <- simulated_Stock$Cust_ship[i] - simulated_Stock$Sale[i]
simulated_Stock$End_Inventory_Level[i] <- simulated_Stock$Available_Inv_Sale[i]-simulated_Stock$Sale[i]
}else{
simulated_Stock$Start_Inventory_Level[i] <- simulated_Stock$End_Inventory_Level[i-1]
simulated_Stock$Available_Inv_Sale[i] <- simulated_Stock$Start_Inventory_Level[i] + simulated_Stock$Net_incoming_order[i]
simulated_Stock$Sale[i] <- min(simulated_Stock$Cust_ship[i],simulated_Stock$Available_Inv_Sale[i])
simulated_Stock$Backorder[i] <- simulated_Stock$Cust_ship[i] - simulated_Stock$Sale[i]
simulated_Stock$End_Inventory_Level[i] <- simulated_Stock$Available_Inv_Sale[i]-simulated_Stock$Sale[i]
}

}

sim_stock <- data.frame(simulated_Stock$Period,simulated_Stock$Cust_Order,
simulated_Stock$Cust_ship,simulated_Stock$Sale,
simulated_Stock$Backorder,simulated_Stock$Start_Inventory_Level,
simulated_Stock$End_Inventory_Level,simulated_Stock$Order_given,
simulated_Stock$Order_received,simulated_Stock$Order_incoming,
simulated_Stock$Net_incoming_order,simulated_Stock$Available_Inv_Sale)

colnames(sim_stock) <- c("Period","Cust_Order","Cust_Ship","Sale",
"Backorder","Start_Inventory","End_Inventory",
"Order_Given","Order_Received","Order_Incoming",
"Net_Order_Incoming","Available_Inventory")
average_inventory <- mean(sim_stock$End_Inventory)
Number_BO <- sim_stock %>%
filter(Backorder >0) %>%
nrow()
service_level <- round(1 - (Number_BO/nrow(sim_stock)),2)

Thanks to:
Vienna University of Economics and Business Powered By FusionForge