Files
UnrealEngineUWP/Engine/Plugins/AI/MLAdapter/Source/python/unreal/mladapter/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

57 lines
1.7 KiB
Python

# Copyright Epic Games, Inc. All Rights Reserved.
from .utils import dict_from_json
import gym.spaces
from collections import OrderedDict
def create_space(type_name, params):
assert type(type_name) == str
if type_name == 'Discrete':
return gym.spaces.Discrete(params)
elif type_name == 'MultiDiscrete':
return gym.spaces.MultiDiscrete(params)
elif type_name == 'Box':
assert len(params) >= 3
low = params[0]
high = params[1]
shape = params[2:]
return gym.spaces.Box(low, high, shape=shape)
elif type_name == 'Tuple':
return gym_space_from_mladapter(params)
else:
return gym.spaces.Dict({type_name: gym_space_from_mladapter(params)})
def gym_space_from_mladapter(data):
if type(data) == str or type(data) == bytes:
data = dict_from_json(data)
return gym_space_from_list(data) if data is not None else None
def gym_space_from_list(data):
# when called with an OrderedDict we only are about the values. Keys are there for debugging and readers'
# convenience reasons
if type(data) == OrderedDict:
data = list(data.values())
if type(data) != list:
raise TypeError('Only OrderedDict and list are supported while data is of type {}'.format(type(data)))
return None
if len(data) == 0:
return None
spaces = []
for d in data:
if type(d) == OrderedDict:
for k, v in d.items():
spaces.append(create_space(k, v))
else:
spaces.append(gym_space_from_mladapter(d))
if len(spaces) == 1:
return spaces[0]
else:
return gym.spaces.Tuple(tuple(spaces))