Skip to content

Advanced Features

Graph Neural Networks for Observations

Overview

GymPN uses Graph Neural Networks (GNNs) to process Petri Net observations. The network structure directly represents the PN structure, enabling agents to reason about state transitions and dependencies.

Graph Representation

Each observation is represented as a heterogeneous graph with multiple node and edge types:

Node Types: - place: Represents places/variables in the PN - a_transition: Represents action transitions - e_transition: Represents evolution transitions (events) - token: Represents tokens currently in the system - postpone: Special node for postponement action (if enabled)

Edge Types: - input: Token to transition (prerequisite) - output: Transition to token (produces) - reset: Transition resets token

Available Network Architectures

GNNPolicyNetwork

Used for learning action probabilities:

from gympn.networks import GNNPolicyNetwork

policy_net = GNNPolicyNetwork(
    input_dim=64,  # Feature dimension (from metadata)
    hidden_dim=128,  # Hidden layer dimension
    output_dim=num_actions,  # Number of actions
    num_layers=2,  # Graph convolution layers
    dropout=0.0  # Dropout rate
)

Architecture: 1. Input layer: input_dim → hidden_dim 2. Graph convolutions: hidden_dim → hidden_dim (repeated num_layers times) 3. Output layer: hidden_dim → output_dim

GNNValueNetwork

Used for estimating expected returns:

from gympn.networks import GNNValueNetwork

value_net = GNNValueNetwork(
    input_dim=64,
    hidden_dim=128,
    num_layers=2,
    dropout=0.0
)

Output: Single scalar value per graph

Custom Network Design

You can design custom networks for your specific problem:

import torch
from torch_geometric.nn import HeteroConv, GCNConv
from torch_geometric.data import HeteroData

class CustomGNNPolicy(torch.nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim, num_layers=2):
        super().__init__()

        self.layers = torch.nn.ModuleList()
        for i in range(num_layers):
            in_dim = input_dim if i == 0 else hidden_dim
            self.layers.append(
                HeteroConv({
                    ('place', 'input', 'a_transition'): GCNConv(in_dim, hidden_dim),
                    ('place', 'output', 'a_transition'): GCNConv(in_dim, hidden_dim),
                    # ... more edge types
                }, aggr='add')
            )

        self.head = torch.nn.Linear(hidden_dim, output_dim)

    def forward(self, data):
        x_dict = data.x_dict
        edge_index_dict = data.edge_index_dict

        for layer in self.layers:
            x_dict = layer(x_dict, edge_index_dict)
            x_dict = {key: x.relu() for key, x in x_dict.items()}

        # Aggregate action node features
        a_trans_features = x_dict['a_transition']
        action_logits = self.head(a_trans_features)
        return action_logits

Getting Feature Dimensions

Use make_metadata() to get required dimensions:

metadata = env.make_metadata()

print(f"Node types: {metadata['node_types']}")
print(f"Feature dim: {metadata['features']}")
print(f"Num actions: {metadata['num_actions']}")
print(f"Has postpone: {metadata['has_postpone']}")

Distributed Training (Parallelization)

Using Multiprocessing

GymPN automatically parallelizes episode collection:

agent.train(
    env,
    episodes=64,
    epochs=100,
    num_workers=4  # Collect episodes in parallel
)

How it works: 1. Main process orchestrates training 2. Worker processes collect episodes independently 3. Episodes gathered into batch 4. Main process updates networks 5. Repeat

Performance Considerations

Speedup roughly scales with number of workers:
- 1 worker: baseline
- 4 workers: ~3.5x speedup
- 8 workers: ~7x speedup
- Beyond 8: diminishing returns (GIL, communication overhead)

Recommended settings:

# For CPU-bound (most cases)
num_workers = min(4, cpu_count() - 1)

# For GPU (set use_gpu=True)
num_workers = min(2, cpu_count() - 1)  # Smaller to avoid memory issues

Memory Efficiency

To reduce memory per worker:

config = {
    "num_workers": 4,
    "episodes": 64,           # Divide by number of workers for same total
    "batch_size": 64,         # Can be larger than episodes if distributed
    "use_gpu": False,         # CPU is more memory efficient for workers
}

Graph Visualization

Visualizing Network Structure

from gympn.visualisation import Visualisation

vis = Visualisation(env)

# Show network structure
vis.plot_net()

# Show a specific marking
vis.plot_marking(observable_net)

# Animate episode
vis.animate_episode(
    env,
    solver,
    max_steps=100,
    save_path='episode.gif'
)

Custom Visualization

import matplotlib.pyplot as plt
import networkx as nx

# Get network structure
places = [v.name for v in env.variables]
transitions = [t.name for t in env.events + env.actions]

# Build graph
G = nx.DiGraph()
G.add_nodes_from(places, node_type='place')
G.add_nodes_from(transitions, node_type='transition')

# Add edges based on preconditions/postconditions
for action in env.actions:
    for precond in action.preconditions:
        G.add_edge(precond.name, action.name)
    for postcond in action.postconditions:
        G.add_edge(action.name, postcond.name)

# Draw
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
plt.show()

Custom Reward Functions

Complex Reward Design

def complex_reward(token_binding):
    """
    Example: Multi-objective reward function
    Rewards completion but penalizes lateness
    """
    task, resource = token_binding

    # Base completion reward
    reward = 1.0

    # Penalty for using slow resource
    if resource['code_employee'] == 1:  # Slow employee
        reward -= 0.3

    # Bonus for fast completion
    if resource['code_employee'] == 0:  # Fast employee
        reward += 0.2

    return reward

agency.add_event(
    [busy], [employee],
    complete,
    name='complete',
    reward_function=complex_reward
)

Reward Shaping

To guide agent learning:

def shaped_reward(token_binding):
    """Reward with potential-based shaping."""
    task, resource = token_binding

    base_reward = 1.0

    # Potential-based shaping (safe for convergence)
    phi_current = compute_potential(task, resource)
    phi_next = 0  # Terminal state

    shaping = 0.99 * phi_next - phi_current

    return base_reward + shaping

def compute_potential(task, resource):
    """Compute potential based on task characteristics."""
    # Lower potential for harder tasks
    if task['task_type'] == 0:
        return 0.5  # Simple task
    else:
        return 1.0  # Complex task

Observation Filtering and Normalization

Custom Observations

class CustomObservationProcessor:
    def __init__(self, env):
        self.env = env

    def process(self, observable_net):
        """Convert observable net to custom representation."""
        # Get graph representation
        graph = self.env.make_observation(observable_net)

        # Add custom features
        graph.x_dict['place'] = self._add_place_features(
            graph.x_dict['place'],
            observable_net
        )

        return graph

    def _add_place_features(self, features, observable_net):
        """Add domain-specific features."""
        # Example: Add queue length as feature
        for i, place in enumerate(self.env.variables):
            tokens = observable_net.marking[place.name]
            queue_length = len(tokens)
            features[i, 0] = queue_length  # Feature slot 0 = queue length

        return features