Lindo Systems

! Infinite period newsvendor problem with lost sales,
    setup cost, and lead time = 1.!(Lost_sales1.lng).
  Solved by formulating as a Markov decision problem;
 ! Keywords: Markov decision process, inventory, lost sales;
SETS: 
 INVL;   ! Possible inventory levels;
 ORDER;  ! Possible order amounts;
 DD: PD; ! Possible demands & probability;
   ! Our decision transitions: Given a current inventory level, the
      next state is the inventory level & order level;
 DTRAN(INVL, ORDER): CD, X;
   ! Probabilistic transitions: given I0 and R0, the demand D gives
     the new inventory level I1 = max(0,I0-D0) + R, so
                           R <= I1 <= I0 + R;
 PTRAN(INVL,ORDER, INVL)|  &2 #LE# &3 #AND# &3 #LE# &1 + &2 - 1 : CP, PR;

! Notation:
  X(i,r) = joint probability of entering period
            in with inventory i available to satisfy today's demand
            and deciding to order an amount r to be delivered tomorrow.
   PR(i,r,s)= probability of having an amount s available for tomorrow,
            given there is i available today, and amount r on order
            for tomorrow.
   CD(i,r)= cost of making decision t when state is s,
   CP(i,r,s)= expected cost when nature moves state to u given state i,r
   PD(d) = probability demand is d-1, for d = 1, 2, ...;
ENDSETS
DATA:
  V = 18; ! Revenue per unit sold;
  P = 2;  ! Explicit shortage penalty on each lost sale;
  H = .7;  ! Holding cost/unit per period;
  C = 3;  ! Purchase cost per unit;
  K = 1; ! Fixed order cost;
! Demand distribution. Lowest corresponds to 0 demand;
  DD =  D0   D1   D2   D3   D4;
  PD = .01  .90  .04  .03  .02;
 ORDER = R0..R5;
 INVL = I0..I7; ! Possible inventory levels. Lowest
                    corresponds to 0 inventory;
ENDDATA
 
!--------------------------------------------------;
! The sequence of events each day is:
  1) Begin period with an amount i, including order about to
     arrive, available to satisfy today's demand,
  2) We order an amount r for delivery tomorrow,
  3) Order scheduled for today arrives,
  3) Demand occurs and revenue is realized,
  4) Shortage penalties or holding charges are assessed;
CALC:
! Compute costs of our decisions or deterministic transitions, 
    note order size is r-1;
  @FOR(DTRAN(i,r): CD(i,r) = K*(r#GT#1) + C*(r-1));

! Compute conditional probabilities and 
   expected costs of nature's moves, or Probabilistic transitions,
   with i = current inventory level, r = order size, s = new
   inventory level available tomorrow. Note, indexing starts at 1, 
   so index 1 corresponds to inventory or demand = 0;
  ! No stockout today case;
  ND = @SIZE(DD);
  @FOR(PTRAN(i,r,s) | r #LT# s:
    PR(i,r,s) = @IF(i+r-s #LE# ND,PD(i+r-s),0);
    CP(i,r,s) = H *(s-r) - V*(i+r-s-1);
      );
! Out of stock today case;
  @FOR(PTRAN(i,r,s) |  r #eq# s: 
    PR(i,r,s) =   @SUM(DD(u)| u #ge# i: PD(u));
    @IFC( PR(i,r,s) #GT# 0:
       CP(i,r,s) = P*@SUM(DD(u)| u #ge# i: PD(u)*(u-i))/PR(i,r,s) - V*(i-1);
      @ELSE
       CP(i,r,s) = 0;
        );
      );
ENDCALC

!Minimize the average cost per period;
MIN=@SUM(DTRAN( i,r): CD(i,r)*X(i,r))
  + @SUM(PTRAN(i,r,s): CP(i,r,s)*X(i,r)*PR(i,r,s));
!The probabilities must sum to 1;
  @SUM( DTRAN(i,r): X( i,r)) = 1;
!Rate at which we exit state i = rate of entry to i;
  @FOR( INVL(i):
    @SUM( DTRAN(i,r): X(i,r))= 
    @SUM( PTRAN(i0,r0,s1)|s1 #EQ# i: X(i0,r0)* PR(i0,r0,i))
            );