mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-24 04:11:36 +02:00
removed positionmix entirely (now also in collection)
This commit is contained in:
@ -1,16 +1,14 @@
|
|||||||
from typing import List
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from marl_factory_grid.environment import constants as c
|
from marl_factory_grid.environment import constants as c
|
||||||
from marl_factory_grid.environment.entity.entity import Entity
|
from marl_factory_grid.environment.entity.entity import Entity
|
||||||
from marl_factory_grid.environment.entity.object import EnvObject
|
|
||||||
from marl_factory_grid.utils.utility_classes import RenderEntity
|
from marl_factory_grid.utils.utility_classes import RenderEntity
|
||||||
from marl_factory_grid.utils import helpers as h
|
|
||||||
|
|
||||||
|
|
||||||
class Wall(Entity):
|
class Wall(Entity):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def var_can_collide(self):
|
def var_can_collide(self):
|
||||||
return True
|
return True
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
from marl_factory_grid.environment.entity.agent import Agent
|
from marl_factory_grid.environment.entity.agent import Agent
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Agents(PositionMixin, Collection):
|
class Agents(Collection):
|
||||||
_entity = Agent
|
_entity = Agent
|
||||||
is_blocking_light = False
|
|
||||||
can_move = True
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
from marl_factory_grid.environment.entity.entity import Entity
|
||||||
from marl_factory_grid.environment.groups.objects import _Objects
|
from marl_factory_grid.environment.groups.objects import _Objects
|
||||||
from marl_factory_grid.environment.entity.object import EnvObject
|
from marl_factory_grid.environment.entity.object import EnvObject
|
||||||
|
|
||||||
|
|
||||||
class Collection(_Objects):
|
class Collection(_Objects):
|
||||||
_entity = EnvObject
|
_entity = EnvObject # entity? object? objects?
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def var_is_blocking_light(self):
|
def var_is_blocking_light(self):
|
||||||
@ -20,11 +23,12 @@ class Collection(_Objects):
|
|||||||
@property
|
@property
|
||||||
def var_has_position(self):
|
def var_has_position(self):
|
||||||
return False # alles was posmixin hat true
|
return False # alles was posmixin hat true
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def var_has_bound(self):
|
def var_has_bound(self):
|
||||||
return False # batteries, globalpos, inventories true
|
return False # batteries, globalpos, inventories true
|
||||||
|
|
||||||
@property # beide bounds hier? inventory can be bound
|
@property # beide bounds hier? inventory can be bound
|
||||||
def var_can_be_bound(self):
|
def var_can_be_bound(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -62,3 +66,44 @@ class Collection(_Objects):
|
|||||||
return next((idx for idx, x in enumerate(self) if x.belongs_to_entity(entity)))
|
return next((idx for idx, x in enumerate(self) if x.belongs_to_entity(entity)))
|
||||||
except (StopIteration, AttributeError):
|
except (StopIteration, AttributeError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def spawn(self, coords: List[Tuple[(int, int)]]):
|
||||||
|
self.add_items([self._entity(pos) for pos in coords])
|
||||||
|
|
||||||
|
def render(self):
|
||||||
|
return [y for y in [x.render() for x in self] if y is not None]
|
||||||
|
|
||||||
|
@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 __delitem__(self, name):
|
||||||
|
idx, obj = next((i, obj) for i, obj in enumerate(self) if obj.name == name)
|
||||||
|
try:
|
||||||
|
for observer in obj.observers:
|
||||||
|
observer.notify_del_entity(obj)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
super().__delitem__(name)
|
||||||
|
|
||||||
|
def by_pos(self, pos: (int, int)):
|
||||||
|
pos = tuple(pos)
|
||||||
|
try:
|
||||||
|
return self.pos_dict[pos]
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
print()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def positions(self):
|
||||||
|
return [e.pos for e in self]
|
||||||
|
|
||||||
|
def notify_del_entity(self, entity: Entity):
|
||||||
|
try:
|
||||||
|
self.pos_dict[entity.pos].remove(entity)
|
||||||
|
except (ValueError, AttributeError):
|
||||||
|
pass
|
||||||
|
@ -1,55 +1,4 @@
|
|||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
from marl_factory_grid.environment import constants as c
|
from marl_factory_grid.environment import constants as c
|
||||||
from marl_factory_grid.environment.entity.entity import Entity
|
|
||||||
|
|
||||||
|
|
||||||
class PositionMixin:
|
|
||||||
_entity = Entity
|
|
||||||
var_is_blocking_light: bool = True
|
|
||||||
var_can_collide: bool = True
|
|
||||||
var_has_position: bool = True
|
|
||||||
|
|
||||||
def spawn(self, coords: List[Tuple[(int, int)]]):
|
|
||||||
self.add_items([self._entity(pos) for pos in coords])
|
|
||||||
|
|
||||||
def render(self):
|
|
||||||
return [y for y in [x.render() for x in self] if y is not None]
|
|
||||||
|
|
||||||
@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 __delitem__(self, name):
|
|
||||||
idx, obj = next((i, obj) for i, obj in enumerate(self) if obj.name == name)
|
|
||||||
try:
|
|
||||||
for observer in obj.observers:
|
|
||||||
observer.notify_del_entity(obj)
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
super().__delitem__(name)
|
|
||||||
|
|
||||||
def by_pos(self, pos: (int, int)):
|
|
||||||
pos = tuple(pos)
|
|
||||||
try:
|
|
||||||
return self.pos_dict[pos]
|
|
||||||
except StopIteration:
|
|
||||||
pass
|
|
||||||
except ValueError:
|
|
||||||
print()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def positions(self):
|
|
||||||
return [e.pos for e in self]
|
|
||||||
|
|
||||||
def notify_del_entity(self, entity: Entity):
|
|
||||||
try:
|
|
||||||
self.pos_dict[entity.pos].remove(entity)
|
|
||||||
except (ValueError, AttributeError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences,PyTypeChecker
|
# noinspection PyUnresolvedReferences,PyTypeChecker
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from marl_factory_grid.environment.entity.util import GlobalPosition
|
from marl_factory_grid.environment.entity.util import GlobalPosition
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin, HasBoundMixin
|
|
||||||
from marl_factory_grid.environment.groups.objects import _Objects
|
|
||||||
from marl_factory_grid.modules.zones import Zone
|
|
||||||
from marl_factory_grid.utils import helpers as h
|
|
||||||
from marl_factory_grid.environment import constants as c
|
|
||||||
|
|
||||||
|
|
||||||
class Combined(PositionMixin, Collection):
|
class Combined(Collection):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -38,8 +35,14 @@ class Combined(PositionMixin, Collection):
|
|||||||
class GlobalPositions(Collection):
|
class GlobalPositions(Collection):
|
||||||
|
|
||||||
_entity = GlobalPosition
|
_entity = GlobalPosition
|
||||||
is_blocking_light = False,
|
|
||||||
can_collide = False
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_collide(self):
|
||||||
|
return False
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(GlobalPositions, self).__init__(*args, **kwargs)
|
super(GlobalPositions, self).__init__(*args, **kwargs)
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
from marl_factory_grid.environment import constants as c
|
from marl_factory_grid.environment import constants as c
|
||||||
from marl_factory_grid.environment.entity.wall import Wall
|
from marl_factory_grid.environment.entity.wall import Wall
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Walls(PositionMixin, Collection):
|
class Walls(Collection):
|
||||||
_entity = Wall
|
_entity = Wall
|
||||||
symbol = c.SYMBOL_WALL
|
symbol = c.SYMBOL_WALL
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(Walls, self).__init__(*args, **kwargs)
|
super(Walls, self).__init__(*args, **kwargs)
|
||||||
self._value = c.VALUE_OCCUPIED_CELL
|
self._value = c.VALUE_OCCUPIED_CELL
|
||||||
|
@ -1,13 +1,26 @@
|
|||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin, HasBoundMixin
|
|
||||||
from marl_factory_grid.modules.batteries.entitites import Pod, Battery
|
from marl_factory_grid.modules.batteries.entitites import Pod, Battery
|
||||||
|
|
||||||
|
|
||||||
class Batteries(Collection):
|
class Batteries(Collection):
|
||||||
|
|
||||||
_entity = Battery
|
_entity = Battery
|
||||||
is_blocking_light: bool = False
|
|
||||||
can_collide: bool = False
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_collide(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def obs_tag(self):
|
def obs_tag(self):
|
||||||
@ -21,7 +34,7 @@ class Batteries(Collection):
|
|||||||
self.add_items(batteries)
|
self.add_items(batteries)
|
||||||
|
|
||||||
|
|
||||||
class ChargePods(PositionMixin, Collection):
|
class ChargePods(Collection):
|
||||||
|
|
||||||
_entity = Pod
|
_entity = Pod
|
||||||
|
|
||||||
|
@ -1,14 +1,26 @@
|
|||||||
|
from marl_factory_grid.environment import constants as c
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
from marl_factory_grid.modules.clean_up.entitites import DirtPile
|
from marl_factory_grid.modules.clean_up.entitites import DirtPile
|
||||||
|
|
||||||
from marl_factory_grid.environment import constants as c
|
|
||||||
|
|
||||||
|
class DirtPiles(Collection):
|
||||||
class DirtPiles(PositionMixin, Collection):
|
|
||||||
_entity = DirtPile
|
_entity = DirtPile
|
||||||
is_blocking_light: bool = False
|
|
||||||
can_collide: bool = False
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_collide(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def amount(self):
|
def amount(self):
|
||||||
|
@ -1,14 +1,27 @@
|
|||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
from marl_factory_grid.modules.destinations.entitites import Destination
|
from marl_factory_grid.modules.destinations.entitites import Destination
|
||||||
from marl_factory_grid.environment import constants as c
|
from marl_factory_grid.environment import constants as c
|
||||||
from marl_factory_grid.modules.destinations import constants as d
|
from marl_factory_grid.modules.destinations import constants as d
|
||||||
|
|
||||||
|
|
||||||
class Destinations(PositionMixin, Collection):
|
class Destinations(Collection):
|
||||||
_entity = Destination
|
_entity = Destination
|
||||||
is_blocking_light: bool = False
|
|
||||||
can_collide: bool = False
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_collide(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
from marl_factory_grid.modules.doors import constants as d
|
from marl_factory_grid.modules.doors import constants as d
|
||||||
from marl_factory_grid.modules.doors.entitites import Door
|
from marl_factory_grid.modules.doors.entitites import Door
|
||||||
|
|
||||||
|
|
||||||
class Doors(PositionMixin, Collection):
|
class Doors(Collection):
|
||||||
|
|
||||||
symbol = d.SYMBOL_DOOR
|
symbol = d.SYMBOL_DOOR
|
||||||
_entity = Door
|
_entity = Door
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(Doors, self).__init__(*args, can_collide=True, **kwargs)
|
super(Doors, self).__init__(*args, can_collide=True, **kwargs)
|
||||||
|
|
||||||
|
@ -5,14 +5,18 @@ from marl_factory_grid.environment import constants as c
|
|||||||
|
|
||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.objects import _Objects
|
from marl_factory_grid.environment.groups.objects import _Objects
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin, IsBoundMixin, HasBoundMixin
|
from marl_factory_grid.environment.groups.mixins import IsBoundMixin, HasBoundMixin
|
||||||
from marl_factory_grid.environment.entity.agent import Agent
|
from marl_factory_grid.environment.entity.agent import Agent
|
||||||
from marl_factory_grid.modules.items.entitites import Item, DropOffLocation
|
from marl_factory_grid.modules.items.entitites import Item, DropOffLocation
|
||||||
|
|
||||||
|
|
||||||
class Items(PositionMixin, Collection):
|
class Items(Collection):
|
||||||
_entity = Item
|
_entity = Item
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_blocking_light(self):
|
def is_blocking_light(self):
|
||||||
return False
|
return False
|
||||||
@ -103,17 +107,25 @@ class Inventories(HasBoundMixin, _Objects):
|
|||||||
state[i.INVENTORY].spawn(state[c.AGENT])
|
state[i.INVENTORY].spawn(state[c.AGENT])
|
||||||
|
|
||||||
|
|
||||||
class DropOffLocations(PositionMixin, Collection):
|
class DropOffLocations(Collection):
|
||||||
_entity = DropOffLocation
|
_entity = DropOffLocation
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_blocking_light(self):
|
def var_is_blocking_light(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def can_collide(self):
|
def var_can_collide(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(DropOffLocations, self).__init__(*args, **kwargs)
|
super(DropOffLocations, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
|
|
||||||
from .entitites import Machine
|
from .entitites import Machine
|
||||||
|
|
||||||
|
|
||||||
class Machines(PositionMixin, Collection):
|
class Machines(Collection):
|
||||||
|
|
||||||
_entity = Machine
|
_entity = Machine
|
||||||
is_blocking_light: bool = False
|
|
||||||
can_collide: bool = False
|
@property
|
||||||
|
def var_can_collide(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(Machines, self).__init__(*args, **kwargs)
|
super(Machines, self).__init__(*args, **kwargs)
|
||||||
|
@ -1,18 +1,28 @@
|
|||||||
from marl_factory_grid.environment.groups.collection import Collection
|
from marl_factory_grid.environment.groups.collection import Collection
|
||||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
|
||||||
from .entities import Maintainer
|
from .entities import Maintainer
|
||||||
from ..machines import constants as mc
|
from ..machines import constants as mc
|
||||||
from ..machines.actions import MachineAction
|
from ..machines.actions import MachineAction
|
||||||
from ...utils.states import Gamestate
|
from ...utils.states import Gamestate
|
||||||
|
|
||||||
|
|
||||||
class Maintainers(PositionMixin, Collection):
|
class Maintainers(Collection):
|
||||||
|
|
||||||
_entity = Maintainer
|
_entity = Maintainer
|
||||||
var_can_collide = True
|
|
||||||
var_can_move = True
|
@property
|
||||||
var_is_blocking_light = False
|
def var_can_collide(self):
|
||||||
var_has_position = True
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_can_move(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_is_blocking_light(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def var_has_position(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
Reference in New Issue
Block a user