diff --git a/agent.py b/agent.py index df6b696..40a7f5b 100644 --- a/agent.py +++ b/agent.py @@ -20,7 +20,7 @@ class Agent: self.warehouse = assigned_warehouse self.is_loaded = False self.transported_package = None - self.dest = Node(0, 0) + self.dest = Node(1, 1) self.closed = list() self.open = list() self.path = list() @@ -30,23 +30,23 @@ class Agent: self.open.append(start_node) while self.open: current_node = self.open.pop() + print(current_node.x, current_node.y) self.closed.append(current_node) if current_node.x == self.dest.x and current_node.y == self.dest.y: - while current_node != start_node: - path.append(current_node) + while current_node.x != start_node.x and current_node.y != start_node.y: + self.path.append(current_node) current_node = current_node.parent return True neighbour_list = self.get_neighbours(current_node) for neighbour in neighbour_list: - if neighbour in self.closed: - continue - cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour) - if cost < neighbour.g_cost or neighbour not in self.open: - neighbour.g_cost = cost - neighbour.h_cost = self.heur_cost_est(neighbour, self.dest) - neighbour.parent = current_node - if neighbour not in self.open: - self.open.append(neighbour); + if not self.check_if_closed(neighbour): + cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour) + if (cost < neighbour.g_cost) or not self.check_if_open(neighbour): + neighbour.g_cost = cost + neighbour.h_cost = self.heur_cost_est(neighbour, self.dest) + neighbour.parent = current_node + if not self.check_if_open(neighbour): + self.open.append(neighbour); return False def heur_cost_est(self, nodeA: Node, nodeB: Node): @@ -55,17 +55,32 @@ class Agent: if deltaX > deltaY: return (14 * deltaY) + (10 * (deltaX - deltaY)) return (14 * deltaX) + (10 *(deltaY - deltaX)) + + def check_if_open(self, nodeA: Node): + for node in self.open: + if node.x == nodeA.x and node.y == nodeA.y: + print("open") + return True + return False + + def check_if_closed(self, nodeA: Node): + for node in self.closed: + if node.x == nodeA.x and node.y == nodeA.y: + print("closed") + return True + return False def get_neighbours(self, node: Node): - neighbours = list() - #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)) + neighbours = [] + print(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)) return neighbours def move(self): @@ -78,6 +93,8 @@ class Agent: self.y = next.y def check_if_can_move(self, next_coords: Coordinates): + if next_coords.x < 0 or next_coords.y < 0: + return False next_tile = self.warehouse.tiles[next_coords.x][next_coords.y] tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height