Guide To AI LogoGuide To AI
Unit 18

Reinforcement Learning

learning optimal decision-making through interactions between agents and environments

Agent-Environment Reinforcement Loop
AGENTBrain / PolicyENVWorld / PhysicsAction (a_t)State & Reward (s_t, r_t)

Core Concepts Covered

  • Markov Decision Processes (MDPs) and Bellman equations
  • Value-based (Q-learning, DQN) and Policy-based methods
  • Actor-critic models and reinforcement learning feedback
Local Setup Recommendation

To execute and experiment with the code cells below on your local machine, ensure you have set up your isolated virtual environments and scientific libraries by following the detailed protocols in Unit 03: Environment Setup or run them in Google Colab.

1. Markov Decision Processes (MDPs)

In Reinforcement Learning (RL), we study how an autonomous decision-making Agent can learn to navigate an active Environment to maximize a cumulative mathematical reward signal over time. We formalize this sequential decision-making framework using Markov Decision Processes (MDPs).

An MDP is mathematically defined by a 5-tuple: (S,A,P,R,γ)(S, A, P, R, \gamma):

State Space (SS): The set of all possible situations or configurations of the environment.

Action Space (AA): The set of all possible choices or moves available to the agent.

Transition Probability (P(ss,a)P(s' | s, a)): The probability of transitioning to state ss' given that the agent takes action aa in state ss. This represents the dynamics (physics) of the world.

Reward Function (R(s,a,s)R(s, a, s')): The scalar feedback (reward or penalty) the environment returns after the agent takes a step.

Discount Factor (γ[0,1)\gamma \in [0, 1)): Determines how much the agent values immediate rewards compared to future rewards. A value of γ0\gamma \approx 0 makes the agent 'shortsighted' (focused only on immediate gains), while γ1\gamma \approx 1 makes it highly strategic. It also mathematically guarantees that infinite reward sum sequences converge to a finite limit.

Grid-World Navigation Example: Imagine a robot navigating a 3×33\times 3 grid trying to reach a gold chest (+10+10 reward) while avoiding a lava pit (10-10 penalty). The states SS are the 99 grid coordinates. The actions AA are {Up, Down, Left, Right}\{\text{Up, Down, Left, Right}\}. If the floor is slippery, there might only be an 80%80\% transition probability PP of moving in the commanded direction, with a 10%10\% chance of sliding sideways. The robot must learn a sequence of steps that minimizes the risk of slipping into the lava while reaching the gold!

2. Value Functions and the Bellman Optimality Equations

To decide on optimal actions, the agent must learn to estimate the long-term future value of its current choices. We define a Policy π(as)\pi(a | s) as the probability distribution of actions given the current state. We evaluate policies using two value functions:

State-Value Function (Vπ(s)V^{\pi}(s)): The expected cumulative discounted return starting in state ss and following policy π\pi thereafter.

Action-Value Function (Qπ(s,a)Q^{\pi}(s, a)): (Known as the Q-value). The expected cumulative discounted return starting in state ss, executing action aa, and following policy π\pi thereafter.

These functions satisfy recursive, self-consistent relationships called the Bellman Equations. The Bellman Optimality Equation for the optimal Q-value Q(s,a)Q^*(s, a) is:

Q(s,a)=R(s,a)+γsP(ss,a)maxaQ(s,a)Q^*(s, a) = R(s, a) + \gamma \sum_{s'} P(s' | s, a) \max_{a'} Q^*(s', a'),

Bellman Backup Example: Suppose we are in a state ss and take action aa. The immediate reward is R(s,a)=+2R(s, a) = +2. There is a 100%100\% chance (P=1.0P = 1.0) of transitioning to a next state ss'. In ss', the maximum possible Q-value for any future action is maxaQ(s,a)=10\max_{a'} Q(s', a') = 10. If our discount factor is γ=0.9\gamma = 0.9, the Bellman backup calculates our current Q-value as: Q(s,a)=2+0.9×(1.0×10)=2+9=11Q^*(s, a) = 2 + 0.9 \times (1.0 \times 10) = 2 + 9 = 11 This demonstrates beautifully how value information cascades backwards through time steps to guide the agent's current decisions!

3. Q-Learning, DQNs, and Policy Gradients

How do we solve optimal policies? We use different algorithm classes depending on our data structures:

Tabular Q-Learning: A model-free, off-policy temporal difference algorithm. It updates a table of Q-values Q(s,a)Q(s,a) iteratively without needing to know the environment's transition physics PP. The update rule is:

Q(s,a)Q(s,a)+α[R(s,a)+γmaxaQ(s,a)Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha \left[ R(s, a) + \gamma \max_{a'} Q(s', a') - Q(s, a) \right],

where α\alpha is the learning rate. To balance exploration, we use ϵ\epsilon-greedy selection: the agent takes a random exploratory action with probability ϵ\epsilon, and exploits its best-known action with probability 1ϵ1 - \epsilon.

Deep Q-Networks (DQN): For high-dimensional state spaces (like raw pixels in Atari games), maintaining a Q-table is impossible. DQNs solve this by replacing the lookup table with a Neural Network Q(s,a;θ)Q(s, a; \theta) to approximate optimal Q-values.

Policy Gradients & Actor-Critic: Instead of approximating Q-values, policy gradient methods directly parameterize and optimize the policy πθ(as)\pi_\theta(a|s) using gradient ascent. Actor-Critic architectures combine both: the Critic approximates value functions to evaluate actions, while the Actor updates the policy direction based on the Critic's feedback.

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

In Reinforcement Learning, what is the role of the Discount Factor γ\gamma?

QUESTION 02

Which of the following describes the off-policy Q-Learning update rule?

QUESTION 03

What is the purpose of the ϵ\epsilon-greedy strategy in reinforcement learning?

QUESTION 04

How do Deep Q-Networks (DQN) scale Q-Learning to high-dimensional environments (like Atari raw screen pixels)?

QUESTION 05

In Actor-Critic reinforcement learning systems, what is the role of the 'Critic'?

QUESTION 06

In Reinforcement Learning, what is a 'Policy' π(as)\pi(a|s)?

QUESTION 07

What is the key difference between the State-Value Function V(s)V(s) and the Action-Value Function Q(s,a)Q(s, a)?

QUESTION 08

Which of the following represents the 5-tuple defining a Markov Decision Process (MDP)?

QUESTION 09

Calculate the Q-value Q(s,a)Q^*(s, a) using the Bellman backup given: immediate reward R(s,a)=+3R(s, a) = +3, transition probability P(ss,a)=1.0P(s' | s, a) = 1.0 (deterministic transition), discount factor γ=0.95\gamma = 0.95, and max next-state Q-value maxaQ(s,a)=20\max_{a'} Q^*(s', a') = 20:

QUESTION 10

In Deep Reinforcement Learning (DQN), how are Q-values updated?