From 777fd3669b2f3bc56d51809694c3002aef8b5e9b Mon Sep 17 00:00:00 2001 From: emil Date: Sat, 2 Nov 2024 00:09:48 +0300 Subject: [PATCH] final commit --- main.py | 21 +++++---- main_A_star_testing.py | 14 ++++-- main_Backtracking.py | 50 +++++++++++----------- main_Backtracking_testing.py | 83 ++++++++++++++++++++++++------------ 4 files changed, 101 insertions(+), 67 deletions(-) diff --git a/main.py b/main.py index 2737fbb..c1dcf67 100644 --- a/main.py +++ b/main.py @@ -211,16 +211,15 @@ keymaker = (5,6) initialize_map_dict() calculate_all_h_for_target(keymaker) -'''make_blocked((1,1)) -make_blocked((1,2)) -make_blocked((1,3)) -make_blocked((1,4)) -make_blocked((4,6)) -make_blocked((5,5)) -make_blocked((6,6)) -make_blocked((4,7)) -make_blocked((4,8))''' -#print_map() +make_blocked((0,1)) +make_blocked((1,0)) + +#make_blocked((4,6)) + +#make_blocked((6,6)) + +#make_blocked((4,8)) +print_map() #print_cells_parameters(get_walkable_cells_list(neo)) #time.sleep(0.1) @@ -262,7 +261,7 @@ while (finish == False): 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_map() neo = next_cell steps_count += 1 print(f"m {neo[0]} {neo[1]}") diff --git a/main_A_star_testing.py b/main_A_star_testing.py index 4f32219..ee883d3 100644 --- a/main_A_star_testing.py +++ b/main_A_star_testing.py @@ -28,7 +28,7 @@ with open("20k_testset.txt", "r") as file: line_number = 0 -while(test_number < 1000): +while(test_number < 100): #time.sleep(.5) start_time = time.time() current_test_lines = [] @@ -149,14 +149,22 @@ while(test_number < 1000): average_time = total_time / passed_tests -print("-------RESULTS-------") +print("-------A_STAR_RESULTS-------") print(f"passed tests: {passed_tests}") print(f"failed tests: {failed_tests}") print(f"total time: {total_time}") print(f"average time: {average_time}") ''' +FOR 100 tests +-------A_STAR_RESULTS------- +passed tests: 99 +failed tests: 1 +total time: 18.093406677246094 +average time: 0.1827616836085464 + + FOR 1000 tests --------RESULTS------- +-------A_STAR_RESULTS------- passed tests: 995 failed tests: 5 total time: 184.33025455474854 diff --git a/main_Backtracking.py b/main_Backtracking.py index 96bc899..4d2379b 100644 --- a/main_Backtracking.py +++ b/main_Backtracking.py @@ -2,30 +2,7 @@ grid_map = [] min_distances = [] -def main(): - global grid_map, min_distances - # Create a 9x9 grid map filled with '.' - grid_map = [['.' for temp in range(9)] for temp in range(9)] - # Create a minimum distance grid with initial values set to "infinity" (10000) - min_distances = [[10000 for temp in range(9)] for temp in range(9)] - - # Read perception variant - variant = int(input()) - # Read Keymaker's position - position_input = input().split() - keymaker_x = int(position_input[0]) - keymaker_y = int(position_input[1]) - # Set the starting position (0, 0) with a minimum distance of 0 - min_distances[0][0] = 0 - # Start the recursive pathfinding search from the starting position - find_path(0, 0) - - # Output the result based on the minimum distance to the Keymaker's position - if min_distances[keymaker_y][keymaker_x] == 10000: - print("e -1") # If no path is found, output -1 - else: - print("e " + str(min_distances[keymaker_y][keymaker_x])) # Output the shortest path length def observe(x, y): # Sends a move command and receives information on perceived cells around position (x, y) @@ -73,5 +50,28 @@ def find_path(x, y): observe(x, y) # Final exploration after checking all directions. -if __name__ == "__main__": - main() + +# Create a 9x9 grid map filled with '.' +grid_map = [['.' for temp in range(9)] for temp in range(9)] +# Create a minimum distance grid with initial values set to "infinity" (10000) +min_distances = [[10000 for temp in range(9)] for temp in range(9)] + +# Read perception variant +variant = int(input()) +# Read Keymaker's position +position_input = input().split() +keymaker_x = int(position_input[0]) +keymaker_y = int(position_input[1]) + +# Set the starting position (0, 0) with a minimum distance of 0 +min_distances[0][0] = 0 +# Start the recursive pathfinding search from the starting position +find_path(0, 0) + +# Output the result based on the minimum distance to the Keymaker's position +if min_distances[keymaker_y][keymaker_x] == 10000: + print("e -1") # If no path is found, output -1 +else: + print("e " + str(min_distances[keymaker_y][keymaker_x])) # Output the shortest path length + + diff --git a/main_Backtracking_testing.py b/main_Backtracking_testing.py index 3c3e775..1338989 100644 --- a/main_Backtracking_testing.py +++ b/main_Backtracking_testing.py @@ -42,37 +42,14 @@ while(test_number < 1000): grid_map = [] min_distances = [] - def main(): - global grid_map, min_distances - # Create a 9x9 grid map filled with '.' - grid_map = [['.' for temp in range(9)] for temp in range(9)] - # Create a minimum distance grid with initial values set to "infinity" (10000) - min_distances = [[10000 for temp in range(9)] for temp in range(9)] - - # Read perception variant - perception_radius = int(current_test_lines[1][0]) - # Read Keymaker's position - keymaker_x = int(current_test_lines[2][1]) - keymaker_y = int(current_test_lines[2][4]) - - # Set the starting position (0, 0) with a minimum distance of 0 - min_distances[0][0] = 0 - # Start the recursive pathfinding search from the starting position - find_path(0, 0) - - # Output the result based on the minimum distance to the Keymaker's position - if min_distances[keymaker_y][keymaker_x] == 10000: - print("e -1") # If no path is found, output -1 - else: - print("e " + str(min_distances[keymaker_y][keymaker_x])) # Output the shortest path length def observe(x, y): # Sends a move command and receives information on perceived cells around position (x, y) - print(f"m {x} {y}") + #print(f"m {x} {y}") for x_temp in range(len(test_map_matrix)): for y_temp in range(len(test_map_matrix)): - if (x,y) in get_percepted_cells((x, y)) and test_map_matrix[x_temp][y_temp] != ".": - grid_map[x][y] = test_map_matrix[x][y] + if (x_temp,y_temp) in get_percepted_cells((x, y)) and test_map_matrix[x_temp][y_temp] != ".": + grid_map[x_temp][y_temp] = test_map_matrix[x_temp][y_temp] def find_path(x, y): # Explore surroundings from the current position (x, y) @@ -106,6 +83,56 @@ while(test_number < 1000): observe(x, y) # Final exploration after checking all directions. - if __name__ == "__main__": - main() + + + # Create a 9x9 grid map filled with '.' + grid_map = [['.' for temp in range(9)] for temp in range(9)] + # Create a minimum distance grid with initial values set to "infinity" (10000) + min_distances = [[10000 for temp in range(9)] for temp in range(9)] + + # Read perception variant + perception_radius = int(current_test_lines[1][0]) + # Read Keymaker's position + keymaker_x = int(current_test_lines[2][1]) + keymaker_y = int(current_test_lines[2][4]) test_map_matrix = current_test_lines[3:12] + # Set the starting position (0, 0) with a minimum distance of 0 + min_distances[0][0] = 0 + # Start the recursive pathfinding search from the starting position + find_path(0, 0) + + # Output the result based on the minimum distance to the Keymaker's position + if min_distances[keymaker_y][keymaker_x] == 10000: + print("e -1") # If no path is found, output -1 + time.sleep(1) + failed_tests += 1 + test_number += 1 + end_time = time.time() + test_time = end_time - start_time + total_time += 0 # we don't consider failed tests in statistics + + else: + print("e " + str(min_distances[keymaker_y][keymaker_x])) # Output the shortest path length + time.sleep(1) + passed_tests += 1 + test_number += 1 + end_time = time.time() + test_time = end_time - start_time + total_time += test_time + +average_time = total_time / passed_tests + +print("-------BACKTRACKING_RESULTS-------") +print(f"passed tests: {passed_tests}") +print(f"failed tests: {failed_tests}") +print(f"total time: {total_time}") +print(f"average time: {average_time}") + +''' +FOR 100 tests +-------BACKTRACKING_RESULTS------- +passed tests: 99 +failed tests: 1 +total time: 156.93781638145447 +average time: 1.58523046849954 +''' \ No newline at end of file