Skip to content

Commit 1dbe507

Browse files
author
Chris Elion
authored
cleanup port logic in UnityEnvironment (#3673)
1 parent 964d389 commit 1dbe507

File tree

10 files changed

+46
-13
lines changed

10 files changed

+46
-13
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Format of console output has changed slightly and now matches the name of the model/summary directory. (#3630, #3616)
1313
- Raise the wall in CrawlerStatic scene to prevent Agent from falling off. (#3650)
1414
- Renamed 'Generalization' feature to 'Environment Parameter Randomization'.
15+
- The way that UnityEnvironment decides the port was changed. If no port is specified, the behavior will depend on the `file_name` parameter. If it is `None`, 5004 (the editor port) will be used; otherwise 5005 (the base environment port) will be used.
1516

1617
## [0.15.0-preview] - 2020-03-18
1718
### Major Changes

docs/Custom-SideChannels.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ messages to the Unity environment from Python using it.
193193
string_log = StringLogChannel()
194194

195195
# We start the communication with the Unity Editor and pass the string_log side channel as input
196-
env = UnityEnvironment(base_port=UnityEnvironment.DEFAULT_EDITOR_PORT, side_channels=[string_log])
196+
env = UnityEnvironment(side_channels=[string_log])
197197
env.reset()
198198
string_log.send_string("The environment was reset")
199199

docs/Python-API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ as `envs`. For example, if the filename of your Unity environment is `3DBall`, i
5656

5757
```python
5858
from mlagents_envs.environment import UnityEnvironment
59-
env = UnityEnvironment(file_name="3DBall", base_port=5005, seed=1, side_channels=[])
59+
env = UnityEnvironment(file_name="3DBall", seed=1, side_channels=[])
6060
```
6161

6262
- `file_name` is the name of the environment binary (located in the root
@@ -239,7 +239,7 @@ from mlagents_envs.side_channel.engine_configuration_channel import EngineConfig
239239

240240
channel = EngineConfigurationChannel()
241241

242-
env = UnityEnvironment(base_port = UnityEnvironment.DEFAULT_EDITOR_PORT, side_channels = [channel])
242+
env = UnityEnvironment(side_channels=[channel])
243243

244244
channel.set_configuration_parameters(time_scale = 2.0)
245245

@@ -267,7 +267,7 @@ from mlagents_envs.side_channel.float_properties_channel import FloatPropertiesC
267267

268268
channel = FloatPropertiesChannel()
269269

270-
env = UnityEnvironment(base_port = UnityEnvironment.DEFAULT_EDITOR_PORT, side_channels = [channel])
270+
env = UnityEnvironment(side_channels=[channel])
271271

272272
channel.set_property("parameter_1", 2.0)
273273

gym-unity/gym_unity/envs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
:param no_graphics: Whether to run the Unity simulator in no-graphics mode
5858
:param allow_multiple_visual_obs: If True, return a list of visual observations instead of only one.
5959
"""
60-
base_port = 5005
60+
base_port = UnityEnvironment.BASE_ENVIRONMENT_PORT
6161
if environment_filename is None:
6262
base_port = UnityEnvironment.DEFAULT_EDITOR_PORT
6363

ml-agents-envs/mlagents_envs/communicator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def __init__(self, worker_id=0, base_port=5005):
88
"""
99
Python side of the communication. Must be used in pair with the right Unity Communicator equivalent.
1010
11+
:int worker_id: Offset from base_port. Used for training multiple environments simultaneously.
1112
:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
12-
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
1313
"""
1414

1515
def initialize(self, inputs: UnityInputProto) -> UnityOutputProto:

ml-agents-envs/mlagents_envs/environment.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,18 @@ class UnityEnvironment(BaseEnv):
6565
# isn't specified, this port will be used.
6666
DEFAULT_EDITOR_PORT = 5004
6767

68+
# Default base port for environments. Each environment will be offset from this
69+
# by it's worker_id.
70+
BASE_ENVIRONMENT_PORT = 5005
71+
6872
# Command line argument used to pass the port to the executable environment.
6973
PORT_COMMAND_LINE_ARG = "--mlagents-port"
7074

7175
def __init__(
7276
self,
7377
file_name: Optional[str] = None,
7478
worker_id: int = 0,
75-
base_port: int = 5005,
79+
base_port: Optional[int] = None,
7680
seed: int = 0,
7781
docker_training: bool = False,
7882
no_graphics: bool = False,
@@ -87,7 +91,8 @@ def __init__(
8791
8892
:string file_name: Name of Unity environment binary.
8993
:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
90-
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
94+
If no environment is specified (i.e. file_name is None), the DEFAULT_EDITOR_PORT will be used.
95+
:int worker_id: Offset from base_port. Used for training multiple environments simultaneously.
9196
:bool docker_training: Informs this class whether the process is being run within a container.
9297
:bool no_graphics: Whether to run the Unity simulator in no-graphics mode
9398
:int timeout_wait: Time (in seconds) to wait for connection from environment.
@@ -96,6 +101,12 @@ def __init__(
96101
"""
97102
args = args or []
98103
atexit.register(self._close)
104+
# If base port is not specified, use BASE_ENVIRONMENT_PORT if we have
105+
# an environment, otherwise DEFAULT_EDITOR_PORT
106+
if base_port is None:
107+
base_port = (
108+
self.BASE_ENVIRONMENT_PORT if file_name else self.DEFAULT_EDITOR_PORT
109+
)
99110
self.port = base_port + worker_id
100111
self._buffer_size = 12000
101112
# If true, this means the environment was successfully loaded

ml-agents-envs/mlagents_envs/mock_communicator.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ def __init__(
2727
):
2828
"""
2929
Python side of the grpc communication. Python is the client and Unity the server
30-
31-
:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
32-
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
3330
"""
3431
super().__init__()
3532
self.is_discrete = discrete_action

ml-agents-envs/mlagents_envs/rpc_communicator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def __init__(self, worker_id=0, base_port=5005, timeout_wait=30):
3636
3737
3838
:int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
39-
:int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
39+
:int worker_id: Offset from base_port. Used for training multiple environments simultaneously.
40+
:int timeout_wait: Timeout (in seconds) to wait for a response before exiting.
4041
"""
4142
super().__init__(worker_id, base_port)
4243
self.port = base_port + worker_id

ml-agents-envs/mlagents_envs/tests/test_envs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ def test_initialization(mock_communicator, mock_launcher):
2626
env.close()
2727

2828

29+
@pytest.mark.parametrize(
30+
"base_port,file_name,expected",
31+
[
32+
# Non-None base port value will always be used
33+
(6001, "foo.exe", 6001),
34+
# No port specified and environment specified, so use BASE_ENVIRONMENT_PORT
35+
(None, "foo.exe", UnityEnvironment.BASE_ENVIRONMENT_PORT),
36+
# No port specified and no environment, so use DEFAULT_EDITOR_PORT
37+
(None, None, UnityEnvironment.DEFAULT_EDITOR_PORT),
38+
],
39+
)
40+
@mock.patch("mlagents_envs.environment.UnityEnvironment.executable_launcher")
41+
@mock.patch("mlagents_envs.environment.UnityEnvironment.get_communicator")
42+
def test_port_defaults(
43+
mock_communicator, mock_launcher, base_port, file_name, expected
44+
):
45+
mock_communicator.return_value = MockCommunicator(
46+
discrete_action=False, visual_inputs=0
47+
)
48+
env = UnityEnvironment(file_name=file_name, worker_id=0, base_port=base_port)
49+
assert expected == env.port
50+
51+
2952
@mock.patch("mlagents_envs.environment.UnityEnvironment.executable_launcher")
3053
@mock.patch("mlagents_envs.environment.UnityEnvironment.get_communicator")
3154
def test_reset(mock_communicator, mock_launcher):

ml-agents/mlagents/trainers/learn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _create_parser():
9191
)
9292
argparser.add_argument(
9393
"--base-port",
94-
default=5005,
94+
default=UnityEnvironment.BASE_ENVIRONMENT_PORT,
9595
type=int,
9696
help="Base port for environment communication",
9797
)

0 commit comments

Comments
 (0)