mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-24 04:11:36 +02:00
renaming
This commit is contained in:
0
marl_factory_grid/modules/destinations/__init__.py
Normal file
0
marl_factory_grid/modules/destinations/__init__.py
Normal file
23
marl_factory_grid/modules/destinations/actions.py
Normal file
23
marl_factory_grid/modules/destinations/actions.py
Normal file
@ -0,0 +1,23 @@
|
||||
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.destinations import constants as d, rewards as r
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class DestAction(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.DESTINATION)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if destination := state[d.DESTINATION].by_pos(entity.pos):
|
||||
valid = destination.do_wait_action(entity)
|
||||
state.print(f'{entity.name} just waited at {entity.pos}')
|
||||
else:
|
||||
valid = c.NOT_VALID
|
||||
state.print(f'{entity.name} just tried to do_wait_action do_wait_action at {entity.pos} but failed')
|
||||
return ActionResult(entity=entity, identifier=self._identifier, validity=valid,
|
||||
reward=r.WAIT_VALID if valid else r.WAIT_FAIL)
|
14
marl_factory_grid/modules/destinations/constants.py
Normal file
14
marl_factory_grid/modules/destinations/constants.py
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
# Destination Env
|
||||
DESTINATION = 'Destinations'
|
||||
DEST_SYMBOL = 1
|
||||
DEST_REACHED_REWARD = 0.5
|
||||
DEST_REACHED = 'ReachedDestinations'
|
||||
|
||||
WAIT_ON_DEST = 'WAIT'
|
||||
|
||||
MODE_SINGLE = 'SINGLE'
|
||||
MODE_GROUPED = 'GROUPED'
|
||||
|
||||
DONE_ALL = 'DONE_ALL'
|
||||
DONE_SINGLE = 'DONE_SINGLE'
|
BIN
marl_factory_grid/modules/destinations/destinations.png
Normal file
BIN
marl_factory_grid/modules/destinations/destinations.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
51
marl_factory_grid/modules/destinations/entitites.py
Normal file
51
marl_factory_grid/modules/destinations/entitites.py
Normal file
@ -0,0 +1,51 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from marl_factory_grid.environment.entity.agent import Agent
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils.render import RenderEntity
|
||||
from marl_factory_grid.modules.destinations import constants as d
|
||||
|
||||
|
||||
class Destination(Entity):
|
||||
|
||||
@property
|
||||
def any_agent_has_dwelled(self):
|
||||
return bool(len(self._per_agent_times))
|
||||
|
||||
@property
|
||||
def currently_dwelling_names(self):
|
||||
return list(self._per_agent_times.keys())
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return d.DEST_SYMBOL
|
||||
|
||||
def __init__(self, *args, dwell_time: int = 0, **kwargs):
|
||||
super(Destination, self).__init__(*args, **kwargs)
|
||||
self.dwell_time = dwell_time
|
||||
self._per_agent_times = defaultdict(lambda: dwell_time)
|
||||
|
||||
def do_wait_action(self, agent: Agent):
|
||||
self._per_agent_times[agent.name] -= 1
|
||||
return c.VALID
|
||||
|
||||
def leave(self, agent: Agent):
|
||||
del self._per_agent_times[agent.name]
|
||||
|
||||
@property
|
||||
def is_considered_reached(self):
|
||||
agent_at_position = any(c.AGENT.lower() in x.name.lower() for x in self.tile.guests_that_can_collide)
|
||||
return (agent_at_position and not self.dwell_time) or any(x == 0 for x in self._per_agent_times.values())
|
||||
|
||||
def agent_is_dwelling(self, agent: Agent):
|
||||
return self._per_agent_times[agent.name] < self.dwell_time
|
||||
|
||||
def summarize_state(self) -> dict:
|
||||
state_summary = super().summarize_state()
|
||||
state_summary.update(per_agent_times=[
|
||||
dict(belongs_to=key, time=val) for key, val in self._per_agent_times.keys()], dwell_time=self.dwell_time)
|
||||
return state_summary
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(d.DESTINATION, self.pos)
|
28
marl_factory_grid/modules/destinations/groups.py
Normal file
28
marl_factory_grid/modules/destinations/groups.py
Normal file
@ -0,0 +1,28 @@
|
||||
from marl_factory_grid.environment.groups.env_objects import EnvObjects
|
||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
||||
from marl_factory_grid.modules.destinations.entitites import Destination
|
||||
|
||||
|
||||
class Destinations(PositionMixin, EnvObjects):
|
||||
|
||||
_entity = Destination
|
||||
is_blocking_light: bool = False
|
||||
can_collide: bool = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
return super(Destinations, self).__repr__()
|
||||
|
||||
|
||||
class ReachedDestinations(Destinations):
|
||||
_entity = Destination
|
||||
is_blocking_light = False
|
||||
can_collide = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ReachedDestinations, self).__init__(*args, **kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
return super(ReachedDestinations, self).__repr__()
|
3
marl_factory_grid/modules/destinations/rewards.py
Normal file
3
marl_factory_grid/modules/destinations/rewards.py
Normal file
@ -0,0 +1,3 @@
|
||||
WAIT_VALID: float = 0.1
|
||||
WAIT_FAIL: float = -0.1
|
||||
DEST_REACHED: float = 5.0
|
89
marl_factory_grid/modules/destinations/rules.py
Normal file
89
marl_factory_grid/modules/destinations/rules.py
Normal file
@ -0,0 +1,89 @@
|
||||
from typing import List, Union
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.utils.results import TickResult, DoneResult
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
from marl_factory_grid.modules.destinations import constants as d, rewards as r
|
||||
from marl_factory_grid.modules.destinations.entitites import Destination
|
||||
|
||||
|
||||
class DestinationReach(Rule):
|
||||
|
||||
def __init__(self, n_dests: int = 1, tiles: Union[List, None] = None):
|
||||
super(DestinationReach, self).__init__()
|
||||
self.n_dests = n_dests or len(tiles)
|
||||
self._tiles = tiles
|
||||
|
||||
def tick_step(self, state) -> List[TickResult]:
|
||||
|
||||
for dest in list(state[d.DESTINATION].values()):
|
||||
if dest.is_considered_reached:
|
||||
dest.change_parent_collection(state[d.DEST_REACHED])
|
||||
state.print(f'{dest.name} is reached now, removing...')
|
||||
else:
|
||||
for agent_name in dest.currently_dwelling_names:
|
||||
agent = state[c.AGENT][agent_name]
|
||||
if agent.pos == dest.pos:
|
||||
state.print(f'{agent.name} is still waiting.')
|
||||
pass
|
||||
else:
|
||||
dest.leave(agent)
|
||||
state.print(f'{agent.name} left the destination early.')
|
||||
return [TickResult(self.name, validity=c.VALID, reward=0, entity=None)]
|
||||
|
||||
def tick_post_step(self, state) -> List[TickResult]:
|
||||
results = list()
|
||||
for reached_dest in state[d.DEST_REACHED]:
|
||||
for guest in reached_dest.tile.guests:
|
||||
if guest in state[c.AGENT]:
|
||||
state.print(f'{guest.name} just reached destination at {guest.pos}')
|
||||
state[d.DEST_REACHED].delete_env_object(reached_dest)
|
||||
results.append(TickResult(self.name, validity=c.VALID, reward=r.DEST_REACHED, entity=guest))
|
||||
return results
|
||||
|
||||
|
||||
class DestinationDone(Rule):
|
||||
|
||||
def __init__(self):
|
||||
super(DestinationDone, self).__init__()
|
||||
|
||||
def on_check_done(self, state) -> List[DoneResult]:
|
||||
if not len(state[d.DESTINATION]):
|
||||
return [DoneResult(self.name, validity=c.VALID, reward=r.DEST_REACHED)]
|
||||
return []
|
||||
|
||||
|
||||
class DestinationSpawn(Rule):
|
||||
|
||||
def __init__(self, spawn_frequency: int = 5, n_dests: int = 1,
|
||||
spawn_mode: str = d.MODE_GROUPED):
|
||||
super(DestinationSpawn, self).__init__()
|
||||
self.spawn_frequency = spawn_frequency
|
||||
self.n_dests = n_dests
|
||||
self.spawn_mode = spawn_mode
|
||||
|
||||
def on_init(self, state):
|
||||
# noinspection PyAttributeOutsideInit
|
||||
self._dest_spawn_timer = self.spawn_frequency
|
||||
self.trigger_destination_spawn(self.n_dests, state)
|
||||
pass
|
||||
|
||||
def tick_pre_step(self, state) -> List[TickResult]:
|
||||
pass
|
||||
|
||||
def tick_step(self, state) -> List[TickResult]:
|
||||
if n_dest_spawn := max(0, self.n_dests - len(state[d.DESTINATION])):
|
||||
if self.spawn_mode == d.MODE_GROUPED and n_dest_spawn == self.n_dests:
|
||||
validity = state.rules['DestinationReach'].trigger_destination_spawn(n_dest_spawn, state)
|
||||
return [TickResult(self.name, validity=validity, entity=None, value=n_dest_spawn)]
|
||||
|
||||
@staticmethod
|
||||
def trigger_destination_spawn(n_dests, state, tiles=None):
|
||||
tiles = tiles or state[c.FLOOR].empty_tiles[:n_dests]
|
||||
if destinations := [Destination(tile) for tile in tiles]:
|
||||
state[d.DESTINATION].add_items(destinations)
|
||||
state.print(f'{n_dests} new destinations have been spawned')
|
||||
return c.VALID
|
||||
else:
|
||||
state.print('No Destiantions are spawning, limit is reached.')
|
||||
return c.NOT_VALID
|
Reference in New Issue
Block a user