all vars are properties, removed comments

This commit is contained in:
Chanumask
2023-10-22 12:05:38 +02:00
parent b1fe698d52
commit e87bd3aaa0
19 changed files with 66 additions and 193 deletions

View File

@@ -80,7 +80,7 @@ General:
level_name: rooms # 'double', 'large', 'simple', ... level_name: rooms # 'double', 'large', 'simple', ...
``` ```
... or create your own , maybe with the help of [asciiflow.com](https://asciiflow.com/#/). ... or create your own , maybe with the help of [asciiflow.com](https://asciiflow.com/#/).
Make sure to use `#` as [Walls](marl_factory_grid/environment/entity/wall_floor.py), `-` as free (walkable) [Floor](marl_factory_grid/environment/entity/wall_floor.py)-Tiles, `D` for [Walls](./modules/doors/entities.py). Make sure to use `#` as [Walls](marl_factory_grid/environment/entity/wall.py), `-` as free (walkable) [Floor](marl_factory_grid/environment/entity/wall.py)-Tiles, `D` for [Walls](./modules/doors/entities.py).
Other Entites (define you own) may bring their own `Symbols` Other Entites (define you own) may bring their own `Symbols`
#### Entites #### Entites

View File

@@ -5,10 +5,8 @@ import numpy as np
from .object import Object from .object import Object
from .. import constants as c from .. import constants as c
from .object import EnvObject
from ...utils.utility_classes import RenderEntity
from ...utils.render import RenderEntity
from ...utils.results import ActionResult from ...utils.results import ActionResult
from ...utils.utility_classes import RenderEntity
class Entity(Object, abc.ABC): class Entity(Object, abc.ABC):
@@ -152,7 +150,6 @@ class Entity(Object, abc.ABC):
pos = tuple(pos) pos = tuple(pos)
try: try:
return self.state.entities.pos_dict[pos] return self.state.entities.pos_dict[pos]
# return next(e for e in self if e.pos == pos)
except StopIteration: except StopIteration:
pass pass
except ValueError: except ValueError:

View File

@@ -13,7 +13,7 @@ class Object:
return True return True
@property @property
def var_has_position(self): # brauchen wir das hier jetzt? def var_has_position(self):
try: try:
return self.pos != c.VALUE_NO_POS or False return self.pos != c.VALUE_NO_POS or False
except AttributeError: except AttributeError:

View File

@@ -0,0 +1,31 @@
from typing import List
import numpy as np
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.entity.object import EnvObject
from marl_factory_grid.utils.utility_classes import RenderEntity
from marl_factory_grid.utils import helpers as h
class Wall(Entity):
@property
def var_can_collide(self):
return True
@property
def encoding(self):
return c.VALUE_OCCUPIED_CELL
def render(self):
return RenderEntity(c.WALL, self.pos)
@property
def var_is_blocking_pos(self):
return True
@property
def var_is_blocking_light(self):
return True

View File

@@ -1,121 +0,0 @@
from typing import List
import numpy as np
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.entity.object import EnvObject
from marl_factory_grid.utils.utility_classes import RenderEntity
from marl_factory_grid.utils import helpers as h
class Floor(EnvObject):
@property
def var_has_position(self):
return True
@property
def var_can_collide(self):
return False
@property
def var_can_move(self):
return False
@property
def var_is_blocking_pos(self):
return False
@property
def var_is_blocking_light(self):
return False
@property
def encoding(self):
return c.VALUE_OCCUPIED_CELL
# @property
# def guests_that_can_collide(self):
# return [x for x in self.guests if x.var_can_collide]
@property
def guests(self):
return self._guests.values()
@property
def x(self):
return self.pos[0]
@property
def y(self):
return self.pos[1]
@property
def is_blocked(self):
return any([x.var_is_blocking_pos for x in self.guests])
def __init__(self, pos, **kwargs):
super(Floor, self).__init__(**kwargs)
self._guests = dict()
self.pos = tuple(pos)
self._neighboring_floor: List[Floor] = list()
self._blocked_by = None
def __len__(self):
return len(self._guests)
def is_empty(self):
return not len(self._guests)
def is_occupied(self):
return bool(len(self._guests))
def enter(self, guest, spawn=False):
same_pos = guest.name not in self._guests
not_blocked = not self.is_blocked
no_become_blocked_when_occupied = not (guest.var_is_blocking_pos and self.is_occupied())
not_introduce_collision = not (spawn and guest.var_can_collide and any(x.var_can_collide for x in self.guests))
if same_pos and not_blocked and no_become_blocked_when_occupied and not_introduce_collision:
self._guests.update({guest.name: guest})
return c.VALID
else:
return c.NOT_VALID
def leave(self, guest):
try:
del self._guests[guest.name]
except (ValueError, KeyError):
return c.NOT_VALID
return c.VALID
def __repr__(self):
return f'{self.name}(@{self.pos})'
def summarize_state(self, **_):
return dict(name=self.name, x=int(self.x), y=int(self.y))
def render(self):
return None
class Wall(Entity):
@property
def var_can_collide(self):
return True
@property
def encoding(self):
return c.VALUE_OCCUPIED_CELL
def render(self):
return RenderEntity(c.WALL, self.pos)
@property
def var_is_blocking_pos(self):
return True
@property
def var_is_blocking_light(self):
return True

View File

@@ -4,13 +4,29 @@ from marl_factory_grid.environment.entity.object import EnvObject
class Collection(Objects): class Collection(Objects):
_entity = EnvObject _entity = EnvObject
var_is_blocking_light: bool = False
var_can_collide: bool = False
var_can_move: bool = False
var_has_position: bool = False # alles was posmixin hat true @property
var_has_bound = False # batteries, globalpos, inventories true def var_is_blocking_light(self):
var_can_be_bound: bool = False # == ^ return False
@property
def var_can_collide(self):
return False
@property
def var_can_move(self):
return False
@property
def var_has_position(self):
return False # alles was posmixin hat true
@property
def var_has_bound(self):
return False # batteries, globalpos, inventories true
@property # beide bounds hier? inventory can be bound
def var_can_be_bound(self):
return False
@property @property
def encodings(self): def encodings(self):

View File

@@ -33,9 +33,6 @@ class Entities(Objects):
self.pos_dict = defaultdict(list) self.pos_dict = defaultdict(list)
super().__init__() super().__init__()
# def all_floors(self):
# return[key for key, val in self.pos_dict.items() if any('floor' in x.name.lower() for x in val)]
def guests_that_can_collide(self, pos): def guests_that_can_collide(self, pos):
return[x for val in self.pos_dict[pos] for x in val if x.var_can_collide] return[x for val in self.pos_dict[pos] for x in val if x.var_can_collide]
@@ -91,8 +88,6 @@ class Entities(Objects):
def by_pos(self, pos: (int, int)): def by_pos(self, pos: (int, int)):
return self.pos_dict[pos] return self.pos_dict[pos]
# found_entities = [y for y in (x.by_pos(pos) for x in self.values() if hasattr(x, 'by_pos')) if y is not None]
# return found_entities
@property @property
def positions(self): def positions(self):

View File

@@ -1,10 +1,7 @@
from typing import List, Tuple from typing import List, Tuple
import numpy as np
from marl_factory_grid.environment import constants as c from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.entity import Entity from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.entity.wall_floor import Floor
class PositionMixin: class PositionMixin:
@@ -39,7 +36,6 @@ class PositionMixin:
pos = tuple(pos) pos = tuple(pos)
try: try:
return self.pos_dict[pos] return self.pos_dict[pos]
# return next(e for e in self if e.pos == pos)
except StopIteration: except StopIteration:
pass pass
except ValueError: except ValueError:

View File

@@ -54,7 +54,6 @@ class Objects:
assert self._data[item.name] is None, f'{item.name} allready exists!!!' assert self._data[item.name] is None, f'{item.name} allready exists!!!'
self._data.update({item.name: item}) self._data.update({item.name: item})
item.set_collection(self) item.set_collection(self)
# self.notify_add_entity(item)
for observer in self.observers: for observer in self.observers:
observer.notify_add_entity(item) observer.notify_add_entity(item)
return self return self

View File

@@ -1,10 +1,7 @@
import random
from typing import List, Tuple
from marl_factory_grid.environment import constants as c from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.wall import Wall
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
from marl_factory_grid.environment.groups.mixins import PositionMixin from marl_factory_grid.environment.groups.mixins import PositionMixin
from marl_factory_grid.environment.entity.wall_floor import Wall, Floor
class Walls(PositionMixin, Collection): class Walls(PositionMixin, Collection):
@@ -28,16 +25,3 @@ class Walls(PositionMixin, Collection):
return super().by_pos(pos)[0] return super().by_pos(pos)[0]
except IndexError: except IndexError:
return None return None
class Floors(Walls):
_entity = Floor
symbol = c.SYMBOL_FLOOR
var_is_blocking_light: bool = False
var_can_collide: bool = False
def __init__(self, *args, **kwargs):
super(Floors, self).__init__(*args, **kwargs)
self._value = c.VALUE_FREE_CELL

View File

@@ -1,5 +1,3 @@
from typing import NamedTuple, Union
# Battery Env # Battery Env
CHARGE_PODS = 'ChargePods' CHARGE_PODS = 'ChargePods'
BATTERIES = 'Batteries' BATTERIES = 'Batteries'

View File

@@ -1,10 +1,8 @@
from marl_factory_grid.environment.entity.mixin import BoundEntityMixin
from marl_factory_grid.environment.entity.object import EnvObject, Object
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment import constants as c from marl_factory_grid.environment import constants as c
from marl_factory_grid.utils.utility_classes import RenderEntity from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.entity.object import Object
from marl_factory_grid.modules.batteries import constants as b from marl_factory_grid.modules.batteries import constants as b
from marl_factory_grid.utils.utility_classes import RenderEntity
class Battery(Object): class Battery(Object):
@@ -70,7 +68,6 @@ class Pod(Entity):
def charge_battery(self, battery: Battery): def charge_battery(self, battery: Battery):
if battery.charge_level == 1.0: if battery.charge_level == 1.0:
return c.NOT_VALID return c.NOT_VALID
# if sum(guest for guest in self.tile.guests if 'agent' in guest.name.lower()) > 1:
if sum(1 for key, val in self.state.entities.pos_dict[self.pos] for guest in val if if sum(1 for key, val in self.state.entities.pos_dict[self.pos] for guest in val if
'agent' in guest.name.lower()) > 1: 'agent' in guest.name.lower()) > 1:
return c.NOT_VALID return c.NOT_VALID

View File

@@ -45,10 +45,6 @@ class DirtPiles(PositionMixin, Collection):
def trigger_dirt_spawn(self, state, initial_spawn=False) -> bool: def trigger_dirt_spawn(self, state, initial_spawn=False) -> bool:
free_for_dirt = [x for x in state.entities.floorlist if len(state.entities.pos_dict[x]) == 1 or ( free_for_dirt = [x for x in state.entities.floorlist if len(state.entities.pos_dict[x]) == 1 or (
len(state.entities.pos_dict[x]) == 2 and isinstance(next(y for y in x), DirtPile))] len(state.entities.pos_dict[x]) == 2 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) state.rng.shuffle(free_for_dirt)
var = self.dirt_spawn_r_var var = self.dirt_spawn_r_var

View File

@@ -54,7 +54,7 @@ class DirtSmearOnMove(Rule):
if is_move(entity.state.identifier) and entity.state.validity == c.VALID: 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 old_pos_dirt := state[d.DIRT].by_pos(entity.last_pos):
if smeared_dirt := round(old_pos_dirt.amount * self.smear_amount, 2): if smeared_dirt := round(old_pos_dirt.amount * self.smear_amount, 2):
if state[d.DIRT].spawn(entity.pos, amount=smeared_dirt): # pos statt tile if state[d.DIRT].spawn(entity.pos, amount=smeared_dirt):
results.append(TickResult(identifier=self.name, entity=entity, results.append(TickResult(identifier=self.name, entity=entity,
reward=0, validity=c.VALID)) reward=0, validity=c.VALID))
return results return results

View File

@@ -1,11 +1,10 @@
from collections import defaultdict from collections import defaultdict
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.agent import Agent from marl_factory_grid.environment.entity.agent import Agent
from marl_factory_grid.environment.entity.entity import Entity from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.mixin import BoundEntityMixin
from marl_factory_grid.utils.utility_classes import RenderEntity
from marl_factory_grid.modules.destinations import constants as d from marl_factory_grid.modules.destinations import constants as d
from marl_factory_grid.utils.utility_classes import RenderEntity
class Destination(Entity): class Destination(Entity):

View File

@@ -84,14 +84,12 @@ class Maintainer(Entity):
def _door_is_close(self, state): def _door_is_close(self, state):
state.print("Found a door that is close.") state.print("Found a door that is close.")
try: try:
# return next(y for x in self.tile.neighboring_floor for y in x.guests if do.DOOR in y.name)
return next(y for x in state.entities.neighboring_positions(self.state.pos) for y in state.entities.pos_dict[x] if do.DOOR in y.name) return next(y for x in state.entities.neighboring_positions(self.state.pos) for y in state.entities.pos_dict[x] if do.DOOR in y.name)
except StopIteration: except StopIteration:
return None return None
def _predict_move(self, state): def _predict_move(self, state):
next_pos = self._path[0] next_pos = self._path[0]
# if len(state[c.FLOORS].by_pos(next_pos).guests_that_can_collide) > 0:
if any(x for x in state.entities.pos_dict[next_pos] if x.var_can_collide) > 0: if any(x for x in state.entities.pos_dict[next_pos] if x.var_can_collide) > 0:
action = c.NOOP action = c.NOOP
else: else:

View File

@@ -1,14 +1,10 @@
from typing import List
from .entities import Maintainer
from marl_factory_grid.environment.entity.wall_floor import Floor
from marl_factory_grid.environment.groups.collection import Collection from marl_factory_grid.environment.groups.collection import Collection
from marl_factory_grid.environment.groups.mixins import PositionMixin from marl_factory_grid.environment.groups.mixins import PositionMixin
from .entities import Maintainer
from ..machines import constants as mc
from ..machines.actions import MachineAction from ..machines.actions import MachineAction
from ...utils.states import Gamestate from ...utils.states import Gamestate
from ..machines import constants as mc
class Maintainers(PositionMixin, Collection): class Maintainers(PositionMixin, Collection):

View File

@@ -1,12 +1,7 @@
import random import random
from typing import List, Tuple from typing import List, Tuple
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.environment.entity.object import Object from marl_factory_grid.environment.entity.object import Object
from marl_factory_grid.utils.utility_classes import RenderEntity
from marl_factory_grid.environment import constants as c
from marl_factory_grid.modules.doors import constants as d
class Zone(Object): class Zone(Object):

View File

@@ -57,7 +57,7 @@ class Gamestate(object):
@property @property
def moving_entites(self): def moving_entites(self):
return [y for x in self.entities for y in x if x.var_can_move] # wird das aus dem String gelesen? return [y for x in self.entities for y in x if x.var_can_move]
def __init__(self, entities, agents_conf, rules: Dict[str, dict], env_seed=69, verbose=False): def __init__(self, entities, agents_conf, rules: Dict[str, dict], env_seed=69, verbose=False):
self.entities = entities self.entities = entities
@@ -110,15 +110,12 @@ class Gamestate(object):
results.extend(on_check_done_result) results.extend(on_check_done_result)
return results return results
def get_all_pos_with_collisions(self) -> List[Tuple[(int, int)]]: def get_all_pos_with_collisions(self) -> List[Tuple[(int, int)]]:
positions = [pos for pos, entity_list_for_position in self.entities.pos_dict.items() positions = [pos for pos, entity_list_for_position in self.entities.pos_dict.items()
if any([e.var_can_collide for e in entity_list_for_position])] if any([e.var_can_collide for e in entity_list_for_position])]
return positions return positions
def check_move_validity(self, moving_entity, position): def check_move_validity(self, moving_entity, position):
# if (guest.name not in self._guests and not self.is_blocked)
# and not (guest.var_is_blocking_pos and self.is_occupied()):
if moving_entity.pos != position and not any( if moving_entity.pos != position and not any(
entity.var_is_blocking_pos for entity in self.entities.pos_dict[position]) and not ( entity.var_is_blocking_pos for entity in self.entities.pos_dict[position]) and not (
moving_entity.var_is_blocking_pos and self.entities.is_occupied(position)): moving_entity.var_is_blocking_pos and self.entities.is_occupied(position)):