一个端到端、轻量级且灵活的游戏研究平台
ELF is an Extensive, Lightweight and Flexible platform for game research, in particular for real-time strategy (RTS) games. On the C++-side, ELF hosts multiple games in parallel with C++ threading. On the Python side, ELF returns one batch of game state at a time, making it very friendly for modern RL. In comparison, other platforms (e.g., OpenAI Gym) wraps one single game instance with one Python interface. This makes concurrent game execution a bit complicated, which is a requirement of many modern reinforcement learning algorithms.
Besides, ELF now also provides a Python version for running concurrent game environments, by Python multiprocessing with ZeroMQ inter-process communication. See ./ex_elfpy.py for a simple example.
For research on RTS games, ELF comes with an fast RTS engine, and three concrete environments: MiniRTS, Capture the Flag and Tower Defense. MiniRTS has all the key dynamics of a real-time strategy game, including gathering resources, building facilities and troops, scouting the unknown territories outside the perceivable regions, and defend/attack the enemy. User can access its internal representation and can freely change the game setting.
ELF has the following characteristics:
End-to-End: ELF offers an end-to-end solution to game research. It provides miniature real-time strategy game environments, concurrent simulation, intuitive APIs, web-based visualzation, and also comes with a reinforcement learning backend empowered by Pytorch with minimal resource requirement.
Extensive: Any game with C/C++ interface can be plugged into this framework by writing a simple wrapper. As an example, we already incorporate Atari games into our framework and show that the simulation speed per core is comparable with single-core version, and is thus much faster than implementation using either multiprocessing or Python multithreading. In the future, we plan to incorporate more environments, e.g., DarkForest Go engine.
Lightweight: ELF runs very fast with minimal overhead. ELF with a simple game (MiniRTS) built on RTS engine runs 40K frame per second per core on a MacBook Pro. Training a model from scratch to play MiniRTS takes a day on 6 CPU + 1 GPU.
Flexible: Pairing between environments and actors is very flexible, e.g., one environment with one agent (e.g., Vanilla A3C), one environment with multiple agents (e.g., Self-play/MCTS), or multiple environment with one actor (e.g., BatchA3C, GA3C). Also, any game built on top of the RTS engine offers full access to its internal representation and dynamics. Besides efficient simulators, we also provide a lightweight yet powerful Reinforcement Learning framework. This framework can host most existing RL algorithms. In this open source release, we have provided state-of-the-art actor-critic algorithms, written in PyTorch.
See here.
You need to have cmake >= 3.8, gcc >= 4.9 and tbb (linux libtbb-dev) in order to install this script successfully.
…
Any game with C/C++ interface can be plugged into this framework by writing a simple wrapper. Currently we have the following environment:
MiniRTS and its extensions (./rts)
A miniature real-time strategy game that captures the key dynamics of its genre, including building workers, collecting resources, exploring unseen territories, defend the enemy and attack them back. The game runs extremely fast (40K FPS per core on a laptop) to faciliate the usage of many existing on-policy reinforcement learning approaches.
Atari games (./atari)
We incorporate Arcade Learning Environment (ALE) into ELF so that you can load any rom and run 1000 concurrent game instances easily.
Go engine (./go)
We reimplement our DarkForest Go engine in ELF platform. Now you can easily load a bunch of .sgf files and train your own Go AI with minimal resource requirements (i.e., a single GPU plus a week).
When you use ELF, please reference the paper with the following BibTex entry:
ELF: An Extensive, Lightweight and Flexible Research Platform for Real-time Strategy Games
Yuandong Tian, Qucheng Gong, Wenling Shang, Yuxin Wu, C. Lawrence Zitnick
NIPS 2017
@article{tian2017elf,
title={ELF: An Extensive, Lightweight and Flexible Research Platform for Real-time Strategy Games},
author={Yuandong Tian and Qucheng Gong and Wenling Shang and Yuxin Wu and C. Lawrence Zitnick},
journal={Advances in Neural Information Processing Systems (NIPS)},
year={2017}
}
Slides in ICML Video Games and Machine Learning (VGML) workshop.
Demo. Top-left is trained bot while bottom-right is rule-based bot.
Check here for detailed documentation. You can also compile your version in ./doc using sphinx.
ELF is very easy to use. The initialization looks like the following:
# We run 1024 games concurrently.
num_games = 1024
# Wait for a batch of 256 games.
batchsize = 256
# The return states contain key 's', 'r' and 'terminal'
# The reply contains key 'a' to be filled from the Python side.
# The definitions of the keys are in the wrapper of the game.
input_spec = dict(s='', r='', terminal='')
reply_spec = dict(a='')
context = Init(num_games, batchsize, input_spec, reply_spec)
The main loop is also very simple:
# Start all game threads and enter main loop.
context.Start()
while True:
# Wait for a batch of game states to be ready
# These games will be blocked, waiting for replies.
batch = context.Wait()
# Apply a model to the game state. The output has key 'pi'
# You can do whatever you want here. E.g., applying your favorite RL algorithms.
output = model(batch)
# Sample from the output to get the actions of this batch.
reply['a'][:] = SampleFromDistribution(output)
# Resume games.
context.Steps()
# Stop all game threads.
context.Stop()
Please check train.py and eval.py for actual runnable codes.
C++ compiler with C++11 support (e.g., gcc >= 4.9) is required. The following libraries are required tbb. CMake >=3.8 is also required.
Python 3.x is required. In addition, you need to install following package: PyTorch version 0.2.0+, tqdm, zmq, msgpack, msgpack_numpy
To train a model for MiniRTS, please first compile ./rts/game_MC (See the instruction in ./rts/ using cmake). Note that a compilation of ./rts/backend is not necessary for training, unless you want to see visualization.
Then please run the following commands in the current directory (you can also reference train_minirts.sh):
…
Note that long horizon (e.g., --T 20) could make the training much faster and (at the same time) stable. With long horizon, you should be able to train it to 70% winrate within 12 hours with 16CPU and 1GPU. You can control the number of CPUs used in the training using taskset -c.
Here is one trained model with 80% winrate against AI_SIMPLE for frameskip=50. Here is one game replay.
The following is a sample output during training:
…
To evaluate a model for MiniRTS, try the following command (you can also reference eval_minirts.sh):
…
Here is an example output (it takes 1 min 40 seconds to evaluate 10k games with 12 CPUs):
Version: dc895b8ea7df8ef7f98a1a031c3224ce878d52f0_
Num Actions: 9
Num unittype: 6
Load from ./save-212808.bin
Version: dc895b8ea7df8ef7f98a1a031c3224ce878d52f0_
Num Actions: 9
Num unittype: 6
100%|████████████████████████████████████████████████████████████████████████████████████████████| 10000/10000 [01:40<00:00, 99.94it/s]
str_acc_win_rate: Accumulated win rate: 0.735 [7295/2628/9923]
best_win_rate: 0.7351607376801297
new_record: True
count: 0
str_win_rate: [0] Win rate: 0.735 [7295/2628/9923], Best win rate: 0.735 [0]
Stop all game threads ...
Try the following script if you want to do self-play in Minirts. It will start with two bots, both starting with the pre-trained model. One bot will be trained over time, while the other is held fixed. If you just want to check their winrate without training, try --actor_only.
sh ./selfplay_minirts.sh [your pre-trained model]
To visualize a trained bot, you can specify --save_replay_prefix [replay_file_prefix] when running eval.py to save (lots of) replays. Note that the same flag can also be applied to training/selfplay.
All replay files contain action sequences, are in .rep and should reproduce the exact same game when loaded. To load the replay in the command line, using the following:
./minirts-backend replay --load_replay [your replay] --vis_after 0
and open the webpage ./rts/frontend/minirts.html to check the game. To load and run the replay in the command line only (e.g, if you just want to quickly see who win the game), try:
./minirts-backend replay_cmd --load_replay [your replay]
暂无开放 Issues,或尚未同步最近议题。