fixed render funciton and obsbuilder

This commit is contained in:
Chanumask
2023-11-02 12:02:03 +01:00
parent ee4d29d50b
commit 4a8c12a5c3
7 changed files with 31 additions and 19 deletions
marl_factory_grid

@ -66,7 +66,13 @@ class Collection(_Objects):
@property
def obs_pairs(self):
return [(x.name, x) for x in self]
pair_list = [(self.name, self)]
try:
if self.var_can_be_bound:
pair_list.extend([(a.name, a) for a in self])
except AttributeError:
pass
return pair_list
def by_entity(self, entity):
try:
@ -81,7 +87,10 @@ class Collection(_Objects):
return None
def render(self):
if self.var_has_position:
return [y for y in [x.render() for x in self] if y is not None]
else:
return []
@classmethod
def from_coordinates(cls, positions: [(int, int)], *args, entity_kwargs=None, **kwargs, ):

@ -44,5 +44,9 @@ class GlobalPositions(Collection):
def var_can_collide(self):
return False
@property
def var_can_be_bound(self):
return True
def __init__(self, *args, **kwargs):
super(GlobalPositions, self).__init__(*args, **kwargs)

@ -49,9 +49,6 @@ class Battery(_Object):
summary.update(dict(belongs_to=self._bound_entity.name, chargeLevel=self.charge_level))
return summary
def render(self):
return None
class Pod(Entity):

@ -23,6 +23,10 @@ class Batteries(Collection):
def var_has_position(self):
return False
@property
def var_can_be_bound(self):
return True
@property
def obs_tag(self):
return self.__class__.__name__

@ -12,7 +12,7 @@ class DoorIndicator(Entity):
return d.VALUE_ACCESS_INDICATOR
def render(self):
return None
return []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@ -135,7 +135,7 @@ class DropOffLocations(Collection):
@staticmethod
def trigger_drop_off_location_spawn(state, n_locations):
empty_positions = state.entities.empty_positions[:n_locations]
empty_positions = state.entities.empty_positions()[:n_locations]
do_entites = state[i.DROP_OFF]
drop_offs = [DropOffLocation(pos) for pos in empty_positions]
do_entites.add_items(drop_offs)

@ -15,7 +15,6 @@ from marl_factory_grid.utils.utility_classes import Floor
class OBSBuilder(object):
default_obs = [c.WALLS, c.OTHERS]
@property
@ -95,20 +94,19 @@ class OBSBuilder(object):
agent_want_obs = self.obs_layers[agent.name]
# Handle in-grid observations aka visible observations (Things on the map, with pos)
visible_entitites = self.ray_caster[agent.name].visible_entities(state.entities.pos_dict)
visible_entities = self.ray_caster[agent.name].visible_entities(state.entities.pos_dict)
pre_sort_obs = defaultdict(lambda: np.zeros(self.obs_shape))
if self.pomdp_r:
for e in set(visible_entitites):
for e in set(visible_entities):
self.place_entity_in_observation(pre_sort_obs[e.obs_tag], agent, e)
else:
for e in set(visible_entitites):
for e in set(visible_entities):
pre_sort_obs[e.obs_tag][e.x, e.y] += e.encoding
pre_sort_obs = dict(pre_sort_obs)
obs = np.zeros((len(agent_want_obs), self.obs_shape[0], self.obs_shape[1]))
for idx, l_name in enumerate(agent_want_obs):
print(l_name)
try:
obs[idx] = pre_sort_obs[l_name]
except KeyError:
@ -125,12 +123,11 @@ class OBSBuilder(object):
try:
# Look for bound entity names!
pattern = re.compile(f'{re.escape(l_name)}(.*){re.escape(agent.name)}')
print(pattern)
name = next((x for x in self.all_obs if pattern.search(x)), None)
e = self.all_obs[name]
except KeyError:
try:
e = next(v for k in self.all_obs.items() if l_name in k and agent.name in k)
e = next(v for k, v in self.all_obs.items() if l_name in k and agent.name in k)
except StopIteration:
raise KeyError(
f'Check for spelling errors! \n '
@ -233,7 +230,7 @@ class RayCaster:
return f'{self.__class__.__name__}({self.agent.name})'
def build_ray_targets(self):
north = np.array([0, -1])*self.pomdp_r
north = np.array([0, -1]) * self.pomdp_r
thetas = [np.deg2rad(deg) for deg in np.linspace(-self.degs // 2, self.degs // 2, self.n_rays)[::-1]]
rot_M = [
[[math.cos(theta), -math.sin(theta)],
@ -266,8 +263,9 @@ class RayCaster:
diag_hits = all([
self.ray_block_cache(
key,
lambda: all(False for e in pos_dict[key] if not e.var_is_blocking_light) and bool(pos_dict[key]))
for key in ((x, y-cy), (x-cx, y))
lambda: all(False for e in pos_dict[key] if not e.var_is_blocking_light) and bool(
pos_dict[key]))
for key in ((x, y - cy), (x - cx, y))
]) if (cx != 0 and cy != 0) else False
visible += entities_hit if not diag_hits else []