fix mismatching signatures of spawn

This commit is contained in:
Chanumask
2023-10-27 17:46:13 +02:00
parent dd5737e3ff
commit 115a79e930
11 changed files with 38 additions and 42 deletions

View File

@@ -69,6 +69,7 @@ General:
verbose: false verbose: false
Rules: Rules:
SpawnAgents: {}
BtryDoneAtDischarge: {} BtryDoneAtDischarge: {}
Collision: Collision:
done_at_collisions: false done_at_collisions: false

View File

@@ -1,8 +1,9 @@
from typing import List, Tuple from typing import List, Tuple, Union
from marl_factory_grid.environment.entity.entity import Entity 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 _Object from marl_factory_grid.environment.entity.object import _Object
import marl_factory_grid.environment.constants as c
class Collection(_Objects): class Collection(_Objects):
@@ -40,6 +41,18 @@ class Collection(_Objects):
super(Collection, self).__init__(*args, **kwargs) super(Collection, self).__init__(*args, **kwargs)
self.size = size self.size = size
def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args): # woihn mit den args
if isinstance(coords_or_quantity, int):
self.add_items([self._entity() for _ in range(coords_or_quantity)])
else:
self.add_items([self._entity(pos) for pos in coords_or_quantity])
return c.VALID
def despawn(self, items: List[_Object]):
items = [items] if isinstance(items, _Object) else items
for item in items:
del self[item]
def add_item(self, item: Entity): def add_item(self, item: Entity):
assert self.var_has_position or (len(self) <= self.size) assert self.var_has_position or (len(self) <= self.size)
super(Collection, self).add_item(item) super(Collection, self).add_item(item)
@@ -67,9 +80,6 @@ class Collection(_Objects):
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): def render(self):
return [y for y in [x.render() for x in self] if y is not None] return [y for y in [x.render() for x in self] if y is not None]

View File

@@ -125,26 +125,6 @@ class _Objects:
repr_dict = {key: val for key, val in self._data.items() if key not in [c.WALLS]} repr_dict = {key: val for key, val in self._data.items() if key not in [c.WALLS]}
return f'{self.__class__.__name__}[{repr_dict}]' return f'{self.__class__.__name__}[{repr_dict}]'
def spawn(self, n: int):
self.add_items([self._entity() for _ in range(n)])
return c.VALID
def despawn(self, items: List[_Object]):
items = [items] if isinstance(items, _Object) else items
for item in items:
del self[item]
# def notify_change_pos(self, entity: object):
# try:
# self.pos_dict[entity.last_pos].remove(entity)
# except (ValueError, AttributeError):
# pass
# if entity.var_has_position:
# try:
# self.pos_dict[entity.pos].append(entity)
# except (ValueError, AttributeError):
# pass
def notify_del_entity(self, entity: _Object): def notify_del_entity(self, entity: _Object):
try: try:
entity.del_observer(self) entity.del_observer(self)

View File

@@ -15,14 +15,6 @@ class Walls(Collection):
super(Walls, self).__init__(*args, **kwargs) super(Walls, self).__init__(*args, **kwargs)
self._value = c.VALUE_OCCUPIED_CELL self._value = c.VALUE_OCCUPIED_CELL
#ToDo: Do we need this? Move to spawn methode?
# @classmethod
# def from_coordinates(cls, argwhere_coordinates, *args, **kwargs):
# tiles = cls(*args, **kwargs)
# # noinspection PyTypeChecker
# tiles.add_items([cls._entity(pos) for pos in argwhere_coordinates])
# return tiles
def by_pos(self, pos: (int, int)): def by_pos(self, pos: (int, int)):
try: try:
return super().by_pos(pos)[0] return super().by_pos(pos)[0]

View File

@@ -1,9 +1,10 @@
from typing import Union, List, Tuple
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
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
@property @property
@@ -33,9 +34,14 @@ class Batteries(Collection):
batteries = [self._entity(initial_charge_level, agent) for _, agent in enumerate(agents)] batteries = [self._entity(initial_charge_level, agent) for _, agent in enumerate(agents)]
self.add_items(batteries) self.add_items(batteries)
# def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args): hat keine pos
# agents = entity_args[0]
# initial_charge_level = entity_args[1]
# batteries = [self._entity(initial_charge_level, agent) for _, agent in enumerate(agents)]
# self.add_items(batteries)
class ChargePods(Collection): class ChargePods(Collection):
_entity = Pod _entity = Pod
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@@ -49,7 +49,7 @@ class BatteryDecharge(Rule):
self.per_action_costs = per_action_costs self.per_action_costs = per_action_costs
self.initial_charge = initial_charge self.initial_charge = initial_charge
def on_init(self, state, lvl_map): def on_init(self, state, lvl_map): # on reset?
assert len(state[c.AGENT]), "There are no agents, did you already spawn them?" assert len(state[c.AGENT]), "There are no agents, did you already spawn them?"
state[b.BATTERIES].spawn(state[c.AGENT], self.initial_charge) state[b.BATTERIES].spawn(state[c.AGENT], self.initial_charge)

View File

@@ -1,3 +1,5 @@
from typing import Union, List, Tuple
from marl_factory_grid.environment import constants as c from marl_factory_grid.environment import constants as c
from marl_factory_grid.utils.results import Result from marl_factory_grid.utils.results import Result
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
@@ -36,9 +38,10 @@ class DirtPiles(Collection):
self.max_global_amount = max_global_amount self.max_global_amount = max_global_amount
self.max_local_amount = max_local_amount self.max_local_amount = max_local_amount
def spawn(self, then_dirty_positions, amount_s) -> Result: def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args):
amount_s = entity_args[0]
spawn_counter = 0 spawn_counter = 0
for idx, pos in enumerate(then_dirty_positions): for idx, pos in enumerate(coords_or_quantity):
if not self.amount > self.max_global_amount: if not self.amount > self.max_global_amount:
amount = amount_s[idx] if isinstance(amount_s, list) else amount_s amount = amount_s[idx] if isinstance(amount_s, list) else amount_s
if dirt := self.by_pos(pos): if dirt := self.by_pos(pos):

View File

@@ -1,3 +1,5 @@
from typing import Union, List, Tuple
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
from .entitites import Machine from .entitites import Machine
@@ -21,3 +23,4 @@ class Machines(Collection):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Machines, self).__init__(*args, **kwargs) super(Machines, self).__init__(*args, **kwargs)

View File

@@ -13,8 +13,7 @@ class MachineRule(Rule):
self.n_machines = n_machines self.n_machines = n_machines
def on_init(self, state, lvl_map): def on_init(self, state, lvl_map):
# TODO Move to spawn!!! state[m.MACHINES].spawn(state.entities.empty_positions())
state[m.MACHINES].add_items(Machine(pos) for pos in state.entities.empty_positions())
def tick_pre_step(self, state) -> List[TickResult]: def tick_pre_step(self, state) -> List[TickResult]:
pass pass

View File

@@ -1,3 +1,5 @@
from typing import Union, List, Tuple
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
from .entities import Maintainer from .entities import Maintainer
from ..machines import constants as mc from ..machines import constants as mc
@@ -27,5 +29,6 @@ class Maintainers(Collection):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def spawn(self, position, state: Gamestate): def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args):
self.add_items([self._entity(state, mc.MACHINES, MachineAction(), pos) for pos in position]) state = entity_args[0]
self.add_items([self._entity(state, mc.MACHINES, MachineAction(), pos) for pos in coords_or_quantity])

View File

@@ -14,7 +14,6 @@ class MaintenanceRule(Rule):
self.n_maintainer = n_maintainer self.n_maintainer = n_maintainer
def on_init(self, state: Gamestate, lvl_map): def on_init(self, state: Gamestate, lvl_map):
# Move to spawn? : #TODO
state[M.MAINTAINERS].spawn(state.entities.empty_positions[:self.n_maintainer], state) state[M.MAINTAINERS].spawn(state.entities.empty_positions[:self.n_maintainer], state)
pass pass