2020-04-05 15:26:28 +02:00
|
|
|
from attributes import PackSize, PackStatus
|
2020-04-02 15:25:15 +02:00
|
|
|
import random
|
2020-04-04 22:03:15 +02:00
|
|
|
import queue
|
2020-04-05 16:49:45 +02:00
|
|
|
from collections import namedtuple
|
2020-04-04 22:03:15 +02:00
|
|
|
import itertools
|
2020-04-05 15:04:38 +02:00
|
|
|
|
2020-04-05 16:49:45 +02:00
|
|
|
Coordinates = namedtuple("Coordinates",'x y')
|
|
|
|
|
2020-04-02 15:25:15 +02:00
|
|
|
class CategoryData:
|
|
|
|
def __init__(self, name, passable=False, can_store=True, location='general', pack_size=PackSize.ALL):
|
|
|
|
self.name = name
|
|
|
|
self.passable = passable
|
|
|
|
self.can_store = can_store
|
|
|
|
self.location = location
|
|
|
|
self.pack_size = pack_size
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Pack:
|
2020-04-05 15:04:38 +02:00
|
|
|
def __init__(self, size=PackSize.MEDIUM, categ='general', lays_on_field=None):
|
2020-04-02 15:25:15 +02:00
|
|
|
self.size = size
|
|
|
|
self.category = categ
|
2020-04-05 15:26:28 +02:00
|
|
|
assert isinstance(lays_on_field, Tile), AssertionError("Attribute lays_on_field must be a Tile object! You know, package cannot lay in void :)")
|
2020-04-05 15:04:38 +02:00
|
|
|
self.lays_on_field = lays_on_field
|
2020-04-05 15:26:28 +02:00
|
|
|
self.status = self.set_status()
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-05 15:26:28 +02:00
|
|
|
def set_status(self):
|
|
|
|
status = PackStatus.LOOSE
|
|
|
|
if self.lays_on_field.category.name == 'Floor':
|
|
|
|
status = PackStatus.LOOSE
|
|
|
|
elif self.lays_on_field.category.name == 'Rack':
|
|
|
|
status = PackStatus.STORED
|
|
|
|
return status
|
2020-04-02 15:25:15 +02:00
|
|
|
|
|
|
|
CATEGORY = {
|
|
|
|
'floor': CategoryData('Floor', True, False), #lava
|
|
|
|
'rack': CategoryData('Rack', False, True),
|
|
|
|
# 'freezer': CategoryData('Freezer', False, True, location='cold_room')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Warehouse:
|
2020-04-05 15:04:38 +02:00
|
|
|
def __init__(self, width, length, no_of_racks, no_of_packages=0):
|
2020-04-02 15:25:15 +02:00
|
|
|
self.width = width
|
2020-04-04 22:03:15 +02:00
|
|
|
self.height = length
|
2020-04-02 15:25:15 +02:00
|
|
|
self.tiles = self.generate_map()
|
2020-04-05 15:04:38 +02:00
|
|
|
self.no_of_racks = no_of_racks
|
|
|
|
self.no_of_packages = no_of_packages
|
2020-04-04 22:03:15 +02:00
|
|
|
self.generate_racks()
|
2020-04-05 16:49:45 +02:00
|
|
|
self.open_isolated_areas()
|
2020-04-05 15:04:38 +02:00
|
|
|
self.packages = self.place_packages(no_of_packages)
|
2020-04-28 01:05:52 +02:00
|
|
|
self.tiles[1][1] = Tile('floor', 1, 1)
|
2020-04-02 15:25:15 +02:00
|
|
|
def __str__(self):
|
2020-04-04 22:03:15 +02:00
|
|
|
return "Magazyn {}x{}".format(self.width, self.height)
|
2020-04-02 15:25:15 +02:00
|
|
|
|
|
|
|
def generate_map(self):
|
|
|
|
warehouse_tiles = []
|
|
|
|
categories = list(CATEGORY.keys())
|
|
|
|
for x in range(self.width):
|
2020-04-04 22:03:15 +02:00
|
|
|
row = [Tile('floor', x, y) for y in range(self.height)]
|
2020-04-02 15:25:15 +02:00
|
|
|
warehouse_tiles.append(row)
|
|
|
|
return warehouse_tiles
|
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def generate_racks(self):
|
|
|
|
q = queue.Queue()
|
2020-04-05 15:04:38 +02:00
|
|
|
rack_counter = 0
|
2020-04-04 22:03:15 +02:00
|
|
|
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)
|
|
|
|
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)
|
2020-04-05 15:04:38 +02:00
|
|
|
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
|
2020-04-04 22:03:15 +02:00
|
|
|
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 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
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-05 16:49:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
def place_packages(self, no_of_packages):
|
|
|
|
packages = []
|
|
|
|
for i in range(no_of_packages):
|
|
|
|
pack_x, pack_y = self._set_package_position()
|
|
|
|
package_field = self.tiles[pack_x][pack_y]
|
|
|
|
new_package = Pack(lays_on_field=package_field)
|
|
|
|
packages.append(new_package)
|
|
|
|
return packages
|
|
|
|
def _set_package_position(self):
|
|
|
|
starting_x, starting_y = random.randrange(self.width), random.randrange(self.height)
|
2020-04-05 15:26:28 +02:00
|
|
|
while not isinstance(self.tiles[starting_x][starting_y], Tile):
|
2020-04-05 15:04:38 +02:00
|
|
|
starting_x, starting_y = random.randrange(self.width), random.randrange(
|
|
|
|
self.height)
|
|
|
|
return starting_x, starting_y
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
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
|
2020-04-05 15:04:38 +02:00
|
|
|
|
2020-04-02 15:25:15 +02:00
|
|
|
class Tile:
|
|
|
|
def __init__(self, category, x_position, y_position):
|
|
|
|
self.category = CATEGORY.get(category, CATEGORY['floor'])
|
|
|
|
self.x_position = x_position
|
|
|
|
self.y_position = y_position
|
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
|
2020-04-02 15:25:15 +02:00
|
|
|
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)
|