> ## Documentation Index
> Fetch the complete documentation index at: https://aegean.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimal Capacity Control

> Protection levels for selling perishable capacity to several price classes.

## Single resource capacity control

Airlines, car rental companies and hotels sell a fixed, perishable capacity: seats on one flight, cars on one day, rooms on one night. Customers who pay a discounted price usually book early, and customers who pay the full price book late. If you accept every early request, you may have nothing left for the late customers who pay more. If you hold back too much, units go unsold. This section derives the capacity control policy that resolves this trade-off for a single resource, following Talluri and van Ryzin (2004).

The policy accepts a request only if its price is greater than the value of the capacity the request uses. That value is the *opportunity cost* (or expected displacement cost) of the unit: the expected future revenue you lose by selling it now instead of protecting it for later.

A value function captures this cost. Let $v(x)$ be the optimal expected revenue when $x$ units of capacity remain. The opportunity cost of one unit is the marginal value $v(x) - v(x-1)$, so the whole problem reduces to computing the value function accurately.

### Two-class model

You allocate a resource of capacity $C$ to two classes of customers. Each class pays a fixed price per unit. Let $p_d$ and $p_f$ be the prices of the discount and full-price classes, and let $D_d$ and $D_f$ be their demands. The model assumes that

* $p_d < p_f$, and
* $D_d$ and $D_f$ are independent random variables.

All discount demand arrives before any full-price demand. Real bookings interleave, but this simplification has been shown to cause only small losses relative to the optimal policy.

You choose a *protection level* $y$ in advance: the number of units reserved for the full-price class. The *booking limit* for the discount class is then

$C_d = C - y.$

The discount class cannot occupy more units than it demands, so it sells

$S_d = \min(C_d, D_d).$

The capacity left for the full-price class is

$C_f = C - \min(C_d, D_d) = \max(y, C - D_d),$

and the full-price class sells

$S_f = \min(C_f, D_f).$

The expected revenue is $\mathbb{E}[p_d S_d + p_f S_f]$, where the expectation is over both demands. Maximizing over the protection level gives

$$
\begin{aligned}
v_d(C) &= \max_{0 \le y \le C} \mathbb{E}\left[p_d S_d + p_f S_f\right] \\
&= \max_{0 \le y \le C} \mathbb{E}\left[p_d \min(C-y, D_d) + p_f \min\left(\max(y, C-D_d), D_f\right)\right].
\end{aligned}
$$

The price ratio $r = p_d / p_f$ suggests how $y$ should behave. When $r$ is small ($p_f \gg p_d$), you protect most of the capacity for the full-price class. When $r$ is close to 1, you protect very little, because a discount sale earns almost as much as a full-price sale. Between these extremes, $y$ depends on the tail of the full-price demand, $P(D_f \ge y)$: a protected unit earns revenue only if full-price demand actually reaches it. Dynamic programming finds the optimal $y$.

### Dynamic programming solution

The dynamic program has one stage per class. Define $v_d(C)$ as the optimal expected revenue just before discount demand is observed, with $C$ units available. Selling everything at the full price bounds it from above: $v_d(C) \le p_f C$.

Define $v_f(x)$ as the optimal expected revenue just before full-price demand is observed, with $x$ units left. Nothing remains to decide at this stage, so

$v_f(x) = \mathbb{E}\left[p_f \min(x, D_f)\right] = p_f \sum_{j=1}^{x} P(D_f \ge j).$

The last step uses an identity for a nonnegative integer demand $D$ and an integer $x$:

$$
\begin{aligned}
\mathbb{E}\left[\min(x, D)\right] &= \sum_{j=0}^{\infty} \min(x, j)\, P(D = j) \\
&= 1 \cdot P(D=1) + \dots + x \cdot P(D=x) + x \cdot P(D=x+1) + \dots \\
&= P(D \ge 1) + P(D \ge 2) + \dots + P(D \ge x) = \sum_{j=1}^{x} P(D \ge j).
\end{aligned}
$$

The dynamic program relates $v_d(C)$ to $v_f$. After discount demand, $C_f$ units are left and the second stage earns $v_f(C_f)$ in expectation, so

$$
\begin{aligned}
v_d(C) &= \max_{0 \le y \le C} \mathbb{E}\left[p_d \min(C-y, D_d) + v_f\left(\max(y, C-D_d)\right)\right] \\
&= \max_{0 \le y \le C} W(y, C).
\end{aligned}
$$

To find the best $y$, look at the change in $W$ from one more protected unit:

$\Delta W(y, C) = W(y, C) - W(y-1, C) = \left[p_f P(D_f \ge y) - p_d\right] P(D_d > C - y).$

To see why, compare the two expectations case by case. If $D_d \le C - y$, discount demand never reaches the booking limit under either protection level. Both terms equal $p_d D_d + v_f(C - D_d)$, and the difference is zero. If $D_d > C - y$, the discount class fills its booking limit in both cases, and

$$
\begin{aligned}
\Delta W &= \left[p_d (C-y) + v_f(y)\right] - \left[p_d (C-y+1) + v_f(y-1)\right] \\
&= \Delta v_f(y) - p_d,
\end{aligned}
$$

where $\Delta v_f(y) = v_f(y) - v_f(y-1) = p_f P(D_f \ge y)$ is the marginal value of the $y$th protected unit. Weighting the second case by its probability $P(D_d > C - y)$ gives the result.

The bracket $p_f P(D_f \ge y) - p_d$ decreases in $y$. At $y = 0$ it equals $p_f - p_d > 0$, and as $y \to \infty$ it tends to $-p_d < 0$. So $W(y, C)$ increases with $y$ while the bracket is positive and decreases after it turns negative, and its maximum sits where the bracket changes sign. The factor $P(D_d > C - y)$ is never negative, so it cannot move that point: the optimal protection level does not depend on the discount demand.

#### Optimal protection level and maximum revenue

The optimal protection level is the largest $y$ at which one more protected unit is still worth more than a discount sale:

$y^* = \max\left\{ y \in \mathbb{N} : p_f P(D_f \ge y) \ge p_d \right\} = \max\left\{ y \in \mathbb{N} : P(D_f \ge y) \ge \frac{p_d}{p_f} \right\}.$

This is known as Littlewood's rule. The optimal booking limit for the discount class is

$b^* = (C - y^*)^+.$

The maximum expected revenue starting with $C$ units is

$v_d(C) = \begin{cases} W(y^*, C) & \text{if } y^* \le C, \\ W(C, C) & \text{if } y^* > C. \end{cases}$

You can compute $W(y^*, C)$ recursively from

$W(y, C) = W(y-1, C) + \left[p_f P(D_f \ge y) - p_d\right] P(D_d > C - y),$

starting at

$W(0, C) = p_d\, \mathbb{E}\left[\min(C, D_d)\right] + \mathbb{E}\left[v_f\left(\max(0, C - D_d)\right)\right].$

The identity above gives the first expectation,

$\mathbb{E}\left[\min(C, D_d)\right] = \sum_{j=1}^{C} P(D_d \ge j),$

and the second one is a finite sum, because $v_f(0) = 0$:

$\mathbb{E}\left[v_f\left(\max(0, C - D_d)\right)\right] = \sum_{j=0}^{C} v_f(C - j)\, P(D_d = j).$

Starting from $W(0, C)$, you compute $W(1, C)$, $W(2, C)$ and so on up to $W(y^*, C)$.

### Multiple classes

With $n$ classes, number them by price so that class 1 pays the most. Class $n$ books first and class 1 books last. Stage $j$ handles class $j$, and $v_j(x)$ is the optimal expected revenue with $x$ units left before class $j$ demand arrives, with $v_0(x) = 0$.

Let $y = y_{j-1}^*$ be the protection level for classes $j-1, \dots, 1$, with $y_0^* = 0$. For $x \le y$, class $j$ gets nothing and $v_j(x) = v_{j-1}(x)$. For $x > y$, class $j$ can buy up to $x - y$ units, and

$$
\begin{aligned}
v_j(x) = {} & \sum_{k=0}^{x-y-1} \left[p_j k + v_{j-1}(x-k)\right] P(D_j = k) \\
& + \left[p_j (x-y) + v_{j-1}(y)\right] P(D_j \ge x-y).
\end{aligned}
$$

The first sum covers demand below the booking limit, where every request is accepted. The second term covers demand that reaches the limit. The protection level for classes $j, \dots, 1$ is the largest $x$ at which a unit is still worth more than the price of the next class:

$y_j^* = \max\left\{ x : v_j(x) - v_j(x-1) > p_{j+1} \right\}.$

The function below computes the protection levels for Poisson demand, one stage at a time. Each stage reuses the value function and protection level of the stage before it.

```python theme={null}
import numpy as np
from scipy.stats import poisson


def protection_levels(C, prices, demand_means):
    """Multi-class capacity control by dynamic programming.

    Classes are ordered by price, class 1 the highest. Class n books first and
    class 1 books last. Demand of class j is Poisson with mean demand_means[j-1].
    Returns the nested protection levels y_1*, ..., y_{n-1}* and v_n(C).
    """
    x = np.arange(C + 1)
    v = np.zeros(C + 1)                       # v_0(x) = 0: no demand left
    levels = []
    for j, (p, mean) in enumerate(zip(prices, demand_means)):
        y = levels[-1] if levels else 0       # capacity protected for higher classes
        pmf = poisson.pmf(x, mean)            # P(D_j = k)
        ccdf = poisson.sf(x - 1, mean)        # P(D_j >= k)
        v_new = v.copy()                      # for x <= y nothing is sold to class j
        for c in range(y + 1, C + 1):
            k = np.arange(c - y)              # demand below the booking limit c - y
            sold_all = np.sum((p * k + v[c - k]) * pmf[k])
            limit_hit = (p * (c - y) + v[y]) * ccdf[c - y]
            v_new[c] = sold_all + limit_hit
        v = v_new
        if j + 1 < len(prices):               # protect while a unit is worth more than the next price
            delta_v = np.diff(v)              # delta_v[c-1] = v(c) - v(c-1)
            above = np.nonzero(delta_v > prices[j + 1])[0]
            levels.append(int(above[-1]) + 1 if above.size else 0)
    return levels, v[C]


for C, prices, means in [(100, [1000, 450], [40, 15]),
                         (50, [100, 90, 80, 70], [10, 15, 25, 15])]:
    levels, revenue = protection_levels(C, prices, means)
    print(f"C={C}: protection levels {levels}, expected revenue {revenue:.2f}")
```

```output theme={null}
C=100: protection levels [41], expected revenue 46750.00
C=50: protection levels [6, 20, 44], expected revenue 4142.72
```

The first example has capacity $C = 100$ and two classes:

| Class | Price | Demand | Optimal protection level |
| - | - | - | - |
| 1 | 1000 | Poisson(40) | 41 |
| 2 | 450 | Poisson(15) | (none) |

The marginal value of capacity as a function of the remaining capacity:

![Marginal value of one unit of capacity against remaining capacity for the two-class example](https://mintcdn.com/aegeanaiinc/DzThCvmowgedLKrz/aiml-common/lectures/mdp/mdp-workshop/yield-management-capacity-control/images/marginal-value-two-class.png?fit=max\&auto=format\&n=DzThCvmowgedLKrz\&q=85\&s=14a3675afa62256ac0ced9b7f264a4e8)

*Marginal value as a function of the remaining capacity, two classes*

The optimal protection limits for the same example:

![Optimal protection limits for the two-class example](https://mintcdn.com/aegeanaiinc/DzThCvmowgedLKrz/aiml-common/lectures/mdp/mdp-workshop/yield-management-capacity-control/images/protection-limits-two-class.png?fit=max\&auto=format\&n=DzThCvmowgedLKrz\&q=85\&s=e0fdb4c0953c5cfe27706c320d17e9a4)

*Optimal protection limits, two classes*

The same dynamic program handles more classes. The second example has capacity $C = 50$ and four classes:

| Class | Price | Demand | Optimal protection level |
| - | - | - | - |
| 1 | 100 | Poisson(10) | 6 |
| 2 | 90 | Poisson(15) | 20 |
| 3 | 80 | Poisson(25) | 44 |
| 4 | 70 | Poisson(15) | (none) |

The lowest class has no protection level of its own, because no cheaper class comes after it.

![Marginal value of capacity against remaining capacity for each stage of the four-class example](https://mintcdn.com/aegeanaiinc/DzThCvmowgedLKrz/aiml-common/lectures/mdp/mdp-workshop/yield-management-capacity-control/images/marginal-value-multiclass.png?fit=max\&auto=format\&n=DzThCvmowgedLKrz\&q=85\&s=b18978d86297f8aefade030512314f7e)

*Marginal value as a function of the remaining capacity for each stage, four classes*

The marginal value of capacity decreases as the remaining capacity grows: the more units you have left, the less any one of them is worth. It also increases with the stage index. At a higher stage more classes are still to come, so a unit kept back has more chances to be sold at a good price.

The optimal protection limits for the four-class example:

![Optimal protection limits for the four-class example](https://mintcdn.com/aegeanaiinc/DzThCvmowgedLKrz/aiml-common/lectures/mdp/mdp-workshop/yield-management-capacity-control/images/protection-limits-multi-class.png?fit=max\&auto=format\&n=DzThCvmowgedLKrz\&q=85\&s=9f9d5ff925d2e1c320738d7f083dadd9)

*Optimal protection limits, four classes*

## References

K. Talluri and G. van Ryzin, *The Theory and Practice of Revenue Management*, Springer, 2004.

***

Found an error on this page? [Report an issue](https://github.com/aegean-ai/eaia-feedback/issues/new?template=page-issue.yml\&page=/aiml-common/lectures/mdp/mdp-workshop/yield-management-capacity-control).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.