Deep Reinforcement Learning (DRL) has achieved incredible feats, from mastering complex games to controlling robotic limbs. However, DRL policies are often "black boxes"—massive neural networks that are nearly impossible for humans to interpret and notoriously brittle when faced with scenarios slightly different from their training.
A new paper from researchers at National Taiwan University, titled "Hierarchical Programmatic Reinforcement Learning via Learning to Compose Programs," offers a compelling alternative. By combining the structure of computer programs with the flexibility of hierarchical reinforcement learning, they have created a system that does not just learn a task—it writes a human-readable script to solve it.
The Problem with Current Programmatic RL
Traditional DRL produces policies that are hard to trust because we cannot "read" why they make certain decisions. Programmatic RL aims to fix this by representing policies as source code in a Domain-Specific Language (DSL).
A recent state-of-the-art method called LEAPS (Learning Embeddings for lAtent Program Synthesis) attempted this by learning a continuous "embedding space" of programs and searching that space for a solution. However, LEAPS hit two major walls:
- Limited Distribution: It could only find programs similar in length and complexity to its pre-generated training dataset. It could not synthesize a long, complex program if it had only been trained on short ones.
- Poor Credit Assignment: It evaluated candidate programs based solely on their total return. If a program was 90% correct but failed at the very end, LEAPS struggled to identify which specific parts were working and which were not.
The Solution: Hierarchical Programmatic RL (HPRL)
The authors propose Hierarchical Programmatic Reinforcement Learning (HPRL) to solve these issues. The framework operates in three main stages:
- Learning a Program Embedding Space: Using a Variational Autoencoder (VAE) architecture, discrete program tokens (like
if frontIsClear: move()) are mapped into a continuous vector space. The authors improved this process by filtering out meaningless or repetitive programs from the training set, ensuring the latent space is diverse and behaviorally smooth. - Embedding Compression: Because high-dimensional action spaces are challenging for RL agents, a compression layer is added to squeeze the program embeddings into a compact 64-dimensional space.
- The Meta-Policy (The "Composer"): Instead of searching for one giant program, HPRL trains a meta-policy (using PPO or SAC). At each "macro" time step, the meta-policy observes the environment and picks a program embedding as an action. This embedding is decoded into a sub-program and executed. The process repeats, effectively stitching together a sequence of programs to solve the task.
Key Insights
- Compositionality is Key: By treating program synthesis as a sequential decision-making problem, the model can generate "out-of-distribution" programs. It can compose a 100-step behavior even if its VAE was only trained on 40-step snippets.
- Dense Rewards for Code: Because the meta-policy picks sub-programs sequentially, the system receives rewards after each sub-program finishes. This provides much clearer feedback and precise credit assignment compared to waiting for a single, monolithic program to finish executing.
- Temporal Abstraction: In HPRL, the "action" is not a primitive move like "turn left"; it is an entire logic block. This allows the agent to plan at a much higher level.
Datasets and Key Results
The study evaluates HPRL in the Karel domain, a classic educational programming environment where an agent navigates a grid, avoids walls, and manipulates markers. The authors used the standard six-task KAREL Problem Set and introduced KAREL-HARD, a more grueling benchmark demanding longer, logic-heavy programs.
HPRL significantly outperformed all baselines, including standard DRL and LEAPS:
- Perfect Scores: HPRL-PPO achieved a 1.00 mean return (perfect score) across all six standard Karel tasks.
- Solving the Hard Stuff: On the
SEEDERtask (placing markers on every empty grid), LEAPS achieved a score of 0.51, while HPRL-PPO reached 0.58. InONESTROKE, HPRL-PPO hit 0.80 compared to LEAPS' 0.65. - Out-of-Distribution Synthesis: In a controlled experiment to reconstruct target programs of varying lengths, HPRL maintained high accuracy for programs twice as long as anything in its training set. In contrast, LEAPS' performance plummeted once the target length exceeded its training data.
Limitations and Future Work
While HPRL is a major step forward, the authors note a few areas for future growth:
- Deterministic Environments: The current Karel domain is deterministic. Adapting this to stochastic environments (where actions might fail) would require the DSL to handle more complex error-checking logic.
- Fixed Horizon: Currently, the meta-policy composes a fixed number of programs. A more flexible approach would involve learning a "termination function" so the agent can dynamically decide when the script is complete.
- DSL Specificity: The method relies on a pre-defined Domain-Specific Language. Moving toward more general languages (like Python) using Large Language Models (LLMs) as the embedding space is a promising future direction.
Summary updated