Skip to content

Testing gym with basic environment #3725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .yamato/gym-interface-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_gym_interface_{{ editor.version }}:
commands:
- pip install pyyaml
- python -u -m ml-agents.tests.yamato.setup_venv
- ./venv/bin/python ml-agents/tests/yamato/scripts/run_gym.py
- ./venv/bin/python ml-agents/tests/yamato/scripts/run_gym.py --env=Project/testPlayer-Basic
dependencies:
- .yamato/standalone-build-test.yml#test_mac_standalone_{{ editor.version }}
triggers:
Expand Down
2 changes: 1 addition & 1 deletion .yamato/python-ll-api-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_mac_ll_api_{{ editor.version }}:
- python -u -m ml-agents.tests.yamato.setup_venv
- ./venv/bin/python ml-agents/tests/yamato/scripts/run_llapi.py
dependencies:
- .yamato/standalone-build-test.yml#test_mac_standalone_{{ editor.version }}
- .yamato/standalone-build-test.yml#test_mac_standalone_{{ editor.version }} --env=Project/testPlayer
triggers:
cancel_old_ci: true
changes:
Expand Down
1 change: 1 addition & 0 deletions .yamato/standalone-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test_mac_standalone_{{ editor.version }}:
commands:
- pip install pyyaml
- python -u -m ml-agents.tests.yamato.standalone_build_tests
- python -u -m ml-agents.tests.yamato.standalone_build_tests --scene=Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity
triggers:
cancel_old_ci: true
changes:
Expand Down
27 changes: 10 additions & 17 deletions ml-agents/tests/yamato/scripts/run_gym.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import numpy as np

from gym_unity.envs import UnityEnv

Expand All @@ -9,36 +8,30 @@ def main(env_name):
Run the gym test using the specified environment
:param env_name: Name of the Unity environment binary to launch
"""
multi_env = UnityEnv(
env_name, worker_id=1, use_visual=False, multiagent=True, no_graphics=True
)
env = UnityEnv(env_name, worker_id=1, use_visual=False, no_graphics=True)

try:
# Examine environment parameters
print(str(multi_env))
print(str(env))

# Reset the environment
initial_observations = multi_env.reset()
initial_observations = env.reset()

if len(multi_env.observation_space.shape) == 1:
if len(env.observation_space.shape) == 1:
# Examine the initial vector observation
print("Agent observations look like: \n{}".format(initial_observations[0]))
print("Agent observations look like: \n{}".format(initial_observations))

for _episode in range(10):
multi_env.reset()
env.reset()
done = False
episode_rewards = 0
while not done:
actions = [
multi_env.action_space.sample()
for agent in range(multi_env.number_agents)
]
observations, rewards, dones, info = multi_env.step(actions)
episode_rewards += np.mean(rewards)
done = dones[0]
actions = env.action_space.sample()
obs, reward, done, _ = env.step(actions)
episode_rewards += reward
print("Total reward this episode: {}".format(episode_rewards))
finally:
multi_env.close()
env.close()


if __name__ == "__main__":
Expand Down
18 changes: 15 additions & 3 deletions ml-agents/tests/yamato/standalone_build_tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import sys
import argparse

from .yamato_utils import get_base_path, run_standalone_build


def main():
def main(scene_path):
base_path = get_base_path()
print(f"Running in base path {base_path}")

returncode = run_standalone_build(base_path, verbose=True)
executable_name = None
if scene_path is not None:
executable_name = scene_path.strip(".unity")
executable_name = executable_name.split("/")[-1]
executable_name = "testPlayer-" + executable_name

returncode = run_standalone_build(
base_path, verbose=True, output_path=executable_name, scene_path=scene_path
)

if returncode == 0:
print("Test run SUCCEEDED!")
Expand All @@ -18,4 +27,7 @@ def main():


if __name__ == "__main__":
main()
parser = argparse.ArgumentParser()
parser.add_argument("--scene", default=None)
args = parser.parse_args()
main(args.scene)
7 changes: 6 additions & 1 deletion ml-agents/tests/yamato/yamato_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def get_base_path():


def run_standalone_build(
base_path: str, verbose: bool = False, output_path: str = None
base_path: str,
verbose: bool = False,
output_path: str = None,
scene_path: str = None,
) -> int:
"""
Run BuildStandalonePlayerOSX test to produce a player. The location defaults to Project/testPlayer.
Expand All @@ -45,6 +48,8 @@ def run_standalone_build(
test_args += ["-logfile", "-"]
if output_path is not None:
test_args += ["--mlagents-build-output-path", output_path]
if scene_path is not None:
test_args += ["--mlagents-build-scene-path", scene_path]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole PR make sense to me. Just wondering who is parsing this --mlagents-build-scene-path arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Unity-Technologies/ml-agents/blob/master/Project/Assets/ML-Agents/Editor/Tests/StandaloneBuildTest.cs#L11
That would be the BuildStandalonePlayerOSX method in the StandaloneBuildTest.cs file.

print(f"{' '.join(test_args)} ...")

timeout = 30 * 60 # 30 minutes, just in case
Expand Down