Skip to content

Discrete Event Simulation problem

2 messages · jism7690

#
Hi All,

I am attempting to simulation an inventory model on R and I am having some
problems. I believe the problem is when I define my demand rate is stays
constant throughout so when I do need to reorder the model does not
recognise it as I have the initial supply arrival time set to infinity at
the start as no order has been played but I have an if statement saying, if
the level falls below a certain point then an order should be placed, I have
the expected time to order arrival set to be before the the sale but this is
not working.

Any Help would be appreciated. Thanks

maxStock = 100
minStock = 20
t.max=1100.0

LAST = t.max
START = 0

GetDemand<-function() START + runif(1,min=0,max=2)

main <- function(t.max,maxStock,minStock)
{
	index = 0
  t.current = START                               #### Starting Conditions
	t.demand = START
	t.supply = START
	inventory = 50
	order_costs = 0
	storage_costs = 0
	sales = 0
	sum = list(inventory = 50,order_costs = 0,storage_costs = 0,sales =0)

	while(index < LAST){
	index = index + 1
	t.demand = GetDemand()  ### expected time to next sale
	t.supply = Inf		### expected time to arrival of order, Infinity as order
has not been placed
	t.next =min(t.demand,t.supply)    ###next event either sale or supply is
the one with imminent arrival
	k = maxStock - inventory

  if(inventory > minStock)
  {
    	t.supply = Inf		### expected time to arrival of order, Infinity as
order has not been placed
  }
  if(inventory > 0)
      storage_costs = (t.next-t.current)*0.10*inventory
  t.current = t.next
  
	 if (inventory < minStock)
   {                       					###Need to Order
	 k = maxStock - inventory
	 order_costs = 50 + 0.02*k
	 sum$order_costs = sum$order_costs + order_costs
	 t.supply =  t.current + 1.0
    }

	if(t.current ==t.demand)
  {
  sum$inventory = sum$inventory - 1.0				                           
####Sale made
	sum$sales = sum$sales + 1.0
	t.demand = runif(1,min=0,max=2)
	}
	
	if(t.current == t.supply)
  {							                        ####Order Arrives
	sum$inventory = sum$inventory + k
	k = 0
	t.supply = Inf
	}
	
  if(inventory < maxStock)
  {
  k = maxStock - inventory
  sum$storage_costs = sum$storage_costs + storage_costs
  sum$order_costs = sum$order_costs + order_costs
  sum$inventory = sum$invneotry + k - sum$sales
  sum$sales = sum$sales + sales
  sum$sales = sum$sales + sales
  }

}
	sis = list(Time = index,StorageCosts =sum$storage_costs,OrderCosts=
sum$order_costs, FinalInventoryLevel=sum$inventory,sales=sum$sales ,
AverageCosts =((sum$order_costs + sum$storage_costs)/index))
	return(sis)
	}
main()

--
View this message in context: http://r.789695.n4.nabble.com/Discrete-Event-Simulation-problem-tp4377276p4377276.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
I have made some chances and I believe now the only problem  is making the
system reorder. Please any help would be great


test <- function(seed = 123456789, maxStock= 100, minStock =
20,t.max=1100,inventory =50)
{

LAST = t.max
START = 0
t.demand = START
t.supply = START
t.current = START

GetDemand<-function()
{
t.demand <<- t.demand + runif(1,min=0,max=5)
return(t.demand)
}
GetSupply <-function(){
if (inventory < minStock)
{
t.supply <<- t.supply + 1.0
}
else
t.supply <<- Inf
return(t.supply)
 }
 
main <- function(seed)
{
  if(seed > 0)
    set.seed(seed)
	index = 0
      t.current = START                              #### Starting
Conditions
	t.demand = START
	t.supply = START
	minStock = 20
	maxStock = 100
	inventory = 50
	order_costs = 0
	storage_costs = 0
	sum = list(inventory = 50,order_costs = 0,storage_costs = 0)
	
	
	while(index < LAST){
	index = index + 1
	t.demand = GetDemand()  ### expected time to next sale
	t.supply = GetSupply()		### expected time to arrival of order, Infinity as
order has not been placed
	t.next =min(t.demand,t.supply)    ###next event either sale or supply is
the one with imminent arrival
	k = maxStock - inventory
	t.current = t.next -min(t.demand,t.max)
  if(inventory > 0) {
      storage_costs = (t.next-t.current)*0.10*inventory
      }

	 if (inventory < minStock)
   {                       					    ###Need to Order
	 k = maxStock - inventory
	 order_costs = 50 + 0.02*k
	 sum$order_costs = sum$order_costs + order_costs
	 t.supply =  GetSupply()
    }
	if(t.next ==t.demand)
  {	
	inventory <<- inventory - 1				                            ####Sale made
	sum$inventory = sum$inventory - 1.0
	t.demand = GetDemand()
	}

	if(t.next == t.supply)
  {							                        ####Order Arrives
	sum$inventory = sum$inventory + k
	k = 0
	t.supply = GetSupply()
	}

  if(inventory < maxStock)
  {
  k = maxStock - inventory
  sum$storage_costs = sum$storage_costs + storage_costs
  sum$order_costs = sum$order_costs + order_costs
  }

}
  options(digits = 5)
	sis = list(Time = index,StorageCosts =sum$storage_costs,OrderCosts=
sum$order_costs,AverageCosts =((sum$order_costs +
sum$storage_costs)/index),Inventory = sum$inventory)
	return(sis)
	}
return(main(seed))
}


--
View this message in context: http://r.789695.n4.nabble.com/Discrete-Event-Simulation-problem-tp4377276p4383464.html
Sent from the R help mailing list archive at Nabble.com.