Merge branch 'main' into 'documentation'

# Conflicts:
#   README.md
This commit is contained in:
Friedrich, Joel 2024-02-14 14:04:52 +01:00
commit 65a93b256c
10 changed files with 15 additions and 133 deletions

View File

@ -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:

View File

@ -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:

View File

@ -1,3 +0,0 @@
from .entitites import Zone
from .groups import Zones
from .rules import AgentSingleZonePlacement

View File

@ -1,4 +0,0 @@
# Names / Identifiers
ZONES = 'Zones' # Identifier of Zone-objects and groups (groups).
ZONE = 'Zone' # -||-

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -132,13 +132,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:
@ -155,6 +167,7 @@ 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.get(action, {})

View File

@ -100,7 +100,6 @@ class EnvRecorder(Wrapper):
n_dests=0,
dwell_time=0,
spawn_frequency=0,
spawn_in_other_zone=False,
spawn_mode=''
)
rewards_dest = dict(

View File

@ -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']