new rules, new spawn logic, small fixes, default and narrow corridor debugged

This commit is contained in:
Steffen Illium
2023-11-09 17:50:20 +01:00
parent 9b9c6e0385
commit 06a5130b25
67 changed files with 768 additions and 921 deletions

View File

@@ -48,9 +48,9 @@ class Move(Action, abc.ABC):
reward = r.MOVEMENTS_VALID if move_validity else r.MOVEMENTS_FAIL
return ActionResult(entity=entity, identifier=self._identifier, validity=move_validity, reward=reward)
else: # There is no place to go, propably collision
# This is currently handeld by the Collision rule, so that it can be switched on and off by conf.yml
# This is currently handeld by the WatchCollisions rule, so that it can be switched on and off by conf.yml
# return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=r.COLLISION)
return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID, reward=0)
return ActionResult(entity=entity, identifier=self._identifier, validity=c.NOT_VALID)
def _calc_new_pos(self, pos):
x_diff, y_diff = MOVEMAP[self._identifier]

View File

@@ -10,6 +10,7 @@ AGENT = 'Agent' # Identifier of Agent-objects an
OTHERS = 'Other'
COMBINED = 'Combined'
GLOBALPOSITIONS = 'GlobalPositions' # Identifier of the global position slice
SPAWN_ENTITY_RULE = 'SpawnEntity'
# Attributes
IS_BLOCKING_LIGHT = 'var_is_blocking_light'
@@ -29,7 +30,7 @@ VALUE_NO_POS = (-9999, -9999) # Invalid Position value used in the e
ACTION = 'action' # Identifier of Action-objects and groups (groups).
COLLISION = 'Collision' # Identifier to use in the context of collitions.
COLLISION = 'Collisions' # Identifier to use in the context of collitions.
# LAST_POS = 'LAST_POS' # Identifiert for retrieving an enitites last pos.
VALIDITY = 'VALIDITY' # Identifiert for retrieving the Validity of Action, Tick, etc. ...
@@ -54,3 +55,5 @@ NOOP = 'Noop'
# Result Identifier
MOVEMENTS_VALID = 'motion_valid'
MOVEMENTS_FAIL = 'motion_not_valid'
DEFAULT_PATH = 'environment'
MODULE_PATH = 'modules'

View File

@@ -12,14 +12,6 @@ from marl_factory_grid.environment import constants as c
class Agent(Entity):
@property
def var_is_blocking_light(self):
return False
@property
def var_can_move(self):
return True
@property
def var_is_paralyzed(self):
return len(self._paralyzed)
@@ -28,14 +20,6 @@ class Agent(Entity):
def paralyze_reasons(self):
return [x for x in self._paralyzed]
@property
def var_is_blocking_pos(self):
return False
@property
def var_has_position(self):
return True
@property
def obs_tag(self):
return self.name
@@ -48,10 +32,6 @@ class Agent(Entity):
def observations(self):
return self._observations
@property
def var_can_collide(self):
return True
def step_result(self):
pass
@@ -60,16 +40,21 @@ class Agent(Entity):
return self._collection
@property
def state(self):
return self._state or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID, reward=0)
def var_is_blocking_pos(self):
return self._is_blocking_pos
def __init__(self, actions: List[Action], observations: List[str], *args, **kwargs):
@property
def state(self):
return self._state or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID)
def __init__(self, actions: List[Action], observations: List[str], *args, is_blocking_pos=False, **kwargs):
super(Agent, self).__init__(*args, **kwargs)
self._paralyzed = set()
self.step_result = dict()
self._actions = actions
self._observations = observations
self._state: Union[Result, None] = None
self._is_blocking_pos = is_blocking_pos
# noinspection PyAttributeOutsideInit
def clear_temp_state(self):

View File

@@ -14,7 +14,7 @@ class Entity(_Object, abc.ABC):
@property
def state(self):
return self._status or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID, reward=0)
return self._status or ActionResult(entity=self, identifier=c.NOOP, validity=c.VALID)
@property
def var_has_position(self):
@@ -60,6 +60,10 @@ class Entity(_Object, abc.ABC):
def pos(self):
return self._pos
def set_pos(self, pos):
assert isinstance(pos, tuple) and len(pos) == 2
self._pos = pos
@property
def last_pos(self):
try:
@@ -84,7 +88,7 @@ class Entity(_Object, abc.ABC):
for observer in self.observers:
observer.notify_del_entity(self)
self._view_directory = curr_pos[0] - next_pos[0], curr_pos[1] - next_pos[1]
self._pos = next_pos
self.set_pos(next_pos)
for observer in self.observers:
observer.notify_add_entity(self)
return valid
@@ -93,7 +97,7 @@ class Entity(_Object, abc.ABC):
def __init__(self, pos, bind_to=None, **kwargs):
super().__init__(**kwargs)
self._status = None
self._pos = pos
self.set_pos(pos)
self._last_pos = pos
if bind_to:
try:
@@ -109,8 +113,9 @@ class Entity(_Object, abc.ABC):
def render(self):
return RenderEntity(self.__class__.__name__.lower(), self.pos)
def __repr__(self):
return super(Entity, self).__repr__() + f'(@{self.pos})'
@abc.abstractmethod
def render(self):
return RenderEntity(self.__class__.__name__.lower(), self.pos)
@property
def obs_tag(self):
@@ -149,4 +154,4 @@ class Entity(_Object, abc.ABC):
except StopIteration:
pass
except ValueError:
print()
pass

View File

@@ -1,24 +0,0 @@
# noinspection PyAttributeOutsideInit
class BoundEntityMixin:
@property
def bound_entity(self):
return self._bound_entity
@property
def name(self):
if self.bound_entity:
return f'{self.__class__.__name__}({self.bound_entity.name})'
else:
pass
def belongs_to_entity(self, entity):
return entity == self.bound_entity
def bind_to(self, entity):
self._bound_entity = entity
def unbind(self):
self._bound_entity = None

View File

@@ -13,10 +13,6 @@ class _Object:
def __bool__(self):
return True
@property
def var_has_position(self):
return False
@property
def var_can_be_bound(self):
try:
@@ -30,22 +26,14 @@ class _Object:
@property
def name(self):
if self._str_ident is not None:
name = f'{self.__class__.__name__}[{self._str_ident}]'
else:
name = f'{self.__class__.__name__}#{self.u_int}'
if self.bound_entity:
name = h.add_bound_name(name, self.bound_entity)
if self.var_has_position:
name = h.add_pos_name(name, self)
return name
return f'{self.__class__.__name__}[{self.identifier}]'
@property
def identifier(self):
if self._str_ident is not None:
return self._str_ident
else:
return self.name
return self.u_int
def reset_uid(self):
self._u_idx = defaultdict(lambda: 0)
@@ -62,7 +50,15 @@ class _Object:
print(f'Following kwargs were passed, but ignored: {kwargs}')
def __repr__(self):
return f'{self.name}'
name = self.name
if self.bound_entity:
name = h.add_bound_name(name, self.bound_entity)
try:
if self.var_has_position:
name = h.add_pos_name(name, self)
except (AttributeError):
pass
return name
def __eq__(self, other) -> bool:
return other == self.identifier
@@ -88,7 +84,7 @@ class _Object:
def summarize_state(self):
return dict()
def bind(self, entity):
def bind_to(self, entity):
# noinspection PyAttributeOutsideInit
self._bound_entity = entity
return c.VALID
@@ -100,9 +96,6 @@ class _Object:
def bound_entity(self):
return self._bound_entity
def bind_to(self, entity):
self._bound_entity = entity
def unbind(self):
self._bound_entity = None

View File

@@ -24,7 +24,7 @@ class PlaceHolder(_Object):
@property
def name(self):
return "PlaceHolder"
return self.__class__.__name__
class GlobalPosition(_Object):
@@ -36,7 +36,8 @@ class GlobalPosition(_Object):
else:
return self.bound_entity.pos
def __init__(self, level_shape, *args, normalized: bool = True, **kwargs):
def __init__(self, agent, level_shape, *args, normalized: bool = True, **kwargs):
super(GlobalPosition, self).__init__(*args, **kwargs)
self.bind_to(agent)
self._normalized = normalized
self._shape = level_shape

View File

@@ -5,13 +5,8 @@ from marl_factory_grid.utils.utility_classes import RenderEntity
class Wall(Entity):
@property
def var_has_position(self):
return True
@property
def var_can_collide(self):
return True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def encoding(self):
@@ -19,11 +14,3 @@ class Wall(Entity):
def render(self):
return RenderEntity(c.WALL, self.pos)
@property
def var_is_blocking_pos(self):
return True
@property
def var_is_blocking_light(self):
return True

View File

@@ -87,11 +87,14 @@ class Factory(gym.Env):
entities = self.map.do_init()
# Init rules
rules = self.conf.load_rules()
env_rules = self.conf.load_env_rules()
entity_rules = self.conf.load_entity_spawn_rules(entities)
env_rules.extend(entity_rules)
# Parse the agent conf
parsed_agents_conf = self.conf.parse_agents_conf()
self.state = Gamestate(entities, parsed_agents_conf, rules, self.conf.env_seed, self.conf.verbose)
self.state = Gamestate(entities, parsed_agents_conf, env_rules, self.map.level_shape,
self.conf.env_seed, self.conf.verbose)
# All is set up, trigger entity init with variable pos
# All is set up, trigger additional init (after agent entity spawn etc)

View File

@@ -1,10 +1,15 @@
from marl_factory_grid.environment.entity.agent import Agent
from marl_factory_grid.environment.groups.collection import Collection
from marl_factory_grid.environment.rules import SpawnAgents
class Agents(Collection):
_entity = Agent
@property
def spawn_rule(self):
return {SpawnAgents.__name__: {}}
@property
def var_is_blocking_light(self):
return False

View File

@@ -1,18 +1,25 @@
from typing import List, Tuple, Union
from typing import List, Tuple, Union, Dict
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.groups.objects import _Objects
# noinspection PyProtectedMember
from marl_factory_grid.environment.entity.object import _Object
import marl_factory_grid.environment.constants as c
from marl_factory_grid.utils.results import Result
class Collection(_Objects):
_entity = _Object # entity?
symbol = None
@property
def var_is_blocking_light(self):
return False
@property
def var_is_blocking_pos(self):
return False
@property
def var_can_collide(self):
return False
@@ -23,29 +30,61 @@ class Collection(_Objects):
@property
def var_has_position(self):
return False
# @property
# def var_has_bound(self):
# return False # batteries, globalpos, inventories true
@property
def var_can_be_bound(self):
return False
return True
@property
def encodings(self):
return [x.encoding for x in self]
def __init__(self, size, *args, **kwargs):
super(Collection, self).__init__(*args, **kwargs)
self.size = size
def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args): # woihn mit den args
if isinstance(coords_or_quantity, int):
self.add_items([self._entity() for _ in range(coords_or_quantity)])
@property
def spawn_rule(self):
"""Prevent SpawnRule creation if Objects are spawned by map, Doors e.g."""
if self.symbol:
return None
elif self._spawnrule:
return self._spawnrule
else:
self.add_items([self._entity(pos) for pos in coords_or_quantity])
return {c.SPAWN_ENTITY_RULE: dict(collection=self, coords_or_quantity=self._coords_or_quantity)}
def __init__(self, size, *args, coords_or_quantity: int = None, ignore_blocking=False,
spawnrule: Union[None, Dict[str, dict]] = None,
**kwargs):
super(Collection, self).__init__(*args, **kwargs)
self._coords_or_quantity = coords_or_quantity
self.size = size
self._spawnrule = spawnrule
self._ignore_blocking = ignore_blocking
def trigger_spawn(self, state, *entity_args, coords_or_quantity=None, ignore_blocking=False, **entity_kwargs):
coords_or_quantity = coords_or_quantity if coords_or_quantity else self._coords_or_quantity
if self.var_has_position:
if isinstance(coords_or_quantity, int):
if ignore_blocking or self._ignore_blocking:
coords_or_quantity = state.entities.floorlist[:coords_or_quantity]
else:
coords_or_quantity = state.get_n_random_free_positions(coords_or_quantity)
self.spawn(coords_or_quantity, *entity_args, **entity_kwargs)
state.print(f'{len(coords_or_quantity)} new {self.name} have been spawned at {coords_or_quantity}')
return Result(identifier=f'{self.name}_spawn', validity=c.VALID, value=len(coords_or_quantity))
else:
if isinstance(coords_or_quantity, int):
self.spawn(coords_or_quantity, *entity_args, **entity_kwargs)
state.print(f'{coords_or_quantity} new {self.name} have been spawned randomly.')
return Result(identifier=f'{self.name}_spawn', validity=c.VALID, value=coords_or_quantity)
else:
raise ValueError(f'{self._entity.__name__} has no position!')
def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args, **entity_kwargs):
if self.var_has_position:
if isinstance(coords_or_quantity, int):
raise ValueError(f'{self._entity.__name__} should have a position!')
else:
self.add_items([self._entity(pos, *entity_args, **entity_kwargs) for pos in coords_or_quantity])
else:
if isinstance(coords_or_quantity, int):
self.add_items([self._entity(*entity_args, **entity_kwargs) for _ in range(coords_or_quantity)])
else:
raise ValueError(f'{self._entity.__name__} has no position!')
return c.VALID
def despawn(self, items: List[_Object]):
@@ -115,7 +154,7 @@ class Collection(_Objects):
except StopIteration:
pass
except ValueError:
print()
pass
@property
def positions(self):

View File

@@ -1,6 +1,6 @@
from collections import defaultdict
from operator import itemgetter
from random import shuffle, random
from random import shuffle
from typing import Dict
from marl_factory_grid.environment.groups.objects import _Objects
@@ -12,10 +12,10 @@ class Entities(_Objects):
@staticmethod
def neighboring_positions(pos):
return (POS_MASK + pos).reshape(-1, 2)
return [tuple(x) for x in (POS_MASK + pos).reshape(-1, 2)]
def get_entities_near_pos(self, pos):
return [y for x in itemgetter(*(tuple(x) for x in self.neighboring_positions(pos)))(self.pos_dict) for y in x]
return [y for x in itemgetter(*self.neighboring_positions(pos))(self.pos_dict) for y in x]
def render(self):
return [y for x in self for y in x.render() if x is not None]
@@ -35,8 +35,9 @@ class Entities(_Objects):
super().__init__()
def guests_that_can_collide(self, pos):
return[x for val in self.pos_dict[pos] for x in val if x.var_can_collide]
return [x for val in self.pos_dict[pos] for x in val if x.var_can_collide]
@property
def empty_positions(self):
empty_positions = [key for key in self.floorlist if not self.pos_dict[key]]
shuffle(empty_positions)
@@ -48,11 +49,23 @@ class Entities(_Objects):
shuffle(empty_positions)
return empty_positions
def is_blocked(self):
return[key for key, val in self.pos_dict.items() if any([x.var_is_blocking_pos for x in val])]
@property
def blocked_positions(self):
blocked_positions = [key for key, val in self.pos_dict.items() if any([x.var_is_blocking_pos for x in val])]
shuffle(blocked_positions)
return blocked_positions
def is_not_blocked(self):
return[key for key, val in self.pos_dict.items() if not all([x.var_is_blocking_pos for x in val])]
@property
def free_positions_generator(self):
generator = (
key for key in self.floorlist if all(not x.var_can_collide and not x.var_is_blocking_pos
for x in self.pos_dict[key])
)
return generator
@property
def free_positions_list(self):
return [x for x in self.free_positions_generator]
def iter_entities(self):
return iter((x for sublist in self.values() for x in sublist))
@@ -92,3 +105,6 @@ class Entities(_Objects):
@property
def positions(self):
return [k for k, v in self.pos_dict.items() for _ in v]
def is_occupied(self, pos):
return len([x for x in self.pos_dict[pos] if x.var_can_collide or x.var_is_blocking_pos]) >= 1

View File

@@ -4,10 +4,6 @@ from marl_factory_grid.environment import constants as c
# noinspection PyUnresolvedReferences,PyTypeChecker
class IsBoundMixin:
@property
def name(self):
return f'{self.__class__.__name__}({self._bound_entity.name})'
def __repr__(self):
return f'{self.__class__.__name__}#{self._bound_entity.name}({self._data})'

View File

@@ -5,11 +5,16 @@ import numpy as np
from marl_factory_grid.environment.entity.object import _Object
import marl_factory_grid.environment.constants as c
from marl_factory_grid.utils import helpers as h
class _Objects:
_entity = _Object
@property
def var_can_be_bound(self):
return False
@property
def observers(self):
return self._observers
@@ -148,12 +153,12 @@ class _Objects:
def by_entity(self, entity):
try:
return next((x for x in self if x.belongs_to_entity(entity)))
return h.get_first(self, filter_by=lambda x: x.belongs_to_entity(entity))
except (StopIteration, AttributeError):
return None
def idx_by_entity(self, entity):
try:
return next((idx for idx, x in enumerate(self) if x.belongs_to_entity(entity)))
return h.get_first_index(self, filter_by=lambda x: x.belongs_to_entity(entity))
except (StopIteration, AttributeError):
return None

View File

@@ -1,7 +1,10 @@
from typing import List, Union
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.util import GlobalPosition
from marl_factory_grid.environment.groups.collection import Collection
from marl_factory_grid.utils.results import Result
from marl_factory_grid.utils.states import Gamestate
class Combined(Collection):
@@ -36,17 +39,17 @@ class GlobalPositions(Collection):
_entity = GlobalPosition
@property
def var_is_blocking_light(self):
return False
@property
def var_can_collide(self):
return False
@property
def var_can_be_bound(self):
return True
var_is_blocking_light = False
var_can_be_bound = True
var_can_collide = False
var_has_position = False
def __init__(self, *args, **kwargs):
super(GlobalPositions, self).__init__(*args, **kwargs)
def spawn(self, agents, level_shape, *args, **kwargs):
self.add_items([self._entity(agent, level_shape, *args, **kwargs) for agent in agents])
return [Result(identifier=f'{self.name}_spawn', validity=c.VALID, value=len(self))]
def trigger_spawn(self, state: Gamestate, *args, **kwargs) -> [Result]:
return self.spawn(state[c.AGENT], state.lvl_shape, *args, **kwargs)

View File

@@ -7,9 +7,12 @@ class Walls(Collection):
_entity = Wall
symbol = c.SYMBOL_WALL
@property
def var_has_position(self):
return True
var_can_collide = True
var_is_blocking_light = True
var_can_move = False
var_has_position = True
var_can_be_bound = False
var_is_blocking_pos = True
def __init__(self, *args, **kwargs):
super(Walls, self).__init__(*args, **kwargs)

View File

@@ -1,6 +1,6 @@
import abc
from random import shuffle
from typing import List
from typing import List, Collection, Union
from marl_factory_grid.environment.entity.agent import Agent
from marl_factory_grid.utils import helpers as h
@@ -39,6 +39,29 @@ class Rule(abc.ABC):
return []
class SpawnEntity(Rule):
@property
def _collection(self) -> Collection:
return Collection()
@property
def name(self):
return f'{self.__class__.__name__}({self.collection.name})'
def __init__(self, collection, coords_or_quantity, ignore_blocking=False):
super().__init__()
self.coords_or_quantity = coords_or_quantity
self.collection = collection
self.ignore_blocking = ignore_blocking
def on_init(self, state, lvl_map) -> [TickResult]:
results = self.collection.trigger_spawn(state, ignore_blocking=self.ignore_blocking)
pos_str = f' on: {[x.pos for x in self.collection]}' if self.collection.var_has_position else ''
state.print(f'Initial {self.collection.__class__.__name__} were spawned{pos_str}')
return results
class SpawnAgents(Rule):
def __init__(self):
@@ -46,14 +69,14 @@ class SpawnAgents(Rule):
pass
def on_init(self, state, lvl_map):
agent_conf = state.agents_conf
# agents = Agents(lvl_map.size)
agents = state[c.AGENT]
empty_positions = state.entities.empty_positions()[:len(agent_conf)]
for agent_name in agent_conf:
actions = agent_conf[agent_name]['actions'].copy()
observations = agent_conf[agent_name]['observations'].copy()
positions = agent_conf[agent_name]['positions'].copy()
empty_positions = state.entities.empty_positions[:len(state.agents_conf)]
for agent_name, agent_conf in state.agents_conf.items():
actions = agent_conf['actions'].copy()
observations = agent_conf['observations'].copy()
positions = agent_conf['positions'].copy()
other = agent_conf['other'].copy()
if positions:
shuffle(positions)
while True:
@@ -61,18 +84,18 @@ class SpawnAgents(Rule):
pos = positions.pop()
except IndexError:
raise ValueError(f'It was not possible to spawn an Agent on the available position: '
f'\n{agent_name[agent_name]["positions"].copy()}')
if agents.by_pos(pos) and state.check_pos_validity(pos):
f'\n{agent_conf["positions"].copy()}')
if bool(agents.by_pos(pos)) or not state.check_pos_validity(pos):
continue
else:
agents.add_item(Agent(actions, observations, pos, str_ident=agent_name))
agents.add_item(Agent(actions, observations, pos, str_ident=agent_name, **other))
break
else:
agents.add_item(Agent(actions, observations, empty_positions.pop(), str_ident=agent_name))
agents.add_item(Agent(actions, observations, empty_positions.pop(), str_ident=agent_name, **other))
pass
class MaxStepsReached(Rule):
class DoneAtMaxStepsReached(Rule):
def __init__(self, max_steps: int = 500):
super().__init__()
@@ -83,8 +106,8 @@ class MaxStepsReached(Rule):
def on_check_done(self, state):
if self.max_steps <= state.curr_step:
return [DoneResult(validity=c.VALID, identifier=self.name, reward=0)]
return [DoneResult(validity=c.NOT_VALID, identifier=self.name, reward=0)]
return [DoneResult(validity=c.VALID, identifier=self.name)]
return [DoneResult(validity=c.NOT_VALID, identifier=self.name)]
class AssignGlobalPositions(Rule):
@@ -101,7 +124,7 @@ class AssignGlobalPositions(Rule):
return []
class Collision(Rule):
class WatchCollisions(Rule):
def __init__(self, done_at_collisions: bool = False):
super().__init__()
@@ -132,4 +155,4 @@ class Collision(Rule):
move_failed = any(h.is_move(x.state.identifier) and not x.state.validity for x in state[c.AGENT])
if inter_entity_collision_detected or move_failed:
return [DoneResult(validity=c.VALID, identifier=c.COLLISION, reward=r.COLLISION)]
return [DoneResult(validity=c.NOT_VALID, identifier=self.name, reward=0)]
return [DoneResult(validity=c.NOT_VALID, identifier=self.name)]