[R] Discrete Event Simulation problem

jism7690 james.jism.carey at gmail.com
Mon Feb 13 12:31:32 CET 2012


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.



More information about the R-help mailing list