mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-06-21 11:21:35 +02:00
new rules, new spawn logic, small fixes, default and narrow corridor debugged
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from .actions import CleanUp
|
||||
from .entitites import DirtPile
|
||||
from .groups import DirtPiles
|
||||
from .rules import SpawnDirt, EntitiesSmearDirtOnMove, DoneOnAllDirtCleaned
|
||||
from .rules import EntitiesSmearDirtOnMove, DoneOnAllDirtCleaned
|
||||
|
@ -7,22 +7,6 @@ from marl_factory_grid.modules.clean_up import constants as d
|
||||
|
||||
class DirtPile(Entity):
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_move(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_has_position(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
return self._amount
|
||||
|
@ -9,68 +9,55 @@ from marl_factory_grid.modules.clean_up.entitites import DirtPile
|
||||
class DirtPiles(Collection):
|
||||
_entity = DirtPile
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
var_is_blocking_light = False
|
||||
var_can_collide = False
|
||||
var_can_move = False
|
||||
var_has_position = True
|
||||
|
||||
@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
|
||||
def amount(self):
|
||||
def global_amount(self):
|
||||
return sum([dirt.amount for dirt in self])
|
||||
|
||||
def __init__(self, *args,
|
||||
max_local_amount=5,
|
||||
clean_amount=1,
|
||||
max_global_amount: int = 20, **kwargs):
|
||||
max_global_amount: int = 20,
|
||||
coords_or_quantity=10,
|
||||
initial_amount=2,
|
||||
amount_var=0.2,
|
||||
n_var=0.2,
|
||||
**kwargs):
|
||||
super(DirtPiles, self).__init__(*args, **kwargs)
|
||||
self.amount_var = amount_var
|
||||
self.n_var = n_var
|
||||
self.clean_amount = clean_amount
|
||||
self.max_global_amount = max_global_amount
|
||||
self.max_local_amount = max_local_amount
|
||||
self.coords_or_quantity = coords_or_quantity
|
||||
self.initial_amount = initial_amount
|
||||
|
||||
def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args):
|
||||
amount_s = entity_args[0]
|
||||
def trigger_spawn(self, state, coords_or_quantity=0, amount=0) -> [Result]:
|
||||
coords_or_quantity = coords_or_quantity if coords_or_quantity else self.coords_or_quantity
|
||||
n_new = int(abs(coords_or_quantity + (state.rng.uniform(-self.n_var, self.n_var))))
|
||||
n_new = state.get_n_random_free_positions(n_new)
|
||||
|
||||
amounts = [amount if amount else (self.initial_amount + state.rng.uniform(-self.amount_var, self.amount_var))
|
||||
for _ in range(coords_or_quantity)]
|
||||
spawn_counter = 0
|
||||
for idx, pos in enumerate(coords_or_quantity):
|
||||
if not self.amount > self.max_global_amount:
|
||||
amount = amount_s[idx] if isinstance(amount_s, list) else amount_s
|
||||
for idx, (pos, a) in enumerate(zip(n_new, amounts)):
|
||||
if not self.global_amount > self.max_global_amount:
|
||||
if dirt := self.by_pos(pos):
|
||||
dirt = next(dirt.iter())
|
||||
new_value = dirt.amount + amount
|
||||
new_value = dirt.amount + a
|
||||
dirt.set_new_amount(new_value)
|
||||
else:
|
||||
dirt = DirtPile(pos, amount=amount)
|
||||
self.add_item(dirt)
|
||||
super().spawn([pos], amount=a)
|
||||
spawn_counter += 1
|
||||
else:
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.NOT_VALID, reward=0,
|
||||
value=spawn_counter)
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.VALID, reward=0, value=spawn_counter)
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.NOT_VALID, value=spawn_counter)
|
||||
|
||||
def trigger_dirt_spawn(self, n, amount, state, n_var=0.2, amount_var=0.2) -> Result:
|
||||
free_for_dirt = [x for x in state.entities.floorlist if len(state.entities.pos_dict[x]) == 0 or (
|
||||
len(state.entities.pos_dict[x]) >= 1 and isinstance(next(y for y in x), DirtPile))]
|
||||
# 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)
|
||||
|
||||
new_spawn = int(abs(n + (state.rng.uniform(-n_var, n_var))))
|
||||
new_amount_s = [abs(amount + (amount*state.rng.uniform(-amount_var, amount_var))) for _ in range(new_spawn)]
|
||||
n_dirty_positions = free_for_dirt[:new_spawn]
|
||||
return self.spawn(n_dirty_positions, new_amount_s)
|
||||
return Result(identifier=f'{self.name}_spawn', validity=c.VALID, value=spawn_counter)
|
||||
|
||||
def __repr__(self):
|
||||
s = super(DirtPiles, self).__repr__()
|
||||
return f'{s[:-1]}, {self.amount})'
|
||||
return f'{s[:-1]}, {self.global_amount}]'
|
||||
|
@ -22,58 +22,37 @@ class DoneOnAllDirtCleaned(Rule):
|
||||
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=self.reward)]
|
||||
return [DoneResult(validity=c.NOT_VALID, identifier=self.name, reward=0)]
|
||||
return [DoneResult(validity=c.NOT_VALID, identifier=self.name)]
|
||||
|
||||
|
||||
class SpawnDirt(Rule):
|
||||
class RespawnDirt(Rule):
|
||||
|
||||
def __init__(self, initial_n: int = 5, initial_amount: float = 1.3,
|
||||
respawn_n: int = 3, respawn_amount: float = 0.8,
|
||||
n_var: float = 0.2, amount_var: float = 0.2, spawn_freq: int = 15):
|
||||
def __init__(self, respawn_freq: int = 15, respawn_n: int = 5, respawn_amount: float = 1.0):
|
||||
"""
|
||||
Defines the spawn pattern of intial and additional 'Dirt'-entitites.
|
||||
First chooses positions, then trys to spawn dirt until 'respawn_n' or the maximal global amount is reached.
|
||||
If there is allready some, it is topped up to min(max_local_amount, amount).
|
||||
|
||||
:type spawn_freq: int
|
||||
:parameter spawn_freq: In which frequency should this Rule try to spawn new 'Dirt'?
|
||||
:type respawn_freq: int
|
||||
:parameter respawn_freq: In which frequency should this Rule try to spawn new 'Dirt'?
|
||||
:type respawn_n: int
|
||||
:parameter respawn_n: How many respawn positions are considered.
|
||||
:type initial_n: int
|
||||
:parameter initial_n: How much initial positions are considered.
|
||||
:type amount_var: float
|
||||
:parameter amount_var: Variance of amount to spawn.
|
||||
:type n_var: float
|
||||
:parameter n_var: Variance of n to spawn.
|
||||
:type respawn_amount: float
|
||||
:parameter respawn_amount: Defines how much dirt 'amount' is placed every 'spawn_freq' ticks.
|
||||
:type initial_amount: float
|
||||
:parameter initial_amount: Defines how much dirt 'amount' is initially placed.
|
||||
|
||||
"""
|
||||
super().__init__()
|
||||
self.amount_var = amount_var
|
||||
self.n_var = n_var
|
||||
self.respawn_amount = respawn_amount
|
||||
self.respawn_n = respawn_n
|
||||
self.initial_amount = initial_amount
|
||||
self.initial_n = initial_n
|
||||
self.spawn_freq = spawn_freq
|
||||
self._next_dirt_spawn = spawn_freq
|
||||
|
||||
def on_init(self, state, lvl_map) -> str:
|
||||
result = state[d.DIRT].trigger_dirt_spawn(self.initial_n, self.initial_amount, state,
|
||||
n_var=self.n_var, amount_var=self.amount_var)
|
||||
state.print(f'Initial Dirt was spawned on: {[x.pos for x in state[d.DIRT]]}')
|
||||
return result
|
||||
self.respawn_amount = respawn_amount
|
||||
self.respawn_freq = respawn_freq
|
||||
self._next_dirt_spawn = respawn_freq
|
||||
|
||||
def tick_step(self, state):
|
||||
collection = state[d.DIRT]
|
||||
if self._next_dirt_spawn < 0:
|
||||
pass # No DirtPile Spawn
|
||||
elif not self._next_dirt_spawn:
|
||||
result = [state[d.DIRT].trigger_dirt_spawn(self.respawn_n, self.respawn_amount, state,
|
||||
n_var=self.n_var, amount_var=self.amount_var)]
|
||||
self._next_dirt_spawn = self.spawn_freq
|
||||
result = [collection.trigger_spawn(state, coords_or_quantity=self.respawn_n, amount=self.respawn_amount)]
|
||||
self._next_dirt_spawn = self.respawn_freq
|
||||
else:
|
||||
self._next_dirt_spawn -= 1
|
||||
result = []
|
||||
@ -99,8 +78,8 @@ class EntitiesSmearDirtOnMove(Rule):
|
||||
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):
|
||||
old_pos_dirt = next(iter(old_pos_dirt))
|
||||
if smeared_dirt := round(old_pos_dirt.amount * self.smear_ratio, 2):
|
||||
if state[d.DIRT].spawn(entity.pos, amount=smeared_dirt):
|
||||
results.append(TickResult(identifier=self.name, entity=entity,
|
||||
reward=0, validity=c.VALID))
|
||||
results.append(TickResult(identifier=self.name, entity=entity, validity=c.VALID))
|
||||
return results
|
||||
|
Reference in New Issue
Block a user