2019SZI-Projekt/Logic/Apath.py

116 lines
3.1 KiB
Python
Raw Normal View History

2019-04-25 20:56:02 +02:00
import numpy as np
2019-04-15 12:54:29 +02:00
from heapq import * # pylint: disable=unused-wildcard-import
2019-05-01 02:09:06 +02:00
2019-04-27 18:04:06 +02:00
class AStarNode():
2019-04-15 12:19:26 +02:00
2019-04-27 18:04:06 +02:00
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
2019-04-15 12:19:26 +02:00
2019-04-27 18:04:06 +02:00
def __eq__(self, other):
return self.position == other.position
2019-04-15 12:54:29 +02:00
2019-04-29 13:45:56 +02:00
def a_path(table, start, end):
2019-04-15 12:54:29 +02:00
2019-04-27 18:04:06 +02:00
# Create start and end node
start_node = AStarNode(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = AStarNode(None, end)
end_node.g = end_node.h = end_node.f = 0
open_list = []
closed_list = []
# Add the start node
open_list.append(start_node)
# Loop until you find the end
2019-04-28 16:09:06 +02:00
i = 0
2019-04-27 18:04:06 +02:00
while len(open_list) > 0:
2019-04-28 16:09:06 +02:00
print(i)
2019-04-29 13:45:56 +02:00
i = i + 1
2019-04-27 18:04:06 +02:00
current_node = open_list[0]
current_index = 0
2019-04-28 17:54:26 +02:00
# Find current node
2019-04-27 18:04:06 +02:00
for index, item in enumerate(open_list):
2019-04-28 16:09:06 +02:00
if(item.f < current_node.f):
2019-04-27 18:04:06 +02:00
current_node = item
current_index = index
2019-04-28 17:54:26 +02:00
# Pop current off open list and add to closed list
2019-04-27 18:04:06 +02:00
open_list.pop(current_index)
closed_list.append(current_node)
2019-04-28 17:54:26 +02:00
# Found end node
2019-04-27 18:04:06 +02:00
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
2019-04-28 17:54:26 +02:00
return path[::-1]
2019-04-27 18:04:06 +02:00
# Generate children
children = []
2019-04-28 17:54:26 +02:00
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
2019-04-27 18:04:06 +02:00
2019-04-28 17:54:26 +02:00
2019-04-27 18:04:06 +02:00
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
2019-04-28 17:54:26 +02:00
# If in maze
2019-04-27 18:04:06 +02:00
if node_position[0] > (len(table) - 1) or node_position[0] < 0 or node_position[1] > (len(table[len(table)-1]) -1) or node_position[1] < 0:
2019-04-15 12:19:26 +02:00
continue
2019-04-15 12:54:29 +02:00
2019-04-28 17:54:26 +02:00
# If not obstacle
2019-04-27 18:04:06 +02:00
if table[node_position[0]][node_position[1]].field_type == 3:
2019-04-15 12:19:26 +02:00
continue
2019-04-15 12:54:29 +02:00
2019-04-28 17:54:26 +02:00
2019-04-27 18:04:06 +02:00
new_node = AStarNode(current_node, node_position)
children.append(new_node)
2019-04-28 17:54:26 +02:00
2019-04-27 18:04:06 +02:00
for child in children:
2019-04-28 16:09:06 +02:00
2019-04-29 13:45:56 +02:00
def in_closed_list(child: AStarNode):
2019-04-28 16:09:06 +02:00
for closed_child in closed_list:
if child.position == closed_child.position:
return True
2019-04-29 13:45:56 +02:00
return False
2019-04-28 16:09:06 +02:00
2019-04-29 13:45:56 +02:00
def in_open_list(child: AStarNode):
2019-04-28 16:09:06 +02:00
for open_node in open_list:
2019-04-28 17:54:26 +02:00
if child == open_node:
2019-04-28 16:09:06 +02:00
return True
2019-04-29 13:45:56 +02:00
return False
2019-04-28 16:09:06 +02:00
2019-04-27 18:04:06 +02:00
# Child is on the closed list
2019-04-29 13:45:56 +02:00
if in_closed_list(child)==True:
2019-04-28 16:09:06 +02:00
continue
2019-04-27 18:04:06 +02:00
child.g = current_node.g + 1
2019-04-28 16:09:06 +02:00
child.h = max(abs(child.position[0] - end_node.position[0]), abs(child.position[1] - end_node.position[1]))
2019-04-27 18:04:06 +02:00
child.f = child.g + child.h
# Child is already in the open list
2019-04-29 13:45:56 +02:00
if in_open_list(child):
2019-04-28 16:09:06 +02:00
continue
2019-04-27 18:04:06 +02:00
open_list.append(child)
2019-04-28 16:09:06 +02:00