'' @(#) law104.sim 2/25/86
''Inventory system model
''An Introduction to Simulation Using SIMSCRIPT II.5, Chapter 4
PREAMBLE

   PROCESSES INCLUDE DEMAND.GENERATOR AND INVENTORY.REVIEW

      EVERY ORDER.ARRIVAL HAS AN OA.ORDER.SIZE
      DEFINE OA.ORDER.SIZE AS AN INTEGER VARIABLE

   PRIORITY ORDER IS ORDER.ARRIVAL, DEMAND.GENERATOR, AND
      INVENTORY.REVIEW    

   TEMPORARY ENTITIES

      EVERY ITEM HAS AN IT.SPOIL.TIME AND BELONGS TO THE
         STORED.ITEMS.SET
      DEFINE IT.SPOIL.TIME AS A REAL VARIABLE

   DEFINE STORED.ITEMS.SET AS A FIFO SET
   THE SYSTEM OWNS THE STORED.ITEMS.SET

   DEFINE H, INC.COST.PER.ITEM, MAX.DELIVERY.LAG, MAX.SHELF.LIFE,
      MEAN.INTERDEMAND.TIME, MIN.DELIVERY.LAG, MIN.SHELF.LIFE,
      ORDERING.COST, PI, AND SETUP.COST AS REAL VARIABLES

   DEFINE BIG.S, INITIAL.INVENTORY, LITTLE.S, NUM.BACKLOGGED,
      NUM.MONTHS, NUM.ON.ORDER, NUM.POLICIES, SAVESEED1, AND
      SAVESEED2 AS INTEGER VARIABLES

   DEFINE .MONTHS TO MEAN UNITS

   THE SYSTEM HAS A DEMAND.SIZE RANDOM STEP VARIABLE
   DEFINE DEMAND.SIZE AS AN INTEGER, STREAM 2 VARIABLE

   TALLY MEAN.ORDERING.COST AS THE MEAN OF ORDERING.COST

   ACCUMULATE AVG.NUMBER.IN.STORAGE AS THE AVERAGE OF N.STORED.ITEMS.SET

   ACCUMULATE AVG.NUMBER.IN.BACKLOG AS THE AVERAGE OF NUM.BACKLOGGED

END ''PREAMBLE
MAIN    use 1 for input

   DEFINE I AS AN INTEGER VARIABLE

   write as  /, "Test LAW104 - Inventory system model", /
   CALL READ.DATA

   FOR I = 1 TO NUM.POLICIES
   DO
      CALL INITIALIZE
      START SIMULATION
   LOOP
   write as  /, /, "End of Test LAW104", /

END ''MAIN
ROUTINE READ.DATA
 
   LET SAVESEED1 = SEED.V(1)
   LET SAVESEED2 = SEED.V(2)

   READ DEMAND.SIZE, MEAN.INTERDEMAND.TIME, SETUP.COST, 
      INC.COST.PER.ITEM, H, PI, MIN.DELIVERY.LAG,
      MAX.DELIVERY.LAG, MIN.SHELF.LIFE, MAX.SHELF.LIFE,
      INITIAL.INVENTORY, NUM.POLICIES, AND NUM.MONTHS
 
   PRINT 6 LINES THUS



INVENTORY MODEL -- INPUT PARAMETERS
-----------------------------------

   SKIP 3 LINES

   PRINT 19 LINES WITH NUM.MONTHS, INITIAL.INVENTORY, SETUP.COST,
      INC.COST.PER.ITEM, H, PI, MEAN.INTERDEMAND.TIME,
      MIN.DELIVERY.LAG, MAX.DELIVERY.LAG, MIN.SHELF.LIFE, AND
      MAX.SHELF.LIFE THUS

LENGTH OF SIMULATION      **** MONTHS

INITIAL INVENTORY LEVEL   **** ITEMS  

SETUP COST                 ***.**   

INCREMENTAL COST           ***.**

H                          ***.**

PI                         ***.**

INTERDEMAND TIMES ARE EXPONENTIAL WITH MEAN ***.* MONTHS

DELIVERY LAGS ARE UNIFORM ON THE INTERVAL (***.*,***.*) MONTHS

SHELF LIVES ARE UNIFORM ON THE INTERVAL   (***.*,***.*) MONTHS

   SKIP 3 LINES

   PRINT 9 LINES THUS

DISTRIBUTION FUNCTION OF DEMAND SIZES:
--------------------------------------



VALUE     CUMULATIVE PROBABILITY
-----     ----------------------

   FOR EACH RANDOM.E IN DEMAND.SIZE
      PRINT 3 LINES WITH IVALUE.A(RANDOM.E) AND PROB.A(RANDOM.E) THUS

   **                    *.*****

   START NEW PAGE

   PRINT 6 LINES THUS



SIMULATION RESULTS (ALL COSTS ARE IN DOLLARS)
---------------------------------------------

   SKIP 3 LINES
   
   PRINT 5 LINES THUS

             AVERAGE        AVERAGE        AVERAGE         AVERAGE
 POLICY     TOTAL COST   ORDERING COST   HOLDING COST   SHORTAGE COST
---------   ----------   -------------   ------------   -------------

END ''READ.DATA
ROUTINE INITIALIZE

   LET TIME.V = 0
   LET NUM.BACKLOGGED = 0
   LET NUM.ON.ORDER = INITIAL.INVENTORY

   LET SEED.V(1) = SAVESEED1
   LET SEED.V(2) = SAVESEED2
  
   RESET THE TOTALS OF N.STORED.ITEMS.SET, NUM.BACKLOGGED, 
      AND ORDERING.COST 

   WHILE STORED.ITEMS.SET IS NOT EMPTY
   DO
      REMOVE THE FIRST ITEM FROM THE STORED.ITEMS.SET
      DESTROY THIS ITEM
   LOOP
 
   READ LITTLE.S AND BIG.S

   ACTIVATE AN ORDER.ARRIVAL GIVING INITIAL.INVENTORY NOW
   ACTIVATE A DEMAND.GENERATOR NOW
   ACTIVATE AN INVENTORY.REVIEW NOW

END ''INITIALIZE
PROCESS ORDER.ARRIVAL GIVEN ORDER.SIZE

   DEFINE I AND ORDER.SIZE AS INTEGER VARIABLES

   FOR I = 1 TO ORDER.SIZE
   DO

      IF NUM.BACKLOGGED = 0

         CREATE AN ITEM
         LET IT.SPOIL.TIME(ITEM) = TIME.V +
            UNIFORM.F(MIN.SHELF.LIFE,MAX.SHELF.LIFE,4)
         FILE THIS ITEM IN THE STORED.ITEMS.SET

      ELSE

         SUBTRACT 1 FROM NUM.BACKLOGGED

      ALWAYS

   LOOP

   SUBTRACT ORDER.SIZE FROM NUM.ON.ORDER

END ''ORDER.ARRIVAL
PROCESS DEMAND.GENERATOR
 
   DEFINE CURRENT.DEMAND, FOUND, AND I AS INTEGER VARIABLES
 
   WHILE TIME.V < NUM.MONTHS
   DO
      WAIT EXPONENTIAL.F(MEAN.INTERDEMAND.TIME,1) .MONTHS

      LET CURRENT.DEMAND = DEMAND.SIZE
      FOR I = 1 TO CURRENT.DEMAND
      DO
         LET FOUND = 0

         UNTIL (FOUND = 1) OR (STORED.ITEMS.SET IS EMPTY)
         DO
            REMOVE THE FIRST ITEM FROM THE STORED.ITEMS.SET
            IF IT.SPOIL.TIME(ITEM) > TIME.V
               LET FOUND = 1
            ALWAYS
            DESTROY THIS ITEM
         LOOP

         IF FOUND = 0
            ADD 1 TO NUM.BACKLOGGED
         ALWAYS
      LOOP
   LOOP

END ''DEMAND.GENERATOR
PROCESS INVENTORY.REVIEW
  
   DEFINE INVENTORY.LEVEL AND ORDER.SIZE AS INTEGER VARIABLES

   WHILE TIME.V < NUM.MONTHS
   DO
      LET INVENTORY.LEVEL = N.STORED.ITEMS.SET - NUM.BACKLOGGED +
         NUM.ON.ORDER 

      IF INVENTORY.LEVEL < LITTLE.S

         LET ORDER.SIZE = BIG.S - INVENTORY.LEVEL
         ADD ORDER.SIZE TO NUM.ON.ORDER
         LET ORDERING.COST = SETUP.COST +
            (INC.COST.PER.ITEM * ORDER.SIZE) 
         ACTIVATE AN ORDER.ARRIVAL GIVING ORDER.SIZE IN 
            UNIFORM.F(MIN.DELIVERY.LAG,MAX.DELIVERY.LAG,3) .MONTHS

      ELSE

         LET ORDERING.COST = 0

      ALWAYS

      WAIT 1 .MONTHS
   LOOP
 
   CALL REPORT

END ''INVENTORY.REVIEW
ROUTINE REPORT

   DEFINE AVG.HOLDING.COST, AVG.SHORTAGE.COST, AND AVG.TOTAL.COST 
      AS REAL VARIABLES

   LET AVG.HOLDING.COST = H * AVG.NUMBER.IN.STORAGE 
   LET AVG.SHORTAGE.COST = PI * AVG.NUMBER.IN.BACKLOG 
   LET AVG.TOTAL.COST = MEAN.ORDERING.COST + AVG.HOLDING.COST +
       AVG.SHORTAGE.COST 
       
   PRINT 3 LINES WITH LITTLE.S, BIG.S, AVG.TOTAL.COST,
      MEAN.ORDERING.COST, AVG.HOLDING.COST, AND AVG.SHORTAGE.COST THUS

(***,***)     *****.**        *****.**       *****.**        *****.** 

END ''REPORT
