mirror of
				https://github.com/illiumst/marl-factory-grid.git
				synced 2025-10-31 12:37:27 +01:00 
			
		
		
		
	Door Area Indicators
This commit is contained in:
		| @@ -68,6 +68,7 @@ class BaseFactory(gym.Env): | |||||||
|     @property |     @property | ||||||
|     def params(self) -> dict: |     def params(self) -> dict: | ||||||
|         d = {key: val for key, val in self.__dict__.items() if not key.startswith('_') and not key.startswith('__')} |         d = {key: val for key, val in self.__dict__.items() if not key.startswith('_') and not key.startswith('__')} | ||||||
|  |         d['class_name'] = self.__class__.__name__ | ||||||
|         return d |         return d | ||||||
|  |  | ||||||
|     def __enter__(self): |     def __enter__(self): | ||||||
| @@ -83,7 +84,10 @@ class BaseFactory(gym.Env): | |||||||
|                  rewards_base: RewardsBase = RewardsBase(), |                  rewards_base: RewardsBase = RewardsBase(), | ||||||
|                  parse_doors=False, done_at_collision=False, inject_agents: Union[None, List] = None, |                  parse_doors=False, done_at_collision=False, inject_agents: Union[None, List] = None, | ||||||
|                  verbose=False, doors_have_area=True, env_seed=time.time_ns(), individual_rewards=False, |                  verbose=False, doors_have_area=True, env_seed=time.time_ns(), individual_rewards=False, | ||||||
|                  **kwargs): |                  class_name='', **kwargs): | ||||||
|  |  | ||||||
|  |         if class_name: | ||||||
|  |             print(f'You loaded parameters for {class_name}', f'this is: {self.__class__.__name__}') | ||||||
|  |  | ||||||
|         if isinstance(mv_prop, dict): |         if isinstance(mv_prop, dict): | ||||||
|             mv_prop = MovementProperties(**mv_prop) |             mv_prop = MovementProperties(**mv_prop) | ||||||
| @@ -167,7 +171,7 @@ class BaseFactory(gym.Env): | |||||||
|             parsed_doors = np.pad(parsed_doors, self.obs_prop.pomdp_r, 'constant', constant_values=0) |             parsed_doors = np.pad(parsed_doors, self.obs_prop.pomdp_r, 'constant', constant_values=0) | ||||||
|             if np.any(parsed_doors): |             if np.any(parsed_doors): | ||||||
|                 door_tiles = [floor.by_pos(tuple(pos)) for pos in np.argwhere(parsed_doors == c.OCCUPIED_CELL)] |                 door_tiles = [floor.by_pos(tuple(pos)) for pos in np.argwhere(parsed_doors == c.OCCUPIED_CELL)] | ||||||
|                 doors = Doors.from_tiles(door_tiles, self._level_shape, |                 doors = Doors.from_tiles(door_tiles, self._level_shape, have_area=self.doors_have_area, | ||||||
|                                          entity_kwargs=dict(context=floor) |                                          entity_kwargs=dict(context=floor) | ||||||
|                                          ) |                                          ) | ||||||
|                 self._entities.register_additional_items({c.DOORS: doors}) |                 self._entities.register_additional_items({c.DOORS: doors}) | ||||||
|   | |||||||
| @@ -311,7 +311,7 @@ class Door(Entity): | |||||||
|     @property |     @property | ||||||
|     def encoding(self): |     def encoding(self): | ||||||
|         # This is important as it shadow is checked by occupation value |         # This is important as it shadow is checked by occupation value | ||||||
|         return c.OCCUPIED_CELL if self.is_closed else 0.5 |         return c.CLOSED_DOOR_CELL if self.is_closed else c.OPEN_DOOR_CELL | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def str_state(self): |     def str_state(self): | ||||||
|   | |||||||
| @@ -460,7 +460,9 @@ class Agents(MovingEntityObjectRegister): | |||||||
|  |  | ||||||
| class Doors(EntityRegister): | class Doors(EntityRegister): | ||||||
|  |  | ||||||
|     def __init__(self, *args, **kwargs): |     def __init__(self, *args, have_area: bool = False, **kwargs): | ||||||
|  |         self.have_area = have_area | ||||||
|  |         self._area_marked = False | ||||||
|         super(Doors, self).__init__(*args, is_blocking_light=True, can_collide=True, **kwargs) |         super(Doors, self).__init__(*args, is_blocking_light=True, can_collide=True, **kwargs) | ||||||
|  |  | ||||||
|     _accepted_objects = Door |     _accepted_objects = Door | ||||||
| @@ -475,6 +477,18 @@ class Doors(EntityRegister): | |||||||
|         for door in self: |         for door in self: | ||||||
|             door.tick() |             door.tick() | ||||||
|  |  | ||||||
|  |     def as_array(self): | ||||||
|  |         if self.have_area and not self._area_marked: | ||||||
|  |             for door in self: | ||||||
|  |                 for pos in door.access_area: | ||||||
|  |                     if self._individual_slices: | ||||||
|  |                         pass | ||||||
|  |                     else: | ||||||
|  |                         pos = (0, *pos) | ||||||
|  |                     self._lazy_eval_transforms.append((pos, c.ACCESS_DOOR_CELL)) | ||||||
|  |             self._area_marked = True | ||||||
|  |         return super(Doors, self).as_array() | ||||||
|  |  | ||||||
|  |  | ||||||
| class Actions(ObjectRegister): | class Actions(ObjectRegister): | ||||||
|     _accepted_objects = Action |     _accepted_objects = Action | ||||||
|   | |||||||
| @@ -155,7 +155,7 @@ class BatteryFactory(BaseFactory): | |||||||
|         if isinstance(btry_prop, dict): |         if isinstance(btry_prop, dict): | ||||||
|             btry_prop = BatteryProperties(**btry_prop) |             btry_prop = BatteryProperties(**btry_prop) | ||||||
|         if isinstance(rewards_dest, dict): |         if isinstance(rewards_dest, dict): | ||||||
|             rewards_dest = RewardsBtry(**rewards_dest) |             rewards_dest = BatteryProperties(**rewards_dest) | ||||||
|         self.btry_prop = btry_prop |         self.btry_prop = btry_prop | ||||||
|         self.rewards_dest = rewards_dest |         self.rewards_dest = rewards_dest | ||||||
|         super().__init__(*args, **kwargs) |         super().__init__(*args, **kwargs) | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| import time | import time | ||||||
|  | from pathlib import Path | ||||||
| from typing import List, Union, NamedTuple, Dict | from typing import List, Union, NamedTuple, Dict | ||||||
| import random | import random | ||||||
|  |  | ||||||
| @@ -284,7 +285,8 @@ if __name__ == '__main__': | |||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     obs_props = ObservationProperties(render_agents=aro.COMBINED, omit_agent_self=True, |     obs_props = ObservationProperties(render_agents=aro.COMBINED, omit_agent_self=True, | ||||||
|                                       pomdp_r=2, additional_agent_placeholder=None, cast_shadows=True) |                                       pomdp_r=2, additional_agent_placeholder=None, cast_shadows=True, | ||||||
|  |                                       indicate_door_area=True) | ||||||
|  |  | ||||||
|     move_props = {'allow_square_movement': True, |     move_props = {'allow_square_movement': True, | ||||||
|                   'allow_diagonal_movement': False, |                   'allow_diagonal_movement': False, | ||||||
| @@ -295,13 +297,15 @@ if __name__ == '__main__': | |||||||
|  |  | ||||||
|         factory = DirtFactory(n_agents=10, done_at_collision=False, |         factory = DirtFactory(n_agents=10, done_at_collision=False, | ||||||
|                               level_name='rooms', max_steps=1000, |                               level_name='rooms', max_steps=1000, | ||||||
|                               doors_have_area=False, |                               doors_have_area=True, | ||||||
|                               obs_prop=obs_props, parse_doors=True, |                               obs_prop=obs_props, parse_doors=True, | ||||||
|                               verbose=True, |                               verbose=True, | ||||||
|                               mv_prop=move_props, dirt_prop=dirt_props, |                               mv_prop=move_props, dirt_prop=dirt_props, | ||||||
|                               # inject_agents=[TSPDirtAgent], |                               # inject_agents=[TSPDirtAgent], | ||||||
|                               ) |                               ) | ||||||
|  |  | ||||||
|  |         factory.save_params(Path('rewards_param')) | ||||||
|  |  | ||||||
|         # noinspection DuplicatedCode |         # noinspection DuplicatedCode | ||||||
|         n_actions = factory.action_space.n - 1 |         n_actions = factory.action_space.n - 1 | ||||||
|         _ = factory.observation_space |         _ = factory.observation_space | ||||||
|   | |||||||
| @@ -31,11 +31,15 @@ class Constants: | |||||||
|     FREE_CELL           = 0 |     FREE_CELL           = 0 | ||||||
|     OCCUPIED_CELL       = 1 |     OCCUPIED_CELL       = 1 | ||||||
|     SHADOWED_CELL       = -1 |     SHADOWED_CELL       = -1 | ||||||
|  |     ACCESS_DOOR_CELL    = 1/3 | ||||||
|  |     OPEN_DOOR_CELL      = 2/3 | ||||||
|  |     CLOSED_DOOR_CELL    = 3/3 | ||||||
|     NO_POS              = (-9999, -9999) |     NO_POS              = (-9999, -9999) | ||||||
|  |  | ||||||
|     DOORS               = 'Doors' |     DOORS               = 'Doors' | ||||||
|     CLOSED_DOOR         = 'closed' |     CLOSED_DOOR         = 'closed' | ||||||
|     OPEN_DOOR           = 'open' |     OPEN_DOOR           = 'open' | ||||||
|  |     ACCESS_DOOR         = 'access' | ||||||
|  |  | ||||||
|     ACTION              = 'action' |     ACTION              = 'action' | ||||||
|     COLLISION          = 'collision' |     COLLISION          = 'collision' | ||||||
| @@ -87,7 +91,6 @@ class RewardsBase(NamedTuple): | |||||||
|  |  | ||||||
| m = EnvActions | m = EnvActions | ||||||
| c = Constants | c = Constants | ||||||
| r = RewardsBase |  | ||||||
|  |  | ||||||
| ACTIONMAP = defaultdict(lambda: (0, 0), | ACTIONMAP = defaultdict(lambda: (0, 0), | ||||||
|                         {m.NORTH: (-1, 0), m.NORTHEAST: (-1, 1), |                         {m.NORTH: (-1, 0), m.NORTHEAST: (-1, 1), | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ class ObservationProperties(NamedTuple): | |||||||
|     cast_shadows: bool = True |     cast_shadows: bool = True | ||||||
|     frames_to_stack: int = 0 |     frames_to_stack: int = 0 | ||||||
|     pomdp_r: int = 0 |     pomdp_r: int = 0 | ||||||
|  |     indicate_door_area: bool = True | ||||||
|     show_global_position_info: bool = False |     show_global_position_info: bool = False | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -34,4 +35,3 @@ class MarlFrameStack(gym.ObservationWrapper): | |||||||
|         if isinstance(self.env, FrameStack) and self.env.unwrapped.n_agents > 1: |         if isinstance(self.env, FrameStack) and self.env.unwrapped.n_agents > 1: | ||||||
|             return observation[0:].swapaxes(0, 1) |             return observation[0:].swapaxes(0, 1) | ||||||
|         return observation |         return observation | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Steffen Illium
					Steffen Illium