Skip to main content

Letter World Cross-Product Example

This example demonstrates how the cross-product MDP behaves like a standard Gymnasium environment while adding the power of RM/CRMs.

The Letter World Environment

The Letter World is a simple grid environment where an agent navigates to find specific letters:
  • Letter ‘A’ has a 50% chance of turning into letter ‘B’ when visited
  • Letter ‘C’ gives a reward when visited after seeing letter ‘B’
  • The agent must learn to visit ‘A’, hope it turns into ‘B’, and then visit ‘C’
Here’s what the environment looks like:
Where:
  • A represents letter ‘A’ (or ‘B’ after transformation)
  • C represents letter ‘C’
  • x represents the agent

Components

To create our cross-product environment, we need several components:
  1. Ground Environment: The basic grid world (LetterWorld)
  2. Labelling Function: Maps transitions to symbols (LetterWorldLabellingFunction)
  3. Counting Reward Machine: Defines rewards based on symbol history (LetterWorldCountingRewardMachine)
  4. Cross-Product: Combines all the above (LetterWorldCrossProduct)

Setting Up the Environment

First, let’s import the necessary components and create our environment:

Using the Environment Like a Standard Gym Environment

The cross-product environment works just like any other Gymnasium environment:
Output:
The observation is structured as:
  • First part: Ground observation (symbol_seen, agent_row, agent_col)
  • Last part: Machine configuration (in this example, this is the state and counter values)
The CrossProduct class provides a default _get_obs that concatenates the ground observation with a one-hot encoded machine state and raw counter values. This Letter World example overrides it with a custom tabular encoding. You can override _get_obs to define the cross-product state representation as you wish.

Running an Episode

Let’s run a full episode with the cross-product environment:
Sample output:

Using a Specific Action Sequence

You can also execute a specific sequence of actions:
Sample output for this sequence:

What Makes It Special?

The cross-product environment extends a standard Gym environment with:
  1. Symbol Tracking: It tracks which symbols have been seen
  2. Counter Values: It maintains counters as defined by the CRM if one is being used
  3. State Memory: The reward can depend on the history of previously seen symbols
  4. Reward Shaping: Complex reward signals based on achieving specific goals

Conclusion

The cross-product environment combines the simplicity of standard Gym environments with the power of RM/CRMs. This allows you to:
  1. Use it with any RL algorithm designed for Gymnasium environments
  2. Define complex reward structures based on symbol history
  3. Track progress toward multi-step goals
  4. Shape rewards to guide exploration and learning
  5. Benefit from the sample efficiency of counterfactual experiences
This example demonstrates that using RM/CRMs doesn’t require changing your existing RL algorithms, it just gives you more expressive power in defining rewards!

Next Steps