From 96bb160742a0a4d14df949723b32fb3b6bd921ac Mon Sep 17 00:00:00 2001 From: emil Date: Wed, 30 Oct 2024 05:12:41 +0300 Subject: [PATCH] Implement A* (not correct) --- main2.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 135 insertions(+), 15 deletions(-) diff --git a/main2.py b/main2.py index 88f1e24..fa6ef1c 100644 --- a/main2.py +++ b/main2.py @@ -1,3 +1,4 @@ +#import time #TODO TEMP MAP_SIZE = 8 map = [] @@ -17,15 +18,15 @@ def get_walkable_cells(): potential_positions.append((neo[0] - 1, neo[1])) potential_positions.append((neo[0] + 1, neo[1])) for i in potential_positions: - if i[0] in range(MAP_SIZE) and i[1] in range(MAP_SIZE): + if i[0] in range(MAP_SIZE) and i[1] in range(MAP_SIZE) and i not in closed_cells: walkable_cells.append(i) return walkable_cells def get_g(cell:tuple): - return abs((start[0] - cell[0]) + (start[1] - cell[1])) #TODO FROM START OR NEO? + return abs(start[0] - cell[0]) + abs(start[1] - cell[1]) #TODO FROM START OR NEO? def get_h(cell:tuple): - return abs((keymaster[0] - cell[0]) + (keymaster[1] - cell[1])) + return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1]) def get_f(cell:tuple): return get_g(cell) + get_h(cell) @@ -37,28 +38,117 @@ def get_verified_move_position(new_postion:tuple): print("CAN'T MOVE HERE!") #TEMP return neo +def print_wheights(cell:tuple): + print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}") + +def print_map_f(): + print("f") + map_str = "" + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + if x == neo[0] and y == neo[1]: + map_str += " n " + elif (x == keymaster[0] and y == keymaster[1]): + map_str += " k " + else: + f = get_f((x,y)) + + + if len(str(f)) == 2: + f_str = " " + str(f) + else: + f_str = " " + str(f) + " " + map_str += f_str + map_str += "\n" + print(map_str) + +def print_map_g(): + print("g") + map_str = "" + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + if x == neo[0] and y == neo[1]: + map_str += " n " + elif (x == keymaster[0] and y == keymaster[1]): + map_str += " k " + else: + f = get_g((x,y)) + + + if len(str(f)) == 2: + f_str = " " + str(f) + else: + f_str = " " + str(f) + " " + map_str += f_str + map_str += "\n" + print(map_str) + +def print_map_h(): + print("h") + map_str = "" + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + if x == neo[0] and y == neo[1]: + map_str += " n " + elif (x == keymaster[0] and y == keymaster[1]): + map_str += " k " + else: + f = get_h((x,y)) + + + if len(str(f)) == 2: + f_str = " " + str(f) + else: + f_str = " " + str(f) + " " + map_str += f_str + map_str += "\n" + print(map_str) + +def read_system(): + number_of_items = (int)(input()) + items = dict() + if number_of_items == 0: + return False + else: + + for i in range(number_of_items): + input_str_split = input().split(' ') + input_formatted = [] + input_formatted.append((int)(input_str_split[0])) + input_formatted.append((int)(input_str_split[1])) + input_formatted.append((input_str_split[2])) + + + items[(input_formatted[0], input_formatted[1])] = input_formatted[2] + + return items + perception_radius = input() position_input = get_position_input() keymaster = (position_input[0], position_input[1]) +finish = False +closed_cells = [] -print("neo:") -print(neo) -print("keymaster:") -print(keymaster) +#print("neo:") +#print(neo) +#print("keymaster:") +#print(keymaster) walkable_cells_and_f = dict() for i in get_walkable_cells(): walkable_cells_and_f[i] = get_f(i) min_f_cell = min(walkable_cells_and_f, key=walkable_cells_and_f.get) -print("min_f:") -print(min_f_cell) +#print("min_f:") +#print(min_f_cell) #neo = get_verified_move_position(position_input) -print(walkable_cells_and_f) - -while (True): +#print(walkable_cells_and_f) +print(f"m {neo[0]} {neo[1]}") +steps_count = 0 +while (finish == False): + ''' position_input = get_position_input() print(position_input) neo = get_verified_move_position(position_input) @@ -67,7 +157,17 @@ while (True): print("\nkeymaster:") print(keymaster) print("\nwalkabe cells") + ''' #A* + + + recieved_input = read_system() + + if recieved_input != False: + for i in recieved_input.items(): + if i[1] == "P": + closed_cells.append(i[0]) + walkable_cells_and_f = dict() for i in get_walkable_cells(): walkable_cells_and_f[i] = get_f(i) @@ -87,9 +187,27 @@ while (True): # Если таких ячеек несколько, выбираем ту, у которой минимальный h next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell)) + #time.sleep(7) #TODO TEMP + neo = get_verified_move_position(next_cell) + closed_cells.append(next_cell) + print(f"m {next_cell[0]} {next_cell[1]}") + steps_count += 1 + finish = neo == keymaster + ''' + for i in walkable_cells_and_f.items(): + cell = i[0] + print(cell) + print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}") + print(neo) + print(print_wheights(neo)) + print_map_g() + print_map_h() + print_map_f() + print(walkable_cells_and_f) + + + - - print("min_f:") print(min_f_cell) print("min_f_count:") @@ -101,4 +219,6 @@ while (True): print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}") print(walkable_cells_and_f) print("next_cell:") - print(next_cell) \ No newline at end of file + print(next_cell)''' +#print("FINISH!") +print(f"e {steps_count}") \ No newline at end of file