5.1.1Reinforcement Learning Foundations

Agent, environment, state, action, reward

2,540 words12 min readdifficulty · medium

Overview

The agent-environment interaction loop is the foundation of all reinforcement learning. Unlike supervised learning where we're told what the right answer is, RL agents must discover good behavior by trial and error, receiving only feedback (rewards) about how good their actions were.

Figure — Agent, environment, state, action, reward

The Five Components

Key insight: The agent contains the "brain" — the policy (action-choosing strategy) and possibly a model of how the world works.

Key insight: The environment boundary is a design choice. For a robot, we might include the robot's body in the environment and only the control algorithm as the agent. Or we might put motor controllers in the agent and only the external world in the environment.

Why this matters: In a fully observable environment (like chess), the agent sees the true state. In partially observable environments (like poker or a robot with limited sensors), the agent must make decisions from incomplete information.

Why actions are first-class: The agent controls ONLY its actions. It cannot directly control the state or reward — those emerge from the interaction.

Critical insight: Rewards are the ONLY way to communicate the goal. If you want an RL agent to walk forward, reward forward distance. If you accidentally reward not falling, it might learn to lie down!

The Interaction Loop: Deriving the Dynamics

Let's formalize how these components interact over time.

Step 1: Initial state At t=0t=0, the environment is in state s0s_0 (drawn from some initial distribution p(s0)p(s_0)).

Step 2: Agent observes The agent receives observation o0o_0 (in fully observable case, o0=s0o_0 = s_0).

Step 3: Agent acts The agent's policy π\pi maps states/observations to actions. This can be:

  • Deterministic: at=π(st)a_t = \pi(s_t) — same state always gives same action
  • Stochastic: atπ(ast)a_t \sim \pi(a|s_t) — sample action from probability distribution

Step 4: Environment responds The environment receives action a0a_0 and:

  1. Transitions to new state: s1P(ss0,a0)s_1 \sim P(s'|s_0, a_0) where PP is the state transition probability
  2. Emits reward: r0=R(s0,a0,s1)r_0 = R(s_0, a_0, s_1) or r0R(s0,a0)r_0 \sim R(s_0, a_0) if stochastic

Step 5: Loop repeats This produces a trajectory (also called episode or rollout): s0,a0,r0,s1,a1,r1,s2,a2,r2,s_0, a_0, r_0, s_1, a_1, r_1, s_2, a_2, r_2, \ldots

Why this formulation? This is a Markov Decision Process (MDP). The "Markov" part means: given the current state sts_t, the future is independent of the past. Formally: P(st+1,rtst,at,st1,at1,)=P(st+1,rtst,at)P(s_{t+1}, r_t | s_t, a_t, s_{t-1}, a_{t-1}, \ldots) = P(s_{t+1}, r_t | s_t, a_t)

This lets us make decisions based only on the current state, not the entire history.

Finite horizon (episode ends at time TT): Gt=rt+rt+1+rt+2++rT=k=0Ttrt+kG_t = r_t + r_{t+1} + r_{t+2} + \cdots + r_T = \sum_{k=0}^{T-t} r_{t+k}

Why? Simple sum works when episodes have a clear end (game over, task completed).

Infinite horizon with discounting: Gt=rt+γrt+1+γ2rt+2+=k=0γkrt+kG_t = r_t + \gamma r_{t+1} + \gamma^2 r_{t+2} + \cdots = \sum_{k=0}^{\infty} \gamma^k r_{t+k}

where γ[0,1)\gamma \in [0, 1) is the discount factor.

Why discount?

  1. Mathematical: Makes the infinite sum converge (if rewards are bounded)
  2. Practical: Reflects that near-term rewards are more certain/valuable than distant ones
  3. Control: γ\gamma close to 0 = myopic (greedy), γ\gamma close to 1 = far-sighted

Derivation of why this converges: Assume rewards are bounded: rtRmax|r_t| \leq R_{max} for all tt. Gt=k=0γkrt+kk=0γkRmax=Rmaxk=0γk|G_t| = \left|\sum_{k=0}^{\infty} \gamma^k r_{t+k}\right| \leq \sum_{k=0}^{\infty} \gamma^k R_{max} = R_{max} \sum_{k=0}^{\infty} \gamma^k

The geometric series sums to: k=0γk=11γfor γ<1\sum_{k=0}^{\infty} \gamma^k = \frac{1}{1-\gamma} \quad \text{for } |\gamma| < 1

Therefore: GtRmax1γ|G_t| \leq \frac{R_{max}}{1-\gamma} which is finite. ✓

Recursive form (bootstrapping): Gt=rt+γ(rt+1+γrt+2+)=rt+γGt+1G_t = r_t + \gamma(r_{t+1} + \gamma r_{t+2} + \cdots) = r_t + \gamma G_{t+1}

This recursion is the seed of the Bellman equation.

Trajectory example:

t=0: s₀=(1,1), agent observes (1,1), chooses a₀=North
     Environment: valid move, s₁=(1,2), r₀=-1

t=1: s₁=(1,2), agent chooses a₁=East
     Environment: valid move, s₂=(2,2), r₁=-1

t=2: s₂=(2,2), agent chooses a₂=North  
     Environment: hits wall, s₃=(2,2), r₂=-5

t=3: s₃=(2,2), agent chooses a₃=East
     Environment: valid move, s₄=(3,2), r₃=-1

... continues until reaching goal at (5,5)

t=12: s₁₂=(5,5), r₁₂=+10, episode ends

Return calculation (with γ=0.9\gamma=0.9): G0=1+0.9(1)+0.92(5)++0.912(+10)G_0 = -1 + 0.9(-1) + 0.9^2(-5) + \cdots + 0.9^{12}(+10)

Why the step penalty? Without it (r=0r=0 for non-goal states), the agent has no incentive to reach the goal quickly. It could wander forever and never learn a good policy.

Why this reward structure?

  • First term: Negative squared error → penalizes deviation from target (comfortable temperature)
  • Second term: Small penalty on energy use → encourages efficiency
  • Both terms are negative → maximizing reward means minimizing deviations and energy

Trajectory step:

t=0: s₀=(18°C, 10°C), agent chooses a₀=0.8 (80% heater)
     Environment physics: Q = heater_power × max_heating
                Heat_loss = k × (T_inside - T_outside)
                T_new = T_old + (Q - Heat_loss) × dt
     Result: s₁=(19.5°C, 10°C), r₀=-(19.5-21)² - 0.01×0.8² = -2.256

Why continuous actions? Temperature control naturally operates on a continuous scale. Discretizing to {off, low, medium, high} would lose precision.

Common Mistakes

Why it feels right: Rewards tell us what's good, so maximizing each reward seems logical.

The fix: The agent must maximize cumulative discounted reward (return), not immediate reward. Example: In chess, sacrificing a piece (negative immediate reward) to set up checkmate (large future reward) is optimal.

Formal: The agent learns a policy π\pi^* such that: π=argmaxπEτπ[t=0γtrt]\pi^* = \arg\max_\pi \mathbb{E}_{\tau \sim \pi} \left[ \sum_{t=0}^{\infty} \gamma^t r_t \right] Not: π=argmaxπE[rt]\pi = \arg\max_\pi \mathbb{E}[r_t] at each tt independently.

Why it feels right: Losses and rewards both measure performance, and we're used to minimization from ML.

The fix: RL rewards are designed to be maximized. If your task naturally has a loss (tracking error, energy waste), define r=lossr = -\text{loss} so that maximizing reward means minimizing loss.

Example: For distance from goal dd, use r=dr = -d not r=dr = d.

Why it feels right: Simpler states = smaller state space = faster learning.

The fix: If you omit information needed to predict the future, you break the Markov property. The agent will see the same state but experience different outcomes depending on hidden history, making learning impossible.

Example: In Pong, a single frame doesn't show ball velocity. You need 2+ frames or include velocity in the state explicitly. Otherwise, the agent can't predict where the ball will go.

Or picture a feedback loop: Agent → Action → Environment → State+Reward → Agent (closes the loop)

Recall Explain to a 12-Year-Old

Imagine you're learning a new video game, but the controls aren't labeled. You press buttons and see what happens. When you pick up a coin, the game gives you points (that's a reward). When you fall in a hole, you lose points (negative reward).

You (the agent) can see the screen (the state) — where you are, where enemies are. You choose which button to press (action). The game world (environment) updates based on your button: your character moves, enemies move, and it decides how many points you earned.

At first, you try random buttons. But over time, you notice patterns: "When I'm near a cliff and press right, I fall and lose points. When I jump over the cliff instead, I get across safely." You're learning which actions work in which situations. That's exactly what RL agents do, except with math instead of button mashing!

The "return" is like your total score for the whole game. You don't just want to grab the nearest coin (immediate reward) — sometimes it's smarter to skip a coin to avoid danger, getting a better total score at the end.

Connections

#flashcards/ai-ml

What are the five core components of reinforcement learning?
Agent (learner/decision-maker), Environment (the world), State (description of situation), Action (agent's choices), Reward (feedback signal)
What is the difference between environment state and observation?
Environment state is the complete, true state of the world. Observation (agent state) is what the agent actually perceives, which may be partial or noisy. In fully observable environments they're equal.
What is the Markov property in RL?
Given the current state, the future is independent of the past: P(s_{t+1}, r_t | s_t, a_t, history) = P(s_{t+1}, r_t | s_t, a_t). This means the current state contains all information needed to predict the future.
What is the return G_t and why do we use it?
Return is the cumulative discounted reward: G_t = Σ γ^k r_{t+k}. We use it because the agent's goal is to maximize total long-term reward, not just immediate reward. Discounting (γ) makes near-term rewards more valuable than distant ones.
Why do we discount future rewards (use γ < 1)?
Three reasons: (1) Mathematical - makes infinite sums converge, (2) Practical - near-term rewards are more certain, (3) Control - lets us tune short-term vs long-term focus. γ close to 0 = greedy, γ close to 1 = far-sighted.
What is a trajectory in RL?
A sequence of states, actions, and rewards produced by agent-environment interaction: s₀, a₀, r₀, s₁, a₁, r₁, s₂, a₂, r₂, ... Also called episode or rollout.
What's the difference between discrete and continuous actions?
Discrete actions are from a finite set (e.g., {left, right, jump}). Continuous actions are from a continuous range (e.g., steering angle ∈ [-30°, +30°]). Different action spaces require different RL algorithms.
Why is reward the only way to specify the task?
The reward function is the ONLY signal that tells the agent what we want. The agent cannot read our minds — it will maximize whatever reward we define. If the reward doesn't align with our true goal, the agent learns the wrong behavior (reward hacking).
What's wrong with maximizing immediate reward at each step?
This is myopic/greedy behavior. Optimal RL requires maximizing cumulative discounted return, which may require accepting low/negative immediate rewards for better long-term outcomes (e.g., sacrificing a chess piece to set up checkmate).
Why must states satisfy the Markov property?
If important information is hidden in the history, the agent sees the same state but experiences different outcomes depending on that history. This makes learning impossible because the state doesn't contain enough information to predict the future.
What is the recursive form of the return?
G_t = r_t + γ·G_{t+1}. The return at time t equals the immediate reward plus the discounted return from the next step. This recursion leads directly to the Bellman equation.

Concept Map

observes

informs

selects

takes

affects

produces

emits

feedback to

summed into

goal maximize

may be partial in

Agent - learner and decision maker

Environment - the world

State - situation description

Action - agent choice

Reward - scalar signal

Policy - action strategy

Cumulative reward

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Reinforcement Learning mein sabse basic concept hai agent aur environment ka interaction. Socho ek bacha cycle chalana seekh raha hai - woh agent hai. Jab woh pedal marta hai, handle ghumata hai (actions), toh cycle

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections