All Projects → alex-petrenko → megaverse

alex-petrenko / megaverse

Licence: MIT License
High-throughput simulation platform for Artificial Intelligence reseach

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to megaverse

city-builder-games
Resources and info about open source city builder games.
Stars: ✭ 22 (-85.14%)
Mutual labels:  games, simulation
ss3sim
An R package for stock-assessment simulation with Stock Synthesis
Stars: ✭ 34 (-77.03%)
Mutual labels:  simulation
MD-11
A high-quality McDonnell Douglas MD-11 simulation for the open source simulator, FlightGear
Stars: ✭ 21 (-85.81%)
Mutual labels:  simulation
TriangleMeshDistance
Header only, single file, simple and efficient C++11 library to compute the signed distance function (SDF) to a triangle mesh
Stars: ✭ 55 (-62.84%)
Mutual labels:  simulation
toy code
No description or website provided.
Stars: ✭ 78 (-47.3%)
Mutual labels:  simulation
Omega2D
Two-dimensional flow solver with GUI using vortex particle and boundary element methods
Stars: ✭ 17 (-88.51%)
Mutual labels:  simulation
marcizhu
An interactive chess game in a README file!
Stars: ✭ 37 (-75%)
Mutual labels:  games
godesim
ODE system solver made simple. For IVPs (initial value problems).
Stars: ✭ 19 (-87.16%)
Mutual labels:  simulation
differential-growth
adrianton3.github.io/differential-growth/
Stars: ✭ 27 (-81.76%)
Mutual labels:  simulation
HSTPNSim
Using for petri nets simulations
Stars: ✭ 37 (-75%)
Mutual labels:  simulation
TaichiGAME
GPU Accelerated Motion Engine based on Taichi Lang.
Stars: ✭ 35 (-76.35%)
Mutual labels:  simulation
FCEUX-Lua-Script-Collection
Lua scripts for FCEUX (an NES emulator). Multiple games including River City Ransom, Battle Kid
Stars: ✭ 20 (-86.49%)
Mutual labels:  games
nuplan-devkit
The devkit of the nuPlan dataset.
Stars: ✭ 107 (-27.7%)
Mutual labels:  simulation
virl2-client
Client library for the Cisco VIRL 2 Network Simulation Platform
Stars: ✭ 39 (-73.65%)
Mutual labels:  simulation
Fluid-Solver
Rust based marker and cell fluid solver
Stars: ✭ 21 (-85.81%)
Mutual labels:  simulation
Vehicle-Dynamics-Lateral
OpenVD: Vehicle Dynamics - Lateral
Stars: ✭ 50 (-66.22%)
Mutual labels:  simulation
hoomd-examples
HOOMD-blue example scripts.
Stars: ✭ 15 (-89.86%)
Mutual labels:  simulation
NDCell
A fast, flexible, and feature-rich interactive multidimensional cellular automaton simulator written in Rust
Stars: ✭ 20 (-86.49%)
Mutual labels:  simulation
ORBITM
🌏 📡 🌏 📡 ORBITM - Orbit maintenance, propulsion sizing, and life-time estimation of space objects!
Stars: ✭ 28 (-81.08%)
Mutual labels:  simulation
docker
Scripts to build and use docker images including GHDL
Stars: ✭ 27 (-81.76%)
Mutual labels:  simulation

Megaverse

Megaverse is a dedicated high-throughput rendering and simulation engine for Artificial Intelligence research. It features an optimized batched renderer that enables generation of up to 1,000,000 observations per second on a single machine.

Left: RL agent completing a TowerBuilding task. Right: human player solving a randomly generated obstacle course.

Installation

Currently, the easiest way to install Megaverse is to build directly from sources. Note that none of the installation instructions require elevated privileges! We bring most dependencies through conda which makes it easy to set up on a cluster without the use of Docker.

Linux

# 1) Install VulkanSDK from https://vulkan.lunarg.com/sdk/home#linux (download and unzip), or use the following commands:
$ wget wget https://sdk.lunarg.com/sdk/download/1.2.198.1/linux/vulkansdk-linux-x86_64-1.2.198.1.tar.gz
$ mkdir vulkansdk && tar -xzf vulkansdk-linux-x86_64-1.2.198.1.tar.gz --directory vulkansdk

# 2) Add Vulkan SDK binaries to PATH (might need to do it each time recompiling Megaverse is required):
$ cd vulkansdk/1.2.198.1
$ source ./setup-env.sh

# 3) Clone the repo
$ git clone https://github.com/alex-petrenko/megaverse.git

# 4) Init submodules
$ cd megaverse 
$ git submodule update --init --recursive

# 5) Create a conda environment and install dependencies
$ conda create --name megaverse python=3.9
$ conda activate megaverse
(megaverse) $ conda install -c conda-forge opencv bullet cudatoolkit cudatoolkit-dev
(megaverse) $ conda install -c conda-forge 'cmake>=3.13'

# (alternatively you can boostrap from an environment file: conda env create -f environment.yml)

# 6) Install megaverse into a conda env
(megaverse) $ python setup.py develop
(megaverse) $ pip install -e .

# (Optional) 6.1) Build a .whl file to be installed elsewhere
(megaverse) $ python setup.py bdist_wheel

macOS

Although Vulkan-powered batched rendering is not supported on macOS, a limited OpenGL version can be built for local debugging and small-scale experiments on macOS. Installing on mac is very similar to Linux, sans any Vulkan/CUDA dependencies.

# 1) Clone the repo
$ git clone https://github.com/alex-petrenko/megaverse.git

# 2) Init submodules
$ cd megaverse 
$ git submodule update --init --recursive

# 3) Create a conda environment and install dependencies
$ conda create --name megaverse python=3.9
$ conda activate megaverse
(megaverse) $ conda install -c conda-forge opencv bullet
(megaverse) $ conda install -c conda-forge 'cmake>=3.13'

# 4) Install megaverse into a conda env
(megaverse) $ python setup.py develop
(megaverse) $ pip install -e .

# (Optional) 4.1) Build a .whl file to be installed elsewhere
(megaverse) $ python setup.py bdist_wheel

Docker

Since installation does not require elevated priviliges, Docker setup is not required. However, Docker-based installation is also available, see here: docker/README.md.

Examples

Python API

The following script executes a random policy:

import numpy as np
from megaverse.megaverse_env import MegaverseEnv


env = MegaverseEnv(
    'TowerBuilding',
    num_envs=2, num_agents_per_env=2,
    num_simulation_threads=4, use_vulkan=True,
    params={},
)
env.reset()

while True:
    actions = [env.action_space.sample() for _ in range(env.num_agents)]
    obs, rewards, dones, infos = env.step(actions)
    if np.any(dones):
        break

    env.render()

RL Training

Example training script using Sample Factory RL framework. First install the prerequisite:

pip install "sample-factory<2.0"

Then, to train agents in the TowerBuilding environment, execute:

python -m megaverse_rl.train --train_for_seconds=360000000 --train_for_env_steps=2000000000 --algo=APPO --gamma=0.997 --use_rnn=True --rnn_num_layers=2 --num_workers=10 --num_envs_per_worker=2 --ppo_epochs=1 --rollout=32 --recurrence=32 --batch_size=4096 --actor_worker_gpus 0 --num_policies=1 --with_pbt=False --max_grad_norm=0.0 --exploration_loss=symmetric_kl --exploration_loss_coeff=0.001 --megaverse_num_simulation_threads=1 --megaverse_num_envs_per_instance=30 --megaverse_num_agents_per_env=4 --megaverse_use_vulkan=True --policy_workers_per_policy=2 --reward_clip=30 --env=megaverse_TowerBuilding --experiment=TowerBuilding

Observe the behavior of agents by running:

python -m megaverse_rl.enjoy --algo=APPO --env=megaverse_TowerBuilding --experiment=TowerBuilding --megaverse_num_envs_per_instance=1 --fps=20 --megaverse_use_vulkan=True

Development

Setting up a CMake project in IDE

The core functionality of Megaverse is implemented in C++ and uses CMake build system. The easiest way to work on Megaverse C++ codebase is to use an IDE that can import a CMake project (defined by the root CMakeLists.txt in megaverse/src). Any such IDE would need to run cmake in order to build and debug the code. Thus cmake needs to be able to find all the libraries installed through conda (such as Bullet and OpenCV).

The most straightforward way to make sure that libraries can be found is to start IDE directly from the conda environment we defined above (see section Installation/Linux). Specifically, for CLion IDE it would look like this:

$ conda activate megaverse
# navigate to Vulkan SDK installation dir
(megaverse) $ cd vulkansdk-linux-x86_64-1.2.198.1/1.2.198.1/
# make sure that Vulkan env variables are initialized
(megaverse) $ source ./setup-env.sh
# start the IDE from the terminal (assuming clion is in PATH, usually you can do this with Tools->Create Command-Line Launcher)
(megaverse) $ clion & 

Now in the IDE open megaverse/src/CMakeLists.txt as CMake project and you should be able to build and run targets.

Running an IDE without Conda enviroment

Alternatively, if IDE is not run from a conda environment we might need to explicitly specify paths to libraries in IDE's CMake command line (i.e. in CLion that would be Settings -> Build,Execution,Deployment -> CMake -> CMake options). Your CMake options might looks like this:

-DPYTHON_EXECUTABLE=/home/<user>/miniconda3/envs/megaverse/bin/python
-DCMAKE_CUDA_COMPILER=/home/<user>/miniconda3/envs/megaverse/bin/nvcc
-DOpenCV_DIR=/home/<user>/miniconda3/envs/megaverse/lib/cmake/opencv4
-DBUILD_GUI_APPS=ON

Additionally, an environment variable VULKAN_SDK=/home/<user>/all/libs/vulkansdk-linux-x86_64-1.2.198.1/1.2.198.1/x86_64 must be set. In most IDEs this can be set in the same CMake configuration dialogue.

Finally, CMake should be able to find Bullet physics library. There are three ways to accomplish this:

  1. Install libbullet-dev and CMake will find the system-wide installation
  2. To link against bullet installed by Conda you need to make sure your IDE also uses Conda's cmake, rather than cmake bundled with the IDE. In CLion you can change this in Settings -> Build,Execution,Deployment -> Toolchains -> CMake. This way cmake should be able to find Conda's Bullet CMake config <env>/lib/cmake/bullet/BulletConfig.cmake
  3. Alternatively, you can build Bullet from sources and add a CMake option -DBULLET_ROOT pointing to the correct location.

Notable build targets

CMakeLists.txt defines many targets. The following targets are the most useful:

  • megaverse builds the overall project and the Python bindings (see setup.py)
  • run_unit_tests runs Google Tests (see megaverse/src/test)
  • viewer_app builds an interactive application that allows you to control agents with keyboard and explore environments with an overview camera. This one is really designed to interact with a single environment at a time, and is very useful during development and debugging phase. See details below.
  • megaverse_test_app can use the parallel simulation engine and batch renderer to execute many environment at once. See details below.

Using viewer_app

viewer_app can run any scenario in an interactive mode and offers a bunch of command line parameters:

Usage: viewer_app [options] 

Optional arguments:
-h --help           	shows help message and exits [default: false]
-v --version        	prints version information and exits [default: false]
-l --list_scenarios 	list registered scenario names [default: false]
--scenario          	name of the scenario to run [default: "ObstaclesEasy"]
--num_agents        	size of the team, pass value 1 to have just a single agent [default: 2]
--desired_fps       	rendering framerate for human perception; RL agents percieve the world at 15 FPS to avoid frameskip, hence the default value. [default: 15]
--use_opengl        	Whether to use OpenGL renderer instead of fast Vulkan renderer (currently Vulkan is only supported in Linux) [default: false]

Once the app started, use keyboard to control the agent and the camera:

  • WASD and arrow keys to control the agent
  • 1,2,3,4,etc. to switch between agents (if several are present in the environment)
  • Press O to toggle the overview camera, use mouse to control view angle
  • Use UHJK keys to control the position of the camera in the overview mode
  • Press R to reset the episode
  • Press ENTER to toggle Bullet collision debug view (only OpenGL version)
  • ESC to exit the app

Using megaverse_test_app

megaverse_test_app uses parallel interface and is a much easier target to debug compared to Python Gym API.

usage: megaverse_test_app [options] 

This app is designed to test the parallel execution engine and batched renderer
by simulating multiple environments at once. This app uses pretty much the same interface
as the Python Gym environment, sans the Python bindings. Whenever there is a problem
with the environment, it is much easier to debug this app directly, rather
than debugging the same code through Python.

Example, render 12 agents at the same time:
megaverse_test_app --scenario Collect --visualize --num_envs 4 --num_simulation_threads 1 --num_agents 3 --hires

Some performance figures for future reference (on 10-core Intel i9):
megaverse_test_app --scenario Empty --performance_test --num_envs 64 --num_simulation_threads 1 --num_agents 1
yields approximately 75000 FPS
megaverse_test_app --scenario Collect --performance_test --num_envs 64 --num_simulation_threads 1 --num_agents 1
yields approximately 27000 FPS


Optional arguments:
-h --help                	shows help message and exits [default: false]
-v --version             	prints version information and exits [default: false]
-l --list_scenarios      	list registered scenario names [default: false]
--scenario               	name of the scenario to run [default: "ObstaclesEasy"]
--num_agents             	size of the team [default: 2]
--use_opengl             	Whether to use OpenGL renderer instead of fast Vulkan renderer (currently Vulkan is only supported in Linux) [default: false]
--num_envs               	number of parallel environments to simulate [default: 64]
--num_simulation_threads 	number of parallel CPU threads to use for Bullet [default: 1]
--visualize              	Whether to render multiple environments on screen [default: false]
--visualize              	Whether to render multiple environments on screen [default: false]
--delay_ms               	Delay between rendered frames in milliseconds. Use only with --visualize [default: 1]
--performance_test       	Run for a limited number of env frames (currently 200000) to test performance. Uses random actions. [default: false]
--hires                  	Render at high resolution. Only use this parameter with --visualize and if the total number of agents is small [default: false]
--user_actions           	Allows the user to control agents (otherwise will use randomly generated actions). Use only with --visualize [default: false]

Troubleshooting

  • A crash (segfault) on startup can be caused by the incorrect initialization of Vulkan device interface. Possible fixes:
    • sudo apt remove mesa-vulkan-drivers (unless other packages you require depend on this package)
    • Set envvar export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/nvidia_icd.json, point to the location of nvidia_icd.json in your system.

Citation

If you use this repository in your work or otherwise wish to cite it, please make reference to our ICML2021 paper.

@inproceedings{petrenko2021megaverse,
  title={Megaverse: Simulating Embodied Agents at One Million Experiences per Second},
  author={Petrenko, Aleksei and Wijmans, Erik and Shacklett, Brennan and Koltun, Vladlen},
  booktitle={ICML},
  year={2021}
}

For questions, issues, inquiries please email [email protected]. Github issues and pull requests are welcome.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].