CODEX: A Cluster-Based Method for Explainable Reinforcement Learning

Simon Khan PhD, Timothy K. Mathes, Jessica L. Inman, Andrés Colón, Simon KhanarXiv 2023 · DOI

Abstract

Reinforcement Learning (RL) has achieved superhuman performance in various domains, yet the internal logic of these agents often remains a "black box," hindering their deployment in high-stakes, real-world applications. We introduce CODEX (Counterfactual Demonstrations for Explanation), a framework designed to translate complex RL behaviors into concise, human-readable text summaries. By employing semantic clustering on natural language tags and world-model latent states, CODEX identifies the critical "milestones" of an agent's decision-making process. Our evaluation on MiniGrid and StarCraft II environments demonstrates that CODEX can condense long trajectories into essential narrative points with high efficiency and clustering quality, providing a scalable, unsupervised approach to post-hoc RL explanation.

Introduction

As RL agents move from games to robotics and healthcare, the need for transparency becomes paramount. Existing Explainable RL (XRL) methods frequently struggle to account for long-term objectives or the dynamic way an agent's actions influence future observations. This "trust gap" necessitates tools that not only summarize what an agent did but also allow for the exploration of counterfactuals—the "what if" scenarios that help humans build a robust mental model of the AI's logic. CODEX addresses this by creating an automated pipeline to extract semantic meaning from raw agent data.

Methodology

The CODEX framework utilizes a multi-step pipeline to convert environment states and agent actions into a narrative:

  1. Tagging: The system generates natural language tags based on the environment's state (e.g., "The player picks up the key"). In grid-based environments, image moments are used to determine agent orientation.
  2. Embedding: These tags are transformed into high-dimensional vector representations using pre-trained Transformer models such as BERT-base, BERTweet, or paraphrase-MiniLM.
  3. Clustering: Dimensionality reduction via UMAP and clustering via HDBSCAN group similar state-action pairs into semantic clusters that represent distinct phases of the task.
  4. Summarization: A Latent Dirichlet Allocation (LDA) topic model identifies the "exemplar" tag for each cluster. This extractive approach ensures the summary is grounded in actual agent states, preventing the hallucinations common in abstractive AI summaries.
  5. World Modeling: By integrating DreamerV2, a world model that learns latent representations, CODEX can visualize expected outcomes in both factual and counterfactual scenarios.

Experimental Results

Semantic Clustering and Compression

CODEX was tested on MiniGrid and StarCraft II minigames. A primary strength of the method is its high compression capability. In a MiniGrid episode consisting of 145 event tags, CODEX successfully condensed the behavior into just 11 essential milestones—a compression rate of approximately 7.6%—while maintaining the core narrative of the agent's mission.

Visual representation of semantic clusters and the resulting extractive summary in a MiniGrid environment.

Model Performance and Efficiency

The researchers compared several Transformer models for the embedding stage. While larger models like BERT provided slightly higher silhouette scores, the paraphrase-MiniLM model emerged as the most practical choice. It produced episode embeddings in just 0.059 seconds, making it suitable for real-time or edge-device applications without significantly sacrificing clustering quality.

Quantitative comparison of embedding models based on clustering metrics.

As shown in the table above, all tested models achieved high Silhouette Coefficients (above 0.90) and Global Cosine Similarity scores (above 0.96), indicating that the semantic clusters are both distinct and internally consistent.

A significant finding was the alignment between the agent's internal "thoughts" (latent representations in DreamerV2) and the text-based semantic clusters. In MiniGrid, the moment a door is opened serves as a clear transition point in the agent's internal state space, which is accurately captured by the semantic clustering. This suggests that language-based summaries can faithfully represent the underlying logic of the world model.

Conclusion and Future Work

CODEX provides a robust, unsupervised framework for making RL agents explainable. By bridging the gap between latent state spaces and natural language, it allows researchers to audit agent behavior efficiently. Future work will focus on automating the state-tagging process through advanced computer vision and scaling the latent clustering techniques to more complex, high-dimensional environments like full-scale StarCraft II matches. Additionally, interactive tools that allow users to adjust cluster granularity could provide even deeper insights into agent decision-making.

Summary updated

BibTeX
@misc{phd2023codex,
  author = {Simon Khan PhD and Timothy K. Mathes and Jessica L. Inman and Andrés Colón and Simon Khan},
  title = {{CODEX: A Cluster-Based Method for Explainable Reinforcement Learning}},
  year = {2023},
  eprint = {2312.04216},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.04216},
}
Figure