mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-21 11:21:35 +02:00
renaming
This commit is contained in:
0
marl_factory_grid/modules/clean_up/__init__.py
Normal file
0
marl_factory_grid/modules/clean_up/__init__.py
Normal file
36
marl_factory_grid/modules/clean_up/actions.py
Normal file
36
marl_factory_grid/modules/clean_up/actions.py
Normal file
@ -0,0 +1,36 @@
|
||||
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.clean_up import constants as d, rewards as r
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class CleanUp(Action):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(d.CLEAN_UP)
|
||||
|
||||
def do(self, entity, state) -> Union[None, ActionResult]:
|
||||
if dirt := state[d.DIRT].by_pos(entity.pos):
|
||||
new_dirt_amount = dirt.amount - state[d.DIRT].clean_amount
|
||||
|
||||
if new_dirt_amount <= 0:
|
||||
state[d.DIRT].delete_env_object(dirt)
|
||||
else:
|
||||
dirt.set_new_amount(max(new_dirt_amount, c.VALUE_FREE_CELL))
|
||||
valid = c.VALID
|
||||
print_str = f'{entity.name} did just clean up some dirt at {entity.pos}.'
|
||||
state.print(print_str)
|
||||
reward = r.CLEAN_UP_VALID
|
||||
identifier = d.CLEAN_UP
|
||||
else:
|
||||
valid = c.NOT_VALID
|
||||
print_str = f'{entity.name} just tried to clean up some dirt at {entity.pos}, but failed.'
|
||||
state.print(print_str)
|
||||
reward = r.CLEAN_UP_FAIL
|
||||
identifier = d.CLEAN_UP_FAIL
|
||||
|
||||
return ActionResult(identifier=identifier, validity=valid, reward=reward, entity=entity)
|
7
marl_factory_grid/modules/clean_up/constants.py
Normal file
7
marl_factory_grid/modules/clean_up/constants.py
Normal file
@ -0,0 +1,7 @@
|
||||
DIRT = 'DirtPiles'
|
||||
|
||||
CLEAN_UP = 'do_cleanup_action'
|
||||
|
||||
CLEAN_UP_VALID = 'clean_up_valid'
|
||||
CLEAN_UP_FAIL = 'clean_up_fail'
|
||||
CLEAN_UP_ALL = 'all_cleaned_up'
|
BIN
marl_factory_grid/modules/clean_up/dirtpiles.png
Normal file
BIN
marl_factory_grid/modules/clean_up/dirtpiles.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
35
marl_factory_grid/modules/clean_up/entitites.py
Normal file
35
marl_factory_grid/modules/clean_up/entitites.py
Normal file
@ -0,0 +1,35 @@
|
||||
from numpy import random
|
||||
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.utils.render import RenderEntity
|
||||
from marl_factory_grid.modules.clean_up import constants as d
|
||||
|
||||
|
||||
class DirtPile(Entity):
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
return self._amount
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
# Edit this if you want items to be drawn in the ops differntly
|
||||
return self._amount
|
||||
|
||||
def __init__(self, *args, max_local_amount=5, initial_amount=2, spawn_variation=0.05, **kwargs):
|
||||
super(DirtPile, self).__init__(*args, **kwargs)
|
||||
self._amount = abs(initial_amount + (
|
||||
random.normal(loc=0, scale=spawn_variation, size=1).item() * initial_amount)
|
||||
)
|
||||
self.max_local_amount = max_local_amount
|
||||
|
||||
def set_new_amount(self, amount):
|
||||
self._amount = min(amount, self.max_local_amount)
|
||||
|
||||
def summarize_state(self):
|
||||
state_dict = super().summarize_state()
|
||||
state_dict.update(amount=float(self.amount))
|
||||
return state_dict
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(d.DIRT, self.tile.pos, min(0.15 + self.amount, 1.5), 'scale')
|
64
marl_factory_grid/modules/clean_up/groups.py
Normal file
64
marl_factory_grid/modules/clean_up/groups.py
Normal file
@ -0,0 +1,64 @@
|
||||
from marl_factory_grid.environment.groups.env_objects import EnvObjects
|
||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
||||
from marl_factory_grid.environment.entity.wall_floor import Floor
|
||||
from marl_factory_grid.modules.clean_up.entitites import DirtPile
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
|
||||
|
||||
class DirtPiles(PositionMixin, EnvObjects):
|
||||
|
||||
_entity = DirtPile
|
||||
is_blocking_light: bool = False
|
||||
can_collide: bool = False
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
return sum([dirt.amount for dirt in self])
|
||||
|
||||
def __init__(self, *args,
|
||||
initial_amount=2,
|
||||
initial_dirt_ratio=0.05,
|
||||
dirt_spawn_r_var=0.1,
|
||||
max_local_amount=5,
|
||||
clean_amount=1,
|
||||
max_global_amount: int = 20, **kwargs):
|
||||
super(DirtPiles, self).__init__(*args, **kwargs)
|
||||
self.clean_amount = clean_amount
|
||||
self.initial_amount = initial_amount
|
||||
self.initial_dirt_ratio = initial_dirt_ratio
|
||||
self.dirt_spawn_r_var = dirt_spawn_r_var
|
||||
self.max_global_amount = max_global_amount
|
||||
self.max_local_amount = max_local_amount
|
||||
|
||||
def spawn_dirt(self, then_dirty_tiles, amount) -> bool:
|
||||
if isinstance(then_dirty_tiles, Floor):
|
||||
then_dirty_tiles = [then_dirty_tiles]
|
||||
for tile in then_dirty_tiles:
|
||||
if not self.amount > self.max_global_amount:
|
||||
if dirt := self.by_pos(tile.pos):
|
||||
new_value = dirt.amount + amount
|
||||
dirt.set_new_amount(new_value)
|
||||
else:
|
||||
dirt = DirtPile(tile, initial_amount=amount, spawn_variation=self.dirt_spawn_r_var)
|
||||
self.add_item(dirt)
|
||||
else:
|
||||
return c.NOT_VALID
|
||||
return c.VALID
|
||||
|
||||
def trigger_dirt_spawn(self, state, initial_spawn=False) -> bool:
|
||||
free_for_dirt = [x for x in state[c.FLOOR]
|
||||
if len(x.guests) == 0 or (
|
||||
len(x.guests) == 1 and
|
||||
isinstance(next(y for y in x.guests), DirtPile))
|
||||
]
|
||||
state.rng.shuffle(free_for_dirt)
|
||||
|
||||
var = self.dirt_spawn_r_var
|
||||
new_spawn = abs(self.initial_dirt_ratio + (state.rng.uniform(-var, var) if initial_spawn else 0))
|
||||
n_dirt_tiles = max(0, int(new_spawn * len(free_for_dirt)))
|
||||
return self.spawn_dirt(free_for_dirt[:n_dirt_tiles], self.initial_amount)
|
||||
|
||||
def __repr__(self):
|
||||
s = super(DirtPiles, self).__repr__()
|
||||
return f'{s[:-1]}, {self.amount})'
|
3
marl_factory_grid/modules/clean_up/rewards.py
Normal file
3
marl_factory_grid/modules/clean_up/rewards.py
Normal file
@ -0,0 +1,3 @@
|
||||
CLEAN_UP_VALID: float = 0.5
|
||||
CLEAN_UP_FAIL: float = -0.1
|
||||
CLEAN_UP_ALL: float = 4.5
|
15
marl_factory_grid/modules/clean_up/rule_done_on_all_clean.py
Normal file
15
marl_factory_grid/modules/clean_up/rule_done_on_all_clean.py
Normal file
@ -0,0 +1,15 @@
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.utils.results import DoneResult
|
||||
from marl_factory_grid.modules.clean_up import constants as d, rewards as r
|
||||
|
||||
|
||||
class DirtAllCleanDone(Rule):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def on_check_done(self, state) -> [DoneResult]:
|
||||
if len(state[d.DIRT]) == 0 and state.curr_step:
|
||||
return [DoneResult(validity=c.VALID, identifier=self.name, reward=r.CLEAN_UP_ALL)]
|
||||
return [DoneResult(validity=c.NOT_VALID, identifier=self.name, reward=0)]
|
28
marl_factory_grid/modules/clean_up/rule_respawn.py
Normal file
28
marl_factory_grid/modules/clean_up/rule_respawn.py
Normal file
@ -0,0 +1,28 @@
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.utils.results import TickResult
|
||||
|
||||
from marl_factory_grid.modules.clean_up import constants as d
|
||||
|
||||
|
||||
class DirtRespawnRule(Rule):
|
||||
|
||||
def __init__(self, spawn_freq=15):
|
||||
super().__init__()
|
||||
self.spawn_freq = spawn_freq
|
||||
self._next_dirt_spawn = spawn_freq
|
||||
|
||||
def on_init(self, state) -> str:
|
||||
state[d.DIRT].trigger_dirt_spawn(state, initial_spawn=True)
|
||||
return f'Initial Dirt was spawned on: {[x.pos for x in state[d.DIRT]]}'
|
||||
|
||||
def tick_step(self, state):
|
||||
if self._next_dirt_spawn < 0:
|
||||
pass # No DirtPile Spawn
|
||||
elif not self._next_dirt_spawn:
|
||||
validity = state[d.DIRT].trigger_dirt_spawn(state)
|
||||
|
||||
return [TickResult(entity=None, validity=validity, identifier=self.name, reward=0)]
|
||||
self._next_dirt_spawn = self.spawn_freq
|
||||
else:
|
||||
self._next_dirt_spawn -= 1
|
||||
return []
|
24
marl_factory_grid/modules/clean_up/rule_smear_on_move.py
Normal file
24
marl_factory_grid/modules/clean_up/rule_smear_on_move.py
Normal file
@ -0,0 +1,24 @@
|
||||
from marl_factory_grid.environment.rules import Rule
|
||||
from marl_factory_grid.utils.helpers import is_move
|
||||
from marl_factory_grid.utils.results import TickResult
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.modules.clean_up import constants as d
|
||||
|
||||
|
||||
class DirtSmearOnMove(Rule):
|
||||
|
||||
def __init__(self, smear_amount: float = 0.2):
|
||||
super().__init__()
|
||||
self.smear_amount = smear_amount
|
||||
|
||||
def tick_post_step(self, state):
|
||||
results = list()
|
||||
for entity in state.moving_entites:
|
||||
if is_move(entity.state.identifier) and entity.state.validity == c.VALID:
|
||||
if old_pos_dirt := state[d.DIRT].by_pos(entity.last_pos):
|
||||
if smeared_dirt := round(old_pos_dirt.amount * self.smear_amount, 2):
|
||||
if state[d.DIRT].spawn_dirt(entity.tile, amount=smeared_dirt):
|
||||
results.append(TickResult(identifier=self.name, entity=entity,
|
||||
reward=0, validity=c.VALID))
|
||||
return results
|
Reference in New Issue
Block a user