浏览代码
LL-API and gym yamato tests (#3714)
LL-API and gym yamato tests (#3714)
* LL-API and gym yamato tests * log env to stdout * no_graphics=True * rename * break venv into separate command * simplify venv usage, add env as command line * fix venv path/develop/add-fire
GitHub
5 年前
当前提交
9c7a68ea
共有 8 个文件被更改,包括 229 次插入 和 3 次删除
-
14ml-agents/tests/yamato/yamato_utils.py
-
32.yamato/gym-interface-test.yml
-
32.yamato/python-ll-api-test.yml
-
21ml-agents/tests/yamato/setup_venv.py
-
48ml-agents/tests/yamato/scripts/run_gym.py
-
85ml-agents/tests/yamato/scripts/run_llapi.py
-
0/ml-agents/tests/yamato/scripts/__init__.py
|
|||
test_editors: |
|||
- version: 2019.3 |
|||
--- |
|||
{% for editor in test_editors %} |
|||
test_gym_interface_{{ editor.version }}: |
|||
name: Test Mac Gym Interface {{ editor.version }} |
|||
agent: |
|||
type: Unity::VM::osx |
|||
image: ml-agents/ml-agents-bokken-mac:0.1.4-492264 |
|||
flavor: b1.small |
|||
variables: |
|||
UNITY_VERSION: {{ 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 |
|||
dependencies: |
|||
- .yamato/standalone-build-test.yml#test_mac_standalone_{{ editor.version }} |
|||
triggers: |
|||
cancel_old_ci: true |
|||
changes: |
|||
only: |
|||
- "com.unity.ml-agents/**" |
|||
- "Project/**" |
|||
- "ml-agents/**" |
|||
- "ml-agents-envs/**" |
|||
- ".yamato/gym-interface-test.yml" |
|||
except: |
|||
- "*.md" |
|||
- "com.unity.ml-agents/*.md" |
|||
- "com.unity.ml-agents/**/*.md" |
|||
{% endfor %} |
|
|||
test_editors: |
|||
- version: 2019.3 |
|||
--- |
|||
{% for editor in test_editors %} |
|||
test_mac_ll_api_{{ editor.version }}: |
|||
name: Test Mac LL-API {{ editor.version }} |
|||
agent: |
|||
type: Unity::VM::osx |
|||
image: ml-agents/ml-agents-bokken-mac:0.1.4-492264 |
|||
flavor: b1.small |
|||
variables: |
|||
UNITY_VERSION: {{ editor.version }} |
|||
commands: |
|||
- pip install pyyaml |
|||
- 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 }} |
|||
triggers: |
|||
cancel_old_ci: true |
|||
changes: |
|||
only: |
|||
- "com.unity.ml-agents/**" |
|||
- "Project/**" |
|||
- "ml-agents/**" |
|||
- "ml-agents-envs/**" |
|||
- ".yamato/python-ll-api-test.yml" |
|||
except: |
|||
- "*.md" |
|||
- "com.unity.ml-agents/*.md" |
|||
- "com.unity.ml-agents/**/*.md" |
|||
{% endfor %} |
|
|||
import argparse |
|||
|
|||
from .yamato_utils import init_venv |
|||
|
|||
|
|||
def main(): |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument("--mlagents-version", default=None) |
|||
parser.add_argument("--extra-packages", default=None) |
|||
args = parser.parse_args() |
|||
extra_packages = [] |
|||
if args.extra_packages is not None: |
|||
extra_packages = args.extra_packages.split(",") |
|||
|
|||
init_venv( |
|||
mlagents_python_version=args.mlagents_version, extra_packages=extra_packages |
|||
) |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
main() |
|
|||
import argparse |
|||
import numpy as np |
|||
|
|||
from gym_unity.envs import UnityEnv |
|||
|
|||
|
|||
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 |
|||
) |
|||
|
|||
try: |
|||
# Examine environment parameters |
|||
print(str(multi_env)) |
|||
|
|||
# Reset the environment |
|||
initial_observations = multi_env.reset() |
|||
|
|||
if len(multi_env.observation_space.shape) == 1: |
|||
# Examine the initial vector observation |
|||
print("Agent observations look like: \n{}".format(initial_observations[0])) |
|||
|
|||
for _episode in range(10): |
|||
multi_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] |
|||
print("Total reward this episode: {}".format(episode_rewards)) |
|||
finally: |
|||
multi_env.close() |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument("--env", default="Project/testPlayer") |
|||
args = parser.parse_args() |
|||
main(args.env) |
|
|||
import argparse |
|||
import numpy as np |
|||
|
|||
from mlagents_envs.environment import UnityEnvironment |
|||
from mlagents_envs.side_channel.engine_configuration_channel import ( |
|||
EngineConfigurationChannel, |
|||
) |
|||
|
|||
|
|||
def main(env_name): |
|||
""" |
|||
Run the low-level API test using the specified environment |
|||
:param env_name: Name of the Unity environment binary to launch |
|||
""" |
|||
engine_configuration_channel = EngineConfigurationChannel() |
|||
env = UnityEnvironment( |
|||
file_name=env_name, |
|||
side_channels=[engine_configuration_channel], |
|||
no_graphics=True, |
|||
args=["-logFile", "-"], |
|||
) |
|||
|
|||
try: |
|||
# Reset the environment |
|||
env.reset() |
|||
|
|||
# Set the default brain to work with |
|||
group_name = env.get_agent_groups()[0] |
|||
group_spec = env.get_agent_group_spec(group_name) |
|||
|
|||
# Set the time scale of the engine |
|||
engine_configuration_channel.set_configuration_parameters(time_scale=3.0) |
|||
|
|||
# Get the state of the agents |
|||
step_result = env.get_step_result(group_name) |
|||
|
|||
# Examine the number of observations per Agent |
|||
print("Number of observations : ", len(group_spec.observation_shapes)) |
|||
|
|||
# Is there a visual observation ? |
|||
vis_obs = any(len(shape) == 3 for shape in group_spec.observation_shapes) |
|||
print("Is there a visual observation ?", vis_obs) |
|||
|
|||
# Examine the state space for the first observation for the first agent |
|||
print("First Agent observation looks like: \n{}".format(step_result.obs[0][0])) |
|||
|
|||
for _episode in range(10): |
|||
env.reset() |
|||
step_result = env.get_step_result(group_name) |
|||
done = False |
|||
episode_rewards = 0 |
|||
while not done: |
|||
if group_spec.is_action_continuous(): |
|||
action = np.random.randn( |
|||
step_result.n_agents(), group_spec.action_size |
|||
) |
|||
|
|||
elif group_spec.is_action_discrete(): |
|||
branch_size = group_spec.discrete_action_branches |
|||
action = np.column_stack( |
|||
[ |
|||
np.random.randint( |
|||
0, branch_size[i], size=(step_result.n_agents()) |
|||
) |
|||
for i in range(len(branch_size)) |
|||
] |
|||
) |
|||
else: |
|||
# Should never happen |
|||
action = None |
|||
env.set_actions(group_name, action) |
|||
env.step() |
|||
step_result = env.get_step_result(group_name) |
|||
episode_rewards += step_result.reward[0] |
|||
done = step_result.done[0] |
|||
print("Total reward this episode: {}".format(episode_rewards)) |
|||
finally: |
|||
env.close() |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument("--env", default="Project/testPlayer") |
|||
args = parser.parse_args() |
|||
main(args.env) |
撰写
预览
正在加载...
取消
保存
Reference in new issue