OSRS PvP Reinforcement Learning Framework

repository·master·Indexed 41 days ago

https://github.com/naton1/osrs-pvp-reinforcement-learning

A reinforcement learning framework for training AI agents in Old School RuneScape (OSRS) PvP combat using PPO and self-play. The system consists of a Python-based ML core (pvp-ml) and a custom RSPS simulation environment (simulation-rsps) based on Elvarg RSPS. It features a socket-based API for real-time predictions, distributed training support via Ray, and Tensorboard integration for monitoring metrics.

Tokens
4.3K
Snippets
13
Records
26
Agent score
51%

What's inside OSRS PvP Reinforcement Learning

  1. Understand the project components: PvP ML and Simulation RSPS

    master

    The project is divided into two primary functional components:

    • PvP ML (pvp-ml): A Python-based core machine learning system. It orchestrates the training workflow, manages connections to the simulation, and provides a socket-based API for real-time model predictions.
    • Simulation RSPS (simulation-rsps): A platform for conducting simulated PvP fights to train agents efficiently. It is built upon the Elvarg RSPS private server.

    The training system manages the simulation during training sessions, though the simulation can be operated independently.

  2. Understand the Reinforcement Learning Plugin features

    master

    The ReinforcementLearningPlugin extends the game logic to support ML training through several key components:

    • RemoteEnvironmentServer: A socket server that exposes routes for API interactions, including login/logout and step and reset requests (following the OpenAI Gym interface pattern).
    • RemoteEnvironmentPlayerBot: Enables control over training agents via the API.
    • AgentBotLoader: Supports independent ML-driven agents.
    • EnableAgentCommand: Allows for player-controlled agents to be used in conjunction with the system.
  3. Understand the NH Environment

    master

    The NH Environment is specialized for 1v1 Nightsight (NH) fights.

    • Action Space: Uses a MultiDiscrete action space with 11 action heads.
    • Observation Space: Extensive observation space.
    • Contract: For the full technical specification of the observation and action spaces, refer to the environment contract at contracts/environments/NhEnv.json.
  4. How Environment Contracts work

    master

    Environment contracts define the interface between the ML system and the game simulation. They are defined using JSON files located in contracts/environments. These files describe the available actions and observations for a specific environment.

    Existing contracts include:

    • NhEnv.json: The primary environment for the project.
    • DharokEnv.json: A partially implemented test environment for Dharok edge-style combat.
  5. Install and set up OSRS PvP Reinforcement Learning

    master

    To begin using the project, clone the repository and set up the two main subprojects individually. The project consists of a machine learning system and a game simulation.

    1. Clone the repository:
      git clone https://github.com/Naton1/osrs-pvp-reinforcement-learning
    2. Follow the specific setup instructions found in the README of each subproject:
      • pvp-ml/README.md for the machine learning system.
      • simulation-rsps/README.md for the game simulation.

    Once set up, you can train custom models, serve existing models via an API, or evaluate models within the simulation.

    git clone https://github.com/Naton1/osrs-pvp-reinforcement-learning
  6. Run distributed training with Ray

    master

    The project supports distributed rollouts using Ray for cluster or multi-core CPU training.

    • Distributed Training: Use the --distribute flag. If you provide a number, it specifies the parallel rollout count. If omitted, it uses all available CPU cores.
    • AWS Cluster Management:
      • Scale up: ray up cluster.yml
      • Scale down: ray down cluster.yml
      • View Dashboard: ray attach cluster.yml --port-forward=8265
    # Train with a specific number of parallel rollouts
    train --preset <preset> --distribute <parallel-rollout-count>
    
    # Train using all available CPU cores
    train --preset <preset>
    
    # AWS Cluster Management
    ray up cluster.yml
    ray down cluster.yml
    ray attach cluster.yml --port-forward=8265
  7. Train a custom model

    master

    You can train customized models on the simulation for any defined environment by using YAML configuration files.

    1. Define your experiment parameters in a YAML file (refer to pvp-ml/config for existing configurations).
    2. For detailed execution steps, refer to the start a training job guide in the pvp-ml directory.

    To train on a new environment, you must first add it to the system (see 'Add New Environment' below).

  8. Launch the ElvargServer simulation environment

    master

    The ElvargServer provides the simulated game environment for reinforcement learning. By default, it runs the ReinforcementLearningPlugin, which launches a remote environment server for API interactions (connection details are located in the pvp-ml module).

    Requirements:

    • Java 17

    Steps:

    1. Navigate to the ElvargServer directory.
    2. Run the server using Gradle: ./gradlew run.
    cd ElvargServer
    ./gradlew run
  9. Install and setup the PvP ML environment

    master

    The PvP ML core logic requires conda to manage dependencies.

    1. Create the environment: Use the provided environment.yml file.
    2. Activate the environment: Activate the local ./env directory.

    Note on GPU/CPU: By default, training uses a GPU if available. For CPU-only training, you must manually uncomment the cpuonly line in environment.yml before running the creation command.

    # Create the environment
    conda env create -p ./env -f environment.yml
    
    # Activate the environment
    conda activate ./env