151 lines
5.5 KiB
Python
151 lines
5.5 KiB
Python
|
from truck import Truck
|
||
|
|
||
|
class Node():
|
||
|
def __init__(self, parent=None, position=None):
|
||
|
self.parent = parent
|
||
|
self.position = position
|
||
|
|
||
|
self.g = 0
|
||
|
self.h = 0
|
||
|
self.f = 0
|
||
|
|
||
|
def __eq__(self, other):
|
||
|
return self.position == other.position
|
||
|
|
||
|
|
||
|
def astar(maze, start, end):
|
||
|
start_node = Node(position=start)
|
||
|
end_node = Node(position=end)
|
||
|
x=0
|
||
|
open_list = []
|
||
|
closed_list = []
|
||
|
|
||
|
open_list.append(start_node)
|
||
|
|
||
|
while len(open_list) > 0:
|
||
|
|
||
|
current_node = open_list[0]
|
||
|
current_index = 0
|
||
|
for index, item in enumerate(open_list):
|
||
|
if item.f < current_node.f:
|
||
|
current_node = item
|
||
|
current_index = index
|
||
|
|
||
|
open_list.pop(current_index)
|
||
|
closed_list.append(current_node)
|
||
|
|
||
|
if current_node == end_node:
|
||
|
path = []
|
||
|
|
||
|
current = current_node
|
||
|
while current is not None:
|
||
|
path.append(current.position)
|
||
|
if (maze[current.position[0]][current.position[1]] == 1):
|
||
|
x = x + 1
|
||
|
if (maze[current.position[0]][current.position[1]] == 2):
|
||
|
x = x + 3
|
||
|
if (maze[current.position[0]][current.position[1]] == 6):
|
||
|
x = x + 20
|
||
|
if (maze[current.position[0]][current.position[1]] == 7):
|
||
|
x = x + 5
|
||
|
if (maze[current.position[0]][current.position[1]] == 8):
|
||
|
x = x + 1
|
||
|
|
||
|
current = current.parent
|
||
|
return path[::-1]
|
||
|
#return x
|
||
|
|
||
|
children = []
|
||
|
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
|
||
|
|
||
|
node_position = (
|
||
|
current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
|
||
|
|
||
|
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) - 1) or node_position[1] < 0:
|
||
|
continue
|
||
|
|
||
|
if Node(current_node, node_position) in closed_list:
|
||
|
continue
|
||
|
|
||
|
if maze[node_position[0]][node_position[1]] == 9:
|
||
|
continue
|
||
|
|
||
|
new_node = Node(current_node, node_position)
|
||
|
|
||
|
children.append(new_node)
|
||
|
|
||
|
for child in children:
|
||
|
|
||
|
for closed_child in closed_list:
|
||
|
if child == closed_child:
|
||
|
continue
|
||
|
|
||
|
if(maze[child.position[0]][child.position[1]]==1):
|
||
|
child.g = 1
|
||
|
if(maze[child.position[0]][child.position[1]]==2):
|
||
|
child.g = 3
|
||
|
if(maze[child.position[0]][child.position[1]]==6):
|
||
|
child.g = 20
|
||
|
if(maze[child.position[0]][child.position[1]]==7):
|
||
|
child.g = 5
|
||
|
if(maze[child.position[0]][child.position[1]]==8):
|
||
|
child.g = 1
|
||
|
child.h = ((child.position[0] - end_node.position[0]) **
|
||
|
2) + ((child.position[1] - end_node.position[1]) ** 2)
|
||
|
child.f = child.g + child.h
|
||
|
|
||
|
for open_node in open_list:
|
||
|
if child == open_node and child.g > open_node.g:
|
||
|
continue
|
||
|
|
||
|
open_list.append(child)
|
||
|
|
||
|
|
||
|
def main():
|
||
|
|
||
|
maze = [[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
|
||
|
[9, 9, 9, 9, 9, 1, 1, 9, 9, 9, 1, 1, 1, 9, 9, 9, 1,
|
||
|
1, 9, 9, 1, 1, 1, 9, 9, 1, 1, 9, 1, 1, 9, 9],
|
||
|
[9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 9, 1, 1,
|
||
|
8, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 9],
|
||
|
[9, 1, 1, 1, 2, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 9,
|
||
|
1, 0, 1, 7, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 9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