Files
UnrealEngineUWP/Engine/Plugins/AI/MLAdapter/Source/python/tests/test_spaces.py
mieszko zielinski 7848b68be0 Renamed UE4ML plugin to MLAdapter and updated all its contents accordingly
#lockdown nick.whiting
[at]Nick.Whiting, [at]Mikko.Mononen
#rb Nick.Whiting, Mikko.Mononen
#jira UE-111731
#jira UE-111119
#jira UE-111117
#jira UE-111114
#jira UE-111091
#jira UE-111075
#jira UE-111060
#jira UE-111058
#jira UE-111049
#jira UE-111044
#jira UE-111040
#jira UE-111039
#jira UE-111038
#jira UE-111036
#jira UE-111033
#jira UE-111032
#jira UE-111029
#jira UE-111026
#jira UE-111024
#jira UE-111021
#jira UE-111020
#jira UE-111018
#jira UE-111017
#jira UE-111016
#jira UE-111015
#jira UE-111014
#jira UE-111013
#jira UE-111012
#jira UE-110977
#jira UE-110975
#jira UE-110974
#jira UE-110973
#jira UE-110971
#jira UE-110969
#jira UE-110965
#jira UE-110949
#preflight 606ebe61db0bbb00016e242e

#ROBOMERGE-OWNER: mieszko.zielinski
#ROBOMERGE-AUTHOR: mieszko.zielinski
#ROBOMERGE-SOURCE: CL 15955453 in //UE5/Release-5.0-EarlyAccess/...
#ROBOMERGE-BOT: STARSHIP (Release-5.0-EarlyAccess -> Main) (v787-15839533)
#ROBOMERGE-CONFLICT from-shelf

[CL 15975769 by mieszko zielinski in ue5-main branch]
2021-04-12 03:41:10 -04:00

51 lines
2.2 KiB
Python

# Copyright Epic Games, Inc. All Rights Reserved.
import unittest
import unreal.mladapter.utils as utils
import unreal.mladapter.spaces as spaces
from gym.spaces import Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict
import json
simple_box4 = Box(-1, 1, shape=(4,))
simple_box2 = Box(-1, 1, shape=(2,))
json_samples = [(b'{"0":"{\\"Discrete\\":10}"}', Discrete(10)),
(b'{"b":"{\\"MultiDiscrete\\":11}"}', MultiDiscrete(11)),
(b'{"1":"{\\"Box\\":[-1.000000,2.5,5]}"}', Box(-1, 2.5, shape=(5,))),
(b'{"4":"{\\"Tuple\\":[{\\"Box\\":[-1.000000,1.000000,4]},{\\"Box\\":[-2.000000,3.000000,8]}]}"}',
Tuple((simple_box4, Box(-2, 3, shape=(8,))))),
(b'{}', None),
(b'{"AIPerception":"{\\"Tuple\\":[{\\"Box\\":[-1.000000,1.000000,4]},{\\"Box\\":[-1.000000,1.000000,4]}'
b',{\\"Box\\":[-1.000000,1.000000,4]}]}","Attribute":"{\\"Box\\":[-1.000000,1.000000,2]}"}',
Tuple((Tuple((simple_box4, simple_box4, simple_box4)), simple_box2)))]
class SpaceBuildingTest(unittest.TestCase):
def test_invalid_json(self):
self.assertIsNone(spaces.gym_space_from_mladapter(''))
self.assertIsNone(spaces.gym_space_from_mladapter(None))
with self.assertRaises(json.JSONDecodeError):
spaces.gym_space_from_mladapter('notjson')
def test_invalid_parameters(self):
with self.assertRaises(TypeError):
spaces.gym_space_from_list({'0': '0'})
with self.assertRaises(TypeError):
spaces.gym_space_from_list(1)
def test_space_creation(self):
space_samples = [(('Discrete', 12), Discrete(12)),
(('MultiDiscrete', 64), MultiDiscrete(64)),
(('Box', (-2, 2, 7)), Box(-2, 2, shape=(7,)))]
for sample in space_samples:
self.assertEqual(spaces.create_space(*sample[0]), sample[1])
for sample in json_samples:
space = spaces.gym_space_from_mladapter(sample[0])
self.assertEqual(sample[1], space)
if __name__ == '__main__':
unittest.main()