All articles →

100 Exercises / Mathematical optimization / Mathematical Optimization 100 Exercises

Introduction to Dynamic Programming in Manufacturing | Optimizing Inventory and Equipment Maintenance with Python

Use Python dynamic programming for production, inventory, and equipment replacement. Compare value iteration, policy iteration, and Q-learning.

Anticipating Demand Fluctuations and Equipment Deterioration: Dynamic Programming and Sequential Decision-Making 10 Exercises

Set in a fictional precision parts factory, it handles production, inventory, and equipment maintenance with The impact of this week’s decision on next week and beyond incorporated. The target is No.061〜No.070(Dynamic Programming and Sequential Decision-Making).

About the example data This article adapts a training notebook used by Suri Kobo or its representative, Hiroshi Wayama, with permission from the client. All data are fictional and do not describe actual companies or factories.

Manufacturing problem

On the manufacturing floor, increasing production, holding inventory, preventive maintenance, and equipment upgrades cannot be evaluated in a single period. This article introduces dynamic programming (DP) through small examples that select current actions while accounting for future costs.

Common planning difficulties

  • Overproduction to avoid stockouts increases inventory costs and the risk of retention
  • If maintenance is postponed, it can operate this week, but the probability of failure and downtime losses will increase.
  • Extending the lifespan of aging equipment improves short-term financial balances while driving up future maintenance costs.
  • Because conditions and demands change, fixed rules are not always the best

Why the decision is difficult

This is because the effects of actions span time and carry uncertainty. Sequential decision-making compares “current costs” and “future value after action” on the same scale. However, as states, actions, and periods increase, combinations increase rapidly, so model granularity is also an important design decision.

Exercises in this article

No.ThemeJudgment in the manufacturing industry
061What is dynamic programming?Determining the production volume over multiple periods
062Bellman EquationSeparating current expenses from future value
063Shortest Route and Dynamic ProgrammingEvaluating process routes from behind
064The Knapsack Problem and Dynamic ProgrammingSelect improvement projects within your budget
065Inventory Management and Dynamic ProgrammingDetermining order quantities under demand fluctuations
066Equipment ReplacementCompare repair, continuation, and renewal
067Markov Decision ProcessHandling Probabilistic Transitions of Equipment States
068value iterationSeeking solutions with low long-term costs
069Policy IterationSeparating measure evaluation from improvement
070Relationship with Reinforcement LearningLearning Unknown Transitions from Driving Data

Preparing the Python environment

No external data is used. Fix the random number seed so you can reproduce the same result. For clarity, the amount is in thousand yen units.

Run each article in a fresh kernel, starting with [IN: 1] in order. Each [OUT: n] corresponds to the input with the same number. Recorded versions and timings describe the saved output; final digits and solver termination states may vary by environment.

Show environment setup
[IN: 1]
# Run this article from top to bottom in a fresh notebook kernel.
import platform
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from IPython.display import display

rng = np.random.default_rng(42)
plt.rcParams["figure.figsize"] = (7.2, 4.5)
plt.rcParams["axes.unicode_minus"] = False

pd.DataFrame(
    {
        "package": ["Python", "NumPy", "pandas", "Matplotlib"],
        "version": [
            platform.python_version(),
            np.__version__,
            pd.__version__,
            matplotlib.__version__,
        ],
    }
)
[OUT: 1]
package version
0 Python 3.13.1
1 NumPy 2.5.1
2 pandas 3.0.3
3 Matplotlib 3.11.0

Creating the example data

Assume a 12-week precision parts factory. Demand fluctuates gently and randomly, while equipment experiences weekly deterioration. This observation table is for contextual understanding, and for each subsequent exercise, small models are also used to separate decision-making issues.

Show data preparation
[IN: 2]
weeks = np.arange(1, 13)
demand = np.maximum(
    2, np.rint(6 + 1.5 * np.sin(weeks / 2) + rng.normal(0, 1.0, len(weeks)))
).astype(int)
condition = np.maximum(
    45, np.rint(96 - 3.5 * weeks + rng.normal(0, 2.5, len(weeks)))
).astype(int)
factory = pd.DataFrame(
    {"week": weeks, "demand_lots": demand, "equipment_condition": condition}
)
display(factory)

fig, ax1 = plt.subplots()
ax1.plot(weeks, demand, marker="o", label="Demand", color="tab:blue")
ax1.set_xlabel("Week")
ax1.set_ylabel("Demand (lots)", color="tab:blue")
ax2 = ax1.twinx()
ax2.plot(weeks, condition, marker="s", label="Condition", color="tab:red")
ax2.set_ylabel("Equipment condition score", color="tab:red")
ax1.set_title("Synthetic weekly demand and equipment condition")
ax1.grid(True, alpha=0.3)
fig.tight_layout()
plt.show()
[OUT: 2]
week demand_lots equipment_condition
0 1 7 93
1 2 6 92
2 3 8 87
3 4 8 80
4 5 5 79
5 6 5 73
6 7 6 74
7 8 5 68
8 9 5 64
9 10 4 59
10 11 6 61
11 12 6 54

Example data: computed result (figure 1)

No.061: What is Dynamic Programming?

Meaning in Practice

Dynamic programming divides multi-period problems into “states” and “actions,” reusing the same subproblems. Here, starting inventory is considered the condition, production volume is the action, and production costs are minimized over three weeks.

Approach to Analysis and Modeling

With demand dtd_t, opening inventory sts_t, and production ata_t, the next inventory is st+1=st+at−dts_{t+1}=s_t+a_t-d_t. Demand is known in this example, shortages are forbidden, and production and inventory are integers from zero to eight. Vt(s)V_t(s) is the minimum remaining cost. Terminal value is zero, and closing stock in the final week still incurs holding cost.

s′=s+a−dt,Vt(s)=min⁡a{ct(a)+hs′+Vt+1(s′)}.\begin{aligned}s'&=s+a-d_t,\\V_t(s)&=\min_a\{c_t(a)+hs'+V_{t+1}(s')\}.\end{aligned}

Check with Python

[IN: 3]
# Work backward from the terminal period and compare feasible production choices.
dp_demand = [4, 7, 5]
capacity, max_inventory = 8, 8
unit_cost, holding_cost = [5, 8, 6], 1.2
V = {3: {s: 0.0 for s in range(max_inventory + 1)}}
policy = {}
for t in range(2, -1, -1):
    V[t], policy[t] = {}, {}
    for s in range(max_inventory + 1):
        choices = []
        for a in range(capacity + 1):
            nxt = s + a - dp_demand[t]
            if 0 <= nxt <= max_inventory:
                choices.append(
                    (unit_cost[t] * a + holding_cost * nxt + V[t + 1][nxt], a)
                )
        if choices:
            V[t][s], policy[t][s] = min(choices)

s, plan = 0, []
for t in range(3):
    a = policy[t][s]
    nxt = s + a - dp_demand[t]
    plan.append([t + 1, s, dp_demand[t], a, nxt, unit_cost[t] * a + holding_cost * nxt])
    s = nxt
display(
    pd.DataFrame(
        plan,
        columns=[
            "week",
            "opening_inventory",
            "demand",
            "production",
            "closing_inventory",
            "current_cost_kJPY",
        ],
    )
)
print(f"Minimum 3-week cost: {V[0][0]:.1f} kJPY")
[OUT: 3]
week opening_inventory demand production closing_inventory current_cost_kJPY
0 1 0 4 8 4 44.8
1 2 4 7 3 0 24.0
2 3 0 5 5 0 30.0
Minimum 3-week cost: 98.8 kJPY

Reading the results

Because production is more expensive in week two, the plan produces extra in week one and holds inventory. Optimizing each week in isolation misses this saving. On the other hand, if storage capacity, yield, arrangement, and shortage tolerance are excluded, the plan becomes unfeasible, so it is necessary to check state transitions and constraints.

No.062: Bellman’s Equation

Meaning in Practice

The Bellman equation divides current actions into “costs that occur now” and “future costs in the next state.” This breakdown explains why you chose that production volume.

Approach to Analysis and Modeling

If you set the total evaluation value for candidate action aa as Qt(s,a)=ct(s,a)+Vt+1(f(s,a))Q_t(s,a)=c_t(s,a)+V_{t+1}(f(s,a)), it is Vt(s)=min⁡aQt(s,a)V_t(s)=\min_a Q_t(s,a). The principle of optimality states that the remaining parts of the optimal plan are also optimal from the perspective of their reached state.

Check with Python

[IN: 4]
t, s = 0, 0
rows = []
for a in range(capacity + 1):
    nxt = s + a - dp_demand[t]
    if nxt in V[t + 1]:
        immediate = unit_cost[t] * a + holding_cost * nxt
        rows.append([a, nxt, immediate, V[t + 1][nxt], immediate + V[t + 1][nxt]])
bellman = pd.DataFrame(
    rows,
    columns=[
        "production",
        "next_inventory",
        "immediate_cost",
        "future_cost",
        "total_Q",
    ],
)
display(bellman.round(1))
bellman.set_index("production")[["immediate_cost", "future_cost"]].plot(
    kind="bar", stacked=True
)
plt.title("Bellman decomposition at week 1")
plt.xlabel("Production (lots)")
plt.ylabel("Cost (kJPY)")
plt.grid(True, axis="y", alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 4]
production next_inventory immediate_cost future_cost total_Q
0 4 0 20.0 86.0 106.0
1 5 1 26.2 78.0 104.2
2 6 2 32.4 70.0 102.4
3 7 3 38.6 62.0 100.6
4 8 4 44.8 54.0 98.8

No.062: computed result (figure 1)

Reading the results

The more production volume increases, the higher current cost increases, but since you can avoid high-price weeks, future costs decrease. The minimum total action is adopted. During the site briefing, presenting this breakdown makes it easier to share “why we are making extra now.”

No.063: Shortest Path and Dynamic Programming

Meaning in Practice

Route selection for products passing through multiple processes can be represented as dynamic programming, with the current location as the state and the next chosen process as the action. Process time, outsourcing costs, and quality risks are converted into common costs.

Approach to Analysis and Modeling

With directed acyclic graphs, you can calculate V(i)=min⁡j∈N(i){cij+V(j)}V(i)=\min_{j\in N(i)}\{c_{ij}+V(j)\} in reverse order from the endpoint. For general graphs without negative edges, choose solutions that fit the problem structure, such as the Dijkstra method.

Check with Python

[IN: 5]
edges = {
    "Material": [("Cut-A", 4.0), ("Cut-B", 5.0)],
    "Cut-A": [("Heat-1", 7.0), ("Heat-2", 5.0)],
    "Cut-B": [("Heat-1", 4.0), ("Heat-2", 6.0)],
    "Heat-1": [("Inspect", 3.0)],
    "Heat-2": [("Inspect", 4.0)],
    "Inspect": [("Ship", 2.0)],
    "Ship": [],
}
order = ["Ship", "Inspect", "Heat-2", "Heat-1", "Cut-B", "Cut-A", "Material"]
route_value, next_node = {"Ship": 0.0}, {}
for node in order[1:]:
    route_value[node], next_node[node] = min(
        (cost + route_value[j], j) for j, cost in edges[node]
    )
path, node = ["Material"], "Material"
while node != "Ship":
    node = next_node[node]
    path.append(node)
display(
    pd.DataFrame(
        {
            "node": list(route_value),
            "minimum_remaining_cost": list(route_value.values()),
        }
    ).sort_values("minimum_remaining_cost")
)
print("Best route:", " -> ".join(path), f" / cost={route_value['Material']:.1f}")
[OUT: 5]
node minimum_remaining_cost
0 Ship 0.0
1 Inspect 2.0
3 Heat-1 5.0
2 Heat-2 6.0
4 Cut-B 9.0
5 Cut-A 11.0
6 Material 14.0
Best route: Material -> Cut-B -> Heat-1 -> Inspect -> Ship  / cost=14.0

Reading the results

The minimum residual cost from each process to shipment can be determined, allowing you to restore the optimal route. When adding quality loss or lead time to the cost coefficient, the basis for the amount conversion and the prohibition or capability limit are separately managed.

No.064: The Knapsack Problem and Dynamic Programming

Meaning in Practice

When the improvement budget is limited, ranking projects based solely on effect amounts may waste budget combinations. The 0-1 knapsack problem determines the acceptance or rejection of each project simultaneously.

Approach to Analysis and Modeling

Let project ii have investment wiw_i and annual benefit viv_i. Maximize ∑ivixi\sum_i v_i x_i subject to ∑iwixi≤B\sum_i w_i x_i\le B and xi∈{0,1}x_i\in\{0,1\}. table[i, b] stores the maximum benefit from the first ii projects under budget limit bb. During reconstruction, subtract each selected project’s cost from the remaining budget.

Check with Python

[IN: 6]
projects = pd.DataFrame(
    {
        "project": [
            "Vision inspection",
            "Tool monitoring",
            "Energy control",
            "AGV routing",
            "Predictive maintenance",
        ],
        "investment": [6, 4, 3, 5, 7],
        "annual_benefit": [11, 7, 5, 9, 13],
    }
)
B, n = 12, len(projects)
table = np.zeros((n + 1, B + 1), dtype=int)
for i in range(1, n + 1):
    w, v = projects.loc[i - 1, ["investment", "annual_benefit"]]
    for b in range(B + 1):
        table[i, b] = table[i - 1, b]
        if w <= b:
            table[i, b] = max(table[i, b], table[i - 1, b - w] + v)
b, chosen = B, []
for i in range(n, 0, -1):
    if table[i, b] != table[i - 1, b]:
        chosen.append(i - 1)
        b -= projects.loc[i - 1, "investment"]
display(projects.assign(selected=[i in chosen for i in range(n)]))
print(f"Investment={projects.loc[chosen,'investment'].sum()}, benefit={table[n,B]}")
plt.plot(range(B + 1), table[n], marker="o")
plt.title("Best annual benefit by investment budget")
plt.xlabel("Budget")
plt.ylabel("Maximum annual benefit")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 6]
project investment annual_benefit selected
0 Vision inspection 6 11 False
1 Tool monitoring 4 7 False
2 Energy control 3 5 False
3 AGV routing 5 9 True
4 Predictive maintenance 7 13 True
Investment=12, benefit=22

No.064: computed result (figure 1)

Reading the results

The adoption set is the most effective combination across the entire budget. Use scenario analysis or integer programming when benefits are uncertain, projects depend on one another, staffing is limited, or some projects cannot run simultaneously.

No.065: Inventory Management and Dynamic Programming Method

Meaning in Practice

For parts whose demand is not yet certain, the order quantity is determined by considering not only inventory costs but also shortage costs. Shortage fees reflect express shipping, line stoppages, and delivery delays.

Approach to Analysis and Modeling

If demand DtD_t is a random variable, then the Bellman equation containing the expected value

s′=(s+a−Dt)+,ℓ=(Dt−s−a)+,Vt(s)=min⁡a∈A(s){K1{a>0}+ca+E[hs′+pℓ+Vt+1(s′)]}.\begin{aligned} s'&=(s+a-D_t)^+,\\\ell&=(D_t-s-a)^+,\\ V_t(s)&=\min_{a\in A(s)}\Bigl\{K\mathbf{1}_{\{a>0\}}+ca\\ &\quad+\mathbb{E}\bigl[hs'+p\ell+V_{t+1}(s')\bigr]\Bigr\}. \end{aligned}

Here u+=max⁡(u,0)u^+=\max(u,0) and A(s)={a∈{0,…,10}:s+a≤12}A(s)=\{a\in\{0,\ldots,10\}:s+a\le12\}. Orders arrive before that period’s demand, and terminal value is zero.

Here, unmet demand is treated as a shortage for the current period and will not be carried over to the next period.

Check with Python

[IN: 7]
# Orders arrive before demand; unmet demand is lost rather than backlogged.
dvals, probs = np.array([2, 4, 6, 8]), np.array([0.15, 0.35, 0.35, 0.15])
T, Smax, Amax = 3, 12, 10
fixed, purchase, hold, shortage = 6.0, 3.0, 1.0, 9.0
IV = {T: np.zeros(Smax + 1)}
IP = {}
for t in range(T - 1, -1, -1):
    IV[t], IP[t] = np.zeros(Smax + 1), np.zeros(Smax + 1, dtype=int)
    for s in range(Smax + 1):
        qvals = []
        for a in range(Amax + 1):
            if s + a > Smax:
                continue
            exp = fixed * (a > 0) + purchase * a
            for d, pr in zip(dvals, probs):
                nxt = max(s + a - d, 0)
                lost = max(d - s - a, 0)
                exp += pr * (hold * nxt + shortage * lost + IV[t + 1][nxt])
            qvals.append((exp, a))
        IV[t][s], IP[t][s] = min(qvals)
inventory_policy = pd.DataFrame(
    {
        "opening_inventory": range(Smax + 1),
        "order_week1": IP[0],
        "expected_cost_to_go": IV[0],
    }
)
display(inventory_policy.round(2))
plt.step(inventory_policy.opening_inventory, inventory_policy.order_week1, where="mid")
plt.title("Inventory-dependent ordering policy")
plt.xlabel("Opening inventory (lots)")
plt.ylabel("Order quantity (lots)")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 7]
opening_inventory order_week1 expected_cost_to_go
0 0 8 73.07
1 1 7 70.07
2 2 6 67.07
3 3 5 64.07
4 4 0 60.80
5 5 0 55.30
6 6 0 49.80
7 7 0 46.58
8 8 0 43.07
9 9 0 40.66
10 10 0 37.33
11 11 0 35.03
12 12 0 31.33

No.065: computed result (figure 1)

Reading the results

When inventory is low, orders are placed in bulk, and orders are not placed above a certain level. Because there are fixed order costs, it is not a simple replenishment every period. In practice, we also verify updates on demand distribution, order lead times, lot rounding, storage periods, and service levels.

No.066: Equipment Replacement Problem

Meaning in Practice

Equipment renewal involves comparing not only the purchase price but also maintenance costs due to aging, downtime losses, and residual value after renewal over multiple years.

Approach to Analysis and Modeling

Keeping equipment increases its age by one next period; replacement makes it age one next period. discount=0.95 is a discount factor, related to annual discount rate rr by 0.95=1/(1+r)0.95=1/(1+r). State eight represents equipment aged eight or more. Terminal salvage value reduces cost; proceeds from disposing of replaced equipment are omitted.

Check with Python

[IN: 8]
horizon, max_age, discount = 5, 8, 0.95
replace_cost = 65.0
maintenance = np.array([5 + 2 * a + 0.8 * a * a for a in range(max_age + 2)])
EV = {horizon: {a: -max(3, 18 - 2 * a) for a in range(max_age + 1)}}
EP = {}
for t in range(horizon - 1, -1, -1):
    EV[t], EP[t] = {}, {}
    for age in range(max_age + 1):
        keep = maintenance[age] + discount * EV[t + 1][min(age + 1, max_age)]
        replace = replace_cost + maintenance[0] + discount * EV[t + 1][1]
        EV[t][age], EP[t][age] = min((keep, "Keep"), (replace, "Replace"))
eq = pd.DataFrame(
    {
        "equipment_age": range(max_age + 1),
        "decision_now": [EP[0][a] for a in range(max_age + 1)],
        "discounted_cost": [EV[0][a] for a in range(max_age + 1)],
    }
)
display(eq.round(1))
plt.plot(eq.equipment_age, eq.discounted_cost, marker="o")
plt.title("Five-year equipment cost by current age")
plt.xlabel("Current equipment age (years)")
plt.ylabel("Minimum discounted cost (kJPY)")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 8]
equipment_age decision_now discounted_cost
0 0 Keep 53.8
1 1 Keep 81.8
2 2 Keep 100.0
3 3 Keep 109.3
4 4 Keep 116.9
5 5 Replace 118.8
6 6 Replace 118.8
7 7 Replace 118.8
8 8 Replace 118.8

No.066: computed result (figure 1)

Reading the results

Young equipment continues and is replaced as it ages. Since thresholds vary depending on maintenance costs, renewal costs, discount rates, and residual value, a sensitivity table should be attached to the capital investment review rather than a single conclusion.

No.067: Markov Decision Process

Meaning in Practice

The Markov Decision Process (MDP) represents the condition of equipment as a state of health, deterioration, or failure, and the probability of the next state changes depending on operation, maintenance, and replacement.

Approach to Analysis and Modeling

An MDP specifies states SS, actions AA, transition probabilities P(s′∣s,a)P(s'|s,a), costs c(s,a)c(s,a), and a discount factor γ\gamma. We use an infinite-horizon model with 0≤γ<10\le\gamma<1. The Markov assumption means the current state contains the historical information needed to predict the next-state distribution.

Check with Python

[IN: 9]
states = ["Healthy", "Degraded", "Failed"]
actions = ["Run", "Maintain", "Replace"]
P = np.array(
    [
        [[0.85, 0.14, 0.01], [0.95, 0.05, 0], [1, 0, 0]],
        [[0.25, 0.55, 0.20], [0.75, 0.23, 0.02], [1, 0, 0]],
        [[0, 0, 1], [0.55, 0.40, 0.05], [1, 0, 0]],
    ]
)
C = np.array([[2, 8, 25], [5, 10, 25], [45, 35, 25]], dtype=float)
rows = []
for si, s in enumerate(states):
    for ai, a in enumerate(actions):
        rows.append([s, a, C[si, ai], *P[si, ai]])
mdp_table = pd.DataFrame(
    rows, columns=["state", "action", "cost", "P_healthy", "P_degraded", "P_failed"]
)
display(mdp_table)
print("Maximum transition-row error:", np.abs(P.sum(axis=2) - 1).max())
[OUT: 9]
state action cost P_healthy P_degraded P_failed
0 Healthy Run 2.0 0.85 0.14 0.01
1 Healthy Maintain 8.0 0.95 0.05 0.00
2 Healthy Replace 25.0 1.00 0.00 0.00
3 Degraded Run 5.0 0.25 0.55 0.20
4 Degraded Maintain 10.0 0.75 0.23 0.02
5 Degraded Replace 25.0 1.00 0.00 0.00
6 Failed Run 45.0 0.00 0.00 1.00
7 Failed Maintain 35.0 0.55 0.40 0.05
8 Failed Replace 25.0 1.00 0.00 0.00
Maximum transition-row error: 0.0

Reading the results

Even with the same ‘operation,’ the probability of failure is high when deteriorated, and maintenance pays for improvements in transitions. If states omit relevant history, the Markov assumption may fail. We verify whether information necessary for future forecasting, such as vibration, cumulative operation, and near-term maintenance, is included in the condition.

No.068: Value Iteration

Meaning in Practice

The value iteration method repeatedly updates the long-term costs of each state and seeks actions with minimal cost. This forms the basis for establishing standard response rules for each equipment condition.

Approach to Analysis and Modeling

For an infinite-horizon problem with discount factor γ\gamma,

Vk+1(s)=min⁡a{c(s,a)+γ∑s′P(s′∣s,a)Vk(s′)}.\begin{aligned}V_{k+1}(s)=\min_a\Bigl\{&c(s,a)\\&+\gamma\sum_{s'}P(s'|s,a)V_k(s')\Bigr\}.\end{aligned}

Repeat until convergence. Residuals are an indicator of numerical convergence and do not guarantee model validity.

Check with Python

[IN: 10]
# einsum computes expected next-state value for every state-action pair.
gamma = 0.95
value = np.zeros(len(states))
residuals = []
for k in range(1000):
    Q = C + gamma * np.einsum("sak,k->sa", P, value)
    new_value = Q.min(axis=1)
    residuals.append(np.max(np.abs(new_value - value)))
    value = new_value
    if residuals[-1] < 1e-10:
        break
vi_policy = Q.argmin(axis=1)
display(
    pd.DataFrame(
        {
            "state": states,
            "long_run_cost": value,
            "recommended_action": [actions[i] for i in vi_policy],
        }
    ).round(2)
)
plt.semilogy(residuals)
plt.title("Convergence of value iteration")
plt.xlabel("Iteration")
plt.ylabel("Maximum Bellman residual")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print("Iterations:", len(residuals))
[OUT: 10]
state long_run_cost recommended_action
0 Healthy 67.97 Run
1 Degraded 76.94 Maintain
2 Failed 89.57 Replace

No.068: computed result (figure 1)

Iterations: 475

Reading the results

You can get condition-dependent strategies: operation when healthy, maintenance when deteriorating, and replacement when broken. The closer the discount factor is to 1, the more distant the future is emphasized, and the slower convergence becomes. We also check the stability of measures that change costs and transition probabilities.

No.069: Policy Iteration

Meaning in Practice

Policy iteration evaluates the long-term costs of current standard rules and only changes actions where improvement is possible. The comparison with current measures is clear.

Approach to Analysis and Modeling

Fix π\pi to solve (I−γPπ)Vπ=cπ(I-\gamma P_\pi)V_\pi=c_\pi and use its value to improve behavior in each state. If the strategy no longer changes, it will end.

Check with Python

[IN: 11]
pi = np.array([0, 0, 1])  # Healthy=Run, Degraded=Run, Failed=Maintain
history = []
for it in range(20):
    Ppi = P[np.arange(3), pi]
    cpi = C[np.arange(3), pi]
    vpi = np.linalg.solve(np.eye(3) - gamma * Ppi, cpi)
    qpi = C + gamma * np.einsum("sak,k->sa", P, vpi)
    improved = qpi.argmin(axis=1)
    history.append([it, *[actions[x] for x in pi], *vpi])
    if np.array_equal(improved, pi):
        break
    pi = improved
display(
    pd.DataFrame(
        history,
        columns=[
            "iteration",
            "Healthy_action",
            "Degraded_action",
            "Failed_action",
            "V_healthy",
            "V_degraded",
            "V_failed",
        ],
    ).round(2)
)
print("Stable policy:", dict(zip(states, [actions[x] for x in pi])))
[OUT: 11]
iteration Healthy_action Degraded_action Failed_action V_healthy V_degraded V_failed
0 0 Run Run Maintain 90.65 106.94 129.14
1 1 Run Maintain Replace 67.97 76.94 89.57
Stable policy: {'Healthy': 'Run', 'Degraded': 'Maintain', 'Failed': 'Replace'}

Reading the results

From the initial current rules, through evaluation and improvement, we reach the same approach as value iteration. If the number of states is large, the problem of solving linear equations increases, so iterative evaluation and approximation methods are considered.

No.070: Relationship with Reinforcement Learning

Meaning in Practice

DP and MDP assume that transition probabilities and cost models are known. Reinforcement learning estimates value based on the “state, behavior, cost, and next state” obtained through trials. Here, we will learn the same maintenance problems using Q-learning.

Approach to Analysis and Modeling

The cost-minimized version of Q-learning is based on observation samples.

Q(s,a)←Q(s,a)+α[c+γmin⁡a′Q(s′,a′)−Q(s,a)].\begin{aligned}Q(s,a)\leftarrow{}&Q(s,a)\\&+\alpha\Bigl[c+\gamma\min_{a'}Q(s',a')\\&\qquad-Q(s,a)\Bigr].\end{aligned}

This update uses sampled transitions without requiring their probability model, but it needs sufficient exploration. This does not mean unlimited exploration on the actual device.

Check with Python

[IN: 12]
# The learning-rate floor is illustrative; it does not certify exact Q convergence.
qrng = np.random.default_rng(42)
Qlearn = np.zeros((3, 3))
visits = np.zeros((3, 3), int)
episodes, steps = 12000, 25
agreement = []
for ep in range(episodes):
    s = int(qrng.integers(0, 3))
    eps = max(0.03, 0.8 * (1 - ep / episodes))
    for _ in range(steps):
        a = (
            int(qrng.integers(0, 3))
            if qrng.random() < eps
            else int(np.argmin(Qlearn[s]))
        )
        sn = int(qrng.choice(3, p=P[s, a]))
        cost = C[s, a]
        visits[s, a] += 1
        alpha = max(0.02, 0.5 / (visits[s, a] ** 0.55))
        Qlearn[s, a] += alpha * (cost + gamma * Qlearn[sn].min() - Qlearn[s, a])
        s = sn
    agreement.append(np.mean(Qlearn.argmin(axis=1) == vi_policy))
learned = Qlearn.argmin(axis=1)
display(
    pd.DataFrame(
        {
            "state": states,
            "MDP_action": [actions[i] for i in vi_policy],
            "learned_action": [actions[i] for i in learned],
            "minimum_visits": visits.min(axis=1),
        }
    )
)
plt.plot(pd.Series(agreement).rolling(300, min_periods=1).mean())
plt.title("Q-learning policy agreement with model-based optimum")
plt.xlabel("Episode")
plt.ylabel("Share of states with matching action")
plt.ylim(-0.05, 1.05)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 12]
state MDP_action learned_action minimum_visits
0 Healthy Run Run 33523
1 Degraded Maintain Maintain 5006
2 Failed Replace Replace 1222

No.070: computed result (figure 1)

Reading the results

For this seed and training budget, the learned action in each of the three states matches value iteration. The learning rate has a floor of 0.02, so this agreement is not a proof that Q-values converge exactly. Evaluate costs and policy stability using multiple seeds and unused simulated trajectories.

Practical implications

  • Sequential decision-making evaluates not only current-period KPIs but also the future value of the following state
  • Conditions are narrowed down to providing sufficient information for future forecasts, preventing an explosion in the number of states.
  • Prepare explanatory materials not only for optimal measures but also for immediate costs, future costs, and differences from alternative actions
  • Transition probabilities, demand distribution, and cost factors are regularly re-estimated to monitor changes in strategy
  • Rather than a simple binary of DP/MDP if the model is known, or learning if unknown, it combines known information with data.
  • Reinforcement learning exploration is conducted under simulators or safety constraints, without directly connecting to actual machines

Putting the model into practice

TopicChecksMain Deliverables
Decision-makingWho chooses, when, and whatBusiness Flow & Approval Responsibilities
ConditionHow to represent inventory, deterioration, work-in-progress, and delivery datesState Definition & Data Dictionary
Actions and ConstraintsCapacity, safety, lots, and maintenance windowsList of Constraints and Rationale
FeesShortage, downtime, quality, and salvage valueCost Conversion Table & Sensitivity Analysis
probabilityDemand and Fault Transition Estimation AccuracyEstimation Procedure & Verification Results
UtilizationRecalculation, exceptions, model degradationSOP and Monitoring Dashboard

Initially, limit the number of states and actions, then use the current rules as a baseline for backtesting with past data. Starting with decision support where people confirm reasons for recommendations and alternatives, it simultaneously monitors downtime losses, service levels, and workload.

Conclusion

Multi-period production, process routes, improvement projects, inventory, and equipment renewals are represented as DP, and probabilistic equipment maintenance is expanded into MDP, value iteration, policy iteration, and Q-learning. What matters is not the advanced solution itself, but the clarity of the carryover effects as state transitions, making them decision-making rules that can be verified by the field.

Business inquiries

At Suri Kobo, we support sequential decision-making in production, inventory, and equipment maintenance, including problem organization, data design, simulation, optimization PoC, and operational design.

📩 Contact Us: www.surikobo.co.jp/contact
Please feel free to consult us first.