mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2026-04-23 23:37:32 +02:00
Merge branch 'main' into unit_testing
# Conflicts: # marl_factory_grid/environment/factory.py # marl_factory_grid/utils/config_parser.py # marl_factory_grid/utils/states.py
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
import abc
|
||||
from collections import defaultdict
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .object import _Object
|
||||
from .object import Object
|
||||
from .. import constants as c
|
||||
from ...utils.results import ActionResult
|
||||
from ...utils.utility_classes import RenderEntity
|
||||
|
||||
|
||||
class Entity(_Object, abc.ABC):
|
||||
class Entity(Object, abc.ABC):
|
||||
"""Full Env Entity that lives on the environment Grid. Doors, Items, DirtPile etc..."""
|
||||
|
||||
@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 +59,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 +87,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
|
||||
@@ -92,6 +95,7 @@ class Entity(_Object, abc.ABC):
|
||||
|
||||
def __init__(self, pos, bind_to=None, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._view_directory = c.VALUE_NO_POS
|
||||
self._status = None
|
||||
self._pos = pos
|
||||
self._last_pos = pos
|
||||
@@ -109,9 +113,6 @@ 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})'
|
||||
|
||||
@property
|
||||
def obs_tag(self):
|
||||
try:
|
||||
@@ -128,25 +129,3 @@ class Entity(_Object, abc.ABC):
|
||||
self._collection.delete_env_object(self)
|
||||
self._collection = other_collection
|
||||
return self._collection == other_collection
|
||||
|
||||
@classmethod
|
||||
def from_coordinates(cls, positions: [(int, int)], *args, entity_kwargs=None, **kwargs, ):
|
||||
collection = cls(*args, **kwargs)
|
||||
collection.add_items(
|
||||
[cls._entity(tuple(pos), **entity_kwargs if entity_kwargs is not None else {}) for pos in positions])
|
||||
return collection
|
||||
|
||||
def notify_del_entity(self, entity):
|
||||
try:
|
||||
self.pos_dict[entity.pos].remove(entity)
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
|
||||
def by_pos(self, pos: (int, int)):
|
||||
pos = tuple(pos)
|
||||
try:
|
||||
return self.state.entities.pos_dict[pos]
|
||||
except StopIteration:
|
||||
pass
|
||||
except ValueError:
|
||||
print()
|
||||
|
||||
@@ -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
|
||||
@@ -5,7 +5,7 @@ from marl_factory_grid.environment import constants as c
|
||||
import marl_factory_grid.utils.helpers as h
|
||||
|
||||
|
||||
class _Object:
|
||||
class Object:
|
||||
"""Generell Objects for Organisation and Maintanance such as Actions etc..."""
|
||||
|
||||
_u_idx = defaultdict(lambda: 0)
|
||||
@@ -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
|
||||
@@ -71,8 +67,8 @@ class _Object:
|
||||
return hash(self.identifier)
|
||||
|
||||
def _identify_and_count_up(self):
|
||||
idx = _Object._u_idx[self.__class__.__name__]
|
||||
_Object._u_idx[self.__class__.__name__] += 1
|
||||
idx = Object._u_idx[self.__class__.__name__]
|
||||
Object._u_idx[self.__class__.__name__] += 1
|
||||
return idx
|
||||
|
||||
def set_collection(self, collection):
|
||||
@@ -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,84 +96,5 @@ 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
|
||||
|
||||
|
||||
# class EnvObject(_Object):
|
||||
# """Objects that hold Information that are observable, but have no position on the environment grid. Inventories etc..."""
|
||||
#
|
||||
# _u_idx = defaultdict(lambda: 0)
|
||||
#
|
||||
# @property
|
||||
# def obs_tag(self):
|
||||
# try:
|
||||
# return self._collection.name or self.name
|
||||
# except AttributeError:
|
||||
# return self.name
|
||||
#
|
||||
# @property
|
||||
# def var_is_blocking_light(self):
|
||||
# try:
|
||||
# return self._collection.var_is_blocking_light or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
# @property
|
||||
# def var_can_be_bound(self):
|
||||
# try:
|
||||
# return self._collection.var_can_be_bound or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
# @property
|
||||
# def var_can_move(self):
|
||||
# try:
|
||||
# return self._collection.var_can_move or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
# @property
|
||||
# def var_is_blocking_pos(self):
|
||||
# try:
|
||||
# return self._collection.var_is_blocking_pos or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
# @property
|
||||
# def var_has_position(self):
|
||||
# try:
|
||||
# return self._collection.var_has_position or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
# @property
|
||||
# def var_can_collide(self):
|
||||
# try:
|
||||
# return self._collection.var_can_collide or False
|
||||
# except AttributeError:
|
||||
# return False
|
||||
#
|
||||
#
|
||||
# @property
|
||||
# def encoding(self):
|
||||
# return c.VALUE_OCCUPIED_CELL
|
||||
#
|
||||
#
|
||||
# def __init__(self, **kwargs):
|
||||
# self._bound_entity = None
|
||||
# super(EnvObject, self).__init__(**kwargs)
|
||||
#
|
||||
#
|
||||
# def change_parent_collection(self, other_collection):
|
||||
# other_collection.add_item(self)
|
||||
# self._collection.delete_env_object(self)
|
||||
# self._collection = other_collection
|
||||
# return self._collection == other_collection
|
||||
#
|
||||
#
|
||||
# def summarize_state(self):
|
||||
# return dict(name=str(self.name))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
|
||||
from marl_factory_grid.environment.entity.object import _Object
|
||||
from marl_factory_grid.environment.entity.object import Object
|
||||
|
||||
|
||||
##########################################################################
|
||||
@@ -8,7 +8,7 @@ from marl_factory_grid.environment.entity.object import _Object
|
||||
##########################################################################
|
||||
|
||||
|
||||
class PlaceHolder(_Object):
|
||||
class PlaceHolder(Object):
|
||||
|
||||
def __init__(self, *args, fill_value=0, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -24,10 +24,10 @@ class PlaceHolder(_Object):
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return "PlaceHolder"
|
||||
return self.__class__.__name__
|
||||
|
||||
|
||||
class GlobalPosition(_Object):
|
||||
class GlobalPosition(Object):
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -56,15 +56,18 @@ class Factory(gym.Env):
|
||||
self.level_filepath = Path(custom_level_path)
|
||||
else:
|
||||
self.level_filepath = Path(__file__).parent.parent / h.LEVELS_DIR / f'{self.conf.level_name}.txt'
|
||||
self._renderer = None # expensive - don't use; unless required !
|
||||
|
||||
parsed_entities = self.conf.load_entities()
|
||||
self.map = LevelParser(self.level_filepath, parsed_entities, self.conf.pomdp_r)
|
||||
|
||||
# Init for later usage:
|
||||
self.state: Gamestate
|
||||
self.map: LevelParser
|
||||
self.obs_builder: OBSBuilder
|
||||
# noinspection PyTypeChecker
|
||||
self.state: Gamestate = None
|
||||
# noinspection PyTypeChecker
|
||||
self.obs_builder: OBSBuilder = None
|
||||
|
||||
# expensive - don't use; unless required !
|
||||
self._renderer = None
|
||||
|
||||
# reset env to initial state, preparing env for new episode.
|
||||
# returns tuple where the first dict contains initial observation for each agent in the env
|
||||
@@ -74,7 +77,7 @@ class Factory(gym.Env):
|
||||
return self.state.entities[item]
|
||||
|
||||
def reset(self) -> (dict, dict):
|
||||
if hasattr(self, 'state'):
|
||||
if self.state is not None:
|
||||
for entity_group in self.state.entities:
|
||||
try:
|
||||
entity_group[0].reset_uid()
|
||||
@@ -87,12 +90,16 @@ class Factory(gym.Env):
|
||||
entities = self.map.do_init()
|
||||
|
||||
# Init rules
|
||||
rules = self.conf.load_env_rules()
|
||||
env_rules = self.conf.load_env_rules()
|
||||
entity_rules = self.conf.load_entity_spawn_rules(entities)
|
||||
env_rules.extend(entity_rules)
|
||||
|
||||
env_tests = self.conf.load_env_tests() if self.conf.tests else []
|
||||
|
||||
# Parse the agent conf
|
||||
parsed_agents_conf = self.conf.parse_agents_conf()
|
||||
self.state = Gamestate(entities, parsed_agents_conf, rules, env_tests, self.conf.env_seed, self.conf.verbose)
|
||||
self.state = Gamestate(entities, parsed_agents_conf, env_rules, env_tests, 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)
|
||||
@@ -160,7 +167,7 @@ class Factory(gym.Env):
|
||||
# Finalize
|
||||
reward, reward_info, done = self.summarize_step_results(tick_result, done_results)
|
||||
|
||||
info = reward_info
|
||||
info = dict(reward_info)
|
||||
|
||||
info.update(step_reward=sum(reward), step=self.state.curr_step)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
from marl_factory_grid.environment.entity.object import _Object
|
||||
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?
|
||||
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,33 +30,65 @@ 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 self.var_has_position and 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]):
|
||||
items = [items] if isinstance(items, _Object) else items
|
||||
def despawn(self, items: List[Object]):
|
||||
items = [items] if isinstance(items, Object) else items
|
||||
for item in items:
|
||||
del self[item]
|
||||
|
||||
@@ -115,7 +154,7 @@ class Collection(_Objects):
|
||||
except StopIteration:
|
||||
pass
|
||||
except ValueError:
|
||||
print()
|
||||
pass
|
||||
|
||||
@property
|
||||
def positions(self):
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
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
|
||||
from marl_factory_grid.environment.groups.objects import Objects
|
||||
from marl_factory_grid.utils.helpers import POS_MASK
|
||||
|
||||
|
||||
class Entities(_Objects):
|
||||
_entity = _Objects
|
||||
class Entities(Objects):
|
||||
_entity = 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))
|
||||
@@ -74,7 +87,7 @@ class Entities(_Objects):
|
||||
def __delitem__(self, name):
|
||||
assert_str = 'This group of entity does not exist in this collection!'
|
||||
assert any([key for key in name.keys() if key in self.keys()]), assert_str
|
||||
self[name]._observers.delete(self)
|
||||
self[name].del_observer(self)
|
||||
for entity in self[name]:
|
||||
entity.del_observer(self)
|
||||
return super(Entities, self).__delitem__(name)
|
||||
@@ -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
|
||||
|
||||
@@ -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})'
|
||||
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
from collections import defaultdict
|
||||
from typing import List
|
||||
from typing import List, Iterator, Union
|
||||
|
||||
import numpy as np
|
||||
|
||||
from marl_factory_grid.environment.entity.object import _Object
|
||||
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
|
||||
class Objects:
|
||||
_entity = Object
|
||||
|
||||
@property
|
||||
def var_can_be_bound(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def observers(self):
|
||||
@@ -45,7 +50,7 @@ class _Objects:
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
def __iter__(self):
|
||||
def __iter__(self) -> Iterator[Union[Object, None]]:
|
||||
return iter(self.values())
|
||||
|
||||
def add_item(self, item: _entity):
|
||||
@@ -125,13 +130,14 @@ class _Objects:
|
||||
repr_dict = {key: val for key, val in self._data.items() if key not in [c.WALLS]}
|
||||
return f'{self.__class__.__name__}[{repr_dict}]'
|
||||
|
||||
def notify_del_entity(self, entity: _Object):
|
||||
def notify_del_entity(self, entity: Object):
|
||||
try:
|
||||
# noinspection PyUnresolvedReferences
|
||||
self.pos_dict[entity.pos].remove(entity)
|
||||
except (AttributeError, ValueError, IndexError):
|
||||
pass
|
||||
|
||||
def notify_add_entity(self, entity: _Object):
|
||||
def notify_add_entity(self, entity: Object):
|
||||
try:
|
||||
if self not in entity.observers:
|
||||
entity.add_observer(self)
|
||||
@@ -148,12 +154,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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -2,3 +2,4 @@ MOVEMENTS_VALID: float = -0.001
|
||||
MOVEMENTS_FAIL: float = -0.05
|
||||
NOOP: float = -0.01
|
||||
COLLISION: float = -0.5
|
||||
COLLISION_DONE: float = -1
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import abc
|
||||
from random import shuffle
|
||||
from typing import List
|
||||
from typing import List, Collection
|
||||
|
||||
from marl_factory_grid.environment import rewards as r, constants as c
|
||||
from marl_factory_grid.environment.entity.agent import Agent
|
||||
from marl_factory_grid.utils import helpers as h
|
||||
from marl_factory_grid.utils.results import TickResult, DoneResult
|
||||
from marl_factory_grid.environment import rewards as r, constants as c
|
||||
|
||||
|
||||
class Rule(abc.ABC):
|
||||
@@ -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):
|
||||
@@ -95,16 +118,17 @@ class AssignGlobalPositions(Rule):
|
||||
def on_init(self, state, lvl_map):
|
||||
from marl_factory_grid.environment.entity.util import GlobalPosition
|
||||
for agent in state[c.AGENT]:
|
||||
gp = GlobalPosition(lvl_map.level_shape)
|
||||
gp.bind_to(agent)
|
||||
gp = GlobalPosition(agent, lvl_map.level_shape)
|
||||
state[c.GLOBALPOSITIONS].add_item(gp)
|
||||
return []
|
||||
|
||||
|
||||
class Collision(Rule):
|
||||
class WatchCollisions(Rule):
|
||||
|
||||
def __init__(self, done_at_collisions: bool = False):
|
||||
def __init__(self, reward=r.COLLISION, done_at_collisions: bool = False, reward_at_done=r.COLLISION_DONE):
|
||||
super().__init__()
|
||||
self.reward_at_done = reward_at_done
|
||||
self.reward = reward
|
||||
self.done_at_collisions = done_at_collisions
|
||||
self.curr_done = False
|
||||
|
||||
@@ -117,12 +141,12 @@ class Collision(Rule):
|
||||
if len(guests) >= 2:
|
||||
for i, guest in enumerate(guests):
|
||||
try:
|
||||
guest.set_state(TickResult(identifier=c.COLLISION, reward=r.COLLISION,
|
||||
guest.set_state(TickResult(identifier=c.COLLISION, reward=self.reward,
|
||||
validity=c.NOT_VALID, entity=self))
|
||||
except AttributeError:
|
||||
pass
|
||||
results.append(TickResult(entity=guest, identifier=c.COLLISION,
|
||||
reward=r.COLLISION, validity=c.VALID))
|
||||
reward=self.reward, validity=c.VALID))
|
||||
self.curr_done = True if self.done_at_collisions else False
|
||||
return results
|
||||
|
||||
@@ -131,5 +155,5 @@ class Collision(Rule):
|
||||
inter_entity_collision_detected = self.curr_done
|
||||
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.VALID, identifier=c.COLLISION, reward=self.reward_at_done)]
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user