From 0e07e47d513b689c203c0c15696d3779fca04a1f Mon Sep 17 00:00:00 2001 From: Steffen Illium Date: Fri, 1 Dec 2023 11:13:15 +0100 Subject: [PATCH 1/2] Zones Removed --- README.md | 3 - marl_factory_grid/configs/default_config.yaml | 3 - .../configs/two_rooms_one_door.yaml | 4 -- marl_factory_grid/modules/zones/__init__.py | 3 - marl_factory_grid/modules/zones/constants.py | 4 -- marl_factory_grid/modules/zones/entitites.py | 19 ----- marl_factory_grid/modules/zones/groups.py | 26 ------- marl_factory_grid/modules/zones/rules.py | 71 ------------------- marl_factory_grid/utils/logging/recorder.py | 1 - marl_factory_grid/utils/tools.py | 2 +- 10 files changed, 1 insertion(+), 135 deletions(-) delete mode 100644 marl_factory_grid/modules/zones/__init__.py delete mode 100644 marl_factory_grid/modules/zones/constants.py delete mode 100644 marl_factory_grid/modules/zones/entitites.py delete mode 100644 marl_factory_grid/modules/zones/groups.py delete mode 100644 marl_factory_grid/modules/zones/rules.py diff --git a/README.md b/README.md index 77c6673..048c78a 100644 --- a/README.md +++ b/README.md @@ -120,9 +120,6 @@ Just define what your environment needs in a *yaml*-configfile like: Maintainers: coords_or_quantity: 1 - # Zones: Entities representing zones in the environment. - Zones: { } - # Rules section specifies the rules governing the dynamics of the environment. Rules: diff --git a/marl_factory_grid/configs/default_config.yaml b/marl_factory_grid/configs/default_config.yaml index 4ec7a7a..a6a53d9 100644 --- a/marl_factory_grid/configs/default_config.yaml +++ b/marl_factory_grid/configs/default_config.yaml @@ -104,9 +104,6 @@ Entities: Maintainers: coords_or_quantity: 1 - # Zones: Entities representing zones in the environment. - Zones: { } - # Rules section specifies the rules governing the dynamics of the environment. Rules: diff --git a/marl_factory_grid/configs/two_rooms_one_door.yaml b/marl_factory_grid/configs/two_rooms_one_door.yaml index 09659a2..49ab7eb 100644 --- a/marl_factory_grid/configs/two_rooms_one_door.yaml +++ b/marl_factory_grid/configs/two_rooms_one_door.yaml @@ -42,7 +42,6 @@ Entities: Destinations: { } Doors: { } GlobalPositions: { } - Zones: { } Rules: # Environment Dynamics @@ -56,9 +55,6 @@ Rules: # Init AssignGlobalPositions: { } - ZoneInit: { } - AgentSingleZonePlacement: { } - IndividualDestinationZonePlacement: { } # Done Conditions MaxStepsReached: diff --git a/marl_factory_grid/modules/zones/__init__.py b/marl_factory_grid/modules/zones/__init__.py deleted file mode 100644 index 7ae5f3c..0000000 --- a/marl_factory_grid/modules/zones/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .entitites import Zone -from .groups import Zones -from .rules import AgentSingleZonePlacement diff --git a/marl_factory_grid/modules/zones/constants.py b/marl_factory_grid/modules/zones/constants.py deleted file mode 100644 index 135a471..0000000 --- a/marl_factory_grid/modules/zones/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -# Names / Identifiers - -ZONES = 'Zones' # Identifier of Zone-objects and groups (groups). -ZONE = 'Zone' # -||- diff --git a/marl_factory_grid/modules/zones/entitites.py b/marl_factory_grid/modules/zones/entitites.py deleted file mode 100644 index 4aa0f70..0000000 --- a/marl_factory_grid/modules/zones/entitites.py +++ /dev/null @@ -1,19 +0,0 @@ -import random -from typing import List, Tuple - -from marl_factory_grid.environment.entity.object import Object - - -class Zone(Object): - - @property - def positions(self): - return self.coords - - def __init__(self, coords: List[Tuple[(int, int)]], *args, **kwargs): - super(Zone, self).__init__(*args, **kwargs) - self.coords = coords - - @property - def random_pos(self): - return random.choice(self.coords) diff --git a/marl_factory_grid/modules/zones/groups.py b/marl_factory_grid/modules/zones/groups.py deleted file mode 100644 index f5494cd..0000000 --- a/marl_factory_grid/modules/zones/groups.py +++ /dev/null @@ -1,26 +0,0 @@ -from marl_factory_grid.environment.groups.objects import Objects -from marl_factory_grid.modules.zones import Zone - - -class Zones(Objects): - symbol = None - _entity = Zone - - @property - def var_can_move(self): - return False - - def __init__(self, *args, **kwargs): - super(Zones, self).__init__(*args, can_collide=True, **kwargs) - - def by_pos(self, pos): - return self.pos_dict[pos] - - def notify_add_entity(self, entity: Zone): - self.pos_dict.update({key: [entity] for key in entity.positions}) - return True - - def notify_del_entity(self, entity: Zone): - for pos in entity.positions: - self.pos_dict[pos].remove(entity) - return True diff --git a/marl_factory_grid/modules/zones/rules.py b/marl_factory_grid/modules/zones/rules.py deleted file mode 100644 index f308256..0000000 --- a/marl_factory_grid/modules/zones/rules.py +++ /dev/null @@ -1,71 +0,0 @@ -from random import choices, choice - -from . import constants as z, Zone -from .. import Destination -from ..destinations import constants as d -from ...environment.rules import Rule -from ...environment import constants as c - - -class ZoneInit(Rule): - - def __init__(self): - super().__init__() - self._zones = list() - - def on_init(self, state, lvl_map): - z_idx = 1 - - while z_idx: - zone_positions = lvl_map.get_coordinates_for_symbol(z_idx) - if len(zone_positions): - self._zones.append(Zone(zone_positions)) - z_idx += 1 - else: - z_idx = 0 - - def on_reset(self, state): - state[z.ZONES].add_items(self._zones) - return [] - - -class AgentSingleZonePlacement(Rule): - - def __init__(self): - super().__init__() - - def on_reset(self, state): - n_agents = len(state[c.AGENT]) - assert len(state[z.ZONES]) >= n_agents - - z_idxs = choices(list(range(len(state[z.ZONES]))), k=n_agents) - for agent in state[c.AGENT]: - agent.move(state[z.ZONES][z_idxs.pop()].random_pos, state) - return [] - - -class IndividualDestinationZonePlacement(Rule): - - def __init__(self): - raise NotImplementedError("This is pretty new, and needs to be debugged, after the zones") - super().__init__() - - def on_reset(self, state): - for agent in state[c.AGENT]: - self.trigger_spawn(agent, state) - return [] - - @staticmethod - def trigger_spawn(agent, state): - agent_zones = state[z.ZONES].by_pos(agent.pos) - other_zones = [x for x in state[z.ZONES] if x not in agent_zones] - already_has_destination = True - while already_has_destination: - pos = choice(other_zones).random_pos - if state[d.DESTINATION].by_pos(pos) is None: - already_has_destination = False - destination = Destination(pos, bind_to=agent) - - state[d.DESTINATION].add_item(destination) - continue - return c.VALID diff --git a/marl_factory_grid/utils/logging/recorder.py b/marl_factory_grid/utils/logging/recorder.py index 2e9ed2f..96caa5f 100644 --- a/marl_factory_grid/utils/logging/recorder.py +++ b/marl_factory_grid/utils/logging/recorder.py @@ -73,7 +73,6 @@ class EnvRecorder(Wrapper): n_dests=0, dwell_time=0, spawn_frequency=0, - spawn_in_other_zone=False, spawn_mode='' ) rewards_dest = dict( diff --git a/marl_factory_grid/utils/tools.py b/marl_factory_grid/utils/tools.py index ee79965..e264a3a 100644 --- a/marl_factory_grid/utils/tools.py +++ b/marl_factory_grid/utils/tools.py @@ -15,7 +15,7 @@ OBSERVATIONS = 'Observations' RULES = 'Rule' TESTS = 'Tests' EXCLUDED = ['identifier', 'args', 'kwargs', 'Move', 'Agent', 'GlobalPositions', 'Walls', 'Gamestate', 'Path', - 'Iterable', 'Move', 'Result', 'TemplateRule', 'Entities', 'EnvObjects', 'Zones', 'Collection', + 'Iterable', 'Move', 'Result', 'TemplateRule', 'Entities', 'EnvObjects', 'Collection', 'State', 'Object', 'default_valid_reward', 'default_fail_reward', 'size'] From 0d5e3146231c7df3a94ed355dd776f19d06c45e2 Mon Sep 17 00:00:00 2001 From: Steffen Illium Date: Fri, 1 Dec 2023 11:51:39 +0100 Subject: [PATCH 2/2] Error resolved in Configparser --- marl_factory_grid/utils/config_parser.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/marl_factory_grid/utils/config_parser.py b/marl_factory_grid/utils/config_parser.py index de51150..296117b 100644 --- a/marl_factory_grid/utils/config_parser.py +++ b/marl_factory_grid/utils/config_parser.py @@ -127,13 +127,25 @@ class FactoryConfigParser(object): # Actions conf_actions = self.agents[name]['Actions'] actions = list() + # Actions: + # Allowed + # - Noop + # - Move8 + # ---- + # Noop: + # South: + # reward_fail: 0.5 + # ---- + # Forbidden + # - South: + # reward_fail: 0.5 if isinstance(conf_actions, dict): conf_kwargs = conf_actions.copy() conf_actions = list(conf_actions.keys()) elif isinstance(conf_actions, list): conf_kwargs = {} - if isinstance(conf_actions, dict): + if any(isinstance(x, dict) for x in conf_actions): raise ValueError pass for action in conf_actions: @@ -150,9 +162,10 @@ class FactoryConfigParser(object): except AttributeError: class_or_classes = locate_and_import_class(action, self.custom_modules_path) try: + # Handle Lists of Actions (e.g., Move8, Move4, Default) parsed_actions.extend(class_or_classes) for actions_class in class_or_classes: - conf_kwargs[actions_class.__name__] = conf_kwargs[action] + conf_kwargs[actions_class.__name__] = conf_kwargs.get(action, {}) except TypeError: parsed_actions.append(class_or_classes)