RL4CO

repository·main·Indexed 21 days ago

https://github.com/ai4co/rl4co

An extensive Reinforcement Learning (RL) for Combinatorial Optimization (CO) benchmark and unified framework designed to decouple RL science from engineering. Version 0.6.0 provides tools for training and evaluating models on problems such as TSP, CVRP, MTVRP, and FJSP, supporting standard datasets like TSPLIB and CVRPLIB. It features modular model construction, various decoding strategies (greedy, beam search, sampling), and integration with Hydra for configuration management.

Tokens
20.8K
Snippets
70
Records
120
Agent score
75%

What's inside rl4co

  1. Browse RL4CO Examples by Category

    main

    Beyond the quickstarts, the examples/ directory is organized into specialized folders for different use cases:

    • Modeling: Located in modeling/, these examples focus on model architecture and inference.
    • Datasets: Located in datasets/, these examples demonstrate how to use custom datasets for training and evaluation.
    • Advanced Topics: Located in advanced/, these include complex configurations (such as Hydra tutorials) and advanced techniques.
    • Other: Located in other/, containing miscellaneous examples.
  2. Available Routing Problem Environments and Generators

    main

    The rl4co.envs.routing module provides a collection of environments and generators for various routing problems. To solve a routing problem using Reinforcement Learning, you typically instantiate a specific Generator to create problem instances and a corresponding Env to define the MDP (Markov Decision Process) structure.

    Supported routing problems include:

    • Asymmetric Traveling Salesman Problem (ATSP): Uses ATSPEnv and ATSPGenerator.
    • Capacitated Vehicle Routing Problem (CVRP): Uses CVRPEnv and CVRPGenerator.
    • Multiple Traveling Salesman Problem (mTSP): Uses MTSPEnv and MTSPGenerator.
    • Orienteering Problem (OP): Uses OPEnv and OPGenerator.
    • Pickup and Delivery Problem (PDP): Uses PDPEnv and PDPGenerator.
    • Prize Collecting Traveling Salesman Problem (PCTSP): Uses PCTSPEnv and PCTSPGenerator.
    • Split Delivery Vehicle Routing Problem (SDVRP): Uses SDVRPEnv.
    • Stochastic Prize Collecting Traveling Salesman Problem (SPCTSP): Uses SPCTSPEnv.
    • Traveling Salesman Problem (TSP): Uses TSPEnv and TSPGenerator.
    • Multi-Task Vehicle Routing Problem (MTVRP): A comprehensive suite including 16 variants, using MTVRPEnv and MTVRPGenerator.
  3. Use EDA environments for Electronic Design Automation problems

    main

    The rl4co.envs.eda module provides environments for Electronic Design Automation (EDA) tasks. These environments are structured around specific placement problems, typically requiring a generator to create problem instances and an environment to manage the reinforcement learning task.

    Supported problem types include:

    • Decap Placement Problem (DPP)
    • Multi-port Decap Placement Problem (mDPP)
  4. Available Constructive Autoregressive Models in the Zoo

    main

    The rl4co model zoo provides several constructive autoregressive architectures for combinatorial optimization tasks. These models are typically composed of a model (the core architecture), a policy (the decision-making component), and often specialized encoder or decoder modules.

    Available model families include:

    • Attention Model (AM): Standard attention-based architecture.
    • Attention Model - PPO (AM-PPO): AM optimized specifically for Proximal Policy Optimization.
    • Heterogeneous Attention Model (HAM): Includes specialized encoder and attention modules.
    • Matrix Encoding Network (MatNet): Features encoder and decoder components.
    • Multi-Decoder Attention Model (MDAM): Utilizes multiple encoder and decoder modules.
    • POMO: Policy Optimization with Multiple Observers.
    • Pointer Network (PtrNet): Includes encoder, decoder, and critic modules.
    • SymNCO: Includes specialized losses implementations.
  5. Overview of RL4CO components

    main

    RL4CO is a unified framework for Reinforcement Learning (RL) applied to Combinatorial Optimization (CO). The library is structured into three primary components that work together to facilitate reproducible research:

    1. Environments: Represent the problem as a Markov Decision Process (MDP). These are built on top of TorchRL.
    2. Policies: The neural networks responsible for solving the CO problems by mapping states to actions. These are built on PyTorch.
    3. RL Algorithms (Models): The training processes used to optimize the policies. These are built on PyTorch Lightning.
  6. Configure feature embeddings for policies

    main

    RL4CO modularizes the process of transforming raw features into an embedding space via parametrized functions $\phi_\omega$. You can control these components independently or let the library select them automatically by passing the env_name to the policy.

    Supported embedding types include:

    • Node Embeddings $\phi_n$: Transforms $m_n$ node features from the feature space to the embedding space: $[B, N, m_n] \rightarrow [B, N, h]$.
    • Edge Embeddings $\phi_e$: Transforms $m_e$ edge features from the feature space to the embedding space: $[B, E, m_e] \rightarrow [B, E, h]$.
    • Context Embeddings $\phi_c$: Captures contextual information by transforming $m_c$ context features from the current decoding step $s_t$ to the embedding space: $[B, m_c] \rightarrow [B, h]$.
  7. Understand the RL implementation architecture in RL4CO

    main

    RL algorithms in rl4co are implemented as RL4COLitModule classes, which inherit from PyTorch Lightning's pl.LightningModule. This architecture provides several built-in capabilities for training:

    • Standard Lightning Lifecycle: Full support for train_step, val_step, and test_step methods.
    • Automatic Logging: Integration with logging services (e.g., Wandb) via the log_metrics method.
    • Optimizer Management: Automatic configuration through configure_optimizers.
    • RL-Specific Callbacks: Specialized hooks like on_train_epoch_end designed for reinforcement learning workflows.

    An RL algorithm acts as the bridge that takes an Environment (containing problem instances) and a Policy to optimize the policy parameters $\theta$.

  8. Distinguish between Inductive and Transductive RL

    main

    The library supports two primary modes of reinforcement learning:

    1. Inductive RL: The primary training phase where the model learns patterns from a training dataset to generalize to new, unseen problem instances. This amortizes the cost of inference.
    2. Transductive RL (Test-time optimization): A phase where parameters are optimized specifically for target instances during the testing/inference stage.

    A typical workflow involves training a policy $\pi$ using Inductive RL, followed by applying Transductive RL for fine-tuning on specific instances at test time.

  9. Understand the difference between Constructive and Improvement policies

    main

    In RL4CO, policies are categorized based on how they interact with problem instances and solutions:

    1. Constructive Policies: These generate a complete solution from scratch for a given problem instance $\mathbf{x}$. They are further divided into:

      • Autoregressive (AR) policies: Use an encoder to create embeddings and a decoder to iteratively determine a sequence of actions, where each action depends on previous actions and the current state.
      • Non-autoregressive (NAR) policies: Encode the problem into a heuristic (a vector of unnormalized probabilities) representing possible assignments. Solutions are obtained by sampling from this heuristic, often using dynamic masking to ensure feasibility.
    2. Improvement Policies: These take an existing initial solution $\mathbf{a}^{0}$ and iteratively refine it into a higher-quality solution $\mathbf{a}^k$ over a fixed budget of improvements $K$.

  10. Implement Nonautoregressive Policies

    main

    Non-autoregressive policies generate all elements of a solution simultaneously or in parallel, rather than sequentially. The base classes available are:

    • models.common.constructive.nonautoregressive.encoder: For encoding the input problem state.
    • models.common.constructive.nonautoregressive.decoder: For generating the solution components in parallel.
    • models.common.constructive.nonautoregressive.policy: The top-level policy class for non-autoregressive architectures.
  11. Use Improvement Methods to refine solutions

    main
    Improvement methods in rl4co are designed to iteratively refine existing solutions, functioning similarly to local search algorithms. Unlike generative models that build solutions from scratch, these methods focus on optimizing and improving a given initial solution.