Comments, small bugfixes removed legacy elements

This commit is contained in:
Steffen Illium
2023-11-17 12:27:03 +01:00
parent fea327861e
commit 467cc3f793
27 changed files with 569 additions and 64 deletions

View File

@@ -14,37 +14,70 @@ class Agent(Entity):
@property
def var_is_paralyzed(self):
"""
TODO
:return:
"""
return len(self._paralyzed)
@property
def paralyze_reasons(self):
"""
TODO
:return:
"""
return [x for x in self._paralyzed]
@property
def obs_tag(self):
"""Internal Usage"""
return self.name
@property
def actions(self):
"""
TODO
:return:
"""
return self._actions
@property
def observations(self):
"""
TODO
:return:
"""
return self._observations
def step_result(self):
pass
"""
TODO
FIXME THINK ITS LEGACY... Not Used any more
@property
def collection(self):
return self._collection
:return:
"""
pass
@property
def var_is_blocking_pos(self):
return self._is_blocking_pos
def __init__(self, actions: List[Action], observations: List[str], *args, is_blocking_pos=False, **kwargs):
"""
TODO
:return:
"""
super(Agent, self).__init__(*args, **kwargs)
self._paralyzed = set()
self.step_result = dict()
@@ -54,26 +87,53 @@ class Agent(Entity):
self._is_blocking_pos = is_blocking_pos
def summarize_state(self):
"""
TODO
:return:
"""
state_dict = super().summarize_state()
state_dict.update(valid=bool(self.state.validity), action=str(self.state.identifier))
return state_dict
def set_state(self, action_result):
self._status = action_result
def set_state(self, state):
"""
TODO
:return:
"""
self._status = state
return c.VALID
def paralyze(self, reason):
"""
TODO
:return:
"""
self._paralyzed.add(reason)
return c.VALID
def de_paralyze(self, reason):
def de_paralyze(self, reason) -> bool:
"""
TODO
:return:
"""
try:
self._paralyzed.remove(reason)
return c.VALID
except KeyError:
return c.NOT_VALID
def render(self):
i = next(idx for idx, x in enumerate(self._collection) if x.name == self.name)
def render(self) -> RenderEntity:
i = self.collection.idx_by_entity(self)
assert i is not None
curr_state = self.state
if curr_state.identifier == c.COLLISION:
render_state = renderer.STATE_COLLISION

View File

@@ -4,23 +4,40 @@ import numpy as np
from .object import Object
from .. import constants as c
from ...utils.results import ActionResult
from ...utils.results import State
from ...utils.utility_classes import RenderEntity
class Entity(Object, abc.ABC):
"""Full Env Entity that lives on the environment Grid. Doors, Items, DirtPile etc..."""
@property
def state(self):
return self._status or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID)
"""
TODO
:return:
"""
return self._status or State(entity=self, identifier=c.NOOP, validity=c.VALID)
@property
def var_has_position(self):
"""
TODO
:return:
"""
return self.pos != c.VALUE_NO_POS
@property
def var_is_blocking_light(self):
"""
TODO
:return:
"""
try:
return self._collection.var_is_blocking_light or False
except AttributeError:
@@ -28,6 +45,12 @@ class Entity(Object, abc.ABC):
@property
def var_can_move(self):
"""
TODO
:return:
"""
try:
return self._collection.var_can_move or False
except AttributeError:
@@ -35,6 +58,12 @@ class Entity(Object, abc.ABC):
@property
def var_is_blocking_pos(self):
"""
TODO
:return:
"""
try:
return self._collection.var_is_blocking_pos or False
except AttributeError:
@@ -42,6 +71,12 @@ class Entity(Object, abc.ABC):
@property
def var_can_collide(self):
"""
TODO
:return:
"""
try:
return self._collection.var_can_collide or False
except AttributeError:
@@ -49,22 +84,53 @@ class Entity(Object, abc.ABC):
@property
def x(self):
"""
TODO
:return:
"""
return self.pos[0]
@property
def y(self):
"""
TODO
:return:
"""
return self.pos[1]
@property
def pos(self):
"""
TODO
:return:
"""
return self._pos
def set_pos(self, pos):
def set_pos(self, pos) -> bool:
"""
TODO
:return:
"""
assert isinstance(pos, tuple) and len(pos) == 2
self._pos = pos
return c.VALID
@property
def last_pos(self):
"""
TODO
:return:
"""
try:
return self._last_pos
except AttributeError:
@@ -74,12 +140,24 @@ class Entity(Object, abc.ABC):
@property
def direction_of_view(self):
"""
TODO
:return:
"""
if self._last_pos != c.VALUE_NO_POS:
return 0, 0
else:
return np.subtract(self._last_pos, self.pos)
def move(self, next_pos, state):
"""
TODO
:return:
"""
next_pos = next_pos
curr_pos = self._pos
if not_same_pos := curr_pos != next_pos:
@@ -95,11 +173,19 @@ class Entity(Object, abc.ABC):
return not_same_pos
def __init__(self, pos, bind_to=None, **kwargs):
"""
Full Env Entity that lives on the environment Grid. Doors, Items, DirtPile etc...
TODO
:return:
"""
super().__init__(**kwargs)
self._view_directory = c.VALUE_NO_POS
self._status = None
self._pos = pos
self._last_pos = pos
self._collection = None
if bind_to:
try:
self.bind_to(bind_to)
@@ -108,14 +194,27 @@ class Entity(Object, abc.ABC):
exit()
def summarize_state(self) -> dict:
"""
TODO
:return:
"""
return dict(name=str(self.name), x=int(self.x), y=int(self.y), can_collide=bool(self.var_can_collide))
@abc.abstractmethod
def render(self):
"""
TODO
:return:
"""
return RenderEntity(self.__class__.__name__.lower(), self.pos)
@property
def obs_tag(self):
"""Internal Usage"""
try:
return self._collection.name or self.name
except AttributeError:
@@ -123,10 +222,32 @@ class Entity(Object, abc.ABC):
@property
def encoding(self):
"""
TODO
:return:
"""
return c.VALUE_OCCUPIED_CELL
def change_parent_collection(self, other_collection):
"""
TODO
:return:
"""
other_collection.add_item(self)
self._collection.delete_env_object(self)
self._collection = other_collection
return self._collection == other_collection
@property
def collection(self):
"""
TODO
:return:
"""
return self._collection

View File

@@ -6,40 +6,85 @@ import marl_factory_grid.utils.helpers as h
class Object:
"""Generell Objects for Organisation and Maintanance such as Actions etc..."""
_u_idx = defaultdict(lambda: 0)
def __bool__(self):
return True
@property
def bound_entity(self):
"""
TODO
:return:
"""
return self._bound_entity
@property
def var_can_be_bound(self):
def var_can_be_bound(self) -> bool:
"""
TODO
Indicates if it is possible to bind this object to another Entity or Object.
:return: Whether this object can be bound.
"""
try:
return self._collection.var_can_be_bound or False
except AttributeError:
return False
@property
def observers(self):
def observers(self) -> set:
"""
TODO
:return:
"""
return self._observers
@property
def name(self):
"""
TODO
:return:
"""
return f'{self.__class__.__name__}[{self.identifier}]'
@property
def identifier(self):
"""
TODO
:return:
"""
if self._str_ident is not None:
return self._str_ident
else:
return self.u_int
def reset_uid(self):
"""
TODO
:return:
"""
self._u_idx = defaultdict(lambda: 0)
return True
def __init__(self, str_ident: Union[str, None] = None, **kwargs):
"""
Generell Objects for Organisation and Maintanance such as Actions etc...
TODO
:param str_ident:
:return:
"""
self._status = None
self._bound_entity = None
self._observers = set()
@@ -50,6 +95,9 @@ class Object:
if kwargs:
print(f'Following kwargs were passed, but ignored: {kwargs}')
def __bool__(self) -> bool:
return True
def __repr__(self):
name = self.name
if self.bound_entity:
@@ -67,39 +115,61 @@ class Object:
def __hash__(self):
return hash(self.identifier)
def _identify_and_count_up(self):
def _identify_and_count_up(self) -> int:
"""Internal Usage"""
idx = Object._u_idx[self.__class__.__name__]
Object._u_idx[self.__class__.__name__] += 1
return idx
def set_collection(self, collection):
"""Internal Usage"""
self._collection = collection
return self
def add_observer(self, observer):
"""Internal Usage"""
self.observers.add(observer)
observer.notify_add_entity(self)
return self
def del_observer(self, observer):
"""Internal Usage"""
self.observers.remove(observer)
return self
def summarize_state(self):
return dict()
def clear_temp_state(self):
"""Internal Usage"""
self._status = None
return self
def bind_to(self, entity):
# noinspection PyAttributeOutsideInit
"""
TODO
:return:
"""
self._bound_entity = entity
return c.VALID
def belongs_to_entity(self, entity):
"""
TODO
:return:
"""
return self._bound_entity == entity
@property
def bound_entity(self):
return self._bound_entity
def unbind(self):
"""
TODO
:return:
"""
previously_bound = self._bound_entity
self._bound_entity = None
return previously_bound

View File

@@ -11,15 +11,33 @@ from marl_factory_grid.environment.entity.object import Object
class PlaceHolder(Object):
def __init__(self, *args, fill_value=0, **kwargs):
"""
TODO
:return:
"""
super().__init__(*args, **kwargs)
self._fill_value = fill_value
@property
def can_collide(self):
def var_can_collide(self):
"""
TODO
:return:
"""
return False
@property
def encoding(self):
"""
TODO
:return:
"""
return self._fill_value
@property
@@ -29,14 +47,30 @@ class PlaceHolder(Object):
class GlobalPosition(Object):
@property
def obs_tag(self):
return self.name
@property
def encoding(self):
"""
TODO
:return:
"""
if self._normalized:
return tuple(np.divide(self._bound_entity.pos, self._shape))
else:
return self.bound_entity.pos
def __init__(self, agent, level_shape, *args, normalized: bool = True, **kwargs):
"""
TODO
:return:
"""
super(GlobalPosition, self).__init__(*args, **kwargs)
self.bind_to(agent)
self._normalized = normalized

View File

@@ -6,6 +6,12 @@ from marl_factory_grid.utils.utility_classes import RenderEntity
class Wall(Entity):
def __init__(self, *args, **kwargs):
"""
TODO
:return:
"""
super().__init__(*args, **kwargs)
@property