Skip to content

ding.envs.common.env_element

ding.envs.common.env_element

Full Source Code

../ding/envs/common/env_element.py

1from abc import ABC, abstractmethod 2from collections import namedtuple 3from typing import Any 4 5EnvElementInfo = namedtuple('EnvElementInfo', ['shape', 'value']) 6 7 8class IEnvElement(ABC): 9 10 @abstractmethod 11 def __repr__(self) -> str: 12 raise NotImplementedError 13 14 @property 15 @abstractmethod 16 def info(self) -> Any: 17 raise NotImplementedError 18 19 20class EnvElement(IEnvElement): 21 _instance = None 22 _name = 'EnvElement' 23 24 def __init__(self, *args, **kwargs) -> None: 25 # placeholder 26 # self._shape = None 27 # self._value = None 28 # self._to_agent_processor = None 29 # self._from_agent_processor = None 30 self._init(*args, **kwargs) 31 self._check() 32 33 @abstractmethod 34 def _init(*args, **kwargs) -> None: 35 raise NotImplementedError 36 37 def __repr__(self) -> str: 38 return '{}: {}'.format(self._name, self._details()) 39 40 @abstractmethod 41 def _details(self) -> str: 42 raise NotImplementedError 43 44 def _check(self) -> None: 45 flag = [ 46 hasattr(self, '_shape'), 47 hasattr(self, '_value'), 48 # hasattr(self, '_to_agent_processor'), 49 # hasattr(self, '_from_agent_processor'), 50 ] 51 assert all(flag), 'this class {} is not a legal subclass of EnvElement({})'.format(self.__class__, flag) 52 53 @property 54 def info(self) -> 'EnvElementInfo': 55 return EnvElementInfo( 56 shape=self._shape, 57 value=self._value, 58 # to_agent_processor=self._to_agent_processor, 59 # from_agent_processor=self._from_agent_processor 60 )