Knowledge / Artificial Intelligence and Data Science / Reinforcement learning basics

Reinforcement learning basics

Artificial Intelligence and Data Science (B.E./B.Tech) · Machine learning · free study notes for exams, placements and interviews

In short

Reinforcement learning (RL) is the branch of machine learning where an agent learns by acting in an environment and receiving numeric rewards, with no labelled dataset telling it the right action. The formal model is the Markov decision process, and the goal is a policy that maximises expected cumulative discounted reward. It is examined in GATE Data Science & AI (DA), the AI/DS papers of Anna University, VTU, SPPU, AKTU and the AICTE model curriculum, and appears constantly in placement interviews for recommender, pricing, logistics and robotics teams. Indian users include Flipkart and Myntra (ranking and pricing experiments), Swiggy and Zomato (delivery-partner allocation), Ola and Uber (surge and dispatch), Jio and Airtel (network and offer optimisation) and research groups at IISc, IITs, Microsoft Research India and Google DeepMind India.

Explained

What RL is, and how it differs from supervised and unsupervised learning

SupervisedUnsupervisedReinforcement
Training signalCorrect label (x, y)None - structure onlyScalar reward, possibly delayed
Data sourceFixed datasetFixed datasetGenerated by the agent's own actions
ObjectiveMinimise prediction lossCompression / groupingMaximise expected return
Typical outputClassifier / regressorClusters, componentsPolicy π(a given s)

The Markov decision process (MDP)

Dynamic programming: solving a known MDP

Model-free prediction and control: MC, TD, SARSA, Q-learning

Function approximation and deep RL

Reward design, evaluation and safety

Key points

Formulas and examples

Return (discounted): Gₜ = Σ_{k=0}^{∞} γk R_{t+k+1} (γ dimensionless, 0 ≤ γ < 1; R in reward units - rupees, points, seconds saved)
State value: Vπ(s) = Eπ[Gₜ given Sₜ = s] Action value: Qπ(s,a) = Eπ[Gₜ given Sₜ = s, Aₜ = a]
Bellman expectation (V): Vπ(s) = Σₐ π(a given s) Σ_{s′} P(s′ given s,a) [ R(s,a,s′) + γ Vπ(s′) ]
Bellman optimality (V): V\*(s) = maxₐ Σ_{s′} P(s′ given s,a) [ R(s,a,s′) + γ V\*(s′) ]
Bellman optimality (Q): Q\*(s,a) = Σ_{s′} P(s′ given s,a) [ R(s,a,s′) + γ max_{a′} Q\*(s′,a′) ]
Monte Carlo update: V(Sₜ) ← V(Sₜ) + α [ Gₜ − V(Sₜ) ] (α = learning rate, 0 < α ≤ 1)
TD(0) update: V(Sₜ) ← V(Sₜ) + α [ Rₜ₊₁ + γ V(Sₜ₊₁) − V(Sₜ) ]; TD error δₜ = Rₜ₊₁ + γV(Sₜ₊₁) − V(Sₜ)
SARSA: Q(Sₜ,Aₜ) ← Q(Sₜ,Aₜ) + α [ Rₜ₊₁ + γ Q(Sₜ₊₁,Aₜ₊₁) − Q(Sₜ,Aₜ) ]
Q-learning: Q(Sₜ,Aₜ) ← Q(Sₜ,Aₜ) + α [ Rₜ₊₁ + γ max_{a′} Q(Sₜ₊₁,a′) − Q(Sₜ,Aₜ) ]
ε-greedy: π(a given s) = 1 − ε + ε/|A| for the greedy action, ε/|A| otherwise
Softmax (Boltzmann): π(a given s) = exp(Q(s,a)/τ) / Σ_b exp(Q(s,b)/τ) (τ = temperature)
Policy gradient (REINFORCE): ∇θ J(θ) = E[ (Gₜ − b(Sₜ)) ∇θ ln π(Aₜ given Sₜ; θ) ]
Bandit regret after T pulls: Regret = T·μ\* − Σ_{t=1}^{T} E[μ_{Aₜ}] (μ\* = best arm's mean reward)

Example 1 - computing a discounted return. An ad-serving agent collects rewards (₹ profit per impression) of 2, 0, 3, 5 over four steps, γ = 0.9, then the episode ends.

Example 2 - infinite-horizon value of a constant reward. A running machine yields R = ₹10 every step for ever, γ = 0.95.

Example 3 - one TD(0) and one Q-learning update. Given α = 0.1, γ = 0.9, V(s) = 5, V(s′) = 8, observed R = 2.

Now Q-learning with Q(s, a₁) = 3, and next state s′ has Q(s′, up) = 6, Q(s′, down) = 9, R = 1, same α and γ.

Example 4 - value iteration on a two-state MDP. States A and B. In A: action *stay* gives R = 0 and returns to A; action *move* gives R = 1 and goes to B. B is terminal with V(B) = 0. γ = 0.9.

Example 5 - ε-greedy action probabilities and expected reward. Four actions, Q = (1.0, 4.0, 2.0, 3.0), ε = 0.2.

AlgorithmModel neededPolicy typeBootstrapsSuited to
Value / policy iterationYesOff-policy (planning)YesSmall known MDPs
Monte Carlo controlNoOn-policyNoEpisodic, high variance tolerable
SARSANoOn-policyYesOnline control where safety matters
Q-learningNoOff-policyYesLearning optimum while exploring
DQNNoOff-policyYesLarge state space, discrete actions
PPO / A2CNoOn-policyYes (critic)Continuous actions, robotics, RLHF
DDPG / TD3 / SACNoOff-policyYesContinuous control, sample-efficient
# Tabular Q-learning: the whole algorithm in a dozen lines
import numpy as np
Q = np.zeros((n_states, n_actions))
alpha, gamma, eps = 0.1, 0.9, 1.0
for ep in range(episodes):
    s, done = env.reset(), False
    while not done:
        a = env.action_space.sample() if np.random.rand() < eps else int(np.argmax(Q[s]))
        s2, r, done, _ = env.step(a)
        target = r + (0 if done else gamma * Q[s2].max())   # 0 at a terminal state
        Q[s, a] += alpha * (target - Q[s, a])
        s = s2
    eps = max(0.05, eps * 0.995)        # decay towards greedy (GLIE)
policy = Q.argmax(axis=1)

Where it is used

Interview questions

Common mistakes

Related topics

Machine learning workflow · Model evaluation metrics · Overfitting, bias-variance and regularisation · Deep learning and neural networks · Markov chains and stochastic processes · Operations research and dynamic programming

Open in the Knowledge reader →Find jobs →