The Enchanted Maze: Where Our Journey Begins
Alice pushed aside the hanging ivy that veiled the stone archway, her heart pounding with a mixture of fear and excitement. Before her lay a maze unlike any she had ever seen—the hedges were made of shimmering emerald leaves that seemed to whisper secrets with every breeze, and the paths glowed faintly under the twilight sky. A small brass plaque on the arch read: "Every step reveals its truth; only the wise shall find the treasure." Without a second thought, Alice stepped inside.
Immediately, the ground beneath her feet tingled. A soft chime echoed, and a golden numeral "+1" floated briefly in the air before dissolving. "A reward?" she wondered. She took another step—this time to the right—and a low grumble resonated; the number "-1" appeared, darker and heavier. "A penalty," she deduced. With each movement, the maze responded: some paths illuminated with warm light and pleasant sounds, others darkened with ominous rumbles. Alice realized she was engaged in a silent dialogue with the labyrinth itself—a conversation of actions and consequences.
This moment captures the essence of reinforcement learning (RL): an agent (Alice) interacting with an environment (the maze), learning from the outcomes of its actions through rewards and penalties. No one told her the "correct" path; she had to discover it through trial and error, guided only by the scalar feedback the maze provided. As we follow Alice's journey, we will unpack the fundamental components of RL—agent, environment, states, actions, rewards—and see how they form the bedrock of machines that learn from experience.
---
The Agent: Alice as the Learning Entity
At the heart of every reinforcement learning system lies the agent. The agent is the learner and decision-maker—the active entity that perceives its environment and acts upon it. In our story, Alice is the agent. She is not merely a passive observer; she is driven by curiosity and a goal: to find the treasure hidden within the maze. Her every movement is a choice, and each choice is an attempt to maximize something she cannot yet fully comprehend—a cumulative measure of good outcomes, which we will later formalize as *return*.
What makes Alice an RL agent? Three key characteristics define her role:
1. Autonomy: Alice operates without external directives. There is no map, no guidebook, no voice shouting "turn left!" from the sidelines. She must rely on her own sensors (sight, hearing, touch) and her own reasoning. 2. Goal-directedness: Although the maze does not hand her a list of objectives, Alice has an intrinsic aim—to reach the treasure. This goal is implicit in her desire to accumulate positive rewards and avoid penalties. In RL terms, she seeks to maximize her long-term reward. 3. Learning from Interaction: Every step Alice takes changes her understanding. When she receives a "+1" after moving north, she notes that this action, from that particular location, led to a positive outcome. When she receives a "-1" after moving east into a thicket, she learns to associate that action-state pair with discomfort. Over time, these experiences shape her behavior.
Consider a simpler analogy: a child learning to walk. The child is the agent, the living room is the environment, the child's posture and location are states, and the act of taking a step is an action. The reward might be the parent's smile (positive) or the pain of a bump (negative). No one explicitly programs the child's brain with equations for balance; through countless trials—standing, wobbling, falling, getting up—the child learns to walk. Alice's journey through the enchanted maze is a deliberate, magnified version of this same process.
It is crucial to distinguish the agent from the environment. The agent *is* Alice—her decision-making core, her "brain." Everything else—the maze walls, the glowing paths, the sound effects, the treasure—is part of the environment. The environment responds to the agent's actions but does not itself decide what the agent does. This separation is fundamental: it allows us to model any learning system as a clear loop of perception, decision, and outcome.
---
The Environment: The Maze as a Dynamic World
If Alice is the agent, then the enchanted maze is her environment—the world within which she operates and which she cannot fully control. In reinforcement learning, the environment is everything that is external to the agent. It includes the physical space, the rules that govern how that space changes, and the feedback mechanism (rewards) that signals the desirability of states or actions.
The maze is not a static backdrop; it is an active, responsive entity. When Alice moves, the environment updates its internal state and provides a new observation and reward. This dynamic nature is captured by the Markov property (which we will explore in later chapters): the next state and reward depend only on the current state and the agent's action, not on the full history of past states. For now, think of it as the maze having a "memory" only of where Alice currently stands and what she just did—not how she got there.
Let us break down the maze's components:
- **State space**: All possible configurations of the maze. In our simple Gridworld (introduced later), this is every coordinate pair (row, column) within the grid. But in a more complex enchanted maze, the state might also include time of day, whether certain gates are open, or if Alice is carrying a magic item. The environment defines what constitutes a state.
- **Transition dynamics**: The rules that determine what happens when Alice takes an action. If she moves north from cell (2,3), does she end up in (1,3)? Is there a wall? Does a hidden pit open? These rules are the environment's "physics."
- **Reward function**: A critical part of the environment. It is a (usually hidden) function that assigns a numerical value to each state transition (or state). When Alice steps from (2,3) to (1,3), the environment consults its reward function and decides: "This move earns +1" or "This move costs -2." The agent never sees this function directly; it only experiences its outputs.
The environment can be deterministic or stochastic. In a deterministic maze, the same action from the same state always leads to the same next state and reward. In a stochastic (random) environment, there might be probabilities involved: moving north from (2,3) might have an 80% chance of success, a 10% chance of slipping into a penalty, and a 10% chance of staying put. Our enchanted maze, as first encountered, appears deterministic—every step has a fixed consequence—but later we might discover that the whispering hedges sometimes shift, introducing randomness.
A key insight: the environment is the *problem* the agent must solve. The maze's layout, its reward distribution, and its dynamics define the task. A different maze—with walls in different places, or with the treasure guarded by a dragon that moves randomly—would require a different solution. The agent's job is to adapt its behavior to whatever environment it faces, using only the feedback provided.
---
States: The Agent's Local Reality
A state is a snapshot of the environment at a given moment—a complete description of all relevant information that the agent can use to make a decision. For Alice, what constitutes a state? At the simplest level, it is her coordinates in the maze: "I am at row 2, column 3." But states can be richer.
In our Gridworld example, a state $s$ might be defined as: $$ s = (x, y) $$ where $x$ and $y$ are integer coordinates on a 2D grid. This is a fully observable state: Alice can see exactly where she is (assuming the maze is not foggy). However, in many real-world problems, the agent has only partial observability. Imagine if the maze were shrouded in mist, and Alice could only see the adjacent cells. Then her state would be a local view, not the global map. This leads to **Partially Ob