Skip to content

Commit

Permalink
Merge pull request #764 from Microsoft/prep37
Browse files Browse the repository at this point in the history
Prep37
  • Loading branch information
AndKram committed Nov 5, 2018
2 parents abc82d2 + 8b65204 commit e79632b
Show file tree
Hide file tree
Showing 48 changed files with 4,179 additions and 120 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Microsoft Corporation
Copyright (c) 2016, 2018 Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
Expand Down
2 changes: 1 addition & 1 deletion Malmo/samples/Python_examples/team_reward_test.py
Expand Up @@ -77,7 +77,7 @@ def safeStartMission(agent_host, my_mission, my_client_pool, my_mission_record,
print("Will wait and retry.", max_attempts - used_attempts, "attempts left.")
time.sleep(2)
else:
print("Other error:", e.message)
print("Other error: ", str(e))
print("Waiting will not help here - bailing immediately.")
exit(1)
if used_attempts == max_attempts:
Expand Down
62 changes: 62 additions & 0 deletions MalmoEnv/README.md
@@ -0,0 +1,62 @@
# MalmoEnv (Prototype) #

MalmoEnv is an OpenAI "gym" like Python Environment for Malmo/Minecraft, directly implemented Python to Java Minecraft.

A python "gym env" is created and used to run an agent in a Malmo mission. Each env has a remote Minecraft instance
associated to it (by DNS name or IP and Port). For multi-agent missions, the first agent's (role 0) Minecraft
client instance is used as a coordinator to allow all agents to rendezvous on mission starts (i.e. on env resets).

As it's python only, you just need this one package, its direct dependencies and (Java) Minecraft!

## Examples of use: ##

Install dependencies:

Java8 JDK, python3, git

`pip install gym lxml numpy pillow`

To prepare Minecraft (after cloning this repository with
`git clone -b malmoenv https://github.com/Microsoft/malmo.git`):

`cd Minecraft`

`(echo -n "malmo.version=" && cat ../VERSION) > ./src/main/resources/version.properties`

Running a single agent example mission (run each command in different cmd prompt/shells):

`./launchClient.sh -port 9000 -env` or (On Windows) `launchClient.bat -port 9000 -env`

(In another shell) `cd MalmoEnv` optionally run `python3 setup.py install`

`python3 run.py --mission missions/mobchase_single_agent.xml --port 9000 --episodes 10`

A two agent example mission (run each command in different cmd prompt/shells):

`./launchClient.sh -port 9000 -env`

`./launchClient.sh -port 9001 -env`

In the two agent case, running each agent in it's own shell, the run script (for agents other than the first) is given two ports
- the first for the mission coordinator and a second (port2) for the other agent's Minecraft:

`python3 run.py --mission missions/mobchase_two_agents.xml --port 9000 --role 0 --experimentUniqueId "test1"`

`python3 run.py --mission missions/mobchase_two_agents.xml --port 9000 --port2 9001 --role 1 --experimentUniqueId "test1"`

## Running multi-threaded multi-agent examples: ##

`python3 runmultiagent.py --mission missions/mobchase_two_agents.xml

## Installing with pip ##

If you install with `pip3 install malmoenv` then you can download the Minecraft mod
(assuming you have git available from the command line) with:

`python3 -c "import malmoenv.bootstrap();malmoenv.bootstrap.download()`

The sample missions will be in ./MalmoPlatform/MalmoEnv/missions.

`malmoenv.bootstrap.launchMinecraft(9000)` can be used to start up the Malmo Minecraft Mod
listening for MalmoEnv connections on port 9000 after downloading Malmo.

22 changes: 22 additions & 0 deletions MalmoEnv/malmoenv/__init__.py
@@ -0,0 +1,22 @@
# ------------------------------------------------------------------------------------------------
# Copyright (c) 2018 Microsoft Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------------------------

from malmoenv.core import ActionSpace, StringActionSpace, VisualObservationSpace, Env, make

__all__ = ['ActionSpace', 'StringActionSpace', 'VisualObservationSpace', 'Env', 'make']
88 changes: 88 additions & 0 deletions MalmoEnv/malmoenv/bootstrap.py
@@ -0,0 +1,88 @@
# ------------------------------------------------------------------------------------------------
# Copyright (c) 2018 Microsoft Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------------------------
import os
import subprocess
import pathlib

from malmoenv.version import malmo_version


def download(branch=None, build=True, installdir="MalmoPlatform"):
"""Download Malmo from github and build (by default) the Minecraft Mod.
Example usage: import malmoenv.bootstrap; malmoenv.bootstrap.download()
Args:
branch: optional branch to clone. TODO Default is release version.
build: build the Mod unless build arg is given as False.
installdir: the install dir name. Defaults to MalmoPlatform.
Returns:
The path for the Malmo Minecraft mod.
"""

if branch is None:
branch = malmo_version

subprocess.check_call(["git", "clone", "-b", branch, "https://github.com/Microsoft/malmo.git", installdir])

return setup(build=build, installdir=installdir)


def setup(build=True, installdir="MalmoPlatform"):
"""Set up Minecraft for use with the MalmoEnv gym environment"""

gradlew = './gradlew'
if os.name == 'nt':
gradlew = 'gradlew.bat'

cwd = os.getcwd()
os.chdir(installdir)
os.chdir("Minecraft")
try:
# Create the version properties file.
pathlib.Path("src/main/resources/version.properties").write_text("malmomod.version={}\n".format(malmo_version))
# Optionally do a test build.
if build:
subprocess.check_call([gradlew, "setupDecompWorkspace", "build", "testClasses",
"-x", "test", "--stacktrace", "-Pversion={}".format(malmo_version)])
minecraft_dir = os.getcwd()
finally:
os.chdir(cwd)
return minecraft_dir


def launch_minecraft(port, installdir="MalmoPlatform", replaceable=False):
"""Launch Minecraft listening for malmoenv connections.
Args:
port: the TCP port to listen on.
installdir: the install dir name. Defaults to MalmoPlatform.
Must be same as given (or defaulted) in download call if used.
replaceable: whether or not to automatically restart Minecraft (default is false).
"""
launch_script = 'launchClient.sh'
if os.name == 'nt':
launch_script = 'launchClient.bat'
cwd = os.getcwd()
os.chdir(installdir)
os.chdir("Minecraft")
try:
cmd = [launch_script, '-port', str(port), '-env']
if replaceable:
cmd.append('-replaceable')
subprocess.check_call(cmd)
finally:
os.chdir(cwd)

0 comments on commit e79632b

Please sign in to comment.