mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-09-13 22:44:00 +02:00
fix mismatching signatures of spawn
This commit is contained in:
@@ -69,6 +69,7 @@ General:
|
||||
verbose: false
|
||||
|
||||
Rules:
|
||||
SpawnAgents: {}
|
||||
BtryDoneAtDischarge: {}
|
||||
Collision:
|
||||
done_at_collisions: false
|
||||
|
@@ -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.groups.objects import _Objects
|
||||
from marl_factory_grid.environment.entity.object import _Object
|
||||
import marl_factory_grid.environment.constants as c
|
||||
|
||||
|
||||
class Collection(_Objects):
|
||||
@@ -40,6 +41,18 @@ class Collection(_Objects):
|
||||
super(Collection, self).__init__(*args, **kwargs)
|
||||
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):
|
||||
assert self.var_has_position or (len(self) <= self.size)
|
||||
super(Collection, self).add_item(item)
|
||||
@@ -67,9 +80,6 @@ class Collection(_Objects):
|
||||
except (StopIteration, AttributeError):
|
||||
return None
|
||||
|
||||
def spawn(self, coords: List[Tuple[(int, int)]]):
|
||||
self.add_items([self._entity(pos) for pos in coords])
|
||||
|
||||
def render(self):
|
||||
return [y for y in [x.render() for x in self] if y is not None]
|
||||
|
||||
|
@@ -125,26 +125,6 @@ class _Objects:
|
||||
repr_dict = {key: val for key, val in self._data.items() if key not in [c.WALLS]}
|
||||
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):
|
||||
try:
|
||||
entity.del_observer(self)
|
||||
|
@@ -15,14 +15,6 @@ class Walls(Collection):
|
||||
super(Walls, self).__init__(*args, **kwargs)
|
||||
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)):
|
||||
try:
|
||||
return super().by_pos(pos)[0]
|
||||
|
@@ -1,9 +1,10 @@
|
||||
from typing import Union, List, Tuple
|
||||
|
||||
from marl_factory_grid.environment.groups.collection import Collection
|
||||
from marl_factory_grid.modules.batteries.entitites import Pod, Battery
|
||||
|
||||
|
||||
class Batteries(Collection):
|
||||
|
||||
_entity = Battery
|
||||
|
||||
@property
|
||||
@@ -33,9 +34,14 @@ class Batteries(Collection):
|
||||
batteries = [self._entity(initial_charge_level, agent) for _, agent in enumerate(agents)]
|
||||
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):
|
||||
|
||||
_entity = Pod
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@@ -49,7 +49,7 @@ class BatteryDecharge(Rule):
|
||||
self.per_action_costs = per_action_costs
|
||||
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?"
|
||||
state[b.BATTERIES].spawn(state[c.AGENT], self.initial_charge)
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
from typing import Union, List, Tuple
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.utils.results import Result
|
||||
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_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
|
||||
for idx, pos in enumerate(then_dirty_positions):
|
||||
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
|
||||
if dirt := self.by_pos(pos):
|
||||
|
@@ -1,3 +1,5 @@
|
||||
from typing import Union, List, Tuple
|
||||
|
||||
from marl_factory_grid.environment.groups.collection import Collection
|
||||
|
||||
from .entitites import Machine
|
||||
@@ -21,3 +23,4 @@ class Machines(Collection):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Machines, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@@ -13,8 +13,7 @@ class MachineRule(Rule):
|
||||
self.n_machines = n_machines
|
||||
|
||||
def on_init(self, state, lvl_map):
|
||||
# TODO Move to spawn!!!
|
||||
state[m.MACHINES].add_items(Machine(pos) for pos in state.entities.empty_positions())
|
||||
state[m.MACHINES].spawn(state.entities.empty_positions())
|
||||
|
||||
def tick_pre_step(self, state) -> List[TickResult]:
|
||||
pass
|
||||
|
@@ -1,3 +1,5 @@
|
||||
from typing import Union, List, Tuple
|
||||
|
||||
from marl_factory_grid.environment.groups.collection import Collection
|
||||
from .entities import Maintainer
|
||||
from ..machines import constants as mc
|
||||
@@ -27,5 +29,6 @@ class Maintainers(Collection):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def spawn(self, position, state: Gamestate):
|
||||
self.add_items([self._entity(state, mc.MACHINES, MachineAction(), pos) for pos in position])
|
||||
def spawn(self, coords_or_quantity: Union[int, List[Tuple[(int, int)]]], *entity_args):
|
||||
state = entity_args[0]
|
||||
self.add_items([self._entity(state, mc.MACHINES, MachineAction(), pos) for pos in coords_or_quantity])
|
||||
|
@@ -14,7 +14,6 @@ class MaintenanceRule(Rule):
|
||||
self.n_maintainer = n_maintainer
|
||||
|
||||
def on_init(self, state: Gamestate, lvl_map):
|
||||
# Move to spawn? : #TODO
|
||||
state[M.MAINTAINERS].spawn(state.entities.empty_positions[:self.n_maintainer], state)
|
||||
pass
|
||||
|
||||
|
Reference in New Issue
Block a user