Skip to content

Commit 48c3619

Browse files
author
Jonathan Harper
authored
Support both 32-bit and 64-bit types in UnityEnvironment (#1471)
We check for the single brain case in UnityEnvironment by checking for applicable non-dict types in the step arguments. However for ints and floats we just use `np.int_` and `np.float_` for the check, which are the defaults for your system. This means if you are using an application (like baselines in #1448) which uses the wrong int/float size an error will be thrown. This change explicitly allows both 32 and 64-bit numbers.
1 parent d18b994 commit 48c3619

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ml-agents/mlagents/envs/environment.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121

2222
class UnityEnvironment(object):
23+
SCALAR_ACTION_TYPES = (int, np.int32, np.int64, float, np.float32, np.float64)
24+
SINGLE_BRAIN_ACTION_TYPES = SCALAR_ACTION_TYPES + (list, np.ndarray)
25+
SINGLE_BRAIN_TEXT_TYPES = (str, list, np.ndarray)
26+
2327
def __init__(self, file_name=None, worker_id=0,
2428
base_port=5005, seed=0,
2529
docker_training=False, no_graphics=False):
@@ -270,7 +274,7 @@ def step(self, vector_action=None, memory=None, text_action=None, value=None) ->
270274

271275
# Check that environment is loaded, and episode is currently running.
272276
if self._loaded and not self._global_done and self._global_done is not None:
273-
if isinstance(vector_action, (int, np.int_, float, np.float_, list, np.ndarray)):
277+
if isinstance(vector_action, self.SINGLE_BRAIN_ACTION_TYPES):
274278
if self._num_external_brains == 1:
275279
vector_action = {self._external_brain_names[0]: vector_action}
276280
elif self._num_external_brains > 1:
@@ -282,7 +286,7 @@ def step(self, vector_action=None, memory=None, text_action=None, value=None) ->
282286
"There are no external brains in the environment, "
283287
"step cannot take a vector_action input")
284288

285-
if isinstance(memory, (int, np.int_, float, np.float_, list, np.ndarray)):
289+
if isinstance(memory, self.SINGLE_BRAIN_ACTION_TYPES):
286290
if self._num_external_brains == 1:
287291
memory = {self._external_brain_names[0]: memory}
288292
elif self._num_external_brains > 1:
@@ -294,7 +298,7 @@ def step(self, vector_action=None, memory=None, text_action=None, value=None) ->
294298
"There are no external brains in the environment, "
295299
"step cannot take a memory input")
296300

297-
if isinstance(text_action, (str, list, np.ndarray)):
301+
if isinstance(text_action, self.SINGLE_BRAIN_TEXT_TYPES):
298302
if self._num_external_brains == 1:
299303
text_action = {self._external_brain_names[0]: text_action}
300304
elif self._num_external_brains > 1:
@@ -306,7 +310,7 @@ def step(self, vector_action=None, memory=None, text_action=None, value=None) ->
306310
"There are no external brains in the environment, "
307311
"step cannot take a value input")
308312

309-
if isinstance(value, (int, np.int_, float, np.float_, list, np.ndarray)):
313+
if isinstance(value, self.SINGLE_BRAIN_ACTION_TYPES):
310314
if self._num_external_brains == 1:
311315
value = {self._external_brain_names[0]: value}
312316
elif self._num_external_brains > 1:
@@ -419,14 +423,14 @@ def _close(self):
419423
if self.proc1 is not None:
420424
self.proc1.kill()
421425

422-
@staticmethod
423-
def _flatten(arr):
426+
@classmethod
427+
def _flatten(cls, arr):
424428
"""
425429
Converts arrays to list.
426430
:param arr: numpy vector.
427431
:return: flattened list.
428432
"""
429-
if isinstance(arr, (int, np.int_, float, np.float_)):
433+
if isinstance(arr, cls.SCALAR_ACTION_TYPES):
430434
arr = [float(arr)]
431435
if isinstance(arr, np.ndarray):
432436
arr = arr.tolist()

0 commit comments

Comments
 (0)