A* done, start making tests

This commit is contained in:
emil
2024-11-01 17:32:52 +03:00
parent 91658b26df
commit bc8c3a90d5
6 changed files with 280240 additions and 18 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
/.idea
/.venv
/Graphs
/__pycache__
/__pycache__
/other
+280000
View File
File diff suppressed because it is too large Load Diff
+31 -17
View File
@@ -1,4 +1,4 @@
import time
#import time
MAP_SIZE = 9
start = (0, 0)
neo = start
@@ -179,14 +179,17 @@ def calculate_minimal_cell(cells:list, filter=None):
def roll_back(looking_for_cell:tuple):
global neo
time.sleep(0.1)
#time.sleep(0.1)
while(get_previous(neo) != None and looking_for_cell not in get_walkable_cells_list(neo)):
print_cells_parameters(get_walkable_cells_list(neo))
print("roll back")
print(f"target: ({looking_for_cell[0]},{looking_for_cell[1]})")
print_map()
#print_cells_parameters(get_walkable_cells_list(neo))
#print("roll back")
#print(f"target: ({looking_for_cell[0]},{looking_for_cell[1]})")
#print_map()
inputs = read_system()
neo = get_previous(neo)
time.sleep(0.1)
steps_count += 1
print(f"m {neo[0]} {neo[1]}")
#time.sleep(0.1)
def get_position_input():
position_input_list = input().split(" ")
@@ -208,7 +211,7 @@ keymaker = (5,6)
initialize_map_dict()
calculate_all_h_for_target(keymaker)
make_blocked((1,1))
'''make_blocked((1,1))
make_blocked((1,2))
make_blocked((1,3))
make_blocked((1,4))
@@ -216,14 +219,23 @@ make_blocked((4,6))
make_blocked((5,5))
make_blocked((6,6))
make_blocked((4,7))
make_blocked((4,8))
print_map()
make_blocked((4,8))'''
#print_map()
print_cells_parameters(get_walkable_cells_list(neo))
time.sleep(0.1)
#print_cells_parameters(get_walkable_cells_list(neo))
#time.sleep(0.1)
finish = False
seeking_for_target = False
perception_radius = input()
keymaster = get_position_input()
print("m 0 0")
while (finish == False):
inputs = read_system()
if inputs != False:
for inpt in inputs.items():
if inpt[1] == "P":
make_blocked(inpt[0])
make_closed(neo)
for cell in get_walkable_cells_list(neo):
if get_status(cell) == ".":
@@ -249,11 +261,13 @@ while (finish == False):
next_cell = calculate_minimal_cell(get_walkable_cells_list(neo))
assign_previous(next_cell, calculate_cell_with_minimal_g(get_walkable_cells_list(next_cell), "-"))
previous = get_previous(next_cell)
print_cells_parameters(get_walkable_cells_list(neo))
print_map()
#print_cells_parameters(get_walkable_cells_list(neo))
#print_map()
neo = next_cell
time.sleep(0.2)
steps_count += 1
print(f"m {neo[0]} {neo[1]}")
#time.sleep(0.2)
if neo == keymaker:
finish = True
# TODO MAKE CHECK IF NO PATH EXISTS
# TODO MAKE CHECK IF NO PATH EXISTS
print(f"e {steps_count}")
+72
View File
@@ -0,0 +1,72 @@
import sys
import heapq
min_costs = [[100]*9 for temp in range(9)]
hs = [[0]*9 for temp in range(9)]
astar_map = [['.']*9 for temp in range(9)]
visited_nodes = [[False]*9 for temp in range(9)]
node_parents = [[None]*9 for temp in range(9)]
perception_radius = int(input())
input_list = input().split()
goal_x, goal_y = int(input_list[0]), int(input_list[1])
for i in range(9):
for j in range(9):
hs[j][i] = abs(j - goal_y) + abs(i - goal_x)
min_costs[j][i] = 100
min_costs[0][0] = 0
priority_queue = []
heapq.heappush(priority_queue, (min_costs[0][0] + hs[0][0], 0, 0))
while len(priority_queue) != 0:
temp, current_x, current_y = heapq.heappop(priority_queue)
if visited_nodes[current_y][current_x]:
continue
visited_nodes[current_y][current_x] = True
parent_node = node_parents[current_y][current_x]
path_to_current = []
path_to_current.append((current_x, current_y))
while parent_node is not None:
path_to_current.append(parent_node)
parent_node = node_parents[parent_node[1]][parent_node[0]]
for i in reversed(range(len(path_to_current))):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
neighbor_x = current_x + dx
neighbor_y = current_y + dy
if 0 <= neighbor_x < 9 and 0 <= neighbor_y < 9 and not visited_nodes[neighbor_y][neighbor_x] and astar_map[neighbor_y][neighbor_x] not in ('P', 'A', 'S'):
if min_costs[neighbor_y][neighbor_x] > min_costs[current_y][current_x] + 1:
node_parents[neighbor_y][neighbor_x] = (current_x, current_y)
min_costs[neighbor_y][neighbor_x] = min_costs[current_y][current_x] + 1
heapq.heappush(priority_queue, (min_costs[neighbor_y][neighbor_x] + hs[neighbor_y][neighbor_x], neighbor_x, neighbor_y))
for i in range(len(path_to_current)):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
if min_costs[goal_y][goal_x] != 100:
print(f"e {min_costs[goal_y][goal_x]}")
else:
print("e -1")
+76
View File
@@ -0,0 +1,76 @@
import sys
import heapq
min_costs = [[100]*9 for temp in range(9)]
hs = [[0]*9 for temp in range(9)]
astar_map = [['.']*9 for temp in range(9)]
visited_nodes = [[False]*9 for temp in range(9)]
node_parents = [[None]*9 for temp in range(9)]
perception_radius = int(input())
input_list = input().split()
goal_x, goal_y = int(input_list[0]), int(input_list[1])
for i in range(9):
for j in range(9):
hs[j][i] = abs(j - goal_y) + abs(i - goal_x)
min_costs[j][i] = 100
min_costs[0][0] = 0
priority_queue = []
heapq.heappush(priority_queue, (min_costs[0][0] + hs[0][0], 0, 0))
while len(priority_queue) != 0:
temp, current_x, current_y = heapq.heappop(priority_queue)
if visited_nodes[current_y][current_x]:
continue
visited_nodes[current_y][current_x] = True
parent_node = node_parents[current_y][current_x]
path_to_current = []
path_to_current.append((current_x, current_y))
while parent_node is not None:
path_to_current.append(parent_node)
parent_node = node_parents[parent_node[1]][parent_node[0]]
for i in reversed(range(len(path_to_current))):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
neighbor_x = current_x + dx
neighbor_y = current_y + dy
if 0 <= neighbor_x < 9 and 0 <= neighbor_y < 9 and not visited_nodes[neighbor_y][neighbor_x] and astar_map[neighbor_y][neighbor_x] not in ('P', 'A', 'S'):
if min_costs[neighbor_y][neighbor_x] > min_costs[current_y][current_x] + 1:
node_parents[neighbor_y][neighbor_x] = (current_x, current_y)
min_costs[neighbor_y][neighbor_x] = min_costs[current_y][current_x] + 1
heapq.heappush(priority_queue, (min_costs[neighbor_y][neighbor_x] + hs[neighbor_y][neighbor_x], neighbor_x, neighbor_y))
for i in range(len(path_to_current)):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
if min_costs[goal_y][goal_x] != 100:
print(f"e {min_costs[goal_y][goal_x]}")
else:
print("e -1")
test_number = 0
while(test_number != 1000):
+59
View File
@@ -0,0 +1,59 @@
map_grid = []
minDists = []
keymaker = []
def main():
global map_grid, minDists
map_grid = [['.' for _ in range(9)] for _ in range(9)]
minDists = [[100 for _ in range(9)] for _ in range(9)]
n = int(input())
position_input = input().split()
x = position_input[0]
y = position_input[1]
#keymaker.append(int(x))
#keymaker.append(int(y))
#print(keymaker)
minDists[0][0] = 0
findShortestPath(0, 0)
if minDists[y][x] == 100:
print("e -1")
else:
print("e " + str(minDists[y][x]))
def exploreMap(x, y):
#if x == keymaker[0] and y == keymaker[1]:
# print("e " + str(minDists[y][x]))
# exit(0)
print(f"m {x} {y}")
n = int(input())
for _ in range(n):
inpt = input().split()
posX, posY, character = inpt[0], inpt[1], inpt[2]
posX = int(posX)
posY = int(posY)
character = character[0]
map_grid[posY][posX] = character
def findShortestPath(x, y):
exploreMap(x, y)
if x + 1 < 9 and map_grid[y][x + 1] not in ('P', 'A', 'S') and minDists[y][x + 1] > minDists[y][x] + 1:
minDists[y][x + 1] = minDists[y][x] + 1
findShortestPath(x + 1, y)
exploreMap(x, y)
if x - 1 >= 0 and map_grid[y][x - 1] not in ('P', 'A', 'S') and minDists[y][x - 1] > minDists[y][x] + 1:
minDists[y][x - 1] = minDists[y][x] + 1
findShortestPath(x - 1, y)
exploreMap(x, y)
if y + 1 < 9 and map_grid[y + 1][x] not in ('P', 'A', 'S') and minDists[y + 1][x] > minDists[y][x] + 1:
minDists[y + 1][x] = minDists[y][x] + 1
findShortestPath(x, y + 1)
exploreMap(x, y)
if y - 1 >= 0 and map_grid[y - 1][x] not in ('P', 'A', 'S') and minDists[y - 1][x] > minDists[y][x] + 1:
minDists[y - 1][x] = minDists[y][x] + 1
findShortestPath(x, y - 1)
exploreMap(x, y)
if __name__ == "__main__":
main()