Counting Reward Automata: Sample Efficient Reinforcement Learning Through The Exploitation of Reward Function Structure

Tristan Bester, Benjamin Rosman, Steven James, Geraud Nangue Tasse

TL;DR

Existing state machine approaches in reinforcement learning (like Reward Machines) struggle with tasks that require counting or memory, because they are limited to regular languages. This paper introduces Counting Reward Automata (CRA), which augment reward state machines with integer counters, unlocking the ability to represent any computable reward function (up to Turing-complete languages). Paired with a counterfactual Q-learning algorithm, CRAs dramatically reduce automaton complexity and boost sample efficiency on complex, long-horizon tasks.

What Problem Does This Paper Address?

Reinforcement learning (RL) often struggles with long-horizon, temporally extended tasks where rewards depend on past history (non-Markovian rewards). While Reward Machines (RMs) use finite state machines to expose reward structure to the agent, they are fundamentally restricted to regular languages.

This is a severe bottleneck: an RM cannot natively express basic counting behaviors (such as "pick up $N$ items, then perform $N$ corresponding deliveries"). To handle even bounded counting, standard RMs require duplicating states for every possible count value, causing the automaton's size to explode and forcing agents to learn separate policies for different task lengths.

Methods

The authors introduce Counting Reward Automata (CRA) and an associated learning framework:

  • CRA Formulation: Based on counter machines (finite automata equipped with $k$ integer counters that can be incremented, decremented, and tested for zero). Because a 2-counter machine is Turing-complete, a CRA can represent any reward function expressible in formal grammar hierarchies (regular, context-free, and context-sensitive).
  • Automaton-Augmented MDP (AAMDP): Combines the ground environment state $s$ with the CRA configuration $\langle u, c \rangle$ (machine state $u$ and counter vector $c$), restoring the Markov property over the augmented state space.
  • Counterfactual Q-Learning (CQL): Extends counterfactual reasoning from RMs to counter machines. For every physical transition $\langle s, a, s' \rangle$ experienced in the environment, CQL simulates what transition and reward would have occurred had the CRA been in any other valid internal configuration $\langle u_i, c_j \rangle$. This synthetic experience allows off-policy updates across many machine states at once.
  • LLM-Driven Specification: Demonstrates that Large Language Models (e.g., ChatGPT) can translate natural language task descriptions into formal specifications and corresponding automata.

Datasets / Environments

The authors evaluated their approach on two simulated benchmarks:

  1. LetterEnv: A grid environment where an agent observes letter symbols. Configured for a context-free language (CFL) task: $L = \{A^N B C^N : N \in \mathbb{N}\}$ (or $A^N B C D^N$), where the symbol $A$ vanishes after $N$ visits and the agent must visit $C$ exactly $N$ times.
  2. Office Gridworld: A navigation grid with obstacles ("decorations"), a mail room, a coffee machine, and an office destination. Configured for a context-sensitive task: collect an arbitrary $N$ pieces of mail, brew $N$ coffees, and deliver both to office employees without breaking decorations.

Key Insights and Ideas

  • Exponential Compression of Automata: Instead of constructing hundreds or thousands of explicit states and edges for varying task lengths $N$, a CRA handles variable-length tasks with a tiny, static state machine (e.g., 3 non-terminal states: Fetch Mail, Make Coffee, Deliver), leaving the quantity tracking to integer counters.
  • Generalization Across Problem Instances: Because the counter abstracts the sequence length $N$, a single trained CRA policy solves the task for any $N$, whereas classical RMs require learning distinct policies for each specific count.
  • Data-Free Counterfactuals: Because automaton state transitions and reward functions are known symbolic models, counterfactual replay generates free training signals for unvisited automaton states without requiring an environmental dynamics model.

Key Results

  • Automaton Complexity: In the Office Gridworld task, the number of states and transitions required by an RM grew rapidly with task string length $N$ (exceeding $10^4$ edges as $N$ scaled toward 500), while the CRA configuration remained constant at 3 non-terminal states and a handful of transitions.
  • Sample Efficiency:
  • On the context-free LetterEnv task, CQL learned a single policy solving all $N$, significantly outperforming baselines (CRM and CRM with Reward Shaping), which required retraining or scaled poorly.
  • In Office Gridworld, CQL reached near-perfect episode reward ($\approx 0.9$) within roughly 12,000 episodes, whereas standard Q-learning and baseline RM approaches learned much more slowly or failed to make progress.
  • Function Approximation: Combined with Deep Q-Networks (DQN), CRA specifications derived via natural language prompts achieved optimal reward within 10,000–12,000 environment steps.

Limitations and Future Work

  • Counter State Space Bounds: Tabular CQL loops over all counter values $\{1, \dots, \Gamma\}^k$ per step. While bounded by trajectory horizon $H$ and partially mitigated by caching observed counter states, naive counterfactual iteration could become expensive if the number of counters $k$ or the horizon $\Gamma$ grows large.
  • Automated Non-Regular Synthesis: Although the paper shows LLMs translating simple regular language tasks into automata, automated synthesis of full counter machines (handling increments, decrements, and zero-tests) directly from complex natural language remains an open area for end-to-end automation.

Summary updated

BibTeX
@inproceedings{bester2024counting,
  author = {Tristan Bester and Benjamin Rosman and Steven James and Geraud Nangue Tasse},
  title = {{Counting Reward Automata: Sample Efficient Reinforcement Learning Through The Exploitation of Reward Function Structure}},
  booktitle = {NuCLeaR @ AAAI},
  year = {2024},
  url = {https://openreview.net/forum?id=XfXLM8uIxH},
}
Figure