mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-11-13 05:53:50 +01:00
renaming
This commit is contained in:
0
marl_factory_grid/modules/doors/__init__.py
Normal file
0
marl_factory_grid/modules/doors/__init__.py
Normal file
28
marl_factory_grid/modules/doors/actions.py
Normal file
28
marl_factory_grid/modules/doors/actions.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from typing import Union
|
||||
|
||||
from marl_factory_grid.environment.actions import Action
|
||||
from marl_factory_grid.utils.results import ActionResult
|
||||
|
||||
from marl_factory_grid.modules.doors import constants as d, rewards as r
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class DoorUse(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.ACTION_DOOR_USE)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
# Check if agent really is standing on a door:
|
||||
e = state.entities.get_near_pos(entity.pos)
|
||||
try:
|
||||
door = next(x for x in e if x.name.startswith(d.DOOR))
|
||||
valid = door.use()
|
||||
state.print(f'{entity.name} just used a {door.name} at {door.pos}')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid, reward=r.USE_DOOR_VALID)
|
||||
|
||||
except StopIteration:
|
||||
# When he doesn't...
|
||||
state.print(f'{entity.name} just tried to use a door at {entity.pos}, but there is none.')
|
||||
return ActionResult(entity=entity, identifier=self._identifier,
|
||||
validity=c.NOT_VALID, reward=r.USE_DOOR_FAIL)
|
||||
18
marl_factory_grid/modules/doors/constants.py
Normal file
18
marl_factory_grid/modules/doors/constants.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Names / Identifiers
|
||||
DOOR = 'Door' # Identifier of Single-Door Entities.
|
||||
DOORS = 'Doors' # Identifier of Door-objects and groups (groups).
|
||||
|
||||
# Symbols (in map)
|
||||
SYMBOL_DOOR = 'D' # Door _identifier for resolving the string based map files.
|
||||
|
||||
# Values
|
||||
VALUE_ACCESS_INDICATOR = 1 / 3 # Access-door-Cell value used in observation
|
||||
VALUE_OPEN_DOOR = 2 / 3 # Open-door-Cell value used in observation
|
||||
VALUE_CLOSED_DOOR = 3 / 3 # Closed-door-Cell value used in observation
|
||||
|
||||
# States
|
||||
STATE_CLOSED = 'closed' # Identifier to compare door-is-closed state
|
||||
STATE_OPEN = 'open' # Identifier to compare door-is-open state
|
||||
|
||||
# Actions
|
||||
ACTION_DOOR_USE = 'use_door'
|
||||
BIN
marl_factory_grid/modules/doors/door_closed.png
Normal file
BIN
marl_factory_grid/modules/doors/door_closed.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 699 B |
BIN
marl_factory_grid/modules/doors/door_open.png
Normal file
BIN
marl_factory_grid/modules/doors/door_open.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.1 KiB |
97
marl_factory_grid/modules/doors/entitites.py
Normal file
97
marl_factory_grid/modules/doors/entitites.py
Normal file
@@ -0,0 +1,97 @@
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.utils.render import RenderEntity
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
from marl_factory_grid.modules.doors import constants as d
|
||||
|
||||
|
||||
class DoorIndicator(Entity):
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return d.VALUE_ACCESS_INDICATOR
|
||||
|
||||
def render(self):
|
||||
return None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.__delattr__('move')
|
||||
|
||||
|
||||
class Door(Entity):
|
||||
|
||||
@property
|
||||
def is_blocking_pos(self):
|
||||
return False if self.is_open else True
|
||||
|
||||
@property
|
||||
def is_blocking_light(self):
|
||||
return False if self.is_open else True
|
||||
|
||||
@property
|
||||
def can_collide(self):
|
||||
return False if self.is_open else True
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return d.VALUE_CLOSED_DOOR if self.is_closed else d.VALUE_OPEN_DOOR
|
||||
|
||||
@property
|
||||
def str_state(self):
|
||||
return 'open' if self.is_open else 'closed'
|
||||
|
||||
def __init__(self, *args, closed_on_init=True, auto_close_interval=10, indicate_area=False, **kwargs):
|
||||
self._state = d.STATE_CLOSED
|
||||
super(Door, self).__init__(*args, **kwargs)
|
||||
self.auto_close_interval = auto_close_interval
|
||||
self.time_to_close = 0
|
||||
if not closed_on_init:
|
||||
self._open()
|
||||
if indicate_area:
|
||||
self._collection.add_items([DoorIndicator(x) for x in self.tile.neighboring_floor])
|
||||
|
||||
def summarize_state(self):
|
||||
state_dict = super().summarize_state()
|
||||
state_dict.update(state=str(self.str_state), time_to_close=int(self.time_to_close))
|
||||
return state_dict
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
return self._state == d.STATE_CLOSED
|
||||
|
||||
@property
|
||||
def is_open(self):
|
||||
return self._state == d.STATE_OPEN
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
return self._state
|
||||
|
||||
def render(self):
|
||||
name, state = 'door_open' if self.is_open else 'door_closed', 'blank'
|
||||
return RenderEntity(name, self.pos, 1, 'none', state, self.identifier_int + 1)
|
||||
|
||||
def use(self):
|
||||
if self._state == d.STATE_OPEN:
|
||||
self._close()
|
||||
else:
|
||||
self._open()
|
||||
return c.VALID
|
||||
|
||||
def tick(self):
|
||||
if self.is_open and len(self.tile) == 1 and self.time_to_close:
|
||||
self.time_to_close -= 1
|
||||
return c.NOT_VALID
|
||||
elif self.is_open and not self.time_to_close and len(self.tile) == 1:
|
||||
self.use()
|
||||
return c.VALID
|
||||
else:
|
||||
return c.NOT_VALID
|
||||
|
||||
def _open(self):
|
||||
self._state = d.STATE_OPEN
|
||||
self.time_to_close = self.auto_close_interval
|
||||
|
||||
def _close(self):
|
||||
self._state = d.STATE_CLOSED
|
||||
28
marl_factory_grid/modules/doors/groups.py
Normal file
28
marl_factory_grid/modules/doors/groups.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from typing import Union
|
||||
|
||||
from marl_factory_grid.environment.groups.env_objects import EnvObjects
|
||||
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.entitites import Door
|
||||
|
||||
|
||||
class Doors(PositionMixin, EnvObjects):
|
||||
|
||||
symbol = d.SYMBOL_DOOR
|
||||
_entity = Door
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Doors, self).__init__(*args, can_collide=True, **kwargs)
|
||||
|
||||
def get_near_position(self, position: (int, int)) -> Union[None, Door]:
|
||||
try:
|
||||
return next(door for door in self if position in door.tile.neighboring_floor_pos)
|
||||
except StopIteration:
|
||||
return None
|
||||
|
||||
def tick_doors(self):
|
||||
result_dict = dict()
|
||||
for door in self:
|
||||
did_tick = door.tick()
|
||||
result_dict.update({door.name: did_tick})
|
||||
return result_dict
|
||||
2
marl_factory_grid/modules/doors/rewards.py
Normal file
2
marl_factory_grid/modules/doors/rewards.py
Normal file
@@ -0,0 +1,2 @@
|
||||
USE_DOOR_VALID: float = -0.00
|
||||
USE_DOOR_FAIL: float = -0.01
|
||||
21
marl_factory_grid/modules/doors/rule_door_auto_close.py
Normal file
21
marl_factory_grid/modules/doors/rule_door_auto_close.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils.results import TickResult
|
||||
from marl_factory_grid.modules.doors import constants as d
|
||||
|
||||
|
||||
class DoorAutoClose(Rule):
|
||||
|
||||
def __init__(self, close_frequency: int = 10):
|
||||
super().__init__()
|
||||
self.close_frequency = close_frequency
|
||||
|
||||
def tick_step(self, state):
|
||||
if doors := state[d.DOORS]:
|
||||
doors_tick_result = doors.tick_doors()
|
||||
doors_that_ticked = [key for key, val in doors_tick_result.items() if val]
|
||||
state.print(f'{doors_that_ticked} were auto-closed'
|
||||
if doors_that_ticked else 'No Doors were auto-closed')
|
||||
return [TickResult(self.name, validity=c.VALID, value=0)]
|
||||
state.print('There are no doors, but you loaded the corresponding Module')
|
||||
return []
|
||||
Reference in New Issue
Block a user