Skip to content

Commit

Permalink
update to match upstream folder changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yurirocha15 committed Jan 22, 2021
1 parent ae8a2e2 commit 6b6c548
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from unittest.mock import patch

from ray.rllib.env.unity3d_env import Unity3DEnv
from ray.rllib.env.wrappers.unity3d_env import Unity3DEnv


@patch("mlagents_envs.environment.UnityEnvironment")
Expand Down
21 changes: 16 additions & 5 deletions rllib/env/wrappers/unity3d_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class Unity3DEnv(MultiAgentEnv):
inside an RLlib PolicyClient for cloud/distributed training of Unity games.
"""

_BASE_PORT = 5004
# Default base port when connecting directly to the Editor
_BASE_PORT_EDITOR = 5004
# Default base port when connecting to a compiled environment
_BASE_PORT_ENVIRONMENT = 5005
# The worker_id for each environment instance
_WORKER_ID = 0

def __init__(self,
file_name: str = None,
Expand Down Expand Up @@ -73,18 +78,24 @@ def __init__(self,
# environments (num_workers >> 1). Otherwise, would lead to port
# conflicts sometimes.
time.sleep(random.randint(1, 10))
port_ = port or self._BASE_PORT
self._BASE_PORT += 1
port_ = port or (self._BASE_PORT_ENVIRONMENT
if file_name else self._BASE_PORT_EDITOR)
# cache the worker_id and
# increase it for the next environment
worker_id_ = Unity3DEnv._WORKER_ID if file_name else 0
Unity3DEnv._WORKER_ID += 1
try:
self.unity_env = UnityEnvironment(
file_name=file_name,
worker_id=0,
worker_id=worker_id_,
base_port=port_,
seed=seed,
no_graphics=no_graphics,
timeout_wait=timeout_wait,
)
print("Created UnityEnvironment for port {}".format(port_))
print(
"Created UnityEnvironment for port {}".format(port_ +
worker_id_))
except mlagents_envs.exception.UnityWorkerInUseException:
pass
else:
Expand Down

0 comments on commit 6b6c548

Please sign in to comment.