Key ideas
- Reinforcement learning (RL): an agent learns by interacting with an environment, and the reward is its only feedback on whether an action was good. By the reward hypothesis, every goal can be written as maximising the expected cumulative reward, the expected return1. Nobody tells it the correct action, unlike supervised learning, so RL needs an environment to act in (often a simulator such as a game or physics engine) and a reward function that scores outcomes.
- The loop: the environment gives a state ; the agent picks an action ; the environment moves to and returns a reward ; and so on, producing the trajectory 1.
- Markov decision process (MDP): states, actions, transition probabilities , a reward and a discount 5. The Markov property says the next state depends only on the current state and action, not on the earlier history, so the agent can decide from the current state alone1,5. Whether it holds depends on what you call the state: a robot whose action is an acceleration is not Markov if the state is only its position, but it is with (position, velocity)5. A single game frame shows positions but not velocities.
- State vs observation: a state describes the world completely (chess, where the board is fully visible); an observation is partial (Super Mario Bros, where you see only part of the level)1.
- Action space: discrete (finitely many actions, e.g. 4 moves) or continuous (e.g. any steering angle)1. State spaces can likewise be discrete (a board position) or continuous (CartPole's four real numbers).
- Reward and discounting: rewards arriving sooner are more predictable, so future rewards are discounted by , typically 0.95–0.99; a larger makes the agent care more about the long term1. With the return of a never-ending trajectory stays finite5.
- Tasks: episodic tasks have a terminal state and split into episodes (a Mario level); continuing tasks never end (automated trading)2.
- Exploration vs exploitation: trying actions to learn more about the environment versus using what is already known to collect reward; an agent that only exploits can settle for a small nearby reward and miss a much larger one3.
- Policy: the agent's brain, mapping a state to an action. A deterministic policy always returns the same action for a state; a stochastic policy returns a distribution 4. Policy-based methods learn the policy directly; value-based methods learn a value function (expected return from a state) and act by moving to the most valuable state4.
- Model-based vs model-free: a learned model of the environment lets the agent plan ahead and can greatly improve sample efficiency, but the agent may exploit the model's errors and do well in the model yet badly in reality6. Among model-free methods, policy optimisation is usually on-policy, stable and reliable; Q-learning is usually off-policy, reuses old data and is more sample-efficient but less stable6.
Formulas
- A reward of 1 forever is worth , e.g. 100 for .
- The optimal policy maximises the expected return from every state.
Worked example
An episode gives and ends. With :
- forwards: ;
- backwards with : , , , .
With the same episode is worth only , and with just the first reward, 1: a small makes the agent short-sighted.
In code
import gymnasium as gym
env = gym.make("CartPole-v1")
print(env.observation_space.shape, env.action_space) # (4,) Discrete(2)
obs, info = env.reset(seed=0); env.action_space.seed(0)
G, gamma, t, done = 0.0, 0.99, 0, False
while not done: # one episode
a = env.action_space.sample() # random policy
obs, r, terminated, truncated, info = env.step(a) # reward +1 per step
G += gamma**t * r # discounted return G_0
t += 1
done = terminated or truncated # pole fell / time limit
print(t, round(G, 2), round((1 - gamma**t) / (1 - gamma), 2)) # 18 16.55 16.55
CartPole's state is continuous (cart position and velocity, pole angle and angular velocity), while its action space is discrete: push left or right.
Common pitfalls
- Reward function, value function and policy are different things: the reward scores one step, the value is the expected long-run return, the policy chooses actions.
- Chess and Go have discrete, fully observed states; Super Mario Bros gives only a partial observation1.
- A position (or one frame) is not a Markov state when motion matters; add the velocity or stack several frames5.
- Training a network to copy expert moves from a labelled dataset is supervised learning (behavioural cloning), not RL: no reward enters the loss.
- An MDP has transition probabilities whether or not the agent knows them; model-free methods learn without them.
- The danger of a learned model is model bias that the agent exploits, not an inability to plan6.
- Reward shaping changes the optimum. A bonus of per move makes a win on move 7 worth but a win on move 5 only , so the agent may delay a sure win; a per-step penalty can instead make ending the episode early (even by losing) look attractive.
- close to 1 means far-sighted, means only the immediate reward counts1.
References
- Hugging Face Deep Reinforcement Learning Course — Unit 1, The Reinforcement Learning Framework: The RL Process; Markov Property; Observations/States Space; Action Space; Rewards and the discounting
- Hugging Face Deep Reinforcement Learning Course — Unit 1, Type of tasks
- Hugging Face Deep Reinforcement Learning Course — Unit 1, The Exploration/Exploitation trade-off
- Hugging Face Deep Reinforcement Learning Course — Unit 1, Two main approaches for solving RL problems
- Dive into Deep Learning — Sec. 17.1 Markov Decision Process (17.1.1–17.1.3)
- OpenAI Spinning Up, Part 2: Kinds of RL Algorithms — Model-Free vs Model-Based RL; What to Learn