Expressive Reward Synthesis with the Runtime Monitoring Language

Daniel Donnelly, Angelo Ferrando, Francesco Belardinelli

Introduction

In Reinforcement Learning (RL), the reward function is typically a "black box" mapping state-action pairs to scalar values. This lack of internal structure often hinders an agent's ability to learn long-horizon or history-dependent tasks. While Reward Machines (RMs) were introduced to address this by representing rewards as finite-state automata, they are fundamentally limited by the expressivity of regular languages. Standard RMs struggle with tasks involving counting (e.g., "perform action A exactly $N$ times") or parametric conditions (e.g., "if the first item was red, go to the red door"), as these require a manual definition of unique states for every possible parameter value, leading to a "state explosion."

This paper introduces RML Reward Machines, a framework that leverages the Runtime Monitoring Language (RML) to specify complex, non-regular reward functions. By utilizing RML’s built-in memory and parametric event handling, the authors enable agents to learn tasks that exceed the capabilities of traditional finite-state systems.

Methods: Integrating RML into the RL Loop

The core contribution of this work is the integration of RML monitors directly into the reinforcement learning process. The authors propose several technical enhancements to make this feasible:

  • RML-extended MDP: The environment is formally defined as a tuple where the agent’s state is augmented with the internal state of the RML monitor. This ensures that the environment remains Markovian from the agent's perspective; even if a reward depends on deep history, the current augmented state contains all necessary information for decision-making.
  • Automated Reward Shaping: To alleviate the problem of sparse rewards, the framework provides feedback based on two criteria: the monitor's verdict (e.g., Success or Failure) and state transitions. By granting small rewards whenever the RML monitor moves to a new internal state, the system provides automated reward shaping that signals progress toward the ultimate goal.
  • Parametric Event Handling: Unlike standard RMs that match fixed symbols, RML can capture variables from the environment—such as a specific distance $y$ or a count $n$—and store them in memory for use in future logic. This allows for highly flexible task specifications that do not grow in size relative to the parameters involved.

Experimental Evaluation

The researchers evaluated RML Reward Machines using variations of LetterEnv, a grid-world benchmark where agents must visit letters in specific sequences. Two primary variations were tested:

  1. Numerical LetterEnv: The agent observes a number $N$ at the start and must perform a specific action exactly $N$ times.
  2. Conditional LetterEnv: The agent's required behavior changes based on whether a count $N$ is above or below a specific threshold $M$.

Key Insights

  • Breaking the "Regular" Barrier: RML is more expressive than Linear Temporal Logic (LTL) and standard automata, handling context-free and even some non-context-free properties.
  • The Necessity of Visibility: A critical finding was that the visibility of the monitor state is the absolute requirement for convergence. Previous attempts failed because they kept the monitor's state hidden; by exposing it as part of the observation, the agent can distinguish between identical environment positions that represent different stages of a task.
  • Specification Efficiency: For tasks involving thresholds, RML Reward Machines maintain a constant specification size ($O(1)$). In contrast, standard RMs and Counting Reward Automata (CRA) require a number of branches that grows linearly ($O(M)$) with the threshold value.

Results and Performance

In numerical experiments, RML Reward Machines successfully learned tasks for values of $N$ up to 10. Existing CRA-based methods failed for $N > 3$ because they required every possible value to be pre-defined in the automaton. While RML Reward Machines were slightly slower than specialized "Counterfactual" Q-learning methods (which use optimizations not yet implemented in this framework), they proved significantly faster than standard Q-learning and far more robust to varying task parameters.

Limitations and Future Directions

Because RML can store unbounded counters, the monitor's state space could theoretically become infinite, which challenges the convergence guarantees of traditional tabular RL. The authors suggest that Deep RL (function approximation) will be essential for handling these large or continuous state representations in future iterations. Additionally, incorporating counterfactual reasoning—simulating alternative outcomes based on different actions—remains a promising avenue for further optimizing learning speed.

Code Availability: https://github.com/danieldonnelly7/rml_reward_machines

Summary updated

BibTeX
@inproceedings{donnelly2025expressive,
  author = {Daniel Donnelly and Angelo Ferrando and Francesco Belardinelli},
  title = {{Expressive Reward Synthesis with the Runtime Monitoring Language}},
  booktitle = {PRIMA},
  year = {2025},
}
Figure