Labelling Functions
Labelling functions bridge the gap between low-level environment observations and high-level symbolic events that RM/CRMs understand.
Overview
In the RM/CRM framework, labelling functions play a critical role in abstracting raw environment observations into meaningful symbolic events. These events form the vocabulary that the RM/CRM uses to define rewards and transitions.Core Concept
A labelling function maps an environment transition (observation, action, next observation) to a set of symbolic events (a truth assignment for a set of propositions). This abstraction layer provides several benefits:- Modularity: Separate environment dynamics from task specification
- Reusability: Same environment can support different tasks by changing the labelling
- Expressiveness: Define complex tasks in terms of abstract events rather than raw states
Implementation
Base Class
The RM/CRM framework provides a baseLabellingFunction
class that handles the core functionality:
- Type parameters for observation and action types
- An
event
decorator to identify event detection methods - A
__call__
method that runs all event detectors on a transition
Creating a Labelling Function
To create a custom labelling function, follow these steps:-
Define your events as an Enum:
-
Create a subclass of
LabellingFunction
: -
Implement event detection methods with the
@event
decorator:
- Accept the parameters
(self, obs, action, next_obs)
- Return an Enum value when the event is detected
- Return
None
when the event is not detected
How It Works
When you call a labelling function withlf(obs, action, next_obs)
, it:
- Looks for all methods decorated with
@LabellingFunction.event
- Calls each method with the provided transition
- Collects all non-None results into a set
- Returns this set of detected events
Example: Letter World
Let’s examine a complete example from the Letter World environment:- Defines three symbols (A, B, C) as events
- Creates a labelling function that works with numpy array observations and integer actions
- Implements three event detection methods, each checking specific conditions
- Returns the appropriate symbol when conditions are met
Using Labelling Functions
Once defined, you use a labelling function as part of a cross-product environment:Best Practices
When creating labelling functions:- Keep events meaningful: Define events that have semantic meaning for your task
- Be consistent: Ensure event detection is consistent and deterministic
- Handle edge cases: Consider all possible transitions and what they should mean
- Document conditions: Clearly document when each event is triggered
- Use clear naming: Name your event detection methods descriptively
Summary
Labelling functions are a powerful abstraction mechanism in the RM/CRM framework. They:- Convert low-level observations to high-level events
- Allow for modular task specification
- Enable the same environment to support different tasks
- Provide a clean separation between environment dynamics and reward logic