Stellar Grove · Concrete Mathematical Modelling
When to replace a machine
Running costs rise as a machine wears. When is it worth paying for a new one?
Chapter 7 · Optimal decision and reward. A worked example rebuilt from the chapter's topic in Mike Mesterton-Gibbons, *A Concrete Approach to Mathematical Modelling*; the scenario and numbers are our own illustration, not the book's text.
Headline: Replace the machine once it reaches condition 4 of 9. That rule cuts the discounted cost of a new machine by 65% compared with running it into the ground, and averages 8.88 per period in the long run.
- state 4replace at or beyond this condition
- 65%lower lifetime cost than never replacing
- 8.88average cost per period under the optimal rule
The problem
A machine's condition runs from 0 (new) to 9 (worn out). Each period it wears one step with probability 0.6, and its running cost rises with wear (2.0 when new, 35.8 when worn out). Replacing it costs 25 and gives a new machine. Costs are discounted by a period. What replacement rule minimises total expected cost?
The model
This is a Markov decision process: states , actions (keep or replace), costs and transition probabilities . Bellman's principle of optimality says the minimum expected discounted cost satisfies
Value iteration applies this equation repeatedly from until it stops changing (it is a contraction, so it converges); policy iteration alternates exact evaluation of a rule, , with greedy improvement. Both give the same answer here (identical policies, values within 9.2e-11).
The optimal rule is a threshold: keep while the machine is in good condition, replace once wear passes a critical state, because the cost of keeping rises with while the cost of replacing does not.
Try it
Replacement policy explorer
The chart re-solves the Bellman equation (value iteration) each time you move a slider.
Key findings
- A simple threshold is optimal: replace from condition 4 onward. Dynamic programming finds it without searching all keep/replace rules.
- Both extremes are expensive: never replacing costs 460 (discounted, from new), replacing every period 540, the optimal rule 160.
- Sensitivity (try the sliders): halving the replacement cost to 12 moves the threshold to condition 3; faster wear (p = 0.9) leaves the threshold at condition 4; a lower discount factor (γ = 0.8) leaves the threshold at condition 4. The rule is robust to wear speed and impatience over these ranges and driven mainly by the price of a new machine.
import numpy as np
from books.concrete_mathematics import decision as de
n, wear, gamma = 10, 0.6, 0.95
run = 2 + 1.5 * np.arange(n) + 0.25 * np.arange(n) ** 2
keep = np.zeros((n, n))
for s in range(n):
keep[s, min(s + 1, n - 1)] += wear
keep[s, s] += 1 - wear
P = np.stack([keep, np.tile(keep[0], (n, 1))]) # a = 0 keep, 1 replace
R = np.stack([run, np.full(n, 25.0 + run[0])])
V, policy = de.policy_iteration(P, R, gamma, minimize=True)
print(policy, np.round(V[:3], 1))
# → [0 0 0 0 1 1 1 1 1 1] [160.3 170.9 179.3]
Assumptions and extensions
- Known wear probabilities. In practice they are estimated from maintenance logs (Chapter 8).
- Infinite horizon. A plant closing in 3 years should use
decision.backward_induction, which replaces less near the end. - One machine. Fleets add budget constraints that couple the decisions; that is a linear-programming formulation of the same MDP.