Reinforcement Learning: An Evolution from Games to Real-World Impact – Day 78

Reinforcement learning uses rewards from interaction to improve a policy. Q-learning estimates the return from a state–action pair and updates the estimate toward a reward plus a predicted future value.

–>

Reinforcement Learning: An Evolution from Games to Real-World Impact

Reinforcement Learning (RL) is a fascinating branch of machine learning, with its roots stretching back to the 1950s. Although not always in the limelight, RL made a significant impact in various domains, especially in gaming and machine control. In 2013, a team from DeepMind, a British startup, built a system capable of learning and excelling at Atari games using only raw pixels as input—without any knowledge of the game’s rules. This breakthrough led to DeepMind’s famous system, AlphaGo, defeating world Go champions and ignited a global interest in RL.

The Foundations of Reinforcement Learning: How It Works

In RL, an agent interacts with an environment, observes outcomes, and receives feedback through rewards. The agent’s objective is to maximize cumulative rewards over time, learning the best actions through trial and error.

Term Explanation
Agent The software or system making decisions.
Environment The external setting with which the agent interacts.
Reward Feedback from the environment based on the agent’s actions.

Examples of RL Applications

Here are a few tasks RL is well-suited for:

Application Agent Environment Reward
Robot Control Robot control program Real-world physical space Positive for approaching target, negative for incorrect movement
Atari Game Game-playing program Game simulation Points scored in the game
Board Game (e.g., Go) Game-playing program Game board Reward only if it wins the game
Smart Thermostat Thermostat control program Indoor temperature settings Positive for saving energy, negative if adjustments are needed by humans
Stock Market Trading agent Financial market data Monetary gains and losses

Defining Policy: The Agent’s Guide to Action

The approach or “policy” an agent uses to decide its actions is central to RL. A policy can be as straightforward as a set of if-then rules or as complex as a deep neural network that analyzes observations to output actions. Policies can be deterministic or stochastic (random). In training an agent, policy search methods help identify effective policies, often through trial and error.

Policy Search and Optimization Techniques

Method Description
Brute-Force Search Tries various policy parameters systematically, though it’s not feasible for complex policies.
Genetic Algorithms Generates multiple policies, selects the best, and iterates through generations with random mutations.
Policy Gradients (PG) Adjusts policy parameters by evaluating gradients based on rewards to optimize actions.

Getting Started with OpenAI Gym

Training RL agents requires environments where the agent can safely learn and test its policies. OpenAI Gym is a popular toolkit that offers simulated environments for RL training, from Atari games to physical simulations. By using OpenAI Gym, developers can safely experiment and refine RL agents without needing a physical setup.

Setting Up OpenAI Gym

To install and use OpenAI Gym on Colab or a local machine, developers can use the following commands:

# Upgrade Gym and install dependencies on Colab
%pip install -q -U gym
%pip install -q -U gym[classic_control,box2d,atari,accept-rom-license]

These commands install Gym and essential libraries for running classic and Atari environments.

Creating a CartPole Environment

In this 2D environment, a cart must balance a pole by deciding when to move left or right:

import gym
env = gym.make("CartPole-v1", render_mode="rgb_array")
obs, info = env.reset(seed=42)
print(obs)  # Initial observation

Here, obs is a 1D array representing the cart’s position and velocity, as well as the pole’s angle and angular velocity.

Observation Description
Horizontal Position Cart’s position relative to the center (0.0 = center).
Velocity Speed and direction of the cart.
Angle Pole’s tilt relative to vertical (0.0 = perfectly vertical).
Angular Velocity Speed and direction of the pole’s tilt.

Implementing a Simple Policy for CartPole

This policy directs the cart to move left or right based on the pole’s tilt angle:

def basic_policy(obs):
    angle = obs[2]
    return 0 if angle < 0 else 1

The policy adjusts based on the observation, keeping the pole balanced for as long as possible. After running multiple trials, we can evaluate its performance:

totals = []
for episode in range(500):
    episode_rewards = 0
    obs, info = env.reset(seed=episode)
    for step in range(200):
        action = basic_policy(obs)
        obs, reward, done, truncated, info = env.step(action)
        episode_rewards += reward
        if done or truncated:
            break
    totals.append(episode_rewards)

The results, such as the mean and standard deviation of rewards, indicate how well this simple policy performs.

–>

The RL Loop

At the core of RL is the interaction between the agent and the environment.

+---------+       Action        +-------------+
|         | --------------->    |             |
|  Agent  |                     | Environment |
|         |    <--------------- |             |
+---------+    Observation      +-------------+
                   (State, Reward)
    

Figure 1: The Agent-Environment Interaction Loop

  • Agent: Learns and decides which action to take.
  • Environment: Responds to the agent's actions and provides feedback.

Markov Decision Processes (MDPs)

An MDP provides a mathematical framework for modeling RL problems.

Components of an MDP

  1. States (S): All possible situations the agent can be in.
  2. Actions (A): All possible moves the agent can make.
  3. Transition Probability (P): Probability of moving from one state to another given an action.
  4. Reward Function (R): Feedback received after taking an action.
  5. Discount Factor (γ): Determines the importance of future rewards.

Value Functions and the Bellman Equations

Value functions estimate how good it is to be in a state or to perform an action in a state.

State-Value Function Vπ(s)

Vπ(s)=Eπ[∑t=0∞γtrt+1 | s0=s]

Vπ(s): Value of state s under policy π.

Eπ[⋅]: Expected value when following policy π.

∑t=0∞: Sum over all future time steps.

γt: Discount factor raised to the power t (0 ≤ γ ≤ 1).

rt+1: Reward at time t+1.

s0=s: Starting from state s at time t=0.

Action-Value Function Qπ(s,a)

Qπ(s,a)=Eπ[∑t=0∞γtrt+1 | s0=s,a0=a]

Qπ(s,a): Value of taking action a in state s under policy π.

a0=a: Taking action a at the initial time.

Other terms are as previously defined.

The Bellman Expectation Equation

For the state-value function:

Vπ(s)=∑a∈Aπ(a|s)∑s′∈SP(s′|s,a)[R(s,a)+γVπ(s′)]

Vπ(s): Value of state s under policy π.

π(a|s): Probability of taking action a in state s under policy π.

P(s′|s,a): Probability of transitioning to state s′ from s after action a.

R(s,a): Immediate reward after action a in state s.

Vπ(s′): Value of the next state s′.


Q-Learning and Temporal Difference Learning

Q-Learning is a model-free RL algorithm that seeks to learn the optimal policy.

Q-Learning Update Rule

Q(st,at)←Q(st,at)+α[rt+1+γmaxa′Q(st+1,a′)−Q(st,at)]

Q(st,at): Current estimate of the Q-value for state st and action at.

←: Update operation; assigns the new value to Q(st,at).

α: Learning rate (0<α≤1).

rt+1: Reward received after taking action at.

γ: Discount factor.

maxa′Q(st+1,a′): Maximum estimated Q-value for the next state st+1 over all possible actions a′.

Q(st,at): Current Q-value before the update.

Q-Learning Variables

Table 1: Q-Learning Variables
Symbol Description
st Current state
at Action taken at state st
rt+1 Reward received after action at
st+1 Next state after action at
α Learning rate
γ Discount factor
Q(st,at) Current Q-value estimate

Function Approximation with Neural Networks

When dealing with large or continuous state spaces, we use neural networks to approximate the Q-function.

Neural Network Approximation

[Input Layer]: State s_t
        |
[Hidden Layers]
        |
[Output Layer]: Q-values for all actions
    

Figure 2: Neural Network Approximation

  • Input: State representation.
  • Output: Estimated Q-values for possible actions.

Deep Q-Networks (DQN)

DQN combines Q-Learning with deep neural networks.

Key Components of DQN

  1. Experience Replay: Stores agent's experiences and samples mini-batches to break correlation.
  2. Target Network: A separate network to calculate target Q-values, improving stability.

DQN Loss Function

L(θ)=E(s,a,r,s′)[(y−Q(s,a;θ))2]

L(θ): Loss function to minimize during training.

E(s,a,r,s′)[⋅]: Expected value over the experience replay buffer containing tuples (s,a,r,s′).

y: Target Q-value.

Q(s,a;θ): Predicted Q-value from the network for state s and action a, parameterized by θ.

Where the target Q-value y is:

y=r+γmaxa′Q(s′,a′;θ−)

y: Target Q-value.

r: Reward received after taking action a in state s.

γ: Discount factor.

maxa′Q(s′,a′;θ−): Maximum predicted Q-value for the next state s′ over all actions a′, using the target network parameters θ−.

θ−: Parameters of the target network (a delayed copy of θ).


Policy Gradient Methods

Policy Gradient methods optimize the policy directly.

Policy Representation

πθ(a | s): Probability of taking action a in state s, parameterized by θ.

Objective Function

J(θ)=Eπθ[∑t=0∞γt rt+1]

J(θ): Performance measure of the policy.

Eπθ[⋅]: Expected reward under the policy πθ.

γt: Discount factor raised to the power t.

rt+1: Reward received at time t+1.


Actor-Critic Algorithms

Actor-Critic algorithms combine policy-based and value-based methods to improve stability and efficiency.

Actor Update

θactor←θactor+αactor⋅δt⋅∇θactorlogπθactor(at | st)

θactor: Parameters of the actor (policy) network.

αactor: Learning rate for the actor network.

δt: Temporal Difference (TD) error calculated as:

δt=rt+1+γVθcritic(st+1)−Vθcritic(st)

∇θactorlogπθactor(at | st): Gradient of the log-probability of the action taken with respect to the actor’s parameters.

Critic Update

θcritic←θcritic+αcritic⋅δt⋅∇θcriticVθcritic(st)

θcritic: Parameters of the critic (value function) network.

αcritic: Learning rate for the critic network.

δt: Temporal Difference error (as calculated above).

∇θcriticVθcritic(st): Gradient of the value function estimate with respect to the critic's parameters.


Advanced Techniques in Deep RL

Recent advances in RL leverage complex algorithms and architectures to improve performance in challenging environments.

Proximal Policy Optimization (PPO)

PPO improves training stability by limiting the extent to which policy updates can deviate. It applies a clipping function to the policy loss, constraining how much the updated policy can differ from the old policy.

Soft Actor-Critic (SAC)

SAC introduces an entropy term into the reward function, encouraging exploration by making the agent seek stochastic policies. The objective is to maximize both the reward and the policy’s entropy.


Applications of RL with Neural Networks

Gaming

  • AlphaGo: Used RL to achieve superhuman performance in the game of Go by combining deep neural networks with Monte Carlo tree search.
  • Atari Games: Achieved human-level performance on many Atari games using DQN, which learns directly from raw pixel inputs.

Robotics

  • Manipulation: RL helps robots learn to pick and manipulate objects with precision.
  • Locomotion: Agents use RL to learn movement patterns for walking, running, and navigating through obstacles.

Autonomous Vehicles

  • Navigation: RL helps autonomous vehicles make real-time decisions in complex, dynamic environments.

Conclusion

Reinforcement learning, especially when combined with neural networks, has transformed the field of AI, enabling machines to tackle complex tasks across multiple domains. Understanding the mathematical foundations and practical applications of RL helps us appreciate its potential and limitations as we continue to explore its capabilities in the real world.

Step Concept Details
1 Markov Decision Processes (MDP)

An MDP provides a mathematical framework for modeling decision-making where outcomes are partly random and partly under the control of a decision-maker.

Symbol Description
S Set of all possible states
A Set of all possible actions
P(s′|s,a) Probability of transitioning to state s′ from state s after taking action a
R(s,a) Expected reward received after taking action a in state s
γ Discount factor, 0≤γ≤1, which determines the importance of future rewards

Markov Property: The future state depends only on the current state and action, not on the sequence of events that preceded it.

2 Value Functions

Value functions estimate how good it is to be in a given state or to perform a specific action in a state, in terms of expected return (cumulative future rewards).

Function Definition
State-Value Function Vπ(s) Vπ(s)=Eπ[∑t=0∞γtR(st,at) | s0=s]
This represents the expected return starting from state s, and following policy π thereafter.
Action-Value Function Qπ(s,a) Qπ(s,a)=Eπ[∑t=0∞γtR(st,at) | s0=s,a0=a]
This represents the expected return starting from state s, taking action a, and thereafter following policy π.

Explanation: The expectation Eπ is taken over all possible sequences of future states and actions, weighted by the probability of their occurrence under policy π. The discount factor γ reduces the weight of future rewards exponentially, ensuring the sum converges for infinite horizons.

3 Bellman Equations

The Bellman equations provide recursive definitions for the value functions, expressing the value of a state (or state-action pair) in terms of the immediate reward and the value of successor states.

Equation Description
Vπ(s)=∑aπ(a|s)[R(s,a)+γ∑s′P(s′|s,a)Vπ(s′)]
Bellman Expectation Equation for the State-Value Function.
Explanation: The value of state s under policy π is the expected value of the immediate reward R(s,a) plus the discounted value of the next state Vπ(s′), averaged over all possible actions a and successor states s′.
Qπ(s,a)=R(s,a)+γ∑s′P(s′|s,a)∑a′π(a′|s′)Qπ(s′,a′)
Bellman Expectation Equation for the Action-Value Function.
Explanation: The value of taking action a in state s is the immediate reward plus the expected discounted value of the next state-action pair, where the next action a′ is chosen according to policy π.
4 Optimal Value Functions and Policies

Optimal value functions represent the maximum expected return achievable from any state or state-action pair, under any policy.

Function Definition
Optimal State-Value Function V*(s) V*(s)=maxπVπ(s)
The highest expected return achievable from state s by any policy.
Optimal Action-Value Function Q*(s,a) Q*(s,a)=maxπQπ(s,a)
The highest expected return achievable from state s and action a by any policy.

The Bellman Optimality Equations define these optimal value functions recursively:

Equation Description
V*(s)=maxa[R(s,a)+γ∑s′P(s′|s,a)V*(s′)]
Bellman Optimality Equation for V*(s).
Explanation: The optimal value of state s is the maximum expected return obtainable by choosing the best action a, considering both the immediate reward and the discounted optimal value of the next state s′.
Q*(s,a)=R(s,a)+γ∑s′P(s′|s,a)maxa′Q*(s′,a′)
Bellman Optimality Equation for Q*(s,a).
Explanation: The optimal value of taking action a in state s is the immediate reward plus the expected discounted optimal value of the best action a′ in the next state s′.
5 Policies

A policy π defines the agent's behavior by specifying the action to take in each state.

Type Definition
Deterministic Policy π(s)=a
Specifies a single action a to take in state s.
Stochastic Policy π(a|s)=P[At=a | St=s]
Gives a probability distribution over actions in state s.

Policy Improvement: The process of improving a policy by making it greedy with respect to the current value function.

6 Temporal Difference (TD) Learning

TD learning methods update estimates based on a combination of observed rewards and existing estimates.

Term Equation Explanation
TD Error δt δt=Rt+1+γV(St+1)−V(St) The difference between the predicted value and the observed value plus estimated value of the next state.
- Rt+1: Reward received after taking action at time t.
- γV(St+1): Discounted estimate of future value.
- V(St): Current estimate of the value of state St.
Value Update V(St)←V(St)+αδt Adjusts the value estimate of state St towards the target.
- α: Learning rate, determining the step size of the update.
7 Q-Learning

Q-Learning is an off-policy TD control algorithm that aims to learn the optimal action-value function Q*(s,a).

Update Rule Equation Explanation
Q-Value Update
Q(St,At)←Q(St,At)+α[Rt+1+γmaxa′Q(St+1,a′)−Q(St,At)]
- Updates the estimate of Q(St,At) using the reward Rt+1 and the maximum estimated future Q-value.
- α: Learning rate.
- γmaxa′Q(St+1,a′): Estimated optimal future value.

Explanation: The update rule adjusts the current Q-value towards the target, which is the immediate reward plus the discounted estimate of the optimal future Q-value.

8 Function Approximation & Neural Networks

In environments with large or continuous state spaces, it's impractical to maintain a table of Q-values. Function approximation methods, like neural networks, are used to estimate value functions.

Function Approximator Equation
Q-function Approximation Q(s,a;θ)≈Q*(s,a)

Explanation: Here, θ represents the parameters (weights and biases) of the neural network that approximates the Q-function.

9 Deep Q-Networks (DQN)

DQN combines Q-learning with deep neural networks to handle high-dimensional state spaces.

Component Equation Explanation
Loss Function L(θ) L(θ)=Es,a,r,s′[(y−Q(s,a;θ))2] - Measures the difference between the target Q-value y and the estimated Q-value Q(s,a;θ).
- The expectation is over the experience replay buffer.
Target y y=r+γmaxa′Q(s′,a′;θ−) - r: Immediate reward received after action a in state s.
- γmaxa′Q(s′,a′;θ−): Estimated optimal future value using the target network parameters θ−.

Explanation: The loss function quantifies how close the neural network's predictions are to the targets. The target network θ− is a copy of the Q-network θ that is updated less frequently to stabilize learning.

10 Policy Gradient Methods

Policy gradient methods optimize the policy directly by adjusting its parameters to maximize expected return.

Component Equation Explanation
Objective Function J(θ) J(θ)=Eπ[R] - Represents the expected return when following policy π parameterized by θ.
- The goal is to find θ that maximizes J(θ).
Policy Gradient ∇θJ(θ)=Eπ[∇θlogπθ(a|s)Qπ(s,a)] - Provides the direction in parameter space to adjust θ to increase J(θ).
- ∇θlogπθ(a|s): Gradient of the log-probability of action a given state s, with respect to θ.
- Qπ(s,a): Expected return from state s after taking action a.

Explanation: By taking steps proportional to the policy gradient, we improve the policy to maximize expected rewards.

11 REINFORCE Algorithm

REINFORCE is a Monte Carlo policy gradient method that updates the policy based on complete episodes.

Gradient Estimate ∇θJ(θ)≈∑t=0T∇θlogπθ(at|st)Gt
Update Rule θ←θ+α∇θJ(θ)

Explanation:

  • Gt=∑k=tTγk−tRk+1 is the return from time t onward.
  • The gradient estimate sums over the episode, adjusting the policy parameters θ to increase the likelihood of actions that led to higher returns.
  • α is the learning rate.
12 Actor-Critic Methods

Actor-Critic methods combine the benefits of policy gradients and value function approximation.

Component Equation Explanation
TD Error δt δt=Rt+1+γV(st+1;w)−V(st;w) - Measures the discrepancy between the predicted value and the actual reward plus estimated next value.
- V(st;w): Estimated value of state st using parameters w.
Critic Update w←w+αcδt∇wV(st;w) - Updates the critic's parameters w to reduce the TD error.
- αc: Critic's learning rate.
Actor Update θ←θ+αaδt∇θlogπ(at|st;θ) - Updates the actor's parameters θ in the direction suggested by the TD error.
- αa: Actor's learning rate.

Explanation: The critic evaluates the current policy by estimating the value function, while the actor updates the policy parameters in a direction that improves performance according to the critic's feedback.

13 Mathematics Behind Neural Network Updates

Neural networks are trained by adjusting their parameters to minimize a loss function using gradient descent.

Update Rule θ←θ−α∇θL(θ)

Explanation:

  • L(θ): Loss function measuring the error between predicted and target values.
  • ∇θL(θ): Gradient of the loss with respect to the parameters, computed using backpropagation.
  • α: Learning rate determining the step size in parameter space.
  • The negative sign indicates moving in the direction that minimizes the loss.
14 Example: DQN Update Step

Steps to update DQN parameters:

  1. Sample Mini-batch: Sample transitions (sj,aj,rj,s′j) from the replay buffer.
  2. Compute Target: yj=rj+γmaxa′Q(s′j,a′;θ−)
  3. Compute Loss: Lj(θ)=(yj−Q(sj,aj;θ))2
  4. Compute Gradients: ∇θLj(θ)=−2(yj−Q(sj,aj;θ))∇θQ(sj,aj;θ)
  5. Update Parameters: θ←θ+α∇θLj(θ)

Explanation:

  • By sampling a mini-batch, we break the correlation between sequential samples, improving training stability.
  • The target yj is computed using the target network parameters θ− to stabilize learning.
  • The loss measures how close the network's Q-value predictions are to the target values.
  • Gradients are computed with respect to the network's parameters and used to update the network via gradient descent.
15 Exploration vs. Exploitation

Balancing the need to explore new actions to discover their value and exploiting known actions to maximize reward.

Method Description
ε-Greedy Policy - With probability ε, select a random action (exploration).
- With probability 1−ε, select the action with the highest estimated Q-value (exploitation).
- ε is often decayed over time to shift from exploration to exploitation as learning progresses.
16 Advanced Techniques

Enhancements to basic algorithms to improve performance and stability.

  • Double DQN: Addresses overestimation bias by decoupling the selection and evaluation of the action in the target computation.
    y=r+γQ(s′,argmaxa′Q(s′,a′;θ);θ−)
  • Dueling DQN: Separates the estimation of the state value and the advantages of each action, improving learning efficiency.
    Q(s,a;θ,α,β)=V(s;θ,β)+A(s,a;θ,α)−1|A|∑a′A(s,a′;θ,α)
  • Prioritized Experience Replay: Samples more important transitions (with higher TD errors) more frequently, focusing learning on significant experiences.
17 Summary

Reinforcement learning enables agents to learn optimal behaviors through interactions with an environment by maximizing cumulative rewards. The mathematical foundations, including MDPs, value functions, and Bellman equations, provide a framework for understanding and developing RL algorithms. Neural networks serve as powerful function approximators, especially in complex, high-dimensional environments, making advanced algorithms like DQN and policy gradient methods feasible.

Calculate a temporal-difference update

The target for a nonterminal Q-learning transition is the observed reward plus the discounted best estimated value of the next state. Subtract the current estimate to get a temporal-difference error. Apply only a fraction of that error in a tabular update.

y=r+γmaxa′Q(s′,a′),Qnew=Q+α(y−Q)

Suppose the current estimate is 2, reward is 1, the next maximum is 4, discount is 0.9, and update rate is 0.1. The target is 4.6 and the new estimate is 2.26. If the transition truly ends the task, the target is just 1 and the estimate moves to 1.9.

Calculate a temporal-difference updateObserve stateChoose an actionReceive rewardCheck end flagsUpdate valueCompute TD target
Whether the episode ended at a terminal state determines if future value enters the target.

Interpret environment endings correctly

In Gymnasium, reset after either flagged ending. For an external time-limit truncation, an underlying continuing task usually still bootstraps from the next observation. For true task termination, omit the bootstrap contribution.

Deep Q-learning approximates the action-value table with a neural network. Experience replay and a target network can improve stability. Evaluate across independent seeds and report separate training and evaluation policies.

References

Practice and FAQs

Why does a Q-learning target use the next maximum?

It estimates the return after the best next action according to the current value estimates.

What is the target at true termination?

It is the observed reward without a future-value bootstrap.

Does a truncated rollout always mean the underlying process has terminated?

No. A time limit or another external cutoff can end the rollout while the underlying task could continue.

You Can Also Learn Deep Learning in 80 Days Inside the iOS App Continue this roadmap in a focused mobile study experience with offline access, flashcards, notes, and progress tracking.
Open on the App Store