Commit before fith attempt
This commit is contained in:
@@ -17,7 +17,7 @@ map_dict = dict()
|
||||
def initialize_weighted_map_dict():
|
||||
for x in range(MAP_SIZE):
|
||||
for y in range(MAP_SIZE):
|
||||
map_dict[(x, y)] = [float("inf"), float("inf"), float("inf"), '.'] # (x,y) : (h, g, f, status)
|
||||
map_dict[(x, y)] = [0, 0, 0, '.'] # (x,y) : (h, g, f, status) ??? [float("inf"), float("inf"), float("inf"), '.']
|
||||
|
||||
|
||||
def print_map():
|
||||
@@ -39,16 +39,19 @@ def print_map():
|
||||
map_str += "\n"
|
||||
print(map_str)
|
||||
|
||||
def get_g(cell, accumulated_g): # TODO MAYBE FIX NEEDED
|
||||
return accumulated_g + 1
|
||||
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, accumulated_g):
|
||||
def get_f(cell):
|
||||
if map_dict[cell][3] == '=':
|
||||
return float("inf")
|
||||
return get_g(cell, accumulated_g) + get_h(cell)
|
||||
return get_g(cell) + get_h(cell)
|
||||
|
||||
def get_walkable_cells(actor:tuple):
|
||||
potential_positions = [
|
||||
@@ -62,46 +65,91 @@ def get_position_input():
|
||||
return int(position_input_list[0]), int(position_input_list[1])
|
||||
|
||||
def set_status(position:tuple, status:str):
|
||||
map_dict[position][3] = status
|
||||
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 calculate_next_cell(actor, accumulated_g):
|
||||
|
||||
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 in get_walkable_cells(actor):
|
||||
fs.append(get_f(cell, accumulated_g))
|
||||
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 get_walkable_cells(actor):
|
||||
if get_f(cell, accumulated_g) == min_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(get_h(cell))
|
||||
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 get_h(cell) == min_h:
|
||||
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
|
||||
accumulated_g = 0
|
||||
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):
|
||||
@@ -110,32 +158,81 @@ def regenerate_route():
|
||||
|
||||
for cell in get_walkable_cells(observer):
|
||||
map_dict[cell][0] = get_h(cell)
|
||||
map_dict[cell][1] = get_g(cell, accumulated_g)
|
||||
map_dict[cell][2] = get_f(cell, accumulated_g)
|
||||
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, accumulated_g)
|
||||
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
|
||||
#accumulated_g += 1
|
||||
red_cells.append(next_cell)
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
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),'=') # TODO FIX STUCK IN A DEAD END ISSUE
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user