Items and combination of item and dirt

This commit is contained in:
Steffen Illium
2021-08-23 09:51:35 +02:00
parent 244d4eed68
commit d5e4d44823
12 changed files with 647 additions and 445 deletions

View File

@ -5,58 +5,76 @@ from typing import Tuple, Union
import numpy as np
from pathlib import Path
# Constants
class Constants(Enum):
WALL = '#'
DOOR = 'D'
DANGER_ZONE = 'x'
LEVEL = 'level'
AGENT = 'Agent'
FREE_CELL = 0
OCCUPIED_CELL = 1
DOORS = 'doors'
CLOSED_DOOR = 1
OPEN_DOOR = -1
ACTION = auto()
COLLISIONS = auto()
VALID = True
NOT_VALID = False
def __bool__(self):
return bool(self.value)
LEVELS_DIR = 'levels'
TO_BE_AVERAGED = ['dirt_amount', 'dirty_tiles']
IGNORED_DF_COLUMNS = ['Episode', 'Run', 'train_step', 'step', 'index', 'dirt_amount',
'dirty_tile_count', 'terminal_observation', 'episode']
MANHATTAN_MOVES = ['north', 'east', 'south', 'west']
DIAGONAL_MOVES = ['north_east', 'south_east', 'south_west', 'north_west']
NO_POS = (-9999, -9999)
# Constants
class Constants(Enum):
WALL = '#'
DOOR = 'D'
DANGER_ZONE = 'x'
LEVEL = 'level'
AGENT = 'Agent'
FREE_CELL = 0
OCCUPIED_CELL = 1
NO_POS = (-9999, -9999)
ACTIONMAP = defaultdict(lambda: (0, 0), dict(north=(-1, 0), east=(0, 1),
south=(1, 0), west=(0, -1),
north_east=(-1, +1), south_east=(1, 1),
south_west=(+1, -1), north_west=(-1, -1)
)
DOORS = 'doors'
CLOSED_DOOR = 1
OPEN_DOOR = -1
ACTION = auto()
COLLISIONS = auto()
VALID = True
NOT_VALID = False
# Dirt Env
DIRT = 'dirt'
# Item Env
ITEM = 'item'
INVENTORY = 'inventory'
def __bool__(self):
return bool(self.value)
class ManhattanMoves(Enum):
NORTH = 'north'
EAST = 'east'
SOUTH = 'south'
WEST = 'west'
class DiagonalMoves(Enum):
NORTHEAST = 'north_east'
SOUTHEAST = 'south_east'
SOUTHWEST = 'south_west'
NORTHWEST = 'north_west'
class EnvActions(Enum):
NOOP = 'no_op'
USE_DOOR = 'use_door'
CLEAN_UP = 'clean_up'
ITEM_ACTION = 'item_action'
d = DiagonalMoves
m = ManhattanMoves
c = Constants
ACTIONMAP = defaultdict(lambda: (0, 0), {m.NORTH.name: (-1, 0), d.NORTHEAST.name: (-1, +1),
m.EAST.name: (0, 1), d.SOUTHEAST.name: (1, 1),
m.SOUTH.name: (1, 0), d.SOUTHWEST.name: (+1, -1),
m.WEST.name: (0, -1), d.NORTHWEST.name: (-1, -1)
}
)
HORIZONTAL_DOOR_MAP = np.asarray([[0, 0, 0], [1, 0, 1], [0, 0, 0]])
VERTICAL_DOOR_MAP = np.asarray([[0, 1, 0], [0, 0, 0], [0, 1, 0]])
HORIZONTAL_DOOR_ZONE_1 = np.asarray([[1, 1, 1], [0, 0, 0], [0, 0, 0]])
HORIZONTAL_DOOR_ZONE_2 = np.asarray([[0, 0, 0], [0, 0, 0], [1, 1, 1]])
VERTICAL_DOOR_ZONE_1 = np.asarray([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
VERTICAL_DOOR_ZONE_2 = np.asarray([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
# Utility functions
def parse_level(path):
@ -67,13 +85,13 @@ def parse_level(path):
return level
def one_hot_level(level, wall_char: Union[Constants, str] = Constants.WALL):
def one_hot_level(level, wall_char: Union[c, str] = c.WALL):
grid = np.array(level)
binary_grid = np.zeros(grid.shape, dtype=np.int8)
if wall_char in Constants:
binary_grid[grid == wall_char.value] = Constants.OCCUPIED_CELL.value
if wall_char in c:
binary_grid[grid == wall_char.value] = c.OCCUPIED_CELL.value
else:
binary_grid[grid == wall_char] = Constants.OCCUPIED_CELL.value
binary_grid[grid == wall_char] = c.OCCUPIED_CELL.value
return binary_grid
@ -89,7 +107,22 @@ def check_position(slice_to_check_against: np.ndarray, position_to_check: Tuple[
# Check for collision with level walls
valid = valid and not slice_to_check_against[x_pos, y_pos]
return Constants.VALID if valid else Constants.NOT_VALID
return c.VALID if valid else c.NOT_VALID
def asset_str(agent):
# What does this abonimation do?
# if any([x is None for x in [self._slices[j] for j in agent.collisions]]):
# print('error')
col_names = [x.name for x in agent.temp_collisions]
if c.AGENT.value in col_names:
return 'agent_collision', 'blank'
elif not agent.temp_valid or c.LEVEL.name in col_names or c.AGENT.name in col_names:
return c.AGENT.value, 'invalid'
elif agent.temp_valid:
return c.AGENT.value, 'valid'
else:
return c.AGENT.value, 'idle'
if __name__ == '__main__':