CSE-41XX
CS-4101 AI

Lecture 10.2: MDPs and Reinforcement Learning

Understanding Markov Decision Processes, Bellman Equations, Value Iteration, Multiagent MDPs, and Reinforcement Learning.

In traditional supervised machine learning, models require massive amounts of labeled data. Collecting such data is often expensive, and real-world datasets may contain missing, noisy, or untrustworthy values.

Reinforcement Learning (RL) overcomes data collection constraints by enabling an agent to learn directly through trial-and-error interactions with an environment, receiving positive rewards for effective actions and negative rewards for undesirable outcomes.


Markov Decision Processes (MDPs)

A Markov Decision Process (MDP) provides the formal mathematical foundation for decision-making under uncertainty in discrete time steps.

The Markov Property

An MDP assumes the Markov Property (memorylessness): the probability of reaching the next state depends only on the current state and action, not on the sequence of preceding states or past history: P(St+1St,St1,,S0)=P(St+1St)P(S_{t+1} \mid S_t, S_{t-1}, \dots, S_0) = P(S_{t+1} \mid S_t)

Why is the Markov Property important?

  • Efficient Computation: Eliminates the need to store or process full state histories.
  • Mathematical Tractability: Enables recursive algorithms (like Value Iteration and Dynamic Programming).
  • RL Foundation: Forms the backbone of both model-based and model-free reinforcement learning algorithms.

Formal MDP Definition

An MDP is formally specified by a tuple (S,A,T,r,s1)(S, A, T, r, s_1):

  1. States (SS): The set of all valid environmental states.
  2. Actions (AA): The set of all possible actions.
  3. Transition Function (T(s,a,s)T(s, a, s')): The probability distribution of transitioning to state ss' after taking action aa in state ss.
  4. Reward Function (r:SRr: S \rightarrow \mathbb{R}): The immediate scalar reward received for being in state ss.
  5. Start State (s1Ss_1 \in S): The initial starting state of the process.

Concrete 4-State MDP Example

Consider a sample MDP with four states S={s1,s2,s3,s4}S = \{s_1, s_2, s_3, s_4\}, start state s1s_1, and actions A={a1,a2,a3,a4}A = \{a_1, a_2, a_3, a_4\}.

Reward & Transition Tables

State (ss)Reward (r(s)r(s))
s1s_100
s2s_200
s3s_311 (Goal)
s4s_400
From (sis_i)Action (aa)To (sjs_j)T(si,a,sj)T(s_i, a, s_j)
s1s_1a1a_1s1s_10.20.2
s1s_1a1a_1s2s_20.80.8
s1s_1a2a_2s1s_10.20.2
s1s_1a2a_2s4s_40.80.8
s2s_2a2a_2s2s_20.20.2
s2s_2a2a_2s3s_30.80.8
s2s_2a3a_3s2s_20.20.2
s2s_2a3a_3s1s_10.80.8
s3s_3a4a_4s2s_21.01.0
s3s_3a3a_3s4s_41.01.0
s4s_4a1a_1s4s_40.10.1
s4s_4a1a_1s3s_30.90.9
s4s_4a4a_4s4s_40.20.2
s4s_4a4a_4s1s_10.80.8

Policy and Discounted Future Rewards

The Policy Dilemma

Should an agent take 100 actions with no reward to reach a state with a massive reward of 100100, or take 100 actions that each yield a reward of 1?

Key trade-offs in defining agent policies:

  1. Agents don't live forever: Waiting too many steps for a distant reward introduces survival risk.
  2. Immediate rewards are safer: Immediate rewards are more reliable than uncertain future possibilities.
  3. Delaying gratification: Delaying gratification can be intelligent—but only up to a reasonable point.

Discounted Return & The Bellman Equation

To model preference for earlier rewards, we apply a Discount Factor (γ(0,1)\gamma \in (0, 1)) to future rewards. The total discounted return over an infinite sequence of states is: Discounted Return=γ0r(s1)+γ1r(s2)+γ2r(s3)+=t=0γtr(st)\text{Discounted Return} = \gamma^0 r(s_1) + \gamma^1 r(s_2) + \gamma^2 r(s_3) + \dots = \sum_{t=0}^{\infty} \gamma^t r(s_t)

The long-term utility u(s)u(s) of being in state ss under an optimal policy satisfies the Bellman Equation: u(s)=r(s)+γmaxaAsST(s,a,s)u(s)u(s) = r(s) + \gamma \max_{a \in A} \sum_{s' \in S} T(s, a, s') u(s')

System of Bellman Equations

For an environment with nn states, there are nn Bellman equations with nn unknown utility variables.

  • Why can't we solve this directly with matrix algebra? The presence of the non-linear maxa\max_{a} operator prevents direct linear algebra solutions. We must use dynamic programming algorithms like Value Iteration.

The Value Iteration Algorithm

Value Iteration starts with initial arbitrary utility estimates u0(s)u^0(s) and iteratively updates state utilities over time steps tt using the Bellman Update Equation: ut+1(s)r(s)+γmaxaAsST(s,a,s)ut(s)u^{t+1}(s) \leftarrow r(s) + \gamma \max_{a \in A} \sum_{s' \in S} T(s, a, s') u^t(s')

Stopping Criterion & Error Bound (ϵ\epsilon)

Iterations continue until the maximum change in utility across all states in a time step falls below a threshold: maxsSut+1(s)ut(s)<ϵ1γγ\max_{s \in S} |u^{t+1}(s) - u^t(s)| < \epsilon \frac{1 - \gamma}{\gamma} When this holds, the error of the current utility estimates relative to true optimal values is guaranteed to be less than ϵ\epsilon.

Role of Parameters (ϵ\epsilon and γ\gamma)

  • User Error Threshold (ϵ\epsilon): Controls precision. A smaller ϵ\epsilon yields higher accuracy but requires more iterations to converge.
  • High Discount Factor (γ1\gamma \to 1): Agent strongly values long-term future rewards. Slower convergence because tiny changes in distant states propagate across many iterations.
  • Low Discount Factor (γ1\gamma \ll 1): Agent prioritizes immediate rewards. Faster convergence because distant rewards decay rapidly.

[!WARNING] Stopping Criteria Nuances

  1. No Absolute Guarantees: The utility difference criterion signals that further updates yield minimal change, but doesn't guarantee discovering absolute optimal policy values in non-convex or high-dimensional approximations.
  2. Local Optima: The algorithm may stabilize at local optima in complex domains.
  3. Combined Criteria: In practice, value iteration is often combined with a maximum iteration count limit and environmental performance checks.

Value Iteration Algorithm Pseudocode & Execution Trace

VALUE-ITERATION(T, r, gamma, epsilon)
1  do
2      u <- u'
3      delta <- 0
4      for each s in S do
5          u'(s) <- r(s) + gamma * max_a sum_{s'} T(s, a, s') * u(s')
6          if |u'(s) - u(s)| > delta then
7              delta <- |u'(s) - u(s)|
8  until delta < epsilon * (1 - gamma) / gamma
9  return u

Solved Example: Value Iteration

Problem Statement

Consider a Markov Decision Process (MDP) defined by state space S={s1,s2,s3,s4}S = \{s_1, s_2, s_3, s_4\}, action space A={a1,a2,a3,a4}A = \{a_1, a_2, a_3, a_4\}, and discount factor γ=0.5\gamma = 0.5.

  • State Rewards: r(s1)=0r(s_1) = 0, r(s2)=0r(s_2) = 0, r(s3)=1r(s_3) = 1 (Goal State), r(s4)=0r(s_4) = 0
  • Transition Probabilities T(s,a,s)T(s, a, s'):
From (ss)Action (aa)To (ss')Probability T(s,a,s)T(s, a, s')
s1s_1a1a_1s1s_10.20.2
s1s_1a1a_1s2s_20.80.8
s1s_1a2a_2s1s_10.20.2
s1s_1a2a_2s4s_40.80.8
s2s_2a2a_2s2s_20.20.2
s2s_2a2a_2s3s_30.80.8
s2s_2a3a_3s2s_20.20.2
s2s_2a3a_3s1s_10.80.8
s3s_3a4a_4s2s_21.01.0
s3s_3a3a_3s4s_41.01.0
s4s_4a1a_1s4s_40.10.1
s4s_4a1a_1s3s_30.90.9
s4s_4a4a_4s4s_40.20.2
s4s_4a4a_4s1s_10.80.8

Tasks:

  1. Compute state utility values ut(s)u^t(s) for iterations t=0,1,2,3,4t = 0, 1, 2, 3, 4 using Value Iteration starting with u0(s)=0u^0(s) = 0. Show step-by-step action evaluations.
  2. Extract the optimal policy π(s)\pi^*(s) for all states.

Step-by-Step Value Iteration Solution

Iteration t=0t = 0 (Initialization)

Initial state utility estimates are initialized to zero: u0(s1)=0.000,u0(s2)=0.000,u0(s3)=0.000,u0(s4)=0.000u^0(s_1) = 0.000, \quad u^0(s_2) = 0.000, \quad u^0(s_3) = 0.000, \quad u^0(s_4) = 0.000


Iteration t=1t = 1

Update formula: u1(s)=r(s)+0.5maxasT(s,a,s)u0(s)u^1(s) = r(s) + 0.5 \max_{a} \sum_{s'} T(s, a, s') u^0(s')

  • For State s1s_1 (r=0r=0):

    • Action a1a_1: 0.2(0)+0.8(0)=0.0000.2(0) + 0.8(0) = 0.000
    • Action a2a_2: 0.2(0)+0.8(0)=0.0000.2(0) + 0.8(0) = 0.000     u1(s1)=0+0.5(0.000)=0.000\implies u^1(s_1) = 0 + 0.5(0.000) = 0.000
  • For State s2s_2 (r=0r=0):

    • Action a2a_2: 0.2(0)+0.8(0)=0.0000.2(0) + 0.8(0) = 0.000
    • Action a3a_3: 0.2(0)+0.8(0)=0.0000.2(0) + 0.8(0) = 0.000     u1(s2)=0+0.5(0.000)=0.000\implies u^1(s_2) = 0 + 0.5(0.000) = 0.000
  • For State s3s_3 (r=1r=1):

    • Action a4a_4: 1.0(0)=0.0001.0(0) = 0.000
    • Action a3a_3: 1.0(0)=0.0001.0(0) = 0.000     u1(s3)=1+0.5(0.000)=1.000\implies u^1(s_3) = 1 + 0.5(0.000) = 1.000
  • For State s4s_4 (r=0r=0):

    • Action a1a_1: 0.1(0)+0.9(0)=0.0000.1(0) + 0.9(0) = 0.000
    • Action a4a_4: 0.2(0)+0.8(0)=0.0000.2(0) + 0.8(0) = 0.000     u1(s4)=0+0.5(0.000)=0.000\implies u^1(s_4) = 0 + 0.5(0.000) = 0.000

Iteration t=2t = 2

Update formula: u2(s)=r(s)+0.5maxasT(s,a,s)u1(s)u^2(s) = r(s) + 0.5 \max_{a} \sum_{s'} T(s, a, s') u^1(s')

  • For State s1s_1 (r=0r=0):

    • Action a1a_1: 0.2u1(s1)+0.8u1(s2)=0.2(0)+0.8(0)=0.0000.2 u^1(s_1) + 0.8 u^1(s_2) = 0.2(0) + 0.8(0) = 0.000
    • Action a2a_2: 0.2u1(s1)+0.8u1(s4)=0.2(0)+0.8(0)=0.0000.2 u^1(s_1) + 0.8 u^1(s_4) = 0.2(0) + 0.8(0) = 0.000     u2(s1)=0+0.5(0.000)=0.000\implies u^2(s_1) = 0 + 0.5(0.000) = 0.000
  • For State s2s_2 (r=0r=0):

    • Action a2a_2: 0.2u1(s2)+0.8u1(s3)=0.2(0)+0.8(1.000)=0.8000.2 u^1(s_2) + 0.8 u^1(s_3) = 0.2(0) + 0.8(1.000) = 0.800 Max\leftarrow \text{Max}
    • Action a3a_3: 0.2u1(s2)+0.8u1(s1)=0.2(0)+0.8(0)=0.0000.2 u^1(s_2) + 0.8 u^1(s_1) = 0.2(0) + 0.8(0) = 0.000     u2(s2)=0+0.5(0.800)=0.400\implies u^2(s_2) = 0 + 0.5(0.800) = 0.400
  • For State s3s_3 (r=1r=1):

    • Action a4a_4: 1.0u1(s2)=1.0(0)=0.0001.0 u^1(s_2) = 1.0(0) = 0.000
    • Action a3a_3: 1.0u1(s4)=1.0(0)=0.0001.0 u^1(s_4) = 1.0(0) = 0.000     u2(s3)=1+0.5(0.000)=1.000\implies u^2(s_3) = 1 + 0.5(0.000) = 1.000
  • For State s4s_4 (r=0r=0):

    • Action a1a_1: 0.1u1(s4)+0.9u1(s3)=0.1(0)+0.9(1.000)=0.9000.1 u^1(s_4) + 0.9 u^1(s_3) = 0.1(0) + 0.9(1.000) = 0.900 Max\leftarrow \text{Max}
    • Action a4a_4: 0.2u1(s4)+0.8u1(s1)=0.2(0)+0.8(0)=0.0000.2 u^1(s_4) + 0.8 u^1(s_1) = 0.2(0) + 0.8(0) = 0.000     u2(s4)=0+0.5(0.900)=0.450\implies u^2(s_4) = 0 + 0.5(0.900) = 0.450

Iteration t=3t = 3

Update formula: u3(s)=r(s)+0.5maxasT(s,a,s)u2(s)u^3(s) = r(s) + 0.5 \max_{a} \sum_{s'} T(s, a, s') u^2(s')

  • For State s1s_1 (r=0r=0):

    • Action a1a_1: 0.2(0)+0.8(0.400)=0.3200.2(0) + 0.8(0.400) = 0.320
    • Action a2a_2: 0.2(0)+0.8(0.450)=0.3600.2(0) + 0.8(0.450) = 0.360 Max\leftarrow \text{Max}     u3(s1)=0+0.5(0.360)=0.180\implies u^3(s_1) = 0 + 0.5(0.360) = 0.180
  • For State s2s_2 (r=0r=0):

    • Action a2a_2: 0.2(0.400)+0.8(1.000)=0.080+0.800=0.8800.2(0.400) + 0.8(1.000) = 0.080 + 0.800 = 0.880 Max\leftarrow \text{Max}
    • Action a3a_3: 0.2(0.400)+0.8(0)=0.0800.2(0.400) + 0.8(0) = 0.080     u3(s2)=0+0.5(0.880)=0.440\implies u^3(s_2) = 0 + 0.5(0.880) = 0.440
  • For State s3s_3 (r=1r=1):

    • Action a4a_4: 1.0(0.400)=0.4001.0(0.400) = 0.400
    • Action a3a_3: 1.0(0.450)=0.4501.0(0.450) = 0.450 Max\leftarrow \text{Max}     u3(s3)=1+0.5(0.450)=1.225\implies u^3(s_3) = 1 + 0.5(0.450) = 1.225
  • For State s4s_4 (r=0r=0):

    • Action a1a_1: 0.1(0.450)+0.9(1.000)=0.045+0.900=0.9450.1(0.450) + 0.9(1.000) = 0.045 + 0.900 = 0.945 Max\leftarrow \text{Max}
    • Action a4a_4: 0.2(0.450)+0.8(0)=0.0900.2(0.450) + 0.8(0) = 0.090     u3(s4)=0+0.5(0.945)=0.473\implies u^3(s_4) = 0 + 0.5(0.945) = 0.473

Iteration t=4t = 4

Update formula: u4(s)=r(s)+0.5maxasT(s,a,s)u3(s)u^4(s) = r(s) + 0.5 \max_{a} \sum_{s'} T(s, a, s') u^3(s')

  • For State s1s_1 (r=0r=0):

    • Action a1a_1: 0.2(0.180)+0.8(0.440)=0.036+0.352=0.3880.2(0.180) + 0.8(0.440) = 0.036 + 0.352 = 0.388
    • Action a2a_2: 0.2(0.180)+0.8(0.473)=0.036+0.378=0.4140.2(0.180) + 0.8(0.473) = 0.036 + 0.378 = 0.414 Max\leftarrow \text{Max}     u4(s1)=0+0.5(0.414)=0.207\implies u^4(s_1) = 0 + 0.5(0.414) = 0.207
  • For State s2s_2 (r=0r=0):

    • Action a2a_2: 0.2(0.440)+0.8(1.225)=0.088+0.980=1.0680.2(0.440) + 0.8(1.225) = 0.088 + 0.980 = 1.068 Max\leftarrow \text{Max}
    • Action a3a_3: 0.2(0.440)+0.8(0.180)=0.088+0.144=0.2320.2(0.440) + 0.8(0.180) = 0.088 + 0.144 = 0.232     u4(s2)=0+0.5(1.068)=0.534\implies u^4(s_2) = 0 + 0.5(1.068) = 0.534
  • For State s3s_3 (r=1r=1):

    • Action a4a_4: 1.0(0.440)=0.4401.0(0.440) = 0.440
    • Action a3a_3: 1.0(0.473)=0.4731.0(0.473) = 0.473 Max\leftarrow \text{Max}     u4(s3)=1+0.5(0.473)=1.236\implies u^4(s_3) = 1 + 0.5(0.473) = 1.236
  • For State s4s_4 (r=0r=0):

    • Action a1a_1: 0.1(0.473)+0.9(1.225)=0.0473+1.1025=1.1500.1(0.473) + 0.9(1.225) = 0.0473 + 1.1025 = 1.150 Max\leftarrow \text{Max}
    • Action a4a_4: 0.2(0.473)+0.8(0.180)=0.0946+0.1440=0.2390.2(0.473) + 0.8(0.180) = 0.0946 + 0.1440 = 0.239     u4(s4)=0+0.5(1.150)=0.575\implies u^4(s_4) = 0 + 0.5(1.150) = 0.575

Summary Value Iteration Matrix Table

State (ss)t=0t=0t=1t=1t=2t=2t=3t=3t=4t=4
u(s1)u(s_1)0.0000.0000.0000.0000.0000.0000.1800.1800.2070.207
u(s2)u(s_2)0.0000.0000.0000.0000.4000.4000.4400.4400.5340.534
u(s3)u(s_3)0.0000.0001.0001.0001.0001.0001.2251.2251.2361.236
u(s4)u(s_4)0.0000.0000.0000.0000.4500.4500.4730.4730.5750.575

Extracted Optimal Policy (π(s)\pi^*(s))

Evaluating argmaxasT(s,a,s)u4(s)\arg\max_{a} \sum_{s'} T(s, a, s') u^4(s') at each state gives the optimal policy:

  • π(s1)=argmax{a1:0.388,a2:0.414}=a2\pi^*(s_1) = \arg\max \{ a_1: 0.388, \mathbf{a_2: 0.414} \} = a_2
  • π(s2)=argmax{a2:1.068,a3:0.232}=a2\pi^*(s_2) = \arg\max \{ \mathbf{a_2: 1.068}, a_3: 0.232 \} = a_2
  • π(s3)=argmax{a4:0.440,a3:0.473}=a3\pi^*(s_3) = \arg\max \{ a_4: 0.440, \mathbf{a_3: 0.473} \} = a_3
  • π(s4)=argmax{a1:1.150,a4:0.239}=a1\pi^*(s_4) = \arg\max \{ \mathbf{a_1: 1.150}, a_4: 0.239 \} = a_1

π(s1)=a2,π(s2)=a2,π(s3)=a3,π(s4)=a1\pi^*(s_1) = a_2, \quad \pi^*(s_2) = a_2, \quad \pi^*(s_3) = a_3, \quad \pi^*(s_4) = a_1


Multiagent Markov Decision Processes (MMDPs)

When an environment contains multiple interacting agents, single-agent MDP definitions must be extended:

  1. Naive Simplification (Environment Embedding): Fold all other agents' behaviors into the environment transition function T(s,a,s)T(s, a, s'), assuming other agents are non-adaptive background elements. Drawback: Rarely works because other agents adapt their strategies over time, violating the fixed transition function requirement of MDPs.
  2. Joint Action MDPs (Better Method): Extend transitions to take a Joint Action Vector a=(a1,a2,,an)\vec{a} = (a_1, a_2, \dots, a_n), where each element represents the action of agent ii: T(s,a,s)T(s, \vec{a}, s')
  3. Reward Allocation Strategies:
    • Equal Division: Split collective system rewards equally among all agents.
    • Marginal Contribution: Assign rewards proportional to each agent's individual contribution to the total system payoff.

Reinforcement Learning Taxonomy & Trade-offs

If environmental dynamics T(s,a,s)T(s, a, s') and reward function r(s)r(s) are unknown, the agent must use Reinforcement Learning (RL).

Key RL Trade-offs

Trade-off DimensionModel-Free RLModel-Based RL
Sample Efficiency vs. ComputationMore sample-efficient for learning policies directly, but computationally heavy during execution.Highly computationally efficient for planning once a model is learned, but requires substantial data samples to learn an accurate environment model.
Environmental FlexibilityHighly adaptable to changes in environment physics without explicitly rebuilding world models.Requires relearning transition and reward models if environment mechanics change.

On-Policy vs. Off-Policy Reinforcement Learning

A critical distinction in model-free RL is how the agent handles experience generation versus policy optimization:

  • Behavior Policy (μ\mu or πb\pi_b): The policy used by the agent to interact with the environment, select actions, and collect experience tuples (s,a,r,s)(s, a, r, s'). This policy often includes exploration mechanisms (e.g., ϵ\epsilon-greedy).
  • Target Policy (π\pi or πt\pi_t): The policy that the agent is evaluating and attempting to optimize into the optimal strategy π\pi^*.

1. On-Policy Learning (e.g., SARSA)

In On-Policy methods, the target policy is identical to the behavior policy (πt=πb\pi_t = \pi_b). The agent learns the value of the policy it is actively executing. SARSA Update: Q(s,a)Q(s,a)+α[r+γQ(s,a)Q(s,a)]\text{SARSA Update: } Q(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma Q(s', a') - Q(s, a) \right] where aa' is the actual next action chosen by the current behavior policy in state ss'.

  • Characteristics: Conservative and safe. If the behavior policy makes random exploratory mistakes near dangerous states, SARSA lowers the Q-value of those states to reflect the risk of exploration.

2. Off-Policy Learning (e.g., Q-Learning)

In Off-Policy methods, the target policy is independent of the behavior policy (πtπb\pi_t \neq \pi_b). The agent learns the optimal target policy (greedy action selection) while using a different, exploratory behavior policy to gather data. Q-Learning Update: Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]\text{Q-Learning Update: } Q(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma \max_{a'} Q(s', a') - Q(s, a) \right]

  • Characteristics: Evaluates the best possible next action maxaQ(s,a)\max_{a'} Q(s', a'), regardless of which action the behavior policy actually selects next. This allows learning optimal control directly from historical logs, random exploration, or offline data.
Comparison DimensionOn-Policy (SARSA)Off-Policy (Q-Learning)
Target PolicySame as Behavior Policy (πt=πb\pi_t = \pi_b)Optimal Greedy Policy (πt=argmaxaQ(s,a)\pi_t = \arg\max_{a} Q(s, a))
Experience SourceMust be generated live by current policyCan be generated by random exploration, historical logs, or replay buffers
Update Functionr+γQ(s,a)r + \gamma Q(s', a') (uses actual sampled aa')r+γmaxaQ(s,a)r + \gamma \max_{a'} Q(s', a') (uses maximum Q-value at ss')
Risk SensitivityAccounts for exploration risks (e.g. cliff walking safety)Ignores exploration risk, learns true optimal path assuming non-exploratory execution

Q-Learning (Off-Policy Model-Free RL)

Q-learning is an off-policy, model-free, value-based algorithm. The "Q" stands for Quality—representing how valuable taking a specific action is in maximizing long-term cumulative rewards.

The Q-Learning Update Rule

For experience tuple (s,a,r,s)(s, a, r, s'): Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma \max_{a'} Q(s', a') - Q(s, a) \right]

where:

  • α(0,1]\alpha \in (0, 1] is the Learning Rate.
  • γ[0,1]\gamma \in [0, 1] is the Discount Factor.
  • r+γmaxaQ(s,a)Q(s,a)r + \gamma \max_{a'} Q(s', a') - Q(s, a) is the Temporal Difference (TD) Error.

Concrete 3x3 Grid World Q-Learning Example

Consider a 3×33 \times 3 grid world navigation problem with 9 discrete states S={(1,1),(1,2),,(3,3)}S = \{(1,1), (1,2), \dots, (3,3)\} and 4 movement actions A={Left,Right,Up,Down}A = \{\text{Left}, \text{Right}, \text{Up}, \text{Down}\}.

(1,1)
Clear (+0)
(1,2)
Clear (+0)
(1,3)
Goal (+5)
(2,1)
Clear (+0)
(2,2)
Danger (-10)
(2,3)
Clear (+0)
(3,1)
Clear (+0)
(3,2)
Start
(3,3)
Clear (+0)

Environment Specification

  • State Space (9 states): Row-column coordinates (x,y)(x, y) where x{1,2,3}x \in \{1, 2, 3\} (row) and y{1,2,3}y \in \{1, 2, 3\} (column).
  • Action Space (4 actions): {Left,Right,Up,Down}\{\text{Left}, \text{Right}, \text{Up}, \text{Down}\}.
  • Special States & Rewards:
    • Goal State (1,3)(1, 3): Immediate reward r=+5r = +5 (Terminal).
    • Danger State (2,2)(2, 2): Immediate penalty r=10r = -10.
    • Clear State (1,1)(1, 1): Neutral reward r=+0r = +0.
    • Start State (3,2)(3, 2): Initial position of agent.
  • Q-Table Matrix Size: 9 states×4 actions=369 \text{ states} \times 4 \text{ actions} = 36 state-action entries.

Initial Q-Table (All Values Initialized to 0)

State (ss)LeftRightUpDown
(1,1)(1,1)00000000
(1,2)(1,2)00000000
(1,3)(1,3)00000000
(2,1)(2,1)00000000
(2,2)(2,2)00000000
(2,3)(2,3)00000000
(3,1)(3,1)00000000
(3,2)(3,2) (Start)00000000
(3,3)(3,3)00000000

[!NOTE] State-Action Entry Interpretation The table cell at row (3,2)(3, 2) and column Up\text{Up} stores Q((3,2),Up)Q((3, 2), \text{Up}), which represents the agent's estimated long-term reward for taking action Up\text{Up} when standing at state (3,2)(3, 2).


Step-by-Step Q-Learning Execution Trace (α=0.5\alpha = 0.5, γ=0.9\gamma = 0.9)

Step 1: Moving Up into Danger from Start (3,2)(3,2)
  1. Current State: s=(3,2)s = (3, 2)
  2. Selected Action: a=Upa = \text{Up}
  3. Next State: s=(2,2)s' = (2, 2) (Danger Cell)
  4. Observed Reward: r=10r = -10
  5. Calculate Max Future Value: maxaQ((2,2),a)=0\max_{a'} Q((2,2), a') = 0 (all Q-values initially 0)
  6. Temporal Difference Error: TD Error=r+γmaxaQ(s,a)Q(s,a)=10+0.9(0)0=10\text{TD Error} = r + \gamma \max_{a'} Q(s', a') - Q(s, a) = -10 + 0.9(0) - 0 = -10
  7. Q-Table Update: Q((3,2),Up)Q((3,2),Up)+αTD Error=0+0.5(10)=5.0Q((3,2), \text{Up}) \leftarrow Q((3,2), \text{Up}) + \alpha \cdot \text{TD Error} = 0 + 0.5(-10) = -5.0
Step 2: Reaching Goal from (2,3)(2,3)
  1. Current State: s=(2,3)s = (2, 3)
  2. Selected Action: a=Upa = \text{Up}
  3. Next State: s=(1,3)s' = (1, 3) (Goal Cell)
  4. Observed Reward: r=+5r = +5
  5. Calculate Max Future Value: maxaQ((1,3),a)=0\max_{a'} Q((1,3), a') = 0
  6. Temporal Difference Error: TD Error=5+0.9(0)0=+5\text{TD Error} = 5 + 0.9(0) - 0 = +5
  7. Q-Table Update: Q((2,3),Up)0+0.5(+5)=+2.5Q((2,3), \text{Up}) \leftarrow 0 + 0.5(+5) = +2.5
Step 3: Backward Propagation of Value from (3,3)(3,3)
  1. Current State: s=(3,3)s = (3, 3)
  2. Selected Action: a=Upa = \text{Up}
  3. Next State: s=(2,3)s' = (2, 3)
  4. Observed Reward: r=0r = 0
  5. Calculate Max Future Value: maxaQ((2,3),a)=Q((2,3),Up)=+2.5\max_{a'} Q((2,3), a') = Q((2,3), \text{Up}) = +2.5
  6. Temporal Difference Error: TD Error=0+0.9(2.5)0=+2.25\text{TD Error} = 0 + 0.9(2.5) - 0 = +2.25
  7. Q-Table Update: Q((3,3),Up)0+0.5(2.25)=+1.125Q((3,3), \text{Up}) \leftarrow 0 + 0.5(2.25) = +1.125

Updated Q-Table (After Steps 1–3)

State (ss)LeftRightUpDown
(2,3)(2,3)0000+2.5+2.500
(3,2)(3,2) (Start)00005.0-5.000
(3,3)(3,3)0000+1.125+1.12500

Real-World Off-Policy Applications

  • Robot Maze Exploration: A robot uses a completely random exploration policy (behavior policy) to explore a maze, while using recorded experience buffer tuples to train a goal-directed optimal policy (target policy).
  • Advertisement Recommendation Systems: E-commerce systems monitor random user browsing logs to learn an optimal ad targeting strategy off-policy without degrading live user experience.

Exploration vs. Exploitation (ϵ\epsilon-Greedy Policy)

To balance trying new actions with selecting known high-value actions, the agent chooses actions during training using an ϵ\epsilon-greedy decision rule: Generate a uniform random number p[0,1]p \in [0, 1] at each step: π(as)={Random action aAif p<ϵ(Exploration)argmaxaQ(s,a)if pϵ(Exploitation)\pi(a \mid s) = \begin{cases} \text{Random action } a \in A & \text{if } p < \epsilon \quad (\text{Exploration}) \\ \arg\max_{a} Q(s, a) & \text{if } p \ge \epsilon \quad (\text{Exploitation}) \end{cases}

On this page