pymarl3

repository·main·Indexed 41 days ago

https://github.com/tjuhaoxiaotian/pymarl3

An extension of pymarl2 implementing the Hyper Policy Network (HPN) architecture. It provides Multi-Agent Reinforcement Learning (MARL) algorithms with permutation invariance and equivariance, supporting integration with VDN, QMIX, MADDPG, and MAPPO. The framework includes support for SMAC-V1 and SMAC-V2 benchmarks and enables transfer learning across tasks with different agent counts.

Tokens
16.8K
Snippets
61
Records
77
Agent score
51%

What's inside pymarl3

  1. What is the Hyper Policy Network (HPN) architecture?

    main

    The Hyper Policy Network (HPN) is a general-purpose architecture designed to equip Multi-Agent Reinforcement Learning (MARL) algorithms with permutation invariance (PI) and permutation equivariance (PE) properties. It uses hypernetworks to generate customized weights for different input components, ensuring that the same input always receives the same weight, regardless of order.

    Key components include:

    • Permutation Invariant Input Layer: A shared hypernetwork takes an input set $X_j$ (a batch of $m$ inputs of dimension $k$) and generates unique weights $W_i$ for each input $x_i$. The outputs are merged using a permutation-invariant 'sum' function.
    • Permutation Equivariance Output Layer: To maintain equivariance, the weights and biases for agent-related actions (e.g., $\mathcal{A}_i^{\text{attack}}$) are also generated via a hypernetwork. Because the hypernetwork maintains a one-to-one mapping between inputs and weights, changing the input order results in the same change in output order.

    HPN can be integrated into existing algorithms like VDN, QMIX, MADDPG, and MAPPO to improve learning speed and performance.

  2. What is API-HPN and how does it work?

    main

    API-HPN (Agent Permutation Invariant Hypernetwork) is a neural architecture designed to boost Multi-Agent Reinforcement Learning (MARL) by exploiting permutation invariance and equivariance.

    In MARL environments with homogeneous components (like agents or NPCs), the order of inputs shouldn't change the underlying information. API-HPN addresses this using two main modules:

    1. Agent Permutation Invariant Input Layer: Uses a shared hypernetwork to generate unique weights $W_i$ for each input component $x_i$. Each component is processed by its specific weight, and the results are merged using a permutation-invariant 'sum' function. This ensures that shuffling the input components does not change the output.
    2. Agent Permutation Equivariance Output Layer: For outputs that have a one-to-one correspondence with input components (e.g., attack actions targeting specific enemies), a hypernetwork generates weights such that permuting the inputs results in the same permutation of the outputs.

    API-HPN is designed to be a general-purpose component that can be integrated into existing MARL algorithms like VDN, QMIX, MADDPG, and MAPPO to reduce sample complexity and improve convergence.

  3. How HPN handles variable inputs and transfer learning

    main

    Because HPN achieves permutation invariance and equivariance, it naturally handles a variable number of inputs and outputs. This makes it suitable for:

    • Multitask Learning: Designing more efficient models for multiple tasks.
    • Transfer Learning: Directly transferring a learned HPN policy from one task to a new task with a different number of agents. For example, a policy trained on 5m agents can serve as a superior initialization for a 12m agent task compared to training from scratch.
  4. Understand Permutation Invariance and Equivariance in MARL

    main

    To use API-HPN effectively, it is important to distinguish between the two types of permutation properties used in the architecture:

    • Permutation Invariant Function: A function where permuting the input components does not change the output. Mathematically: $f([x_1, x_2, …, x_m]^T) = f(M[x_1, x_2, …, x_m]^T)$, where $M$ is a permutation matrix.
    • Permutation Equivariant Function: A function where permuting the input components results in the same permutation of the output components. Mathematically: $f(M[x_1, x_2, …, x_m]^T) = M[y_1, y_2, …, y_m]^T$.

    Application Example (SMAC): In the StarCraft II micromanagement benchmark (SMAC), an agent's observation can be split into an ally group and an enemy group.

    • Equivariance: Q-values for attack actions (which correspond to specific enemies) should be equivariant to permutations of the enemy group.
    • Invariance: Q-values for move actions (which are general to the agent) should be invariant to the permutations of the entire set.
  5. Compare SMAC-V1 and SMAC-V2 environments

    main

    The pymarl3 repository supports both SMAC-V1 and SMAC-V2 environments without requiring separate installations.

    SMAC-V1

    Standard StarCraft II micromanagement benchmark.

    SMAC-V2

    Introduces additional randomness to challenge MARL algorithms:

    • Random Start Positions: Includes surrounded types (allied units in the middle, enemies surrounding) and reflect_position scenarios (allied and enemy positions are reflections of each other).
    • Random Unit Types: Units are spawned based on pre-fixed probabilities for each race (Protoss, Terran, Zerg), rather than always featuring the same unit types.
    • Note on Field-of-View: While SMAC-V2 typically restricts field-of-view to a cone, pymarl3 keeps the field-of-view and attack as a full circle to focus on PI/PE properties rather than exploration efficiency.
  6. Install and configure SMACv2

    main

    To use the SMACv2 environments in this project, you must first install SMACv2 following the official instructions at https://github.com/oxwhirl/smacv2.

    Important Configuration Step: After installation, ensure that your StarCraft II maps are placed in the correct directory so the environment can locate them: smac/env/starcraft2/maps/SMAC_Maps

    # Note that make sure to put the maps in `smac/env/starcraft2/maps/SMAC_Maps` to
  7. Install the pymarl3 repository

    main

    To set up the environment, follow these three steps:

    1. Clone the repository: Download the source code and enter the directory.
    2. Install StarCraft II: Run the provided script to install StarCraft II along with custom maps.
    3. Install Python dependencies: Run the dependency script to install PyTorch and other required Python packages.

    Ensure you have the necessary permissions to execute the .sh files by using chmod +x.

    # Step-1: Clone the repository and enter the folder.
    git clone git@github.com:tjuHaoXiaotian/pymarl3.git
    cd pymarl3
    
    # Step-2: Install StarCraftII add the custom maps.
    chmod +x install_sc2.sh
    ./install_sc2.sh
    
    # Step-3: Install PyTorch and other Python packages.
    chmod +x install_dependencies.sh
    ./install_dependencies.sh
  8. Run experiments using the command line tool

    main

    To reproduce experimental results, use the src/main.py script. You must specify an algorithm configuration using --config and an environment configuration using --env-config.

    Configuration File Locations:

    • --config targets files located in src/config/algs.
    • --env-config targets files located in src/config/envs.

    Example: Running VDN on SMAC (5m_vs_6m scenario)

    CUDA_VISIBLE_DEVICES="0" python src/main.py --config=api_vdn --env-config=sc2 with env_args.map_name=5m_vs_6m obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6

    Example: Running QMIX on SMAC (5m_vs_6m scenario)

    CUDA_VISIBLE_DEVICES="1" python src/main.py --config=api_qmix --env-config=sc2 with env_args.map_name=5m_vs_6m obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6
    # For SMAC, take the 5m_vs_6m scenario for example.
    CUDA_VISIBLE_DEVICES="0" python src/main.py --config=api_vdn --env-config=sc2 with env_args.map_name=5m_vs_6m obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6
    
    CUDA_VISIBLE_DEVICES="1" python src/main.py --config=api_qmix --env-config=sc2 with env_args.map_name=5m_vs_6m obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6
  9. Reproduce experimental results on SMAC-V2

    main

    To reproduce results on SMAC-V2, use the sc2_v2_terran, sc2_v2_protoss, or sc2_v2_zerg environment configurations. These experiments typically compare hpn_vdn against the standard vdn baseline using the mixer=vdn argument.

    # Example for sc2_v2_terran using hpn_vdn
    CUDA_VISIBLE_DEVICES="1" python src/main.py --config=hpn_vdn --env-config=sc2_v2_terran with obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6 mixer=vdn
  10. Reproduce experimental results on SMAC-V1

    main

    You can reproduce results for various scenarios (e.g., 5m_vs_6m, 3s5z_vs_3s6z, 6h_vs_8z, etc.) using src/main.py.

    Common arguments used in these commands include:

    • --config: Specifies the model configuration (e.g., hpn_qmix, qmix, hpn_qplex, qplex).
    • --env-config: Specifies the environment configuration (e.g., sc2).
    • with: Used to override specific hyperparameter values in the config.
    • env_args.map_name: Sets the specific StarCraft II map.
    • obs_agent_id: Boolean to include agent ID in observations.
    • obs_last_action: Boolean to include last action in observations.
    • runner: Specifies the runner type (e.g., parallel).
    • batch_size_run: Number of environments to run in parallel.
    • buffer_size: Size of the replay buffer.
    • t_max: Total training steps.
    • epsilon_anneal_time: Time steps for epsilon annealing.
    • batch_size: Training batch size.
    • td_lambda: TD lambda value.

    Note: For some maps like 6h_vs_8z or 3s_vs_5z, additional parameters like hpn_head_num may be required.

    # Example for 5m_vs_6m using hpn_qmix
    CUDA_VISIBLE_DEVICES="0" python src/main.py --config=hpn_qmix --env-config=sc2 with env_args.map_name=5m_vs_6m obs_agent_id=True obs_last_action=False runner=parallel batch_size_run=8 buffer_size=5000 t_max=10050000 epsilon_anneal_time=100000 batch_size=128 td_lambda=0.6
  11. Configure algorithms and environments via config files

    main

    The project uses configuration files to set defaults for algorithms and environments. These files are organized in the src/config directory:

    • Algorithm Configs: Located in src/config/algs. Use the --config flag to select one (e.g., --config=api_vdn).
    • Environment Configs: Located in src/config/envs. Use the --env-config flag to select one (e.g., --env-config=sc2).

    You can override specific parameters within these configs using the with keyword followed by key-value pairs (e.g., with env_args.map_name=5m_vs_6m).

  12. How agent visibility and FOV work

    main

    The environment implements visibility based on two modes:

    1. Circular Range: An entity is visible if its distance is less than the agent's unit_sight_range.
    2. Conic FOV: If conic_fov is enabled, an entity is visible only if it is within the unit_sight_range and falls within the agent's field-of-view angle (conic_fov_angle).

    Key methods for visibility:

    • get_visibility_matrix(): Returns a boolean matrix (n_agents, n_agents + n_enemies) where 1 indicates visibility.
    • is_position_in_cone(agent_id, pos, range): Checks if a specific coordinate is within the agent's sight or shoot cone.
    • render_fovs(): Debugging method to draw agent cones in the simulator.