Reinforcement Learning Fundamentals
Reinforcement Learning
Core Idea
Reinforcement learning (RL) is a branch of machine learning concerned with learning control strategies for interacting with complex environments.
Reinforcement learning is a framework for learning how to interact with an environment through experience.
Instead of receiving explicit instructions for every correct action, an agent learns through:
Trial and error
Positive and negative feedback
Rewards that indicate desirable outcomes
Repeated interaction with an environment
The approach is biologically inspired. Animals and humans learn many behaviors by trying actions, observing their consequences, and repeating actions that produce desirable results. Training an animal with treats is a simple example: desired behavior is reinforced with a reward.
RL lies at the intersection of:
Machine learning, because the agent learns from data and experience
Control theory, because the agent must choose actions to control or influence a dynamical system
RL contains two related but distinct components:
A learning framework that describes how an agent interacts with an environment.
An optimization problem that determines which actions or policies produce the greatest future rewards.
Agents and Environments
At the center of reinforcement learning is the interaction between an agent and an environment.
Agent
The agent is the decision-making entity. It has agency because it can choose actions that affect what happens next.
Examples of agents include:
A mouse navigating a maze
A robot learning to walk
A computer playing chess
A robot learning to catch a ball
An animal learning a behavior
A software system learning to control a physical process
Environment
The environment is everything with which the agent interacts. It includes the rules, physical dynamics, other players, and consequences of the agent’s actions.
Examples include:
A maze surrounding a mouse
A chessboard and an opponent
The physical world surrounding a robot
A simulated walking environment
A pendulum and cart governed by physical laws
States, Actions, and Rewards
The basic interaction can be described using three elements:
State, denoted by s: the agent’s current situation or observation
Action, denoted by a: a decision made by the agent
Reward: feedback indicating how desirable the result was
A typical interaction proceeds as follows:
The agent observes or measures its current state s.
The agent chooses an action a.
The environment changes in response to that action.
The agent receives a reward, possibly immediately or after a delay.
The agent observes a new state and continues acting.
The agent generally does not observe the complete state of the environment. For example, a mouse in a maze may know where it currently is and remember some past observations, but it does not necessarily have a top-down map of the entire maze.
Example: A Mouse in a Maze
Consider a mouse attempting to navigate a maze.
Agent: the mouse
Environment: the maze
State: the mouse’s current location and available observations
Actions: turn left, turn right, or move forward
Reward: food received upon reaching the end of the maze
The mouse has control over its actions, but it may receive a reward only after completing a long sequence of movements. This creates a difficult learning problem because the final reward does not directly identify which individual actions were useful.
For example, if the mouse reaches the cheese after making twenty decisions, it is difficult to determine:
Which turns were correct
Which movements were irrelevant
Whether an early action enabled the later success
Whether a particular action should be repeated in a different situation
This difficulty is a central feature of reinforcement learning.
Sparse and Delayed Rewards
A reward is sparse when it occurs infrequently. A reward is delayed when it arrives long after the action that contributed to it.
In the maze example, the mouse might receive no feedback for most of its journey and only receive cheese after reaching the goal. The reward provides information about the entire sequence, but not about each action individually.
Comparison with Supervised Learning
If the mouse received a piece of cheese after every correct turn, the problem would be much closer to traditional supervised learning:
Each action would receive immediate feedback.
The feedback would act like a label.
The agent would know which action was correct or incorrect at each step.
In reinforcement learning, the feedback is much less complete:
Rewards may be delayed.
Rewards may be rare.
Individual actions are not directly labeled.
A successful or unsuccessful outcome may depend on a long sequence of decisions.
The source characterizes this as a form of semi-supervised learning, because the agent still receives supervisory feedback, but only intermittently and with much less information than in classical supervised learning.
Sparse rewards make RL more difficult and often require:
More trials
More data
More exploration
More careful optimization
Games as Reinforcement Learning Environments
Games provide natural examples of reinforcement learning because they contain:
A defined environment
A finite or structured set of actions
Rules governing how actions change the state
Rewards associated with winning, losing, or achieving an objective
Examples include:
Chess
Checkers
Tic-tac-toe
Backgammon
Chess
In chess, the environment includes more than the rules of the game. It also includes an adversarial opponent who is actively trying to defeat the agent.
The agent must:
Observe the current board position
Select a legal move
Predict how the opponent may respond
Adjust its future actions based on the changing board
Attempt to maximize the probability of eventually winning
Chess demonstrates why reinforcement learning is difficult. An agent may play many excellent moves but lose because of one mistake near the end. Since the final reward may only indicate a win or loss, it is difficult to determine which earlier decisions deserve credit.
Backgammon
Backgammon combines deterministic and stochastic elements:
The rules and legal moves are defined.
Dice rolls introduce randomness.
The next state depends on both the agent’s action and the random outcome of the dice.
This makes backgammon an example of a stochastic decision-making environment.
Policies
The main objective of an RL agent is to learn a policy.
A policy specifies what action an agent should take, or the probability with which it should take an action, given its current state.
A policy is commonly written as π. The probability of selecting action a in state s is written as:
This expression means “the probability of taking action a given state s.”
Deterministic and Probabilistic Policies
A deterministic policy always selects the same action in a particular state. A probabilistic policy assigns probabilities to multiple possible actions.
For example, an agent might use the following policy in a particular chess position:
Move a pawn in one direction with probability 0.8
Try an alternative move with probability 0.2
A probabilistic policy can be useful because:
The environment may be stochastic.
An opponent may behave unpredictably.
The agent may be uncertain about which action is best.
The agent needs to explore alternatives.
Repeating only one action may prevent discovery of better strategies.
The term policy is preferred over the more traditional control-theory term control law because RL environments are often probabilistic rather than fully deterministic. The agent’s strategy may therefore need to be probabilistic as well.
Sequential Decision-Making
Reinforcement learning unfolds over time. At each time step, the agent observes a state, selects an action, and may receive a reward.
A sequence can be represented as:
where:
st is the state at time t
at is the action taken at time t
rt is the reward received at time t
T is the final time step
Many intermediate rewards may be zero or absent. In chess, for example, the agent may receive no meaningful reward until the game ends.
The goal is not simply to maximize an immediate reward. It is to choose actions that maximize the total expected reward over the future.
The Credit Assignment Problem
The credit assignment problem is the difficulty of determining which past actions were responsible for a later reward.
This problem is especially severe when:
Rewards are sparse.
Rewards are delayed.
The agent takes many actions before receiving feedback.
The environment is stochastic.
The outcome depends on interactions among multiple actions.
For example, suppose a chess agent loses after playing an otherwise strong game. The final outcome does not reveal whether:
The first move was poor
A midgame decision created a hidden disadvantage
One late mistake caused the loss
A sequence of individually reasonable moves led to a bad position
The credit assignment problem has been recognized as a major challenge since at least the 1960s and remains one of the central problems in reinforcement learning.
Dense and Sparse Rewards
Sparse rewards
Sparse rewards occur only occasionally, such as:
Winning or losing a chess game
Reaching the end of a maze
Successfully catching a ball
Stabilizing a pendulum
Sparse rewards make learning sample inefficient. The agent may need a very large number of attempts before it encounters enough successful examples to learn a good policy.
Dense rewards
Dense rewards provide frequent intermediate feedback. For example, a chess expert might provide feedback after individual moves:
A move creates a strong position.
A move exposes the king.
A move loses material.
A move establishes useful board control.
This additional information helps the agent learn more quickly because it provides clues about the quality of intermediate actions and states.
Reward Shaping
Reward shaping is the use of additional, often human-designed rewards to provide denser feedback during learning.
Instead of rewarding only the final objective, an expert may create proxy rewards for intermediate progress.
Examples include:
Giving a maze agent a small reward for moving closer to the goal
Rewarding a robot for keeping a pendulum near an upright position
Rewarding a chess agent for gaining material or improving board position
Rewarding a ball-catching robot for moving the cup closer to the ball’s trajectory
Reward shaping can:
Reduce the difficulty of the credit assignment problem
Provide more frequent learning signals
Improve sample efficiency
Guide the agent toward useful regions of the state space
However, a proxy reward must be designed carefully. If it does not align with the true objective, the agent may optimize the proxy while failing at the actual task.
Value Functions
A policy specifies what the agent does, but a value function evaluates how beneficial a state is under that policy.
The value of a state is the expected future reward obtained by starting in that state and following a particular policy.
The value of state s under policy π can be represented as:
where:
Vπ(s) is the value of state s under policy π
Eπ denotes an expectation assuming the agent follows policy π
rt+1 is a future reward
γ is the discount factor
T is the time horizon
The value function answers the question:
If the agent starts in state s and follows policy π, how much reward should it expect to receive in the future?
Discounting Future Rewards
The discount factor γ generally satisfies:
The factor γt reduces the contribution of rewards that occur farther in the future.
A smaller γ places more emphasis on immediate rewards.
A larger γ places more emphasis on delayed rewards.
If γ is close to 1, the agent is willing to wait for future rewards.
If γ is close to 0, the agent strongly prioritizes immediate outcomes.
Discounting reflects ideas from economics and psychology: people and decision-making systems often prefer receiving a reward now rather than waiting for an equivalent reward later.
Value Functions in Chess
The set of possible chess positions is extremely large and combinatorial. It is impossible for a human to memorize the exact value of every possible board state.
Instead, players develop approximate rules of thumb for evaluating positions. These approximations serve as informal value functions.
For example, a position may be considered favorable if:
The agent has captured the opponent’s queen.
The agent still has its own queen.
The agent has more material.
The king is relatively safe.
The pieces occupy strategically strong positions.
A simple value estimate might count the points associated with pieces. This is a crude proxy because material alone does not determine the outcome of a chess game. Over time, experience allows the player or learning system to refine the value function to account for more relevant strategic features.
A better value function helps improve the policy because it allows the agent to:
Recognize good and bad states
Prefer actions that lead to valuable positions
Estimate future winning chances
Select moves that increase expected long-term reward
Markov Decision Processes
Many RL environments are modeled as Markov decision processes (MDPs).
An MDP describes an environment in which the probability of the next state depends on:
The current state
The action selected by the agent
The transition can be represented as:
This means the probability of reaching the next state st+1 depends on the current state st and current action at.
Stochastic Transitions
In a deterministic environment, a particular state and action always produce the same next state. In a stochastic environment, the same state-action pair may lead to multiple possible next states.
An MDP can therefore be understood as a process in which:
The agent is in state st.
It selects action at.
The environment samples a next state from P(st+1∣st,at).
The agent receives a reward.
The process repeats.
Backgammon illustrates this structure. The agent follows deterministic game rules, but dice rolls introduce randomness into the state transitions.
Because the environment is probabilistic, the optimal policy may also be probabilistic. The agent must optimize expected future rewards rather than rely on a guaranteed sequence of outcomes.
Policy Optimization
The overall objective of reinforcement learning is to find a policy that maximizes expected future rewards.
This can be expressed conceptually as:
The optimization may involve:
The parameters of a policy
The value function
The model of the environment
The balance between exploration and exploitation
As in many areas of machine learning and control theory, RL optimization can be:
Nonlinear
Nonconvex
Computationally difficult
Dependent on large amounts of data
Machine learning typically solves optimization problems using observed data, while control theory often incorporates known system dynamics and physical constraints. Reinforcement learning combines both perspectives: it learns from experience while attempting to control an environment.
Methods for Optimizing Policies
Several families of methods can be used to optimize RL policies.
Differential Programming
Differential programming uses differentiable models and gradient-based optimization to improve the policy. It is closely related to optimal control and game-theoretic approaches.
Monte Carlo Methods
Monte Carlo methods estimate the quality of actions or states by repeatedly running complete episodes and observing the resulting rewards.
The agent may:
Try a policy or action sequence.
Allow the episode to finish.
Record the total reward.
Use the observed outcome to update its estimates.
Monte Carlo approaches can work without a detailed model, but they may require many trials, especially when rewards are sparse.
Temporal-Difference Learning
Temporal-difference learning updates estimates using information from successive states rather than waiting for an entire episode to finish.
It combines ideas associated with:
Monte Carlo estimation
Dynamic programming
Temporal-difference methods can be model-free, meaning they do not require an explicit model of the environment’s transition dynamics. They update estimates as the agent moves through the environment.
Bellman Optimization
Temporal-difference learning is related to Bellman optimization, which forms a foundation of optimal control and reinforcement learning.
The Bellman perspective expresses the value of a state in terms of:
The immediate reward
The value of future states
The best or expected future decisions
This recursive structure allows an agent to improve its estimates step by step.
Other Optimization Techniques
RL policies can also be optimized using:
Simulated annealing
Evolutionary optimization
Gradient descent
Stochastic gradient descent
Modern neural-network optimization methods such as Adam
Deep neural networks have enabled major advances in reinforcement learning, particularly for problems with large or complex state spaces.
Exploration and Exploitation
A reinforcement learning agent must balance exploration and exploitation.
Exploration
Exploration involves trying actions that the agent has not tried before or does not currently believe are optimal.
Exploration is useful because it can reveal:
Better strategies
New successful actions
Unobserved regions of the state space
Unexpected consequences of actions
However, exploration may temporarily reduce performance because the agent may choose actions that fail.
Exploitation
Exploitation involves using the policy or actions that currently appear to produce the greatest reward.
Exploitation allows the agent to:
Use known successful strategies
Accumulate reward efficiently
Improve performance based on current knowledge
However, excessive exploitation can prevent the agent from discovering a better policy.
The Trade-Off
An agent that only explores may never settle on a successful strategy. An agent that only exploits may become trapped in a suboptimal strategy.
This trade-off is fundamental to:
Reinforcement learning
Machine learning
Control theory
Sequential decision-making
Probabilistic policies often support this balance by choosing the best-known action most of the time while occasionally trying alternatives.
Policy Iteration
Policy iteration is a process in which the agent repeatedly improves its policy using new information from interaction with the environment.
A general policy-iteration process is:
Initialize a policy.
Use the policy to interact with the environment.
Collect states, actions, and rewards.
Estimate which states or actions are valuable.
Modify the policy to favor better actions.
Repeat the process.
The policy becomes progressively better as the agent gains experience and improves its estimates of future rewards.
Policy iteration can be implemented using a variety of optimization techniques, including gradient-based methods, evolutionary methods, and neural networks.
Learning Physical Skills
Learning to Catch a Ball in a Cup
A robot can learn to catch a ball in a cup through a combination of demonstration and trial and error.
A typical process is:
A human provides an initial demonstration showing that the task is possible.
The robot uses visual information from a camera.
The robot observes the ball, cup, and their relative positions.
The robot attempts the task.
It receives feedback based on whether the ball lands in the cup.
It updates its behavior and tries again.
The robot may fail repeatedly at first, but its performance improves over many trials. This resembles how a child might learn a physical skill: an initial demonstration provides guidance, followed by repeated practice and feedback.
This example illustrates:
Imitation learning, in which the agent learns from a human demonstration
Trial-and-error learning, in which the agent improves from repeated attempts
Visual state estimation, in which camera information is used to determine the relevant state
Physical control, in which actions affect a real-world object
The Pendulum Swing-Up Problem
Another classic control problem involves swinging a pendulum mounted on a cart into an upright position and stabilizing it there.
This is difficult because:
The pendulum is nonlinear.
The agent must first build momentum.
The correct control inputs change over time.
The final upright position is unstable.
Successful stabilization requires precise actions.
A purely trial-and-error learner may require thousands of trials and substantial computation before discovering useful behavior. This is highly sample inefficient.
A model-based approach, such as the Pilco learning framework, uses a model of the physical system to learn much more efficiently. Knowledge of physical laws, including Newtonian mechanics represented by:
can help predict how the pendulum will respond to different control actions.
Using a model allows the learner to:
Predict likely future states
Test actions virtually
Avoid wasting as many physical trials
Learn useful behavior from relatively few samples
This contrasts with model-free learning, which attempts to learn effective behavior directly from experience without explicitly modeling the system’s physics.
Q-Learning
Q-learning learns the quality of state-action pairs rather than learning only a policy or only a state-value function.
The Q-function is written as:
It evaluates the quality of taking action a in state s and then following a policy.
The Q-function answers a question such as:
Given the current state and a possible action, how much future reward can be expected if the agent acts optimally afterward?
Unlike the state-value function V(s), which evaluates only a state, the Q-function evaluates both:
The state
The action selected in that state
Q-Learning Update
A common Q-learning update is:
where:
Q(st,at) is the previous estimate
α is the learning rate
rt+1 is the reward received after taking the action
γ is the discount factor
maxa′Q(st+1,a′) represents the quality of the best possible future action
The bracketed quantity is the prediction error used to update the estimate
The term:
represents the immediate reward plus the discounted estimate of the best future outcome.
Choosing Actions with a Q-Function
Once the agent knows the Q-values for a state, it can choose the action with the highest value:
Thus, the Q-function can implicitly define a policy. At any state, the agent examines the available actions and selects the one with the greatest predicted long-term quality.
Q-learning is especially useful because it combines aspects of policy selection and value estimation into one learned function. Modern systems can approximate the Q-function with a deep neural network.
Hindsight Experience Replay
Hindsight experience replay addresses the difficulty of learning from failed attempts, especially when rewards are sparse.
In ordinary learning, an unsuccessful episode may appear useless because the agent did not achieve its intended goal. Hindsight replay instead reinterprets the outcome:
The agent did not accomplish the original goal.
However, it may have reached another state that could be treated as an alternative goal.
The agent can learn that the sequence of actions was successful relative to this alternative goal.
Example: Ball and Cup
Suppose a robot attempts to place a ball in a cup but misses. The ball lands in a different location.
Rather than discarding the experience, the system can treat the actual landing location as an alternative objective:
The original goal was to place the ball in the cup.
The achieved outcome was to move the ball to another location.
The system can learn that the action sequence was effective for reaching that alternative location.
This produces additional learning signals from experiences that would otherwise have generated no reward.
Benefits
Hindsight replay can:
Make failed attempts useful
Increase the amount of effective reinforcement
Improve sample efficiency
Help the agent learn system dynamics
Support learning in complex state spaces
Reduce wasted experience
Provide a form of memory for useful action sequences
The approach also resembles human learning. A person may fail to achieve an intended result but remember that the action produced another potentially useful result. That information can be reused later when the alternative outcome becomes desirable.
Model-Free and Model-Based Learning
The examples illustrate two broad approaches to reinforcement learning.
Model-free learning
A model-free agent learns which actions are effective directly from experience.
It does not require an explicit model of the environment.
It may require many trials.
It can be useful when the environment is difficult to model.
Sparse rewards can make it highly sample inefficient.
Model-based learning
A model-based agent uses or learns a model of the environment’s dynamics.
It predicts how actions change states.
It can simulate possible outcomes.
It may learn more efficiently from fewer real-world trials.
It is especially useful when physical laws or system dynamics are known.
The pendulum example demonstrates why model-based learning can be advantageous. A learner that understands the physics of the pendulum can predict useful control inputs, whereas a learner relying only on random trial and error may need thousands of attempts.
Central Relationships Among RL Concepts
The main concepts of reinforcement learning are closely connected:
The agent observes a state in the environment.
A policy selects an action based on that state.
The environment responds probabilistically with a new state and reward.
The agent uses rewards to estimate values and Q-values.
Sparse or delayed rewards create the credit assignment problem.
Reward shaping and hindsight replay provide additional learning signals.
Exploration allows the agent to discover alternatives.
Exploitation allows the agent to use currently successful strategies.
Optimization methods update the policy or value estimates.
Repeated interaction gradually improves the agent’s ability to maximize future rewards.
The fundamental goal of reinforcement learning is to learn a policy that selects actions producing the greatest expected future reward, despite incomplete information, delayed feedback, and environmental uncertainty.
Reinforcement learning therefore provides a general framework for learning intelligent behavior in both simulated and physical environments.

Ask anything
We've read this whole study set, so questions about it get the best answers.