Search

Search papers and lessons

Constrained Markov Decision Process

Level 4 · 14 min read

A constrained Markov decision process is a sequential decision problem that maximizes reward while keeping a separate cost, averaged over time, below a fixed budget.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Optimization
  • Reinforcement learning
  • ReadReady to readNot yet

TL;DR

A constrained Markov decision process is a decision problem over time, the kind studied in reinforcement learning, with two scores instead of one. The agent earns a reward, and it also incurs a cost, and the cost must stay under a fixed budget. The goal is the best reward among all behaviours that respect the budget. Plain reward maximization can pick a behaviour that is very profitable and very expensive, and the constraint is what rules that behaviour out.

Builds on: Markov Decision Process, Occupancy Measure, Lagrangian Duality · Leads to: safe learning in robotics

Where it's used

A warehouse robot with a battery. A robot picks orders for as many minutes as its battery allows. The reward is orders completed, and the cost is energy drawn from the battery. The designer wants the highest order rate with energy use kept under a limit that protects the battery's life. A plain MDP would push the robot to run its fastest motors all the time, which breaks the energy limit.

A data-centre cooling controller. Each interval the controller picks a fan mode. Reward is computing throughput, and cost is power draw. The facility has a contracted average power limit, and the question is how much throughput is available while meeting it. The worked example near the end uses numbers of this kind.

Safety in automated driving or medicine dosing. Here the cost is a safety signal, such as the chance of a near-collision or a dose above a safe level. The policy is allowed to be aggressive on reward only as long as the expected safety cost stays under a threshold. Researchers use constrained formulations to state that threshold directly instead of folding it into a single reward with an arbitrary weight.

How it works

Take one tiny instance. There is one decision. The agent chooses action A or action B, and the episode ends. Action A gives reward 10 and cost 4. Action B gives reward 4 and cost 0. The budget on expected cost is 2. Let pp be the probability of choosing A. Since the episode ends after one step, there is nothing to discount, so the discount factor is 0.

  1. Describe the behaviour. A behaviour is a Policy (Reinforcement Learning), and here it is a single number: the share pp of A. Check: p=0p = 0 means always B, and p=1p = 1 means always A.
  2. Compute the two expected scores. The expected reward is 10p+4(1−p)=4+6p10p + 4(1-p) = 4 + 6p. Expected cost is 4p+0(1−p)=4p4p + 0(1-p) = 4p. Check: at p=1p = 1 reward is 10 and cost is 4, and at p=0p = 0 reward is 4 and cost is 0.
  3. Apply the budget. Feasible behaviours satisfy 4p≤24p \le 2, so p≤0.5p \le 0.5. Check: p=0.5p = 0.5 gives cost 2, which is exactly the budget, and p=0.6p = 0.6 gives cost 2.4, which breaks it.
  4. Pick the best feasible behaviour. Reward rises with pp, so the best choice is the largest feasible value, p=0.5p = 0.5. Its reward is 4+6×0.5=74 + 6 \times 0.5 = 7. Check: the unconstrained best, p=1p = 1, earns 10 but costs 4, so it is ruled out.
  5. Price the budget. Subtract a penalty from the reward for each unit of cost above the budget, the same idea as a Lagrange multiplier. Use a price λ≥0\lambda \ge 0. The penalized score is 4+6p−λ(4p−2)4 + 6p - \lambda(4p - 2). At λ=1.5\lambda = 1.5 the score is 77 for every pp, because the dependence on pp cancels. Check: 4+6p−1.5(4p−2)=4+6p−6p+3=74 + 6p - 1.5(4p - 2) = 4 + 6p - 6p + 3 = 7.

Step 5 is the important idea. The budget can be handled by a price. Once the price is right, the choice between A and B no longer changes the score, and that common score, 7, is exactly the best feasible reward from step 4.

flowchart LR
  A["Maximize reward subject to cost at most budget"] --> B["Add penalty lambda times cost above budget"]
  B --> C["For a fixed lambda, pick the best behaviour"]
  C --> D["Choose the lambda that gives the lowest score"]
  D --> E["That score is the best feasible reward"]

The plot shows the two expected scores for every share pp. The budget line cuts the feasible range.

00.20.40.60.810246810p, share of action Aexpected value
  • reward
  • cost
  • budget
Reward and cost both rise with p. The budget of 2 caps p at 0.5, where the reward is 7.

The math

Definition: Constrained Markov decision process
A constrained MDP is a tuple of states S\mathcal{S}, actions A\mathcal{A}, transition probabilities P(s′∣s,a)P(s' \mid s, a), a reward r(s,a)r(s, a), a cost c(s,a)c(s, a), a discount factor γ∈[0,1)\gamma \in [0, 1), and a budget dd. A policy π\pi chooses actions, and it must satisfy JC(π)≤dJ_C(\pi) \le d while maximizing JR(π)J_R(\pi), where JR(π)=Eπ[∑t≥0γtr(st,at)]J_R(\pi) = \mathbb{E}^{\pi}\left[\sum_{t \ge 0} \gamma^t r(s_t, a_t)\right] and JC(π)=Eπ[∑t≥0γtc(st,at)]J_C(\pi) = \mathbb{E}^{\pi}\left[\sum_{t \ge 0} \gamma^t c(s_t, a_t)\right].

Here Eπ\mathbb{E}^{\pi} is the expected value when the agent follows π\pi from its start state. The symbol γ\gamma is the discount factor, which weights later steps less. In the tiny instance γ=0\gamma = 0, so only the first step counts.

Step 1 in symbols: the behaviour

A policy π(a∣s)\pi(a \mid s) gives the probability of action aa in state ss. In the tiny instance there is one state and two actions, so the policy is the single number p=π(A)p = \pi(A).

Step 2 in symbols: the two scores

The expected scores are linear in the policy's probabilities. For the tiny instance:

JR=10p+4(1−p)=4+6p,JC=4pJ_R = 10p + 4(1-p) = 4 + 6p, \qquad J_C = 4p

A useful fact follows from the discounted sum. Write the normalized occupancy ρπ(s,a)=(1−γ)∑t≥0γtPr⁡(st=s,at=a)\rho^{\pi}(s, a) = (1-\gamma) \sum_{t \ge 0} \gamma^t \Pr(s_t = s, a_t = a), which is the discounted share of time spent in each pair. Then both scores are weighted sums over ρπ\rho^{\pi}:

JR(π)=11−γ∑s,aρπ(s,a) r(s,a),JC(π)=11−γ∑s,aρπ(s,a) c(s,a)J_R(\pi) = \frac{1}{1-\gamma} \sum_{s,a} \rho^{\pi}(s,a)\, r(s,a), \qquad J_C(\pi) = \frac{1}{1-\gamma} \sum_{s,a} \rho^{\pi}(s,a)\, c(s,a)

This is why the problem is simpler than it looks. In the occupancy variables the constraint and the objective are both linear, so the problem becomes a linear program over a convex set of occupancy measures, and that set is described by the flow equations of the MDP.

Step 3 in symbols: the budget

The feasible set is {π:JC(π)≤d}\{\pi : J_C(\pi) \le d\}. In the tiny instance, 4p≤24p \le 2 gives p≤0.5p \le 0.5.

Step 4 in symbols: the best feasible choice

This is a constrained optimization problem. The best feasible reward is

V⋆=max⁡π{ JR(π):JC(π)≤d }V^{\star} = \max_{\pi} \{\, J_R(\pi) : J_C(\pi) \le d \,\}

For the tiny instance, V⋆=max⁡p≤0.5(4+6p)=7V^{\star} = \max_{p \le 0.5} (4 + 6p) = 7.

Step 5 in symbols: the price of the budget

The Lagrangian is

L(π,λ)=JR(π)−λ(JC(π)−d),λ≥0L(\pi, \lambda) = J_R(\pi) - \lambda \bigl(J_C(\pi) - d\bigr), \qquad \lambda \ge 0

and the dual function is g(λ)=max⁡πL(π,λ)g(\lambda) = \max_{\pi} L(\pi, \lambda). Two facts follow directly. For any feasible π\pi and any λ≥0\lambda \ge 0, JR(π)≤L(π,λ)≤g(λ)J_R(\pi) \le L(\pi, \lambda) \le g(\lambda), so each g(λ)g(\lambda) is an upper bound on V⋆V^{\star}. Minimizing over λ\lambda gives the tightest such bound, min⁡λ≥0g(λ)≥V⋆\min_{\lambda \ge 0} g(\lambda) \ge V^{\star}. This is the dual problem from Lagrangian Duality.

For the tiny instance, L=4+2λ+(6−4λ)pL = 4 + 2\lambda + (6 - 4\lambda)p, so

g(λ)=max⁡p∈[0,1](4+2λ+(6−4λ)p)={4+2λλ≥1.510−2λ0≤λ<1.5g(\lambda) = \max_{p \in [0,1]} \bigl(4 + 2\lambda + (6 - 4\lambda)p\bigr) = \begin{cases} 4 + 2\lambda & \lambda \ge 1.5 \\ 10 - 2\lambda & 0 \le \lambda \lt 1.5 \end{cases}

The minimum is at λ⋆=1.5\lambda^{\star} = 1.5, where g=7=V⋆g = 7 = V^{\star}. There is no gap, which is strong duality. The optimal behaviour is any mixture that makes the pp-term vanish, and the feasibility constraint then picks p=0.5p = 0.5.

Why the answer can be a mixture

Without a constraint, an MDP has an optimal deterministic policy. With a constraint, the best point often lies on the edge of the feasible set, and that edge can only be reached by randomizing. The tiny instance shows this: p=0.5p = 0.5 is a 50/50 mixture of A and B, and neither pure choice is feasible-and-best.

Worked examples

Example
1. Tiny: a budget that does not bind

Use the same two actions, A with reward 10 and cost 4, and B with reward 4 and cost 0. Change the budget to d=4d = 4.

  1. Feasibility: 4p≤44p \le 4 gives p≤1p \le 1. Every behaviour is feasible.
  2. Reward is 4+6p4 + 6p, which rises with pp, so the best choice is p=1p = 1.
  3. The best reward is 4+6×1=104 + 6 \times 1 = 10, and the cost is 44, exactly the budget.
  4. Dual check: g(λ)=max⁡p(4+4λ+(6−4λ)p)g(\lambda) = \max_p \bigl(4 + 4\lambda + (6 - 4\lambda)p\bigr). For 0≤λ≤1.50 \le \lambda \le 1.5 the pp-term is non-negative, so p=1p = 1 and g(λ)=10g(\lambda) = 10. For λ≥1.5\lambda \ge 1.5, g(λ)=4+4λ≥10g(\lambda) = 4 + 4\lambda \ge 10. The minimum value is 10, reached for every λ\lambda in [0,1.5][0, 1.5], and the smallest such price is λ⋆=0\lambda^{\star} = 0.

Interpretation: when the budget is loose, the price is zero and the constraint does nothing. A constrained problem with a slack constraint is the same as an unconstrained MDP.

Example
2. Standard: a discounted version

Use one state with actions A (reward 10, cost 4) and B (reward 4, cost 0), discount γ=0.5\gamma = 0.5, and budget d=4d = 4. A policy picks A with probability pp at every step.

  1. The discount sum for a constant per-step reward xx is x∑t≥00.5t=x/(1−0.5)=2xx \sum_{t \ge 0} 0.5^t = x / (1 - 0.5) = 2x.
  2. Expected reward per step is 10p+4(1−p)=4+6p10p + 4(1-p) = 4 + 6p, so JR=2(4+6p)=8+12pJ_R = 2(4 + 6p) = 8 + 12p.
  3. Expected cost per step is 4p4p, so JC=2×4p=8pJ_C = 2 \times 4p = 8p.
  4. Budget: 8p≤48p \le 4, so p≤0.5p \le 0.5.
  5. Reward is increasing in pp, so the best choice is p=0.5p = 0.5, and JR=8+12×0.5=14J_R = 8 + 12 \times 0.5 = 14.
  6. Dual: g(λ)=max⁡p(8+12p−λ(8p−4))=max⁡p(8+4λ+(12−8λ)p)g(\lambda) = \max_p \bigl(8 + 12p - \lambda(8p - 4)\bigr) = \max_p \bigl(8 + 4\lambda + (12 - 8\lambda)p\bigr).
  7. For λ≥1.5\lambda \ge 1.5 the best pp is 0, so g=8+4λg = 8 + 4\lambda. For λ≤1.5\lambda \le 1.5 the best pp is 1, so g=20−4λg = 20 - 4\lambda.
  8. The two lines meet at λ=1.5\lambda = 1.5, where both equal 1414. Check: 8+4(1.5)=148 + 4(1.5) = 14 and 20−4(1.5)=1420 - 4(1.5) = 14.
  9. The primal optimum is 14 and the dual minimum is 14. There is no gap, and the optimal policy is p=0.5p = 0.5.

Interpretation: discounting doubles the total cost as well as the total reward, so the budget of 4 caps pp at 0.5 here. The price λ⋆=1.5\lambda^{\star} = 1.5 is the reward traded away for each unit of discounted cost.

Example
3. Realistic: a cooling controller with an average power limit

A data-centre controller picks one of two fan modes at each interval. High mode gives throughput 8 and draws power 6. Low mode gives throughput 3 and draws power 1. The site must keep average power at or below 3. Treat each interval as a separate decision, so γ=0\gamma = 0, and let pp be the share of intervals in High mode.

  1. Throughput is 8p+3(1−p)=3+5p8p + 3(1-p) = 3 + 5p.
  2. Power is 6p+1(1−p)=1+5p6p + 1(1-p) = 1 + 5p.
  3. Power limit: 1+5p≤31 + 5p \le 3, so p≤0.4p \le 0.4.
  4. Best throughput is at p=0.4p = 0.4: 3+5×0.4=53 + 5 \times 0.4 = 5.
  5. Check the power at that point: 1+5×0.4=31 + 5 \times 0.4 = 3, exactly the limit.
  6. Price the limit with λ\lambda: the penalized score is 3+5p−λ(1+5p−3)=3+2λ+(5−5λ)p3 + 5p - \lambda(1 + 5p - 3) = 3 + 2\lambda + (5 - 5\lambda)p.
  7. At λ=1\lambda = 1 the pp-term vanishes, so every mix scores 3+2=53 + 2 = 5. That matches the primal best of 5.
  8. Interpretation: the controller runs High mode 40 percent of the time. Pure High mode would give more throughput but breaks the power limit, so the best policy is a mixture.

Try it: the site raises its power limit from 3 to 4. What is the best average throughput? Answer: the power constraint becomes 1+5p≤41 + 5p \le 4, so p≤0.6p \le 0.6. Throughput is 3+5×0.6=63 + 5 \times 0.6 = 6. Check the power: 1+5×0.6=41 + 5 \times 0.6 = 4, exactly the new limit.

Common mistakes and tradeoffs

It is tempting to think that a constraint is just a large penalty in the reward. In fact a penalty with a fixed weight can give the wrong answer, because the right weight depends on the budget. The Lagrange multiplier λ\lambda is the weight that makes the penalty exactly strong enough to satisfy the budget, and finding it is part of solving the problem. In the tiny instance a weight of 1 would not be right, and the answer is only reached at λ=1.5\lambda = 1.5.

It is tempting to think that the optimal policy is always deterministic, as in an ordinary MDP. In fact a constrained problem can need a randomized policy, because the best feasible point can sit on the boundary between two pure policies. The cooling example needs a 40/60 mix of High and Low, and no pure policy achieves the best throughput under the power limit.

It is tempting to think that the constraint holds in every single step. In fact a CMDP constrains the expected discounted total, or the long-run average, of the cost. An individual step can exceed the budget as long as the expected sum stays under it. Designers who need a hard limit on each step must use a different formulation.

When to use it: use a CMDP when there are two quantities to balance, such as reward and safety, throughput and power, or accuracy and compute, and the second one has a hard limit. When not to use it: if the cost can be folded into the reward with a weight that is clearly right, a plain MDP is simpler. Alternatives include reward shaping, where a penalty is added to the reward directly, and safety filters that override the policy when a state is unsafe. Those are simpler but do not give a guaranteed budget.

History

The idea of making decisions step by step under uncertainty began with dynamic programming. Markov decision processes were set out in the 1950s, with Richard Bellman's work on recursive optimal choice as the basis. Lagrange multipliers for constrained optimization are much older, from the work of Joseph-Louis Lagrange in the late 1700s.

Constraints were added to the Markov setting as a separate line of research. Eitan Altman's book Constrained Markov Decision Processes (1999) set out the linear-programming view in the occupancy variables and the duality results that make the problem tractable. The idea caught on in the 2010s, when reinforcement learning became practical for control tasks where safety mattered, and researchers needed a way to state a safety limit without tuning a reward weight by trial and error.

Recap

  • An MDP has one score, reward. A constrained MDP adds a cost and a budget that the cost must respect.
  • The occupancy measure turns both scores into weighted sums, so the problem becomes linear in the occupancy variables.
  • The Lagrangian turns the budget into a price λ\lambda, and minimizing over λ\lambda gives a bound that is tight under strong duality.
  • The best constrained policy can be a mixture of pure policies, because the answer often lies on the edge of the feasible set.

What to read next: Safe Learning in Robotics builds on this framework to study safety in control systems.

Check yourself

QuestionRecall: what does the budget constraint in a constrained MDP limit, and over what quantity is it stated?Show answer
The expected total of the cost, discounted by γ\gamma over time, must stay at or below the budget dd. The constraint is on the expected discounted sum of costs, not on the cost at each step.
QuestionComputation: one decision, action A has reward 6 and cost 3, action B has reward 2 and cost 0, and the budget is 1.5. Let pp be the probability of A. What is the best expected reward?Show answer
Cost is 3p3p, so 3p≤1.53p \le 1.5 and p≤0.5p \le 0.5. Reward is 6p+2(1−p)=2+4p6p + 2(1-p) = 2 + 4p. At p=0.5p = 0.5 this is 2+2=42 + 2 = 4. The best expected reward is 4.
QuestionWhy: an ordinary MDP has an optimal deterministic policy, but a constrained MDP can need a randomized one. Why?Show answer
The best feasible point often lies on the boundary of the feasible set. A pure policy sits at a corner, and a corner can be infeasible or suboptimal. Mixing two pure policies reaches a point on the boundary that a pure policy cannot reach, as in the cooling example where the best throughput needs 40 percent High mode.
QuestionApplied: a delivery robot has an energy budget per hour. The budget is loose enough that the robot never runs out, even at full speed. What is the optimal Lagrange multiplier on energy, and why?Show answer
The multiplier is zero. A slack constraint has no effect on the optimum, so the price of the budget is zero. The solution is the same as the unconstrained MDP, and the budget only matters when it becomes binding.

Sources