2020-04-23 19:43:51 +02:00
|
|
|
from warehouse import Coordinates, Tile, Pack
|
2020-04-28 22:03:48 +02:00
|
|
|
from queue import PriorityQueue
|
|
|
|
from math import sqrt
|
2020-05-01 01:08:08 +02:00
|
|
|
from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus, PackSize
|
2020-05-01 01:19:23 +02:00
|
|
|
import pygame
|
|
|
|
import sys
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-27 22:14:07 +02:00
|
|
|
class Node:
|
2020-05-01 01:08:08 +02:00
|
|
|
def __init__(self, coord_x, coord_y, package=None, is_rack=False):
|
2020-04-27 22:14:07 +02:00
|
|
|
self.x = coord_x
|
|
|
|
self.y = coord_y
|
|
|
|
self.parent = None
|
2020-05-01 01:08:08 +02:00
|
|
|
self.package = package
|
|
|
|
self.is_rack = is_rack
|
2020-04-27 22:14:07 +02:00
|
|
|
self.g_cost = 0
|
|
|
|
self.h_cost = 0
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Node):
|
|
|
|
return self.x == other.x and self.y == self.y
|
|
|
|
return False
|
2020-04-28 22:03:48 +02:00
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return isinstance(other, Node) and self.g_cost < other.g_cost
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Node:{}x{}".format(self.x, self.y)
|
2020-04-28 22:07:58 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
class Agent:
|
2020-04-27 22:14:07 +02:00
|
|
|
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
|
|
|
self.x = start_x
|
|
|
|
self.y = start_y
|
|
|
|
self.radius = radius
|
|
|
|
self.warehouse = assigned_warehouse
|
|
|
|
self.is_loaded = False
|
|
|
|
self.transported_package = None
|
2020-04-29 00:50:38 +02:00
|
|
|
self.direction = "up"
|
2020-04-28 00:41:57 +02:00
|
|
|
self.dest = Node(1, 1)
|
2020-04-27 22:14:07 +02:00
|
|
|
self.closed = list()
|
2020-04-28 22:03:48 +02:00
|
|
|
self.open = PriorityQueue()
|
2020-04-27 22:14:07 +02:00
|
|
|
self.path = list()
|
2020-04-28 22:03:48 +02:00
|
|
|
|
|
|
|
|
2020-04-27 22:14:07 +02:00
|
|
|
def find_path(self):
|
2020-04-28 22:03:48 +02:00
|
|
|
self.closed = []
|
2020-05-01 01:08:08 +02:00
|
|
|
self.path = []
|
2020-04-28 22:03:48 +02:00
|
|
|
self.open = PriorityQueue()
|
2020-05-01 01:08:08 +02:00
|
|
|
if self.is_loaded:
|
|
|
|
rack = self.find_nearest_rack_for(self.transported_package)
|
|
|
|
self.dest = Node(rack.x_position, rack.y_position, is_rack=True)
|
|
|
|
else:
|
|
|
|
package = self.find_nearest_package()
|
|
|
|
self.dest = Node(package.lays_on_field.x_position, package.lays_on_field.y_position, package=package)
|
|
|
|
|
2020-04-27 22:14:07 +02:00
|
|
|
start_node = Node(self.x, self.y)
|
2020-05-01 01:08:08 +02:00
|
|
|
|
2020-04-28 22:03:48 +02:00
|
|
|
self.open.put((0, start_node))
|
2020-04-27 22:14:07 +02:00
|
|
|
while self.open:
|
2020-04-28 22:03:48 +02:00
|
|
|
_, current_node = self.open.get()
|
2020-04-27 22:14:07 +02:00
|
|
|
self.closed.append(current_node)
|
|
|
|
if current_node.x == self.dest.x and current_node.y == self.dest.y:
|
2020-04-28 23:12:57 +02:00
|
|
|
while current_node.x != start_node.x or current_node.y != start_node.y:
|
2020-04-28 00:41:57 +02:00
|
|
|
self.path.append(current_node)
|
2020-04-27 22:14:07 +02:00
|
|
|
current_node = current_node.parent
|
|
|
|
return True
|
|
|
|
neighbour_list = self.get_neighbours(current_node)
|
|
|
|
for neighbour in neighbour_list:
|
2020-04-28 22:03:48 +02:00
|
|
|
cost = current_node.g_cost + self.heuristic(current_node, neighbour)
|
|
|
|
if self.check_if_closed(neighbour):
|
|
|
|
continue
|
|
|
|
if self.check_if_open(neighbour):
|
|
|
|
if neighbour.g_cost > cost:
|
2020-04-28 00:41:57 +02:00
|
|
|
neighbour.g_cost = cost
|
|
|
|
neighbour.parent = current_node
|
2020-04-28 22:03:48 +02:00
|
|
|
else:
|
|
|
|
neighbour.g_cost = cost
|
|
|
|
neighbour.h_cost = self.heuristic(neighbour, self.dest)
|
|
|
|
neighbour.parent = current_node
|
|
|
|
self.open.put((neighbour.g_cost, neighbour))
|
2020-04-27 22:14:07 +02:00
|
|
|
return False
|
2020-04-28 23:12:57 +02:00
|
|
|
|
2020-04-29 00:50:38 +02:00
|
|
|
def turn_left(self):
|
|
|
|
new_direction = TURN_LEFT_DIRECTIONS.get(self.direction, self.direction)
|
|
|
|
self.direction = new_direction
|
|
|
|
|
|
|
|
def turn_right(self):
|
|
|
|
new_direction = TURN_RIGHT_DIRECTIONS.get(self.direction, self.direction)
|
|
|
|
self.direction = new_direction
|
|
|
|
|
2020-04-28 23:12:57 +02:00
|
|
|
def heuristic(self, start: Node, goal: Node):
|
|
|
|
diff_x = pow(goal.x - start.x, 2)
|
|
|
|
diff_y = pow(goal.y - start.y, 2)
|
2020-04-28 22:03:48 +02:00
|
|
|
return round(sqrt(diff_x + diff_y), 3)
|
2020-04-28 00:41:57 +02:00
|
|
|
|
2020-04-28 22:03:48 +02:00
|
|
|
def check_if_open(self, node: Node):
|
|
|
|
return (node.x, node.y) in [(n.x, n.y) for (_,n) in self.open.queue]
|
|
|
|
|
|
|
|
def check_if_closed(self, node: Node):
|
|
|
|
return (node.x, node.y) in [(n.x, n.y) for n in self.closed]
|
|
|
|
|
2020-04-27 22:14:07 +02:00
|
|
|
def get_neighbours(self, node: Node):
|
2020-04-28 00:41:57 +02:00
|
|
|
neighbours = []
|
|
|
|
if self.check_if_can_move(Coordinates(node.x + 1, node.y)):
|
|
|
|
neighbours.append(Node(node.x + 1, node.y))
|
|
|
|
if self.check_if_can_move(Coordinates(node.x - 1, node.y)):
|
|
|
|
neighbours.append(Node(node.x - 1, node.y))
|
|
|
|
if self.check_if_can_move(Coordinates(node.x, node.y + 1)):
|
|
|
|
neighbours.append(Node(node.x, node.y + 1))
|
|
|
|
if self.check_if_can_move(Coordinates(node.x, node.y - 1)):
|
|
|
|
neighbours.append(Node(node.x, node.y - 1))
|
2020-04-27 22:14:07 +02:00
|
|
|
return neighbours
|
|
|
|
|
|
|
|
def move(self):
|
2020-05-01 01:08:08 +02:00
|
|
|
dest_coords = (self.dest.x, self.dest.y)
|
2020-04-27 22:14:07 +02:00
|
|
|
if not self.path:
|
|
|
|
if not self.find_path():
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
next = self.path.pop()
|
2020-04-30 16:36:55 +02:00
|
|
|
star_dir = self.direction
|
|
|
|
if self.x > next.x and not self.direction == 'left':
|
|
|
|
if self.direction == 'down':
|
|
|
|
self.turn_right()
|
|
|
|
else:
|
|
|
|
self.turn_left()
|
|
|
|
elif self.x < next.x and not self.direction == 'right':
|
|
|
|
if self.direction == 'down':
|
|
|
|
self.turn_left()
|
|
|
|
else:
|
|
|
|
self.turn_right()
|
|
|
|
elif self.y > next.y and not self.direction == 'up':
|
|
|
|
if self.direction == 'left':
|
|
|
|
self.turn_right()
|
|
|
|
else:
|
|
|
|
self.turn_left()
|
|
|
|
elif self.y < next.y and not self.direction == 'down':
|
|
|
|
if self.direction == 'right':
|
|
|
|
self.turn_right()
|
|
|
|
else:
|
|
|
|
self.turn_left()
|
2020-05-01 13:28:10 +02:00
|
|
|
|
|
|
|
if (next.x, next.y) == dest_coords:
|
|
|
|
if self.dest.package:
|
|
|
|
self.pick_up_package(self.dest.package)
|
|
|
|
return
|
|
|
|
elif self.dest.is_rack:
|
|
|
|
self.unload_package(self.dest)
|
|
|
|
return
|
|
|
|
|
2020-04-30 16:36:55 +02:00
|
|
|
if star_dir == self.direction:
|
|
|
|
self.x = next.x
|
|
|
|
self.y = next.y
|
|
|
|
else:
|
|
|
|
self.path.append(next)
|
2020-04-28 22:03:48 +02:00
|
|
|
self.closed = []
|
2020-04-30 16:36:55 +02:00
|
|
|
|
2020-05-01 01:08:08 +02:00
|
|
|
def check_if_can_move(self, next_coords: Coordinates, rack_searching=False):
|
2020-04-28 00:45:00 +02:00
|
|
|
tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height
|
2020-05-01 01:08:08 +02:00
|
|
|
tile_passable = True
|
2020-04-28 00:14:29 +02:00
|
|
|
if not tile_on_map:
|
2020-04-28 00:41:57 +02:00
|
|
|
return False
|
2020-04-28 00:45:00 +02:00
|
|
|
next_tile = self.warehouse.tiles[next_coords.x][next_coords.y]
|
2020-05-01 13:48:13 +02:00
|
|
|
if not self.is_loaded or (next_coords.x, next_coords.y) != (self.dest.x, self.dest.y):
|
2020-05-01 01:08:08 +02:00
|
|
|
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
|
2020-04-28 00:14:29 +02:00
|
|
|
return tile_passable
|
|
|
|
|
2020-05-01 01:08:08 +02:00
|
|
|
def find_nearest_package(self):
|
|
|
|
packages_costs = []
|
|
|
|
start_node = Node(self.x, self.y)
|
|
|
|
if not self.warehouse.packages:
|
2020-05-01 01:19:23 +02:00
|
|
|
return None
|
2020-05-01 01:08:08 +02:00
|
|
|
for package in self.warehouse.packages:
|
|
|
|
if package.status == PackStatus.STORED:
|
|
|
|
continue
|
|
|
|
new_node = Node(package.lays_on_field.x_position, package.lays_on_field.y_position)
|
|
|
|
cost = self.heuristic(start_node, new_node)
|
|
|
|
if cost > 0:
|
|
|
|
packages_costs.append((package, cost))
|
2020-05-01 01:19:23 +02:00
|
|
|
if not packages_costs:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2020-05-01 01:08:08 +02:00
|
|
|
package = min(packages_costs, key=lambda l: l[1])[0]
|
|
|
|
return package
|
|
|
|
|
|
|
|
def find_nearest_rack_for(self, package, expand_box=0):
|
|
|
|
weight = package.size
|
|
|
|
accepted_weights = [weight, PackSize.ALL]
|
|
|
|
start_node = Node(self.x, self.y)
|
|
|
|
quarter_x = int(self.warehouse.width/4) + expand_box
|
|
|
|
quarter_y = int(self.warehouse.height/4) + expand_box
|
|
|
|
start_quarter_x = self.x - quarter_x if self.x - quarter_x > 0 else 0
|
|
|
|
end_quarter_x = self.x + quarter_x if self.x + quarter_x < self.warehouse.width else self.warehouse.width - 1
|
|
|
|
start_quarter_y = self.y - quarter_y if self.y - quarter_y > 0 else 0
|
|
|
|
end_quarter_y = self.y + quarter_y if self.y + quarter_y < self.warehouse.height else self.warehouse.height - 1
|
|
|
|
quarter = [row[start_quarter_y:end_quarter_y] for row in self.warehouse.tiles[start_quarter_x:end_quarter_x]]
|
|
|
|
quarter_racks = [[t for t in row if t.category.name == "Rack" and not t.occupied and t.category.pack_size in accepted_weights] for row in quarter]
|
|
|
|
quarter_racks = [t for row in quarter_racks for t in row]
|
|
|
|
racks_costs = []
|
|
|
|
for rack in quarter_racks:
|
|
|
|
new_node = Node(rack.x_position, rack.y_position)
|
|
|
|
cost = self.heuristic(start_node, new_node)
|
|
|
|
if cost > 0:
|
|
|
|
racks_costs.append((rack, cost))
|
2020-05-01 13:48:13 +02:00
|
|
|
|
|
|
|
rack = self.find_nearest_rack_for(package, expand_box + 1) if not racks_costs else min(racks_costs, key=lambda l: l[1])[0]
|
2020-05-01 01:08:08 +02:00
|
|
|
return rack
|
|
|
|
|
|
|
|
|
2020-04-27 22:14:07 +02:00
|
|
|
def pick_up_package(self, pack):
|
2020-05-01 01:08:08 +02:00
|
|
|
self.warehouse.packages.remove(pack)
|
2020-04-27 22:14:07 +02:00
|
|
|
self.is_loaded = True
|
2020-05-01 01:08:08 +02:00
|
|
|
self.warehouse.tiles[pack.lays_on_field.x_position][pack.lays_on_field.y_position].occupied = False
|
|
|
|
self.dest.package = None
|
2020-04-27 22:14:07 +02:00
|
|
|
pack.lays_on_field = None
|
|
|
|
self.transported_package = pack
|
2020-05-01 01:08:08 +02:00
|
|
|
|
|
|
|
def unload_package(self, rack):
|
|
|
|
pack = self.transported_package
|
|
|
|
tile = self.warehouse.tiles[rack.x][rack.y]
|
|
|
|
self.transported_package = None
|
|
|
|
self.is_loaded = False
|
|
|
|
pack.lays_on_field = tile
|
|
|
|
self.warehouse.tiles[rack.x][rack.y].occupied = True
|
|
|
|
pack.status = PackStatus.STORED
|
|
|
|
self.warehouse.packages.append(pack)
|