Skip to content

Commit 5a8c0ff

Browse files
committed
Initial code commit
1 parent f44b422 commit 5a8c0ff

File tree

299 files changed

+53333
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+53333
-0
lines changed

BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
licenses(["notice"])
2+
3+
exports_files(["LICENSE"])

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# DeepMind Lab2D
2+
3+
*A learning environment written in C++ and Lua for the creation of grid worlds.*
4+
5+
<div align="center">
6+
<img src="docs/screenshot.png" alt="DeepMind Lab2D screenshot">
7+
</div>
8+
9+
## About
10+
11+
*DeepMind Lab2D* is a system for the creation of 2D environments for machine
12+
learning. The main goals of the system are ease of use and performance: The
13+
environments are "grid worlds", which are defined with a combination of simple
14+
text-based maps for the layout of the world, and Lua code for its behaviour.
15+
Machine learning agents interact with these environments through one of two
16+
APIs, the Python [`dm_env`](https://github.com/deepmind/dm_env) API or a custom
17+
C API (which is also used by [DeepMind Lab](https://github.com/deepmind/lab)).
18+
Multiple agents are supported.
19+
20+
## Getting started
21+
22+
We provide an example "random" agent in `python/random_agent`, which performs
23+
random actions. This can be used as a base for creating your own agents, and as
24+
a simple tool to preview an environment.
25+
26+
```sh
27+
bazel run -c opt dmlab2d/python:random_agent -- --level_name=clean_up
28+
```
29+
30+
## External dependencies, prerequisites and porting notes
31+
32+
*DeepMind Lab2D* currently ships as source code only. It depends on a few
33+
external software libraries, which we ship in several different ways:
34+
35+
* The `dm_env`, `eigen`, `luajit`, `lua5.1`, `lua5.2`, `luajit`, `png`, `six`
36+
and `zlib` libraries are referenced as external Bazel sources, and Bazel
37+
BUILD files are provided. The dependent code itself should be fairly
38+
portable, but the BUILD rules we ship are specific to Linux on x86. To build
39+
on a different platform you will most likely have to edit those BUILD files.
40+
41+
* A "generic reinforcement learning API" is included in
42+
[`//third_party/rl_api`](third_party/rl_api).
43+
44+
* Several additional libraries are required but are not shipped in any form;
45+
they must be present on your system:
46+
47+
* `Python 2.7 or 3.6` or above with `NumPy` and `PyGame`. It is important
48+
to update the paths to the Python version you are using by editing
49+
[`bazel/python.BUILD`](bazel/python.BUILD).
50+
51+
The build rules are using a few compiler settings that are specific to
52+
GCC/clang. If some flags are not recognized by your compiler (typically those
53+
would be specific warning suppressions), you may have to edit those flags.
54+
55+
## Disclaimer
56+
57+
This is not an official Google product.

build_defs.bzl

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2019 DeepMind Technologies Limited.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ============================================================================
15+
16+
"""A BUILD rule for Python extensions that are defined via pybind11."""
17+
18+
load("@bazel_skylib//lib:collections.bzl", "collections")
19+
20+
def pybind_extension(
21+
name,
22+
deps = [],
23+
data = [],
24+
copts = [],
25+
features = [],
26+
module_name = None,
27+
tags = [],
28+
visibility = None,
29+
**cc_kwargs):
30+
"""Creates a pybind11-based C++ library to extend Python.
31+
32+
Args:
33+
name: A unique name for this target.
34+
deps: The list of other libraries that the C++ target depends upon.
35+
data: The list of files needed by this rule at runtime.
36+
copts: Add these options to the C++ compilation command.
37+
features: Modify the features currently enabled on the package level via
38+
the `features` attribute.
39+
module_name: The extension module's name, if it differs from the rule's name.
40+
Must match the module name specified in the C++ source code, otherwise
41+
it cannot be loaded by Python.
42+
tags: A list of tags to be applied to all generated rules.
43+
visibility: The visibility of all generated rules.
44+
**cc_kwargs: Additional keyword arguments that are passed to a generated
45+
`cc_binary` rule.
46+
47+
Returns:
48+
A `py_library` rule.
49+
"""
50+
pybind11_deps = [
51+
"@pybind11_archive//:pybind11",
52+
"@python_system//:python_headers",
53+
]
54+
pybind11_copts = ["-fexceptions"]
55+
pybind11_features = ["-use_header_modules"]
56+
57+
if module_name == None:
58+
module_name = name
59+
60+
py_kwargs = dict()
61+
62+
if visibility != None:
63+
for kwargs in (cc_kwargs, py_kwargs):
64+
kwargs["visibility"] = visibility
65+
66+
shared_lib_name = module_name + ".so"
67+
native.cc_binary(
68+
name = shared_lib_name,
69+
deps = collections.uniq(deps + pybind11_deps),
70+
copts = collections.uniq(copts + pybind11_copts),
71+
features = collections.uniq(features + pybind11_features),
72+
tags = tags,
73+
linkshared = 1,
74+
linkstatic = 1,
75+
**cc_kwargs
76+
)
77+
78+
return native.py_library(
79+
name = name,
80+
data = data + [module_name + ".so"],
81+
tags = tags,
82+
**py_kwargs
83+
)
84+
85+
def pytype_strict_library(name, **kwargs):
86+
native.py_library(name = name, **kwargs)
87+
88+
def pytype_strict_binary(name, **kwargs):
89+
native.py_binary(name = name, **kwargs)
90+
91+
def py2and3_strict_test(name, **kwargs):
92+
"""A drop-in replacement for py_test that runs tests under both Py2 and Py3.
93+
94+
An invocation `py2and3_test(name = "foo_test", ...)` creates two actual test
95+
targets foo.py2 and foo.py3, which set python_version respectively to "PY2"
96+
and "PY3".
97+
98+
Args:
99+
name: the rule name, which becomes the name of a test suite
100+
**kwargs: forwarded to each py_test
101+
"""
102+
103+
py2 = name + ".py2"
104+
py3 = name + ".py3"
105+
106+
main = kwargs.pop("main", name + ".py")
107+
tags = kwargs.pop("tags", [])
108+
109+
native.py_test(name = py2, main = main, tags = tags, python_version = "PY2", **kwargs)
110+
native.py_test(name = py3, main = main, tags = tags, python_version = "PY3", **kwargs)
111+
native.test_suite(name = name, tags = tags, tests = [py2, py3])

dmlab2d/BUILD

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Description:
2+
# Lab2D ("dmlab2d") is a 2D multiplayer grid-world
3+
# environment for machine learning.
4+
5+
load("//dmlab2d/testing:lua_testing.bzl", "dmlab2d_lua_level_test", "dmlab2d_lua_test")
6+
7+
licenses(["notice"])
8+
9+
exports_files(["LICENSE"])
10+
11+
cc_library(
12+
name = "dmlab2d",
13+
srcs = ["dmlab2d.cc"],
14+
hdrs = ["dmlab2d.h"],
15+
data = [
16+
":game_scripts",
17+
"//dmlab2d/game_scripts/levels/chase_eat",
18+
"//dmlab2d/game_scripts/levels/clean_up",
19+
"//dmlab2d/game_scripts/levels/commons_harvest",
20+
"//dmlab2d/game_scripts/levels/pushbox",
21+
"//dmlab2d/game_scripts/levels/running_with_scissors",
22+
],
23+
visibility = ["//visibility:public"],
24+
deps = [
25+
"//dmlab2d/env_lua_api",
26+
"//dmlab2d/lua:bind",
27+
"//dmlab2d/system/file_system/lua:file_system",
28+
"//dmlab2d/system/generators/pushbox/lua:pushbox",
29+
"//dmlab2d/system/grid_world/lua:lua_world",
30+
"//dmlab2d/system/tile/lua:tile",
31+
"//third_party/rl_api:env_c_api",
32+
"//third_party/rl_api:env_c_api_bind",
33+
"@com_google_absl//absl/memory",
34+
],
35+
)
36+
37+
cc_test(
38+
name = "dmlab2d_test",
39+
srcs = ["dmlab2d_test.cc"],
40+
deps = [
41+
":dmlab2d",
42+
"//dmlab2d/util:test_srcdir",
43+
"//third_party/rl_api:env_c_api",
44+
"//third_party/rl_api:env_c_api_test_suite",
45+
"@com_google_absl//absl/strings",
46+
"@com_google_absl//absl/types:span",
47+
"@com_google_googletest//:gtest_main",
48+
],
49+
)
50+
51+
filegroup(
52+
name = "game_scripts",
53+
srcs = glob(
54+
[
55+
"game_scripts/**/*.lua",
56+
"game_scripts/**/*.png",
57+
],
58+
exclude = [
59+
"game_scripts/**/*_test_data/*",
60+
"game_scripts/**/*_test.lua",
61+
],
62+
),
63+
visibility = ["//dmlab2d/lua_interp:__pkg__"],
64+
)
65+
66+
TEST_SCRIPTS = [test[:-len(".lua")] for test in glob(["**/*_test.lua"])]
67+
68+
test_suite(
69+
name = "lua_tests",
70+
tests = TEST_SCRIPTS,
71+
)
72+
73+
[
74+
dmlab2d_lua_test(name = test_script)
75+
for test_script in TEST_SCRIPTS
76+
]
77+
78+
# Find all directories containing file 'init.lua'.
79+
LEVEL_DIRS = [p[:-len("/init.lua")] for p in glob(["game_scripts/levels/**/init.lua"])]
80+
81+
# Find single levels not in subdirectorys of LEVEL_DIRS
82+
SINGLE_LEVEL_SCRIPTS = [test[:-len(".lua")] for test in glob(
83+
["game_scripts/levels/**/*.lua"],
84+
exclude = ["**/*_test.lua"] + [p + "/**/*.lua" for p in LEVEL_DIRS],
85+
)]
86+
87+
LEVEL_SCRIPTS = SINGLE_LEVEL_SCRIPTS + [p + "/init" for p in LEVEL_DIRS]
88+
89+
test_suite(
90+
name = "lua_level_tests",
91+
tests = LEVEL_SCRIPTS,
92+
)
93+
94+
[
95+
dmlab2d_lua_level_test(name = level_script)
96+
for level_script in LEVEL_SCRIPTS
97+
]

dmlab2d/command_line_interface/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Description:
2+
# Command line interface for profiling and inspecting different Lab2D environments.
3+
4+
licenses(["notice"])
5+
6+
cc_binary(
7+
name = "command_line_interface",
8+
srcs = ["command_line_interface_main.cc"],
9+
visibility = ["//visibility:public"],
10+
deps = [
11+
"//dmlab2d",
12+
"//third_party/rl_api:env_c_api",
13+
"@com_google_absl//absl/flags:flag",
14+
"@com_google_absl//absl/flags:parse",
15+
"@com_google_absl//absl/flags:usage",
16+
"@com_google_absl//absl/strings",
17+
"@com_google_absl//absl/strings:str_format",
18+
"@com_google_absl//absl/types:optional",
19+
],
20+
)

0 commit comments

Comments
 (0)