Introduction
Reinforcement learning (RL) agents typically treat reward functions as "black boxes," learning only through trial and error. In standard RL, the agent performs an action, and the environment returns a scalar value. If the reward is "sparse"—for instance, if an agent only receives a point at the end of a long sequence of tasks—it may spend millions of steps wandering aimlessly before stumbling upon a reward. This paper introduces Reward Machines (RMs), a novel approach that exposes the internal logic of a reward function to an agent using finite state machines. By making the "code" of the reward function visible, RMs allow agents to learn complex, multi-stage tasks with significantly higher sample efficiency.
The Reward Machine Framework
The authors argue that since humans program these reward functions, there is no reason to hide the underlying structure from the agent. A Reward Machine is a type of finite state machine where states represent stages of a task. Transitions between these states are triggered by high-level events or propositions (e.g., "picked up coffee" or "reached the office").
As shown in the figure above, the Reward Machine provides a structured representation of the task. In the Office World example, the agent's progress is tracked through states $u_0$ and $u_1$. The transitions are governed by logical conditions, such as whether the agent has acquired coffee or reached the target location 'o'.
Algorithmic Approaches
To exploit the structure provided by RMs, the authors introduce three main methodologies:
- Counterfactual Experiences for Reward Machines (CRM): This "what if" approach allows for massive data reuse. When an agent takes an action, CRM generates synthetic experiences for every possible state of the reward machine. For example, if an agent picks up a key, it simultaneously learns how that action would have affected its progress regardless of whether it was currently looking for the key, standing by the door, or just starting the level.
- Hierarchical RL for Reward Machines (HRM): This method automatically decomposes a task into sub-problems based on the RM's edges. The agent learns "options" (sub-policies) to move between RM states and a high-level policy to sequence them effectively.
- Automated Reward Shaping (RS): The authors use value iteration on the RM itself—treating the machine as a small, discrete Markov Decision Process—to create a "potential function." This provides the agent with intermediate rewards as it moves closer to task completion, effectively "warming up" the sparse reward signal.
Experimental Evaluation
The researchers evaluated their methods across four distinct environments:
- Office World: A discrete gridworld where agents must perform tasks like "deliver coffee and mail while avoiding decorations."
- Minecraft-like World: A gridworld focused on resource gathering and tool crafting (e.g., "get wood, use toolshed, get grass, use workbench").
- Water World: A continuous state-space environment where an agent must touch colored balls in specific sequences.
- HalfCheetah: A continuous control benchmark modified for multi-stage tasks, such as running back and forth between two points.
Key Insights and Results
The core insight of this work is that the reward function is a program, not just a number. By representing it as an RM, the agent gains the expressive power of regular languages, allowing it to understand loops, sequences, and conditionals.
In terms of performance, the RM-based agents (CRM and HRM) demonstrated remarkable sample efficiency. In the Minecraft domain, they learned successful policies in a fraction of the time required by baselines. In the HalfCheetah "back and forth" task, CRM completed an average of 9 laps per episode, while the standard DDPG baseline failed to find a better-than-random policy even after 30 million steps of computation. Furthermore, CRM and HRM consistently outperformed "cross-product" baselines that simply append the RM state to the environment state without exploiting the transition structure.
Limitations and Future Work
While powerful, RMs rely on "event detectors" to signal when high-level propositions are true. If these detectors are noisy or unavailable, the system's performance degrades. Additionally, CRM and HRM are more computationally expensive per step because they perform multiple Q-updates for every environment interaction.
A major future direction is RM Induction, where the agent learns the structure of the reward machine itself from raw experience rather than relying on human-provided machines. Moving beyond regular languages to more complex grammars could also allow for even more sophisticated task specifications.
Summary updated