From cfeaf81bda3e15c1c99fa876462687992b016d91 Mon Sep 17 00:00:00 2001 From: emil Date: Fri, 1 Nov 2024 03:15:34 +0300 Subject: [PATCH] intermediate commit --- main.py | 355 +++++++++++++++++++++++++++---------------------------- main3.py | 238 +++++++++++++++++++++++++++++++++++++ 2 files changed, 412 insertions(+), 181 deletions(-) create mode 100644 main3.py diff --git a/main.py b/main.py index 66ebdd6..4359362 100644 --- a/main.py +++ b/main.py @@ -2,237 +2,230 @@ import time MAP_SIZE = 9 start = (0, 0) neo = start -observer = neo -keymaster = () +keymaker = () -green_cells = [] #open cells + -red_cells = [] #closed cells - -black_cells = [] #blocked cells = -blue_cells = [] #traversed cells # +open_set = [] +closed_set = [] +blocked_set = [] steps_count = 0 -#accumulated_g = 0 + map_dict = dict() -def initialize_weighted_map_dict(): +def initialize_map_dict(): for x in range(MAP_SIZE): for y in range(MAP_SIZE): - map_dict[(x, y)] = [0, 0, 0, '.'] # (x,y) : (h, g, f, status) ??? [float("inf"), float("inf"), float("inf"), '.'] + map_dict[(x,y)] = [0, 0, ".", None] #g h status previous_cell +def calculate_all_h(): + global keymaker + for item in map_dict.items(): + item[1][1] = abs(keymaker[0] - item[0][0]) + abs(keymaker[1] - item[0][1]) def print_map(): + global neo, keymaker map_str = "" for x in range(MAP_SIZE): for y in range(MAP_SIZE): - if map_dict[(x,y)][3] in "kon": - set_status((x,y), '.') - - for x in range(MAP_SIZE): - for y in range(MAP_SIZE): - - set_status(keymaster, 'k') - set_status(observer, 'o') - set_status(neo, 'n') - - map_str += " " + map_dict[(x, y)][3] + " " + if (x,y) == neo: + map_str += " n " + elif (x,y) == keymaker: + map_str += " k " + else: + map_str += f" {map_dict[(x, y)][2]} " map_str += "\n" print(map_str) -def get_g(cell): # TODO MAYBE FIX NEEDED - #new_g = accumulated_g + 1 - #if accumulated_g + 1 < map_dict[cell][1]: - # new_g = map_dict[cell][1] - return map_dict[cell][1] + 1 +def print_cells_paremeters(cells:list): + str = "" + for cell in cells: + str += f"({cell[0]},{cell[1]}): {map_dict[cell][0]} + {map_dict[cell][1]} = {map_dict[cell][0] + map_dict[cell][1]} ({map_dict[cell][2]}) prev:{map_dict[cell][3]} | " + print(str) -def get_h(cell): - return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1]) - -def get_f(cell): - if map_dict[cell][3] == '=': - return float("inf") - return get_g(cell) + get_h(cell) - -def get_walkable_cells(actor:tuple): +def get_walkable_cells_list(actor:tuple): potential_positions = [ (actor[0], actor[1] + 1), (actor[0], actor[1] - 1), (actor[0] - 1, actor[1]), (actor[0] + 1, actor[1]) ] return [pos for pos in potential_positions if pos[0] in range(MAP_SIZE) and pos[1] in range(MAP_SIZE)] -def get_position_input(): - position_input_list = input().split(" ") - return int(position_input_list[0]), int(position_input_list[1]) +def get_open_set_list(): + open_set = [] + for item in map_dict.items(): + if item[1][2] == "+": + open_set.append(item[0]) + return open_set + +def get_closed_set_list(): + closed_set = [] + for item in map_dict.items(): + if item[1][2] == "-": + closed_set.append(item[0]) + return closed_set -def set_status(position:tuple, status:str): - if status == ".": - map_dict[position] = [0, 0, 0, '.'] +def make_opened(cell:tuple): + map_dict[cell][2] = "+" + +def make_closed(cell:tuple): + map_dict[cell][2] = "-" + +def make_blocked(cell:tuple): + map_dict[cell][2] = "=" + +def get_status(cell:tuple): + return map_dict[cell][2] + +def get_g(cell:tuple): + return map_dict[cell][0] + +def get_h(cell:tuple): + return map_dict[cell][1] + +def get_f(cell:tuple): + return map_dict[cell][0] + map_dict[cell][1] + +def set_g(cell:tuple, value): + map_dict[cell][0] = value + +def set_h(cell:tuple, value): + map_dict[cell][1] = value + +def add_g(cell:tuple, value): + map_dict[cell][0] += value + +def add_h(cell:tuple, value): + map_dict[cell][1] += value + +def assign_previous(cell:tuple, previous_cell:tuple): + map_dict[cell][3] = previous_cell + +def get_previous(cell:tuple): + return map_dict[cell][3] + +def calculate_cell_with_minimal_g(cells:list, filter=None): + selected_cells = [] + if filter == None: + for cell in cells: + if get_status(cell) != "=": + selected_cells.append(cell) else: - map_dict[position][3] = status + for cell in cells: + if get_status(cell) == filter and get_status(cell) != "=": + selected_cells.append(cell) + + gs = [] + for cell in selected_cells: + gs.append(get_g(cell)) # get_f(cell, accumulated_g) + min_g = min(gs) + + min_cells_by_g = [] + for cell in selected_cells: + if get_g(cell) == min_g: # get_f(cell, accumulated_g) + min_cells_by_g.append(cell) -def print_cells_paremeters(cells:list): - str = "" - for cell in cells: - str += f"({cell[0]},{cell[1]}): {map_dict[cell][0]} + {map_dict[cell][1]} = {map_dict[cell][2]} ({map_dict[cell][3]}) | " - print(str) - -def print_cells_dict_paremeters(cells_dict:dict): - str = "" - for cell in cells_dict.items(): - str += f"({cell[0][0]},{cell[0][1]}): {cell[1][0]} + {cell[1][1]} = {cell[1][2]} ({cell[1][3]}) | " - print(str) + + next_cell = min_cells_by_g[0] + return next_cell -def get_local_walkable_cells(actor): - local_walkable_cells = dict() - for cell in get_walkable_cells(actor): - if actor == observer and map_dict[cell][3] == "=": - local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], float("inf"), map_dict[cell][3]] - elif actor == observer and map_dict[cell][3] == "-": - local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2] + 100000, map_dict[cell][3]] - else: - local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] - return local_walkable_cells - -def calculate_next_cell(actor): - - # making local mutable walkable cells dictionary - walkable_cells_dict = dict() - #walkable_cells_dict = get_local_walkable_cells(actor) - for cell in get_walkable_cells(actor): - if actor == observer and map_dict[cell][3] == "=": - walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] - elif actor == observer and map_dict[cell][3] == "-": - walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], 1000000 + map_dict[cell][2], map_dict[cell][3]] - else: - walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] - - + +def calculate_minimal_cell(cells:list, filter=None): + selected_cells = [] + if filter == None: + for cell in cells: + if get_status(cell) != "=": + selected_cells.append(cell) + else: + for cell in cells: + if get_status(cell) == filter and get_status(cell) != "=": + selected_cells.append(cell) + fs = [] - for cell_values in walkable_cells_dict.values(): - fs.append(cell_values[2]) # get_f(cell, accumulated_g) + for cell in selected_cells: + fs.append(get_f(cell)) # get_f(cell, accumulated_g) min_f = min(fs) min_cells_by_f = [] - for cell in walkable_cells_dict.keys(): - if walkable_cells_dict[cell][2] == min_f: # get_f(cell, accumulated_g) + for cell in selected_cells: + if get_f(cell) == min_f: # get_f(cell, accumulated_g) min_cells_by_f.append(cell) hs = [] for cell in min_cells_by_f: - hs.append(walkable_cells_dict[cell][0]) #get_h(cell) + hs.append(get_h(cell)) #get_h(cell) min_h = min(hs) min_cells_by_h = [] for cell in min_cells_by_f: - if walkable_cells_dict[cell][0] == min_h: #get_h(cell) + if get_h(cell) == min_h: #get_h(cell) min_cells_by_h.append(cell) next_cell = min_cells_by_h[0] return next_cell -def read_system(): - number_of_items = int(input()) - if number_of_items == 0: - return False - items = {} - for _ in range(number_of_items): - x, y, status = input().split(' ') - items[(int(x), int(y))] = status - return items - -def regenerate_route(): - global green_cells, red_cells, black_cells, neo, observer - observer = neo - previous_cell = () - finish = False - green_cell_found = False - - - while not finish: - map_dict[observer][1] += 5 - - # setting green cells - for cell in get_walkable_cells(observer): - if map_dict[cell][3] not in "kon-#=": - set_status(cell, '+') - - for cell in get_walkable_cells(observer): - map_dict[cell][0] = get_h(cell) - map_dict[cell][1] = get_g(cell) - map_dict[cell][2] = get_f(cell) - - # check if there is any green cell - green_count = 0 - for cell in get_walkable_cells(observer): - if map_dict[cell][3] == "+": - green_cell_found = True - green_count += 1 - set_status(observer, "-") - if green_count == 0 and green_cell_found: - print("return") - print_cells_paremeters(get_walkable_cells(observer)) - #print_map() - - regenerate_route() - break - - - next_cell = calculate_next_cell(observer) - - - - #print_cells_paremeters(get_walkable_cells(observer)) - print_cells_paremeters(get_walkable_cells(observer)) - print_map() - - previous_cell = observer - observer = next_cell - - - set_status(previous_cell, '-') - - #accumulated_g += 1 - red_cells.append(next_cell) - - - - time.sleep(0.1) - if observer == keymaster: - finish = True - - -def do_step(): +'''def roll_back(looking_for_cell:tuple): global neo - next_cell = calculate_next_cell(neo) - neo = next_cell - print(f"m {neo[0]} {neo[1]}") + time.sleep(0.1) + while(get_previous(neo) != None and looking_for_cell not in get_walkable_cells_list(neo)): + print_cells_paremeters(get_walkable_cells_list(neo)) + print_map() + neo = get_previous(neo) + time.sleep(0.1)''' + +def roll_back(looking_for_cell:tuple): + global neo + time.sleep(0.1) + returned_to_start = False + while(looking_for_cell not in get_walkable_cells_list(neo)): + print_cells_paremeters(get_walkable_cells_list(neo)) + print_map() + if get_previous(neo) != None: + returned_to_start = True + return returned_to_start + else: + neo = get_previous(neo) + time.sleep(0.1) + +# TODO MAYBE MAKE A FUNCTION THAT TECALCULATES ALL "PREVIOSES" ON THE MAP USING assign_previous(cell, calculate_cell_with_minimal_g(get_walkable_cells_list(cell), "-")) +keymaker = (5,6) -perception_radius = 2 #input() -keymaster = (6,4) #get_position_input() +initialize_map_dict() +calculate_all_h() -initialize_weighted_map_dict() -set_status((0,4),'=') -set_status((1,3),'=') -set_status((2,2),'=') -set_status((3,1),'=') -#set_status((4,0),'=') # TODO MAKE ERROR IF ALL PATHS ARE BLOCKED -regenerate_route() +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() +print_cells_paremeters(get_walkable_cells_list(neo)) +time.sleep(0.1) finish = False - while (finish == False): - do_step() + make_closed(neo) + for cell in get_walkable_cells_list(neo): + if get_status(cell) == ".": + make_opened(cell) + # TODO find and connect to minimum f|h closed in its own walkable radius + if get_status(cell) == "+": + if get_g(cell) > (get_g(neo) + 1): + set_g(cell, (get_g(neo) + 1)) # TODO MAKE A CHECK IF EXISTING g SMALLER THAN NEW ONE + + next_cell = calculate_minimal_cell(list(map_dict.keys()), "+") + if next_cell != get_walkable_cells_list(neo): + returned_to_start = roll_back(next_cell) + assign_previous(next_cell, calculate_cell_with_minimal_g(get_walkable_cells_list(next_cell), "-")) + previous = get_previous(next_cell) + print_cells_paremeters(get_walkable_cells_list(neo)) print_map() - recieved_inputs = read_system() - - for inpt in recieved_inputs.items(): - if inpt[1] == "P": - set_status(inpt[0], "=") + neo = next_cell - '''#refreshing the path - for item in map_dict.items(): - if item[1][3] not in "=": - set_status(item[0],".")''' - - regenerate_route() - + time.sleep(0.2) + if neo == keymaker: + finish = True + # TODO MAKE CHECK IF NO PATH EXISTS \ No newline at end of file diff --git a/main3.py b/main3.py new file mode 100644 index 0000000..66ebdd6 --- /dev/null +++ b/main3.py @@ -0,0 +1,238 @@ +import time +MAP_SIZE = 9 +start = (0, 0) +neo = start +observer = neo +keymaster = () + +green_cells = [] #open cells + +red_cells = [] #closed cells - +black_cells = [] #blocked cells = +blue_cells = [] #traversed cells # + +steps_count = 0 +#accumulated_g = 0 +map_dict = dict() + +def initialize_weighted_map_dict(): + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + map_dict[(x, y)] = [0, 0, 0, '.'] # (x,y) : (h, g, f, status) ??? [float("inf"), float("inf"), float("inf"), '.'] + + +def print_map(): + map_str = "" + + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + if map_dict[(x,y)][3] in "kon": + set_status((x,y), '.') + + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + + set_status(keymaster, 'k') + set_status(observer, 'o') + set_status(neo, 'n') + + map_str += " " + map_dict[(x, y)][3] + " " + map_str += "\n" + print(map_str) + +def get_g(cell): # TODO MAYBE FIX NEEDED + #new_g = accumulated_g + 1 + #if accumulated_g + 1 < map_dict[cell][1]: + # new_g = map_dict[cell][1] + return map_dict[cell][1] + 1 + +def get_h(cell): + return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1]) + +def get_f(cell): + if map_dict[cell][3] == '=': + return float("inf") + return get_g(cell) + get_h(cell) + +def get_walkable_cells(actor:tuple): + potential_positions = [ + (actor[0], actor[1] + 1), (actor[0], actor[1] - 1), + (actor[0] - 1, actor[1]), (actor[0] + 1, actor[1]) + ] + return [pos for pos in potential_positions if pos[0] in range(MAP_SIZE) and pos[1] in range(MAP_SIZE)] + +def get_position_input(): + position_input_list = input().split(" ") + return int(position_input_list[0]), int(position_input_list[1]) + +def set_status(position:tuple, status:str): + if status == ".": + map_dict[position] = [0, 0, 0, '.'] + else: + map_dict[position][3] = status + +def print_cells_paremeters(cells:list): + str = "" + for cell in cells: + str += f"({cell[0]},{cell[1]}): {map_dict[cell][0]} + {map_dict[cell][1]} = {map_dict[cell][2]} ({map_dict[cell][3]}) | " + print(str) + +def print_cells_dict_paremeters(cells_dict:dict): + str = "" + for cell in cells_dict.items(): + str += f"({cell[0][0]},{cell[0][1]}): {cell[1][0]} + {cell[1][1]} = {cell[1][2]} ({cell[1][3]}) | " + print(str) + +def get_local_walkable_cells(actor): + local_walkable_cells = dict() + for cell in get_walkable_cells(actor): + if actor == observer and map_dict[cell][3] == "=": + local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], float("inf"), map_dict[cell][3]] + elif actor == observer and map_dict[cell][3] == "-": + local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2] + 100000, map_dict[cell][3]] + else: + local_walkable_cells[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] + return local_walkable_cells + +def calculate_next_cell(actor): + + # making local mutable walkable cells dictionary + walkable_cells_dict = dict() + #walkable_cells_dict = get_local_walkable_cells(actor) + for cell in get_walkable_cells(actor): + if actor == observer and map_dict[cell][3] == "=": + walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] + elif actor == observer and map_dict[cell][3] == "-": + walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], 1000000 + map_dict[cell][2], map_dict[cell][3]] + else: + walkable_cells_dict[cell] = [map_dict[cell][0], map_dict[cell][1], map_dict[cell][2], map_dict[cell][3]] + + + fs = [] + for cell_values in walkable_cells_dict.values(): + fs.append(cell_values[2]) # get_f(cell, accumulated_g) + min_f = min(fs) + + min_cells_by_f = [] + for cell in walkable_cells_dict.keys(): + if walkable_cells_dict[cell][2] == min_f: # get_f(cell, accumulated_g) + min_cells_by_f.append(cell) + + hs = [] + for cell in min_cells_by_f: + hs.append(walkable_cells_dict[cell][0]) #get_h(cell) + min_h = min(hs) + + min_cells_by_h = [] + for cell in min_cells_by_f: + if walkable_cells_dict[cell][0] == min_h: #get_h(cell) + min_cells_by_h.append(cell) + + next_cell = min_cells_by_h[0] + return next_cell + +def read_system(): + number_of_items = int(input()) + if number_of_items == 0: + return False + items = {} + for _ in range(number_of_items): + x, y, status = input().split(' ') + items[(int(x), int(y))] = status + return items + +def regenerate_route(): + global green_cells, red_cells, black_cells, neo, observer + observer = neo + previous_cell = () + finish = False + green_cell_found = False + + + while not finish: + map_dict[observer][1] += 5 + + # setting green cells + for cell in get_walkable_cells(observer): + if map_dict[cell][3] not in "kon-#=": + set_status(cell, '+') + + for cell in get_walkable_cells(observer): + map_dict[cell][0] = get_h(cell) + map_dict[cell][1] = get_g(cell) + map_dict[cell][2] = get_f(cell) + + # check if there is any green cell + green_count = 0 + for cell in get_walkable_cells(observer): + if map_dict[cell][3] == "+": + green_cell_found = True + green_count += 1 + set_status(observer, "-") + if green_count == 0 and green_cell_found: + print("return") + print_cells_paremeters(get_walkable_cells(observer)) + #print_map() + + regenerate_route() + break + + + next_cell = calculate_next_cell(observer) + + + + #print_cells_paremeters(get_walkable_cells(observer)) + print_cells_paremeters(get_walkable_cells(observer)) + print_map() + + previous_cell = observer + observer = next_cell + + + set_status(previous_cell, '-') + + #accumulated_g += 1 + red_cells.append(next_cell) + + + + time.sleep(0.1) + if observer == keymaster: + finish = True + + +def do_step(): + global neo + next_cell = calculate_next_cell(neo) + neo = next_cell + print(f"m {neo[0]} {neo[1]}") + +perception_radius = 2 #input() +keymaster = (6,4) #get_position_input() + +initialize_weighted_map_dict() +set_status((0,4),'=') +set_status((1,3),'=') +set_status((2,2),'=') +set_status((3,1),'=') +#set_status((4,0),'=') # TODO MAKE ERROR IF ALL PATHS ARE BLOCKED +regenerate_route() + +finish = False + +while (finish == False): + do_step() + print_map() + recieved_inputs = read_system() + + for inpt in recieved_inputs.items(): + if inpt[1] == "P": + set_status(inpt[0], "=") + + '''#refreshing the path + for item in map_dict.items(): + if item[1][3] not in "=": + set_status(item[0],".")''' + + regenerate_route() +