Chapter Core Objectives
This chapter establishes the concepts that will be used repeatedly in all subsequent chapters of reinforcement learning:
- state
- action
- state transition
- policy
- reward
- trajectory, ==return==, episode
- Markov decision process, MDP
- Markov property
In one sentence:
Reinforcement learning studies the problem of an agent learning a "good policy" ==from reward feedback== through trial-and-error interaction with an environment; this chapter uniformly describes this process using an MDP.
1.1 Grid World Example
This book uses a grid world example from beginning to end.
- The robot is called an agent.
- The robot moves in a grid, and at each time step it can occupy only one cell.
- White cells can be entered.
- Orange cells are forbidden.
- There is a target cell.
- The agent's goal: find a "good" policy that can reach the target from any initial cell while avoiding entering forbidden cells, hitting boundaries, and taking meaningless detours.
If the agent knows the map in advance, then finding a path is easy;
but reinforcement learning usually assumes that the agent does not know the environment model and must learn through interaction with the environment by trial and error.
Therefore, concepts such as state, action, transition, policy, and reward need to be defined.
1.2 States and Actions
State
The state describes the state of the agent relative to the environment.
In the grid world, the state is the agent's position.
Figure 1.3(a) has 9 cells, so there are 9 states:
\mathcal{S} is called the state space.
Action
In each state, the agent can take 5 actions:
- a_1: up
- a_2: right
- a_3: down
- a_4: left
- a_5: stay still
The set of actions is called the action space:
Different states can have different action spaces. For example, in s_1, moving up or left would hit the boundary, so we can set:
But for simplicity, this book usually takes:
which holds for all states.
1.3 State Transition
Definition
After the agent takes action a in state s, it may transition to another state s'; this process is called state transition.
For example:
means moving right in s_1 and arriving at s_2.
Two Important Examples
-
Hitting the boundary
In s_1, execute a_1, attempting to go up out of bounds.
Since it cannot leave the state space, the agent is bounced back:s_1 \xrightarrow{a_1} s_1 -
Entering a forbidden area
For example, in s_5, execute a_2, attempting to enter s_6.
There are two possibilities:- The forbidden area, although forbidden, can still be entered, but a penalty is incurred;
- The forbidden area cannot be entered, and the agent is bounced back.
This book chooses the first: the forbidden area can be entered, but entering it yields a negative reward.
Therefore:s_5 \xrightarrow{a_2} s_6
Tabular Representation
Table 1.1 is a tabular representation of state transitions:
- Each row corresponds to a state;
- Each column corresponds to an action;
- The cell indicates the next state transitioned to after taking that action.
This kind of table can only describe deterministic state transitions.
Probabilistic Representation
More generally, state transitions are stochastic and are represented by conditional probabilities:
denotes the probability of transitioning to s' after taking action a in state s.
It must satisfy:
For example:
This shows that moving right in s_1 definitely leads to s_2.
If there is random wind, etc., then it may happen that:
For simplicity, the grid world in this book mainly considers deterministic transitions.
1.4 Policy
Definition
A policy tells the agent what action should be taken in each state.
A policy can be represented by arrows, as shown in Figure 1.4(a).
Mathematical Representation
A policy is a conditional probability distribution:
denotes the probability of choosing action a in state s.
It must satisfy:
Deterministic Policy
For example, the policy in Figure 1.4 definitely moves right in s_1:
The probabilities of other actions are 0:
Stochastic Policy
Figure 1.5 is a stochastic policy.
In s_1, the agent may move right or down, each with probability 0.5:
The probabilities of other actions are 0.
Tabular Representation
Table 1.2 is a tabular representation of the policy:
- Each row is a state;
- Each column is an action;
- The cell is the probability of taking that action.
Later, Chapter 8 will ==generalize policies from tabular representations to parameterized function representations==.
1.5 Reward
Definition
After the agent executes action a in state s, it receives a reward from the environment, denoted by r.
The reward is a function of state and action:
Rewards can be positive, negative, or zero.
- Positive reward: encourages the agent to take that action;
- Negative reward: discourages the agent from taking that action.
Reward Settings for the Grid World in This Book
- Attempting to go out of bounds: r_{\text{boundary}}=-1
- Attempting to enter a forbidden area: r_{\text{forbidden}}=-1
- Reaching the target: r_{\text{target}}=+1
- Other cases: r_{\text{other}}=0
Special Handling of the Target State s_9
The reward process does not necessarily terminate after reaching s_9.
- If a_5 is executed in s_9 to stay, the next state is still s_9, and the reward is +1;
- If a_2 is executed in s_9 to move right, the next state is still s_9, but the reward is -1, because it is equivalent to hitting the boundary.
Tabular Representation
Table 1.3 is a tabular representation of rewards:
- Each row is a state;
- Each column is an action;
- The cell is the reward obtained after taking that action.
Key Reminder
One cannot choose actions based only on immediate rewards.
Because a large immediate reward for an action does not necessarily mean a large long-term total return.
Reinforcement learning focuses on the long-term cumulative reward, i.e., return.
Stochastic Rewards
More generally, rewards can also be stochastic, described by:
and satisfy:
In the grid world of this book, the reward process is deterministic.
Significance of Reward Design
Reward can be viewed as a kind of human-machine interface.
By designing rewards, we can ==guide the agent to exhibit the behavior we expect==.
But reward design itself is usually not simple, especially for complex tasks.
1.6 Trajectories, Returns, and Episodes
Trajectory
A trajectory is a state-action-reward chain.
For example, in Figure 1.6(a), the policy generates the trajectory:
The corresponding rewards are:
Return
The return is ==the sum of all rewards along the trajectory==.
For a finite trajectory:
Another trajectory in Figure 1.6(b):
The rewards are:
The return is:
Therefore, the left policy has return 1, and the right policy has return 0, so the left policy is better.
This is consistent with intuition: the right policy passed through the forbidden area.
Immediate Rewards and Future Rewards
==Return = immediate reward + future reward==.
Sometimes the immediate reward is negative, but the future reward is larger, so one cannot be short-sighted.
Actions must be decided based on return, not immediate reward.
Infinitely Long Trajectories and Discounted Return
If after reaching s_9 the agent keeps staying, and each time gets +1, then:
Direct summation diverges.
Therefore, the discounted return is introduced:
where \gamma\in(0,1) is the discount rate.
For example:
Role of the Discount Rate
- Remove the restriction that the process must terminate, allowing infinitely long trajectories;
- Adjust the emphasis on near-term rewards versus long-term rewards:
- When \gamma is close to 0: emphasis on near-term rewards, and the policy is short-sighted;
- When \gamma is close to 1: emphasis on long-term rewards, and the policy is far-sighted and willing to take risks.
Episode
The agent interacts with the environment according to the policy and may stop at some terminal state.
The trajectory obtained in this way is called an episode or trial.
- episodic tasks: tasks with terminal states;
- continuing tasks: no terminal state, and interaction never ends.
How to Convert Episodic to Continuing
There are two ways:
- Treat the terminal state as an absorbing state, where the agent stays forever after reaching it;
- Treat the terminal state as an ordinary state, and the agent can leave and return.
This book adopts the second:
The target state s_9 is treated as an ordinary state, and the action space remains:
Because reaching s_9 can repeatedly yield positive rewards, a discount rate must be used to prevent the return from diverging.
1.7 Markov Decision Process MDP
The previous sections introduced concepts through examples; this section formalizes them using an MDP.
An MDP is a general framework for ==describing stochastic dynamical systems==.
Components of an MDP
1. Sets
- State space: \mathcal{S}
- Action space: \mathcal{A}(s), associated with each state s
- Reward set: \mathcal{R}(s,a), associated with each state-action pair (s,a)
2. Model / dynamics
- State transition probability:
satisfying:
- Reward probability:
satisfying:
3. Policy
denotes the probability of choosing action a in state s, satisfying:
4. Markov Property
The Markov property is "memorylessness":
That is, the next state and reward depend only on the current state and action, ==and are unrelated to earlier history==.
The Markov property is the basis for deriving the Bellman equation.
Is the Model Stationary or Nonstationary?
- stationary model: the model does not change over time;
- nonstationary model: the model changes over time.
This book only considers stationary models.
Relationship Between MDP and Markov Process
Once the policy is fixed, the MDP degenerates into a Markov process, MP.
If the state is finite or countable, the MP is also called a Markov chain.
This book mainly considers finite MDPs: both the number of states and the number of actions are finite.
Agent-Environment Interaction
- agent: the decision-maker, able to perceive states, maintain a policy, and execute actions;
- environment: everything outside the agent;
- after the agent executes an action, the environment returns a new state and reward;
- forming a closed loop.
1.8 Chapter Summary
This chapter used the grid world example to introduce basic concepts of reinforcement learning, and then formalized them using an MDP.
Core concepts include:
- state, action, state transition
- policy
- reward
- trajectory, return, episode
- discounted return
- MDP, Markov property
- agent-environment interaction
These concepts are the foundation of subsequent chapters.
Subsequently, starting from these concepts, we will introduce state value, Bellman equation, optimal policy, value iteration, policy iteration, Monte Carlo, temporal difference, value function approximation, policy gradient, and Actor-Critic.
1.9 Selected Q&A
Q1: Can rewards all be negative or all be positive?
Yes.
What determines encouragement or discouragement is the relative reward value, not the absolute reward value.
For example, add -2 to all original rewards:
- r_{\text{boundary}}=-3
- r_{\text{forbidden}}=-3
- r_{\text{target}}=-1
- r_{\text{other}}=-2
Although all are negative, the optimal policy remains unchanged.
The reason is that the optimal policy is invariant to affine transformations of rewards.
This will be proved in detail in Section 3.5.
Q2: Is the reward a function of the next state?
The reward actually depends on s,a,s'.
But s' is determined by s,a, so it can be written as:
The advantage of writing it this way is that it facilitates establishing the Bellman equation.
Quick Reference of Key Formulas in This Chapter
State space:
Action space:
State transition probability:
Reward probability:
Policy:
Markov property:
Discounted return:
Common Pitfalls in This Chapter
-
Immediate reward is not equal to long-term return.
Do not look only at one-step reward; look at cumulative discounted return. -
A policy is a conditional probability, not the action itself.
A deterministic policy is only a special case of a stochastic policy. -
Tabular representations can only describe deterministic transitions and rewards.
The general case requires conditional probabilities. -
The target state does not necessarily terminate.
This book treats the target state as an ordinary state, where rewards can be obtained repeatedly, so a discount rate is needed. -
After fixing the policy, the MDP degenerates into an MP.
This is the relationship between an MDP and a Markov process. -
Affine transformations of rewards do not change the optimal policy, but they do change state values.
This will be rigorously proved later.
[file content end]