SI_2020/warehouse.py
2020-05-07 21:35:24 +02:00

261 lines
10 KiB
Python

from attributes import PackStatus
import random
import queue
from collections import namedtuple
import itertools
Coordinates = namedtuple("Coordinates",'x y')
class CategoryData:
def __init__(self, name, passable=False, can_store=True, location='general'):
self.name = name
self.passable = passable
self.can_store = can_store
self.location = location
def __repr__(self):
return self.name
class Pack:
def __init__(self, size=5, categ='general', lays_on_field=None):
self.size = size
self.category = categ
assert isinstance(lays_on_field, Tile), AssertionError("Attribute lays_on_field must be a Tile object! You know, package cannot lay in void :)")
self.lays_on_field = lays_on_field
self.status = self.set_status()
def set_status(self):
status = PackStatus.LOOSE
if self.lays_on_field.category.name in ['Floor', 'FridgeFloor']:
status = PackStatus.LOOSE
elif self.lays_on_field.category.name in ['Rack', 'Fridge']:
if self.category == 'freezed' and self.lays_on_field.category.name != 'Fridge':
status = PackStatus.STORED_BAD_LOCATION
else:
status = PackStatus.STORED
return status
CATEGORY = {
'floor': CategoryData('Floor', True, False), #lava
'rack': CategoryData('Rack', False, True),
'fridge_floor': CategoryData('FridgeFloor', True, False, location='cold_room'),
'fridge': CategoryData('Fridge', False, True, location='cold_room')
}
class Warehouse:
def __init__(self, width, length, no_of_racks, no_of_packages=0):
self.width = width
self.height = length
self.tiles = self.generate_map()
self.no_of_racks = no_of_racks
self.storage_types = ["Rack", "Fridge"]
self.no_of_packages = no_of_packages
self.generate_racks()
self.open_isolated_areas()
self.create_fridge(10)
self.packages = self.place_packages(no_of_packages)
self.tiles[1][1] = Tile('floor', 1, 1)
def __str__(self):
return "Magazyn {}x{}".format(self.width, self.height)
def generate_map(self):
warehouse_tiles = []
categories = list(CATEGORY.keys())
for x in range(self.width):
row = [Tile('floor', x, y) for y in range(self.height)]
warehouse_tiles.append(row)
return warehouse_tiles
def generate_racks(self):
q = queue.Queue()
rack_counter = 0
node_x, node_y = random.randrange(1, self.width-1), random.randrange(1, self.height-1)
node = self.tiles[node_x][node_y]
next_node = None
self.tiles[node_x][node_y] = Tile('rack', node_x, node_y, capacity=random.randrange(16, 20))
q.put(node)
while not q.empty():
if next_node is not None:
q.put(next_node)
not_rack_nodes = self.get_not_rack_nodes(node_x, node_y)
if len(not_rack_nodes) == 0:
next_node = q.get()
while len(self.get_not_rack_nodes(next_node.x_position, next_node.y_position)) == 0:
if q.empty():
return
next_node = q.get()
else:
next_node = random.choice(not_rack_nodes)
current_node = next_node if next_node is not None else node
self.break_wall(next_node, current_node)
rack_counter += 1
if rack_counter > self.no_of_racks:
return
node_x = next_node.x_position
node_y = next_node.y_position
self.tiles[node_x][node_y] = Tile('rack', node_x, node_y)
def create_fridge(self, size):
x_corner = random.choice(['left', 'right'])
y_corner = random.choice(['top', 'bottom'])
start_x = None
start_y = None
end_x = None
end_y = None
if x_corner == 'left':
start_x = 0
end_x = size
else:
# import pdb
# pdb.set_trace()
start_x = (self.width-1) - size
end_x = self.width - 1
if y_corner == 'top':
start_y = 0
end_y = size
else:
start_y = (self.height-1) - size
end_y = self.height - 1
rows = self.tiles[start_x:end_x].copy()
for num, row in enumerate(rows, start_x):
for index, tile in enumerate(row[start_y:end_y], start_y):
if self.tiles[num][index].category.name == "Floor":
self.tiles[num][index] = Tile('fridge_floor', num, index)
else:
self.tiles[num][index] = Tile('fridge', num, index, capacity=random.randrange(10, 12))
def get_not_rack_nodes(self, node_x, node_y):
adjacent_tiles = self.get_adjacent_tiles(node_x, node_y)
possible_nodes = [line for line in adjacent_tiles if line.category.name != "Rack"]
return possible_nodes
def open_isolated_areas(self):
racks = self.get_all_racks()
for rack in racks:
isolated = self.check_if_isolated(rack.x_position, rack.y_position)
if isolated:
self.tiles[rack.x_position][rack.y_position] = Tile('floor', rack.x_position, rack.y_position)
tmp_tiles = self.tiles
for row_number, row_tiles in enumerate(self.tiles):
if all([(t.category.name == 'Rack') for t in row_tiles]):
puncture_point = random.randrange(1, self.width-1)
tmp_tiles[row_number][puncture_point] = Tile('floor', row_number, puncture_point)
for column in range(self.height):
column_tiles = [r[column] for r in self.tiles]
if all([(t.category.name == 'Rack') for t in column_tiles]):
puncture_point = random.randrange(1, self.height-1)
tmp_tiles[puncture_point][column] = Tile('floor', row_number, puncture_point)
self.files = tmp_tiles
def check_if_isolated(self, x, y):
diagonals = self.get_diagonals(x,y)
diagonal_racks = [t for t in diagonals if t.category.name == 'Rack']
return len(diagonal_racks) > 0
def get_diagonals(self, x, y):
upper_right = Coordinates(
x + 1 if x + 1 < self.width else self.width - 1,
y + 1 if y + 1 < self.height else self.height - 1
)
upper_left = Coordinates(
x - 1 if x - 1 > 0 else 0,
y + 1 if y + 1 < self.height else self.height - 1
)
bottom_right = Coordinates(
x + 1 if x + 1 < self.width else self.width - 1,
y - 1 if y - 1 > 0 else 0
)
bottom_left = Coordinates(
x - 1 if x - 1 > 0 else 0,
y - 1 if y - 1 > 0 else 0
)
diagonals = [
self.tiles[upper_right.x][upper_right.y],
self.tiles[upper_left.x][upper_left.y],
self.tiles[bottom_right.x][bottom_right.y],
self.tiles[bottom_left.x][bottom_left.y]
]
return diagonals
def get_all_racks(self):
""":return list of Tile objects"""
racks = []
for x in self.tiles:
# row_racks = [t for t in self.tiles[x] if t.category.name == 'Rack']
for y in x:
if y.category.name == 'Rack':
racks.append(y)
return racks
def place_packages(self, no_of_packages: int):
packages = []
for i in range(no_of_packages):
new_package_size = random.randrange(1, 10)
pack_x, pack_y = self._set_package_position(new_package_size)
package_field = self.tiles[pack_x][pack_y]
new_package = Pack(lays_on_field=package_field)
categ_seed = random.random()
if categ_seed > 0.8:
new_package.category = "freezed"
# new_package.set_status()
new_package.size = new_package_size
if package_field.category.name in self.storage_types:
package_field.capacity -= new_package.size
packages.append(new_package)
return packages
def _set_package_position(self, pack_size: int):
starting_x, starting_y = random.randrange(self.width), random.randrange(self.height)
while not isinstance(self.tiles[starting_x][starting_y], Tile) \
and self.tiles[starting_x][starting_y].size - pack_size < 0:
starting_x, starting_y = random.randrange(self.width), random.randrange(
self.height)
return starting_x, starting_y
def get_adjacent_tiles(self, x, y):
x_start = x - 2 if x - 2 >= 0 else 0
x_stop = x + 2 if x + 2 < self.width else self.width-1
y_start = y - 2 if y - 2 >= 0 else 0
y_stop = y + 2 if y + 2 < self.height else self.height - 1
adjacent_tiles = [self.tiles[x_start][y], self.tiles[x_stop][y], self.tiles[x][y_start], self.tiles[x][y_stop]]
return adjacent_tiles
def break_wall(self, next_node, current_node):
wall = self.get_wall(next_node, current_node)
self.tiles[wall.x_position][wall.y_position] = Tile("rack", wall.x_position, wall.y_position)
def get_wall(self, next_node, current_node):
x_relative = 0
y_relative = 0
if next_node.x_position > current_node.x_position:
x_relative = 1
elif next_node.x_position < current_node.x_position:
x_relative = -1
if next_node.y_position > current_node.y_position:
y_relative = 1
elif next_node.y_position < current_node.y_position:
y_relative = -1
wall_x = current_node.x_position + x_relative
wall_y = current_node.y_position + y_relative
wall = self.tiles[wall_x][wall_y]
return wall
class Tile:
def __init__(self, category, x_position, y_position, capacity=10):
self.category = CATEGORY.get(category, CATEGORY['floor'])
self.x_position = x_position
self.y_position = y_position
self.capacity = capacity
def __str__(self):
return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position)
def __repr__(self):
return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position)