5.9 An algorithm for the \((s, S)\) and \((T, S)\) policies
5.9 An algorithm for the \((s, S)\) and \((T, S)\) policies
We develop an algorithm by which we can compute the long-run average cost for a single-item inventory system under periodic review and controlled by the \((s, S)\) or the \((T, S)\) policy. The algorithm combines three ideas: hitting (or stopping) problems, the renewal-reward theorem, and dynamic programming. These ideas are not specific to inventory control: in Section 5.6 we applied them to queueing control, more generally, the shortest-path algorithms of operations research rest on the same type of recursion. In that sense, here we deal with a stochastic shortest-path problem: the state moves randomly, each period adds a cost, and the path ends when the state enters a stopping set.1See Bertsekas (2005) for the theory of stochastic shortest-path problems.
We start with the \((T, S)\) policy, then move on to the \((s, S)\) policy, and then extend to a combination of the other two, the \((T, s, S)\) policy, to be introduced presently.
Recall that under the \((T, S)\) policy, we place a replenishment order every \(T\) periods such that the inventory position \(P\) equals \(S\) after the replenishment. When the position is \(P_k=j,\) we charge the expected inventory cost \(L(j)\) of (5.7.5).
Let us write \(V(n, j)\) for the expected cost until reordering if we start at position \(P=j\) and there are \(n,\) \(0\leq n \leq T,\) periods remaining. By reasoning analogously as in Section 5.6, \(V(n, j)\) satisfies the recursion
\begin{align*} V(n, j) & = \begin{cases} L(j) + \E{V(n-1, j-D)}, & \quad n > 0, \\ K, & \quad n = 0. \end{cases} \end{align*}To see this, observe that when \(P=j\) and \(n>0,\) we first pay \(L(j)\) for keeping inventory during one period, then a demand \(D\) arrives, and for the next period we have one less period to go until ordering. When \(n=0,\) we pay the order cost \(K\) to replenish to \(S.\) Note that this recursion always terminates because \(V(n, \cdot)\) is expressed in terms of \(V(n-1, \cdot),\) so always one period less to go.
Since a (renewal) cycle starts at level \(S,\) the expected cycle cost is \(V(T, S)\) and the expected cycle duration is \(T\) periods. The renewal-reward theorem implies that the long-run average cost is given by \(C(T,S) = V(T, S)/T.\)
With the following terms from dynamic programming, we can describe all three policies, \((T,S),\) \((s,S)\) and \((T,s,S),\) in the same way. For the above recursion, \(L(j)\) is called the running cost, \(K\) the hitting cost, \(\tau\) the hitting time, here \(\tau = T,\) and \(\set{n : n=0}\) the hitting set.2We use the adjectives hitting and stopping interchangeably.
With this terminology we can easily structure the analysis of the \((s,S)\) policy. In view of (2.5.3), when the inventory position \(P\) enters (or hits) the stopping set \(\set{i : i \leq s}\) we place an order and pay a stopping cost \(K.\) When \(P>s,\) we pay the running cost \(L(j)\) and continue for another period. Hence, the expected cost until stopping satisfies the recursion
\begin{align*} V(s, j) = \begin{cases} L(j) + \E{V(s, j-D)}, & \quad j > s, \\ K, & \quad j \leq s. \end{cases} \end{align*}For the hitting time we find
\begin{align*} \tau(s, j) = \begin{cases} 1 + \E{\tau(s, j-D)}, & \quad j > s, \\ 0, & \quad j \leq s, \end{cases} \end{align*}because the running cost is \(1\) if we continue for another period, and when the process hits the stopping set, the time stops counting, so the stopping cost is \(0.\) Note that this recursion is structurally identical to that of \(V.\)
To find an algorithm, we expand the recursion for \(V\) at \(j>s\) as
\begin{align*} V(s,j) & =L(j)+\E{V(s,j-D)} =L(j)+\sum_{k=0}^{\infty}f_kV(s,j-k) \\ & =L(j)+f_0V(s,j)+f_1V(s,j-1)+\cdots. \end{align*}Clearly, \(V(s, j)\) appears at the LHS and RHS, so \(V(s, j)\) is expressed in terms of itself, that is, circular.3This is different from the recursion for the \((T,S)\) policy. The way out is to isolate \(V(s, j)\) by rewriting this as
\begin{align*} (1-f_{0}) V(s, j) = L(j)+f_{1} V(s, j-1) + f_{2} V(s, j-2) + \cdots. \tag{5.9.1} \end{align*}Thus, assuming that4This is not restrictive, because \(\P{D=0} = 1\) implies that there is no demand, in which case there is no need to keep inventory. \(f_0 < 1,\)
\begin{align*} V(s, j) = (1-f_{0})^{-1}\rb{L(j)+\sum_{k=1}^{\infty} f_{k} V(s,j-k)}. \tag{5.9.2} \end{align*}Let \(D^+\) be the demand conditioned on \(D>0,\) so that \(\P{D^+=k}=f_k/(1-f_0)\) for \(k\geq 1.\) Then (5.9.2) becomes
\begin{equation*} V(s,j) = \frac{L(j)}{1-f_0} + \E{V(s, j-D^+)}. \tag{5.9.3} \end{equation*}The position stays at \(j\) for a geometric number of periods with mean \(1/(1-f_0),\) each of which costs \(L(j),\) and then jumps down by \(D^+\geq 1.\) As \(j\) decreases by at least one in each step, the recursion terminates. We can now use one algorithm to compute \(V\) and \(\tau\) by adjusting the running and stopping cost accordingly.
Finally, when the expected cycle cost \(V(s, S)\) and expected hitting time \(\tau(s,S)\) have been computed, the long-run average cost is \(C(s,S) = V(s,S)/\tau(s,S).\)
By combining the two policies above we obtain the \((T,s,S)\) policy. Under this policy we order up to \(S\) as soon as the inventory position is at or below \(s\) or \(T\) periods have passed since the last order, whichever comes first. The recursion for \(V(n, s, j)\) specifies the behavior:
\begin{align*} V(n, s, j) = \begin{cases} L(j) + \E{V(n-1, s, j-D)}, & \quad j > s \text{ and } n>0, \\ K, & \quad j \leq s \text{ or } n = 0. \end{cases} \tag{5.9.4} \end{align*}The structure of the recursion for \(\tau(n, s, j)\) is the same, but with running cost \(1\) and stopping cost \(0.\) The long-run average cost is \(C(T,s,S) = V(T,s,S)/\tau(T,s,S).\)
To see how the \((s,S)\) and \((T,s,S)\) policy relate, realize that when \(T\) becomes very large, the set \(\set{i : i \leq s}\) will be hit with overwhelming probability long before the time-to-go has become \(0.\) Therefore, the average cost under the \((s,S)\) policy must be \(C(s,S) = \lim_{n\to\infty} V(n, s, S)/\tau(n, s, S).\) By similar reasoning, \(C(T,S) = \lim_{s\to-\infty} C(T, s, S).\)
It is interesting to understand why we can obtain the closed-form expression \(\tau_-(q) = q\E{S}/(1-\rho)\) for the drain time in the \(N\)-policy in (5.6.1), while for the \((s,S)\) and \((T,s,S)\) policies we need an algorithm. The queue length of the \(M/G/1\) queue is skip-free to the left: during one service it can jump up by many jobs, but it goes down one job at a time. Hence, to empty a queue with \(q\) jobs, the queue length must pass through \(q-1, q-2, \ldots, 1,\) and each step down takes one busy period, so that \(\tau_-(q) = q\E\U\) is linear in \(q.\) The inventory position, in contrast, can jump down by more than one unit when \(\P{D\geq 2} > 0,\) so it can skip levels and enter the set \(\set{i : i \leq s}\) at different points; then \(\tau(s,j)\) does not split into a sum of identical one-step times. Thus, the behavior of \(V\) and \(\tau\) just above \(s\) is more complicated. Moreover, in the queueing situation, the queue length can never be negative, while in the inventory situation the level \(I\) can take negative values. As a consequence, the cost function \(L(j)\) is not linear,5But convex; see the remark after (5.7.5). hence the idea to substitute \(\tau_-(q) = \alpha q + \beta\) and solve for \(\alpha\) and \(\beta\) will not work in general.
Next, we note that deriving a recursive algorithm for the cost under the \((Q,r)\) policy seems not so attractive. To see why, observe that the stopping cost is given by \(V(r, j) = V(r, j + Q),\) that is, it is no longer constant such as \(K,\) but dependent on other values of \(V.\) Therefore, the simplifying trick that led to (5.9.1) cannot be applied here.
The recursions lead directly to algorithms. We first construct the running-cost function. For a position \(j,\) the inventory level \(\leadtime\) periods later has the same distribution as \(j-X,\) so \(L(j) = \E{c(j-X)}\) is the expectation of \(c\) of (2.5.4) with respect to \(j-X.\)
\begin{algorithm}
\caption{Construct the running-cost function for an inventory model.}
\begin{algorithmic}[1]
\State \textbf{Input:} demand rv \(D,\) lead time \(\leadtime,\) costs \(h,\, b.\)
\State \textbf{Output:} running-cost function \(L.\)
\Function{RunningCost}{\(D,\, \leadtime,\, h,\, b\)}
\State \(X\gets\pr{Convolution}\rb{D,\, \leadtime}\)
\Function{L}{\(j\)}
\Return \((j-X).\pr{E}\rb{\lambdafun{i}{h i^+ + b i^-}}\)
\EndFunction
\Return \(\pr{L}\)
\EndFunction
\end{algorithmic}
\end{algorithm}
Python code
# Run the Python blocks of Algorithmic Patterns and Probabilistic Arithmetic first.
def RunningCost(D, leadtime, h, b):
X = Convolution(D, leadtime)
def L(j):
return (j - X).E(lambda i: h * max(i, 0) + b * max(-i, 0))
return L
R code
# Run the R blocks of Algorithmic Patterns and Probabilistic Arithmetic first.
RunningCost <- function(D, leadtime, h, b) {
force(h); force(b) # Evaluate h and b now, not when L is first called.
X <- Convolution(D, leadtime)
L <- function(j) {
E(j - X, function(i) h * max(i, 0) + b * max(-i, 0))
}
L
}
The function \(\pr{TsSHittingValue}\) returns the function \(V\) for the \((T,s,S)\) policy; like \(\pr{Power}\) in Alg. 1.2.11, it returns a function, not a number. Its arguments are a running-cost function \(R\) and a hitting-cost function \(H.\) With \(R=L\) and \(H(j)=K,\) \(V\) is the expected cost until hitting; with \(R(j)=1\) and \(H(j)=0,\) it is the expected hitting time \(\tau.\) Hence one function computes both terms of \(C = V/\tau.\) It uses the Alg. 1.2.21 algorithm to speed up the recursion. In Exercise 6 you are asked to explain why lines 10 and 11 cannot be combined into \textbf{return} \(\pr{Cache}\rb{\pr{V}}.\)
\begin{algorithm}
\caption{Construct a hitting-value function for a \((T,s,S)\) policy.}
\begin{algorithmic}[1]
\State \textbf{Input:} demand rv \(D,\) running-cost function \(R,\) and hitting-cost function \(H.\)
\State \textbf{Output:} a hitting-value function \(V.\)
\Function{TsSHittingValue}{\(D,\, \pr{R},\, \pr{H}\)}
\Function{Stop?}{\(n,\, s,\, j\)}
\Return \(j\leq s\) or \(n==0\)
\EndFunction
\Function{V}{\(n,\, s,\, j\)}
\If{\(\pr{Stop?}\rb{n,\, s,\, j}\)}
\Return \(\pr{H}\rb{j}\)
\EndIf
\Return \(\pr{R}\rb{j}+D.\pr{E}\rb{\lambdafun{d}{\pr{V}\rb{n-1,\, s,\, j-d}}}\)
\EndFunction
\State \(\pr{V}\gets\pr{Cache}\rb{\pr{V}}\)
\Return \(\pr{V}\)
\EndFunction
\end{algorithmic}
\end{algorithm}
Python code
def TsSHittingValue(D, R, H):
def Stop(n, s, j):
return j <= s or n == 0
def V(n, s, j):
if Stop(n, s, j):
return H(j)
return R(j) + D.E(lambda d: V(n - 1, s, j - d))
V = Cache(V)
return V
R code
TsSHittingValue <- function(D, R, H) {
force(D); force(R); force(H)
Stop <- function(n, s, j) j <= s || n == 0
V <- function(n, s, j) {
if (Stop(n, s, j)) return(H(j))
R(j) + E(D, function(d) V(n - 1, s, j - d))
}
V <- Cache(V)
V
}
The function \(\pr{TsSAverageCost}\) provides the direct interface for a \((T,s,S)\) policy. It uses the hitting-value function once with the inventory costs and once with a running cost of one to obtain the expected cycle cost and cycle duration. With \(s=-\infty\) it gives the \((T,S)\) policy.
\begin{algorithm}
\caption{Compute the long-run average cost of a \((T,s,S)\) policy.}
\begin{algorithmic}[1]
\State \textbf{Input:} demand rv \(D,\) lead time \(\leadtime,\) costs \(h,\, b,\, K,\) levels \(T,\, s,\, S.\)
\State \textbf{Output:} long-run average cost \(C(T,s,S).\)
\Function{TsSAverageCost}{\(D,\, \leadtime,\, h,\, b,\, K,\, T,\, s,\, S\)}
\State \(\pr{L}\gets\pr{RunningCost}\rb{D,\, \leadtime,\, h,\, b}\)
\State \(\pr{V}\gets\pr{TsSHittingValue}\rb{D,\, \pr{L}, \lambdafun{j}{K}}\)
\State \(\tau\gets\pr{TsSHittingValue}\rb{D, \lambdafun{j}{1}, \lambdafun{j}{0}}\)
\Return \(\pr{V}\rb{T,\, s,\, S}/\tau\rb{T,\, s,\, S}\)
\EndFunction
\end{algorithmic}
\end{algorithm}
Python code
def TsSAverageCost(D, leadtime, h, b, K, T, s, S):
L = RunningCost(D, leadtime, h, b)
V = TsSHittingValue(D, L, lambda j: K)
tau = TsSHittingValue(D, lambda j: 1, lambda j: 0)
return V(T, s, S) / tau(T, s, S)
R code
TsSAverageCost <- function(D, leadtime, h, b, K, T, s, S) {
L <- RunningCost(D, leadtime, h, b)
V <- TsSHittingValue(D, L, function(j) K)
tau <- TsSHittingValue(D, function(j) 1, function(j) 0)
V(T, s, S) / tau(T, s, S)
}
For the \(s, S\) policy, the recursion does not reduce a time index. However, (5.9.3) has the same form as the recursion for \(V(n,s,j)\) of the \((T,s,S)\) policy, with \(n=\infty,\) \(D^+\) instead of \(D,\) and running cost \(L(j)/(1-f_0)\) instead of \(L(j).\) As \(D^+\geq 1,\) the recursion still terminates. Hence, \(\pr{sSAverageCost}\) only has to construct \(D^+;\) it can then use \(\pr{TsSHittingValue}\) with \(T=\infty.\) Thus, the \((T,S)\) policy is the \((T,s,S)\) policy with \(s=-\infty,\) and the \((s,S)\) policy is the \((T,s,S)\) policy with \(T=\infty\) and demand \(D^+.\) Note that \(\pr{RunningCost}\) still uses \(D,\) because \(L\) depends on the demand during the lead time, not on \(D^+.\)
\begin{algorithm}
\caption{Compute the long-run average cost of an \((s,S)\) policy.}
\begin{algorithmic}[1]
\State \textbf{Input:} demand rv \(D,\) lead time \(\leadtime,\) costs \(h,\, b,\, K,\) levels \(s,\, S.\)
\State \textbf{Output:} long-run average cost \(C(s,S).\)
\Function{sSAverageCost}{\(D,\, \leadtime,\, h,\, b,\, K,\, s,\, S\)}
\State \(\pr{L}\gets\pr{RunningCost}\rb{D,\, \leadtime,\, h,\, b}\)
\State \(D^+\gets D.\pr{Condition}\rb{\lambdafun{d}{d>0}}\)
\State \(\pr{V}\gets\pr{TsSHittingValue}\rb{D^+,\, \lambdafun{j}{\pr{L}\rb{j}/(1-D(0))},\, \lambdafun{j}{K}}\)
\State \(\tau\gets\pr{TsSHittingValue}\rb{D^+,\, \lambdafun{j}{1/(1-D(0))},\, \lambdafun{j}{0}}\)
\Return \(\pr{V}\rb{\infty,\, s,\, S}/\tau\rb{\infty,\, s,\, S}\)
\EndFunction
\end{algorithmic}
\end{algorithm}
Python code
from math import inf
def sSAverageCost(D, leadtime, h, b, K, s, S):
L = RunningCost(D, leadtime, h, b)
D_plus = D.condition(lambda d: d > 0)
V = TsSHittingValue(D_plus, lambda j: L(j) / (1 - D(0)), lambda j: K)
tau = TsSHittingValue(D_plus, lambda j: 1 / (1 - D(0)), lambda j: 0)
return V(inf, s, S) / tau(inf, s, S)
R code
sSAverageCost <- function(D, leadtime, h, b, K, s, S) {
L <- RunningCost(D, leadtime, h, b)
D_plus <- Condition(D, function(d) d > 0)
V <- TsSHittingValue(D_plus, function(j) L(j) / (1 - D(0)), function(j) K)
tau <- TsSHittingValue(D_plus, function(j) 1 / (1 - D(0)), function(j) 0)
V(Inf, s, S) / tau(Inf, s, S)
}
For the Lighthouse Company case specified in Alg. 2.5.4, take \(s=16\) and \(S=20.\) For the \((s,S)\) policy, the algorithms give the following cycle statistics and long-run average cost.
| \(\tau(s,S)\) | \(V(s,S)\) | \(C(s,S)\) |
|---|---|---|
| 2.2865088 | 72.04810304 | 31.51009217 |
For the \((T,s,S)\) policy, the average cost approaches that of the \((s,S)\) policy as \(T\) increases.
| \(T\) | \(C(T,s,S)\) |
|---|---|
| 1 | 60.3 |
| 2 | 38.59665072 |
| 10 | 31.51019163 |
For the \((T,S)\) policy, taking \(T\) too large is costly, as expected.
| \(T\) | \(C(T,S)\) |
|---|---|
| 1 | 60.3 |
| 2 | 34.54166667 |
| 10 | 35.83618568 |
| 20 | 193.77674739 |
TF 1.
Claim: For the \((s,S)\) policy, the expected cost until stopping \(V(s,j)\) satisfies the same recursion as the one for the \((T,S)\) policy.
Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
False. For the \((s,S)\) policy the recursion is circular in \(j\) and cannot be solved by backward induction in time, and therefore needs the extra factor \(1-f_0;\) this is explicitly contrasted with the \((T,S)\) policy.
TF 2.
Claim: The next formula is a correct implementation to compute the expected time to hit \(s\) under the \((s,S)\) policy:
\begin{align*} \begin{cases} (1-f_{0})^{-1}(1 + \E{\tau(s, j-D)}), & \quad j > s, \\ 0, & \quad j \leq s, \end{cases} \end{align*}Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
False. The expectation includes the term for \(D=0,\) which must be removed before dividing by \(1-f_0.\) Moreover, it is the time to hit the set \(\set{i : i \leq s},\) not the time to hit \(s.\)
TF 3.
Let \(D\) be the demand rv, \(D^+\) the demand conditioned on \(D>0,\) \(L\) the running-cost function, and \(K\) the ordering cost. Consider the calls
\begin{algorithm}
\begin{algorithmic}[1]
\State \(\pr{V}_{sS}\gets\pr{TsSHittingValue}\rb{D^+,\, \lambdafun{j}{\pr{L}\rb{j}/(1-D(0))},\, \lambdafun{j}{K}}\)
\State \(\pr{V}_{TS}\gets\pr{TsSHittingValue}\rb{D,\, \pr{L},\, \lambdafun{j}{K}}\)
\end{algorithmic}
\end{algorithm}
Claim: \(V_{sS}(\infty,s,S)\) is the expected cost until ordering under the \((s,S)\) policy, and \(V_{TS}(T,-\infty,S)\) is the expected cost until ordering under the \((T,S)\) policy.
Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
True. For the \((s,S)\) policy, \(T=\infty,\) and by (5.9.3) the demand is \(D^+\) and the running cost \(L/(1-D(0)).\) For the \((T,S)\) policy, taking \(s=-\infty\) ensures that ordering occurs when the time index reaches zero.
TF 4.
Let \(D\) be the demand rv and suppose that \(\P{D=0}<1.\) Consider the following recursion for the expected cost until ordering under an \((s,S)\) policy.
\begin{algorithm}
\begin{algorithmic}[1]
\Function{V}{\(s,\, j\)}
\If{\(j\leq s\)}
\Return \(K\)
\EndIf
\Return \(\pr{L}\rb{j}+\pr{Sum}\rb{\qb{D(d)\cdot\pr{V}\rb{s,\, j-d}:d\in\pr{Keys}\rb{D},\, \ d>0}}\)
\EndFunction
\end{algorithmic}
\end{algorithm}
Claim: this recursion correctly computes \(V(s,j).\)
Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
False. The term for \(d=0\) was moved to the left-hand side of the recursion, so the sum must be divided by \(1-D(0).\) Equivalently, by (5.9.3), the return value must be \(\pr{L}\rb{j}/(1-D(0)) + D^+.\pr{E}\rb{\lambdafun{d}{\pr{V}\rb{s,\, j-d}}}.\)
TF 5.
Claim: When \(P=j\) and the lead time \(\leadtime>0,\) the inventory cost under the \((T,s,S)\) policy is given by
\begin{align*} c(j) = h j^{+} + b j^{-}. \end{align*}Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
False. It should be \(L(j).\)
Exercise 6.
In Alg. 5.9.2, explain why lines 10 and 11 cannot be combined into the single line \textbf{return} \(\pr{Cache}\rb{\pr{V}}.\)
Hint
Which function does the recursive call \(\pr{V}\rb{n-1,\, s,\, j-d}\) in line 9 reach in each case?
Solution
Did you actually try? Write your solution down first for comparison.
Show the solution
After line 10, the name \(\pr{V}\) refers to the cached function. Hence the recursive call in line 9 also goes through the cache, and each value \(V(n,s,j)\) is computed once. With \textbf{return} \(\pr{Cache}\rb{\pr{V}},\) only the outer call goes through the cache, while the recursive calls in line 9 still reach the uncached \(\pr{V}.\) These calls compute the same values over and over, and their number can grow as \(|\pr{Keys}(D)|^{T}.\)