GithubHelp home page GithubHelp logo

nullspace-colombia / unray-bridge Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 2.0 2.62 MB

Rllib framework for using Unreal Engine 5 (UE5) as external environment for Reinforced Learning training process

License: MIT License

Python 89.87% HTML 2.46% CSS 1.30% JavaScript 6.37%
reinforcement-learning rllib unreal-engine-5

unray-bridge's Introduction

About Unray

Framework for communication between Unreal Engine and Python.

This repository contains all the files needed for usage in Python.

Unreal Engine

Engine Version

We are currently using Unreal Engine 5.1. We recommend using the same version to ensure project stability.

Project Files

In the Maps folder you'll find some examples to run:

Maps

Custom Envs

To create a custom env in Unreal Engine, first create your Agent Blueprint.

You can create your agent based on the parent class of your choice. Once you create the blueprint, go to the Class Settings section.

Class_Settings

In the details panel, find the Interfaces section:

Details_Panel

In the Implemented Interfaces subsection, click the Add button and search for "BI_Agent".

Interface

BI_Agent

Interface_Result

Once you do this, in the blueprint functions, you'll now have these functions:

Override_Functions

You have to implement these functions according to your enviornment.

Function Description
Get Reward Agent Reward
Is Done Function to specify the way the agent finishes the environment
Reset Reset the agent. Create Actor -> True if you want to destroy the actor and spawn it again in a new place.
Get State Get agent observations
Step What the agent does in each step

When you've implemented all these functions and you want to try your environment, you'll have to add a Connector to your map.

In the Blueprints folder, you'll find the connectors for both single agent envs and multiagent envs:

Connectors

Single Agent Environments

If your environment is a single agent env, place a Connector_SA instance in your map. Once you do, you can select it and in the details panel you'll find the Default section. There, you'll find an Actor Agent variable, assign your agent to this variable.

MultiAgent Environments

If your environment is a multiagent env, you'll need to place a Connector_MA instance in your map. Once you do, you can select it and in the details panel you'll find the Default section. There, you'll find an array called Actor Agents.

ConnectorMA_Panel

To ensure the framework can recognise all the agents in your environment, add each agent to the array.

Remember that for each agent in your env, you'll have to implement the Reward, Done, Reset, Get State and Step functions.

Parallel Trainning

If you want to train several envs at the same time, we recommend you create your env as a Blueprint.

In the Blueprints folder you'll find a MultiAgent_Env Blueprint.

Env

You can create your env with this Blueprint as a parent Class.

In the Viewport of your env blueprint class, drag the connector you need from the Content Drawer and place it where you want.

In the Event Graph of your env blueprint class, you'll have to do a few things to configure your env.

First, each env you create will have an ID (which defaults to 1). You can either set this parameter in the Details pannel of your map or create a function to set it automatically.

Then, you need to add the agents in your env to an Agents array, which belongs to the MultiAgent_Env class. To do so, simply search for the Get Agents function and add each of your agents to this array. For example, in the MultiAgent Arena map it looks like this:

AddAgents

Finally, you'll have to add the following functions to your env class:

MultiAgentEnv

This is to set the agents and set the ports in which the communication is going to happen.

UNRAY Bridge

Clone the repo and install the given dependencies. This is just the python-side of the framework. Remember to create or to open a UE5 scene with the official unray-bridge blueprints.

https://github.com/Nullspace-Colombia/unray-bridge.git  && cd unray-bridge 
 pip install -r requirements.txt

We recommend conda for creating a virtualenv and installing the dependendencies. Currently, Ray is available in Python 3.10 or less, so we recommend creating a virtualenv with version 3.10.

Running Examples

There are currently two examples ready for you to run.

Cartpole

Cartpole

In Unreal Engine, go to the maps folder and start the Cartpole map. Once it is running, go to your terminal an inside the unray-bridge folder run:

python main_cartpole.py

If everything is correct, the cartpole will start to move.

MultiAgent Arena

MultiAgentArena_S

In this env, you have two agents competing in a single env.

In Unreal Engine, go to the maps folder and start the MultiAgentArena map. Once it is running, go to your terminal an inside the unray-bridge folder run:

python main_multiagentArena.py

If everything is correct, the agents will start to move.

MultiAgent Arena Parallel Trainning

MultiAgentArena

In Unreal Engine, go to the maps folder and start the MultiAgentArena_BP map. Once it is running, go to your terminal an inside the unray-bridge folder run:

python parallel_multiagentArena.py

If everything is correct, the four envs will start to move.

RL Environment for simple training

Single Agent

[In dev process]

Multiagent

In order to define a custom environment, you have to create an action and observation dictionary. This is called a env_config dict.

# Define the env_config dict for each agent. 
env_config = {
  "agent-1": {
    "observation": <Space>,
    "action": <Space>
    }, 
  "agent-2": {
    "observation": <Space>,
    "action": <Space>
    }
    ...

Each Space is taken from BridgeSpace

from unray_bridge.envs.spaces import BridgeSpaces 

This dictionary defines the independent spaces for each of the agents. Then, the environment is intantiated inherited from MultiAgentBridgeEnv from unray_bridge

from unray_bridge.envs.bridge_env import MultiAgentBridgeEnv

Contructor needs environment name, ip, port and the config.

env = MultiAgentBridgeEnv(
    name = "multiagent-arena",
    ip = 'localhost',
    port = 10110, 
    config = env_config
)

Multiagent Workflow

As well as in the single-agent case, the environment dynamics are defined externally in the UE5 Scenario. The BridgeEnv lets RLlib comunicate with the enviornment via TPC/IP connection, sending the agent actions defined by ray algorithms and reciving the observation vectors from the environment for the trainer to train. The MultiAgentBridgeEnvcreates the connection_handler that allow to maintain the socket communication.

1. How does the multiagent dictionaries are structured for sending to UE5 ?

Suppose we have n-agents in the environment. Each of them with a given a_i action vector. This means that we have a total data of the sum of sizes for each action vector. Hence, stacking these vectors we got the final buffer that is send to the socket server from UE5.

Multiagent Example: Multiagent-Arena

As a simple example we will build a Multiagent-Arena environment in UE5 an train it in ray using the unray-bridge framework.

Img taken from https://github.com/sven1977/rllib_tutorials/blob/main/ray_summit_2021/tutorial_notebook.ipynb

Understanding the environment

As a Unray-bridge philosophy first we have to break down what the environment need. We have two agents that move in the same scenario, given by a 8x8 square grid. They can only move one no-diagonal square for each episode. (The reward system is defined in the image).

Hence we got:

  • Agent 1 and 2 Observation: MultiDiscrete([64])
  • Agent 1 and 2 Action: Discrete([4])

Defining the env_config as follows:

  env_config  = {
    "agent-1":{
        "observation": BridgeSpaces.MultiDiscrete([64], [64]),
        "action": BridgeSpaces.Discrete(4),
    }, 
    "agent-2":{
        "observation": BridgeSpaces.MultiDiscrete([64], [64]),
        "action": BridgeSpaces.Discrete(4),
    }
}

Create the environment

env = MultiAgentBridgeEnv(
    name = "multiagent-arena",
    ip = 'localhost',
    port = 10110, 
    config = env_config
)

unray-bridge's People

Contributors

acmoral avatar gdiaz16 avatar mora200217 avatar vahernandezmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

varamishitha

unray-bridge's Issues

Documentation clarification

Hey Team,

I'm running into issues trying out both examples you have provided.

Issue

When running in command prompt python parallel_multiagentArena.py
I receive this error:
OverflowError: int too big to convert
And when running in command prompt python main_cartpole.py
I receive this error
[ CONNECTION ] connecting to localhost port 10010
Trying to connect...
Trying to connect...
Trying to connect...
Trying to connect...
Connection Timeout!

What's interesting is when running in command prompt netstat -a | findstr :10011 I do get a response from UE5.3
TCP 0.0.0.0:10011 www:0 LISTENING

Steps to reproduce

Create a new VirtualEnv with Anaconda 3.

  1. Use Python 3.10.x (have tried 3.8.x as well)
  2. git clone https://github.com/Nullspace-Colombia/unray-bridge.git
    cd unray-bridge
    pip install -r requirements.txt
    pip install tensorflow
  3. In Anaconda 3 select your new Virtual Environment and click the green play button and select Open Terminal
  4. Enable the UE 5.3 plugin Remote Control API
  5. Restart UE 5.3
  6. Inside Project Settings, select Plugins - Remote Control
  7. Under Remote Control WebSocket Server Port, enter in 10011
  8. Select map MultiAgentArena
  9. Press the Green Play button in the UE 5.3 Editor
  10. In your Terminal (command prompt)
  11. Run in command prompt python main_multiagentArena.py
  12. Wait for error

Hardware

  • Intel Core i9 12900KF
  • ASUS TUF GAMING Z690-PLUS WIFI D4
  • Corsair 32 GB DDR4 @3200mhz
  • Samsung SSD 980 PRO 2TB
  • NVIDIA GeForce RTX 3090

README Suggestion. Provide instruction to update blueprints port variables to match the ports in their respective Python examples

Suggestions for clearer instruction for users exploring Unray examples

Cartpole Example:

  1. Change the port in Connector_SA from 9443 to 10010 to match main_cartpole.py requirements

Before:
image
After:
image

Multiagent Example

  1. Change the port in Connector_MA from 9443 to 10010
  2. Change the port in MultiAgent_Env from 9443 to 10010
  3. Change the port in MultiAgentArena from 9443|0 to 10011 to match main_multiagentArena.py requirements. Ensure the second Base Port under Class Defaults is 10010 which is pulled from MultiAgent_Env

Before:
image
After:
image

Before:
image
After:
image

Before:
image
After:
image

need little help

Hi,
I want to create RL Based Driving Car System, i used Cartpole BP and set everything but i think something was missed in my Implementation,
how to define minus reward or using trace channel to Observation system??!!
I have RL based car and Goal actor in my scene, i used distance between car and goal actor and connected value to Observation function.
Screenshot 2023-11-18 125304
Screenshot 2023-11-18 125333
Screenshot 2023-11-18 125344
Screenshot 2023-11-18 125351
Thank You for your help
Best Regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.