EPyMARL Documentation

repository·main·Indexed 20 days ago

https://github.com/uoe-agents/epymarl

An extended Multi-Agent Reinforcement Learning (MARL) framework built upon PyMARL. It supports modern environments including Gymnasium, PettingZoo, SMACv2, and SMAClite, as well as general-sum reward settings and advanced algorithms such as Pareto-AC, MAPPO, and QMIX. The framework includes tools for hyperparameter searching, Weights and Biases (W&B) logging, and result visualization.

Tokens
8.9K
Snippets
26
Records
35
Agent score
72%

What's inside EPyMARL

  1. Save and load trained models

    main

    Models are stored in the results/models directory.

    Saving: Set save_model = True in your config. The frequency is controlled by save_model_interval.

    Loading: Use checkpoint_path (pointing to the run directory) and load_step.

    • If load_step is omitted, the last checkpoint is loaded.
    • If load_step is provided, the closest checkpoint to that timestep is loaded.
    • To evaluate without training, set evaluate=True.
  2. Plot results with plot_results.py

    main

    The plot_results.py script visualizes logged metrics. It supports window-smoothing, aggregating results across multiple runs, and filtering by algorithm/environment.

    Use the --best_per_alg flag to only plot the best configuration for each algorithm instead of visualizing all configurations.

  3. Install EPyMARL and its dependencies

    main

    To set up the EPyMARL framework, follow these installation steps:

    1. Core Dependencies: Clone the repository and install the base requirements:

      pip install -r requirements.txt
    2. Supported Environments: To install a suite of supported environments (including Level Based Foraging, RWARE, PettingZoo, VMAS, Matrix games, SMAC, SMACv2, and SMAClite), run:

      pip install -r env_requirements.txt
    3. Pareto-AC (PAC) Dependencies: If you intend to use the Pareto-AC algorithm, you must install its specific dependencies:

      pip install -r pac_requirements.txt

    Note: SMAC and SMACv2 require a StarCraft II installation with specific map files. For individual environment installation, refer to their respective repositories.

    pip install -r requirements.txt
    pip install -r env_requirements.txt
    pip install -r pac_requirements.txt
  4. Run benchmark experiments in EPyMARL

    main

    You can run experiments for various benchmark environments using src/main.py. The command structure typically involves specifying a --config (the algorithm), an --env-config (the environment wrapper type), and env_args (environment-specific parameters).

    Common Environment Commands

    Matrix Games (using gymma):

    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=25 env_args.key="matrixgames:penalty-100-nostate-v0"

    Level Based Foraging (LBF) (using gymma):

    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=50 env_args.key="lbforaging:Foraging-8x8-2p-2f-coop-v3"

    RWARE (using gymma):

    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=500 env_args.key="rware:rware-tiny-2ag-v2"

    MPE (PettingZoo) (using gymma): For MPE environments like simple adversary or simple tag, you can use pre-trained policies to make the task cooperative by setting env_args.pretrained_wrapper:

    # For simple tag
    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=25 env_args.key="pz-mpe-simple-tag-v3" env_args.pretrained_wrapper="PretrainedTag"

    SMAC (using sc2):

    python src/main.py --config=qmix --env-config=sc2 with env_args.map_name="3s5z"
    # Example: Running QMIX on a specific SMAC map
    python src/main.py --config=qmix --env-config=sc2 with env_args.map_name="3s5z"
  5. Run experiments in PettingZoo and VMAS

    main

    EPyMARL supports PettingZoo and VMAS via the gymma environment wrapper. Use the env_args.key argument to specify the environment ID.

    # PettingZoo example
    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=25 env_args.key="pz-mpe-simple-spread-v3"
    
    # VMAS example
    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=150 env_args.key="vmas-balance"
  6. Run experiments in SMACv2 and SMAClite

    main

    EPyMARL provides wrappers for SMACv2 and SMAClite environments.

    SMACv2: Use the sc2v2 environment config. You can specify scenarios using env_args.map_name. Prepared configs are located in src/config/envs/smacv2_configs.

    SMAClite: Use the smaclite environment config. You can specify the map via env_args.map_name. To use the optimized C++ RVO2 library instead of the default numpy implementation, set env_args.use_cpp_rvo2=True (requires external installation of the RVO2 library).

    # SMACv2 example
    python src/main.py --config=qmix --env-config=sc2v2 with env_args.map_name="protoss_5_vs_5"
    
    # SMAClite example
    python src/main.py --config=qmix --env-config=smaclite with env_args.time_limit=150 env_args.map_name="MMM"
    
    # SMAClite with optimized C++ RVO2
    python src/main.py --config=qmix --env-config=smaclite with env_args.time_limit=150 env_args.map_name="MMM" env_args.use_cpp_rvo2=True
  7. Run hyperparameter searches with search.py

    main

    The search.py script executes hyperparameter searches based on a configuration file.

    • Local execution: Use the locally argument to run the search on your machine.
    • Cluster execution: Use the single <index> argument to run a specific hyperparameter combination as a single process, useful for batch scripts in cluster environments.
    # Local hyperparameter search
    python search.py run --config=search.config.example.yaml --seeds 5 locally
    
    # Cluster execution (running the 1st configuration in a batch)
    python search.py run --config=search.config.example.yaml --seeds 5 single 1
  8. Register and run custom Gymnasium environments

    main

    To use a custom Gymnasium environment, register it using the standard gymnasium.register API. Then, run it using the gymma environment config, passing the registration ID (including the package prefix if applicable) to env_args.key.

    Note on Rewards:

    • For common reward experiments, the environment should provide a single scalar reward (common_reward=True).
    • For individual reward experiments, the environment should provide one environment per agent (common_reward=False).
    # Registration template
    from gymnasium import register
    
    register(
      id="my-environment-v1",                         # Environment ID
      entry_point="myenv.environment:MyEnvironment",  # Entry point for the class
      kwargs={ ... },                                  # Arguments for __init__
    )
    # Running the custom environment
    python src/main.py --config=qmix --env-config=gymma with env_args.time_limit=50 env_args.key="myenv:my-environment-v1"
  9. Enable Weights and Biases (W&B) logging

    main

    EPyMARL supports logging experiment data to Weights and Biases (W&B).

    1. Install the library: pip install wandb.
    2. Set up your W&B account and follow their standard setup instructions.
    3. Once configured, EPyMARL will be able to log data to your W&B dashboard.
    pip install wandb
  10. Configure individual rewards in general-sum environments

    main

    EPyMARL supports training in environments with individual rewards for all agents (general-sum reward settings).

    • Algorithm Support: Only specific algorithms support general-sum rewards: IA2C, IPPO, MAA2C, MAPPO, IQL, and PAC. Algorithms like COMA, VDN, QMIX, and QTRAN only support common-reward environments.
    • Enabling Individual Rewards: Set common_reward=False in your execution command.
    • Reward Scalarisation: If common_reward=True is used in an individual-reward environment, EPyMARL defaults to summing all rewards. You can change this to the mean operation by setting reward_scalarisation="mean".

    Example running MAPPO in a LBF task with individual rewards:

    python src/main.py --config=mappo --env-config=gymma with env_args.time_limit=50 env_args.key="lbforaging:Foraging-8x8-2p-3f-v3" common_reward=False
  11. Configure Weights and Biases (W&B) logging

    main

    To log results to Weights and Biases, ensure the wandb library is installed and authenticated. Add the following parameters to your YAML configuration:

    • use_wandb: True: Enables W&B logging.
    • wandb_team: <name>: Specifies the W&B team.
    • wandb_project: <name>: Specifies the W&B project.
    • wandb_mode: "online": Forces online logging (default is "offline").

    Model Logging: To upload trained models to the W&B dashboard, set:

    • save_model: True
    • wandb_save_model: True (requires use_wandb: True)
    • save_model_interval: <int>
    use_wandb: True
    wandb_team: "my-team"
    wandb_project: "my-project"
    wandb_mode: "online"
    
    # To also save and upload models
    save_model: True
    wandb_save_model: True
    save_model_interval: 50000
  12. Configure EPyMARL algorithms and environments

    main

    EPyMARL uses YAML files for configuration, located in src/config:

    • src/config/default.yaml: Defines default values for experiment info (e.g., t_max) and hyperparameters.
    • src/config/algs/: Contains algorithm-specific configurations (passed via --config).
    • src/config/envs/: Contains environment-specific configurations (passed via --env-config).