To create an optimal RL policy feature extractor for the given observation space, we can design a PyTorch module that processes each group of entities (player blobs, pellets, and enemies) separately and then combines the extracted features. Here's how we can approach it:
-
Process the Player Blobs:
- Input Shape:
(batch_size, 16, 3)
- Mask Non-existent Blobs: Identify blobs where
mass == -1 and mask them out.
- Feature Extraction: Use an MLP (Multi-Layer Perceptron) to process each blob's features.
- Aggregation: Aggregate the blob features using an attention mechanism or pooling (e.g., mean or max).
-
Process the Pellets and Enemies:
- Input Shape:
(batch_size, 16, 3) for both pellets and enemies.
- Feature Extraction: Use separate MLPs to process pellets and enemies.
- Sequence Processing: Since pellets and enemies are ordered by distance, use an RNN or attention mechanism to process the sequences.
-
Combine Features:
- Concatenate the aggregated features from player blobs, pellets, and enemies.
- Pass the combined features through additional fully-connected layers to produce the final feature representation.
Here's the PyTorch code implementing the feature extractor:
import torch
import torch.nn as nn
import torch.nn.functional as F
class FeatureExtractor(nn.Module):
def __init__(self, embedding_size=64):
super(FeatureExtractor, self).__init__()
# Embedding size for the entities
self.embedding_size = embedding_size
# Player blobs embedding
self.player_mlp = nn.Sequential(
nn.Linear(3, embedding_size),
nn.ReLU(),
nn.Linear(embedding_size, embedding_size)
)
# Pellets embedding
self.pellet_mlp = nn.Sequential(
nn.Linear(3, embedding_size),
nn.ReLU(),
nn.Linear(embedding_size, embedding_size)
)
# Enemies embedding
self.enemy_mlp = nn.Sequential(
nn.Linear(3, embedding_size),
nn.ReLU(),
nn.Linear(embedding_size, embedding_size)
)
# Attention layers for player blobs, pellets, and enemies
self.player_attention = nn.MultiheadAttention(embed_dim=embedding_size, num_heads=4)
self.pellet_attention = nn.MultiheadAttention(embed_dim=embedding_size, num_heads=4)
self.enemy_attention = nn.MultiheadAttention(embed_dim=embedding_size, num_heads=4)
# Final MLP
self.final_mlp = nn.Sequential(
nn.Linear(embedding_size * 3, embedding_size),
nn.ReLU(),
nn.Linear(embedding_size, embedding_size)
)
def forward(self, observations):
# observations shape: (batch_size, 48, 3)
batch_size = observations.size(0)
# Split observations into player blobs, pellets, and enemies
player_blobs = observations[:, 0:16, :] # Shape: (batch_size, 16, 3)
pellets = observations[:, 16:32, :] # Shape: (batch_size, 16, 3)
enemies = observations[:, 32:48, :] # Shape: (batch_size, 16, 3)
# Process player blobs
# Mask non-existent blobs where mass == -1
player_masks = (player_blobs[:, :, 0] != -1).unsqueeze(-1) # Shape: (batch_size, 16, 1)
player_embeddings = self.player_mlp(player_blobs) * player_masks # Apply mask
# Permute for attention: (seq_len, batch_size, embedding_size)
player_embeddings = player_embeddings.permute(1, 0, 2)
# Apply attention
player_attn_output, _ = self.player_attention(player_embeddings, player_embeddings, player_embeddings)
# Aggregate features
player_features = player_attn_output.mean(dim=0) # Shape: (batch_size, embedding_size)
# Process pellets
pellet_embeddings = self.pellet_mlp(pellets)
pellet_embeddings = pellet_embeddings.permute(1, 0, 2)
pellet_attn_output, _ = self.pellet_attention(pellet_embeddings, pellet_embeddings, pellet_embeddings)
pellet_features = pellet_attn_output.mean(dim=0)
# Process enemies
enemy_embeddings = self.enemy_mlp(enemies)
enemy_embeddings = enemy_embeddings.permute(1, 0, 2)
enemy_attn_output, _ = self.enemy_attention(enemy_embeddings, enemy_embeddings, enemy_embeddings)
enemy_features = enemy_attn_output.mean(dim=0)
# Combine features
combined_features = torch.cat([player_features, pellet_features, enemy_features], dim=-1)
final_features = self.final_mlp(combined_features)
return final_features
Explanation:
This architecture allows the model to focus on the most relevant entities using attention mechanisms and effectively combines the information from different groups into a single feature vector suitable for the policy network.