Abstract
Reward machines (RMs) are automata structures that encode (non-Markovian) reward functions for reinforcement learning (RL). RMs can reward any behaviour representable in regular languages and, when paired with RL algorithms that exploit RM structure, have been shown to significantly improve sample efficiency in many domains. In this work, we present pushdown reward machines (pdRMs), an extension of reward machines based on deterministic pushdown automata. pdRMs can recognise and reward temporally extended behaviours representable in deterministic context-free languages, making them more expressive than reward machines. We introduce two variants of pdRM-based policies, one which has access to the entire stack of the pdRM, and one which can only access the top k symbols (for a given constant k) of the stack. We propose a procedure to check when the two kinds of policies (for a given environment, pdRM, and constant k) achieve the same optimal state values. We then provide theoretical results establishing the expressive power of pdRMs, and space complexity results for the proposed learning problems. Lastly, we propose an approach for off-policy RL algorithms that exploits counterfactual experiences with pdRMs. We conclude by providing experimental results showing how agents can be trained to perform tasks representable in deterministic context-free languages using pdRMs.
Introduction
In standard Reinforcement Learning (RL), rewards are typically Markovian, depending only on the current state and action. However, many real-world tasks are non-Markovian, where the reward depends on the history of the agent's actions. While Reward Machines (RMs) were introduced to address this using finite-state automata, they are limited to regular languages. This work introduces Pushdown Reward Machines (pdRMs), which utilize a stack-based memory to handle context-free tasks—such as retracing a path or matching event counts—more efficiently than previous methods.
Pushdown Reward Machines (pdRMs)
The core innovation of this work is the augmentation of a standard Reward Machine with a stack, transforming it into a Deterministic Pushdown Automaton (DPDA). This allows the reward function to recognize Deterministic Context-Free Languages (DCFLs), which are more expressive than the regular languages handled by standard RMs.
To address the potential infinite state space created by a stack, the authors introduce $k$-policies. In this framework, the agent only observes the top $k$ symbols of the stack. For many tasks, such as path retracing, a small $k$ (e.g., $k=1$) is sufficient to reach an optimal solution, as the agent only needs to know the most recent action to determine the next step in reverse.
Learning Algorithms: CpRM
The authors propose CpRM (Counterfactual pdRM), an off-policy learning algorithm designed to exploit the structured memory of the pdRM. When an agent experiences a transition, CpRM performs counterfactual updates by "imagining" the outcome of that transition from all possible pdRM states and stack configurations. This approach significantly boosts sample efficiency by maximizing the information extracted from every interaction.
Key Results
The effectiveness of pdRMs was evaluated across several environments, including LetterEnv and MazeEnv. In LetterEnv, which requires matching counts of different events, agents using CpRM converged to optimal rewards significantly faster than standard Q-learning or counter-based baselines.
In navigation tasks like MazeEnv, $1$-policies demonstrated remarkable scalability. In large $20 \times 20$ mazes, the $1$-policy was the only method that successfully learned to retrace paths, while full-stack policies and counter-based agents failed due to the state-space explosion. Furthermore, in continuous environments like WaterWorld, pdRM-based agents outperformed Recurrent PPO agents, suggesting that structured stack memory is more effective than unstructured neural memory (like LSTMs) for these tasks.
Conclusion
Pushdown Reward Machines provide a powerful "sweet spot" for defining complex rewards. By providing agents with structured memory via a stack, pdRMs enable the solution of context-free tasks that were previously difficult or impossible for standard RMs. Future research will focus on the automated synthesis of pdRMs from high-level specifications or raw data.
Summary updated