Corrected optimizer and landmark attributes in backend

This commit is contained in:
Helldragon67 2024-06-26 14:23:51 +02:00
parent 8e33bd1b3f
commit bec1827891
3 changed files with 34 additions and 30 deletions

View File

@ -19,9 +19,9 @@ def print_res(L: List[Landmark], L_tot):
dist = 0 dist = 0
for elem in L : for elem in L :
if elem.name != 'start' : if elem.time_to_reach_next is not None :
print('- ' + elem.name + ', time to reach = ' + str(elem.time_to_reach)) print('- ' + elem.name + ', time to reach next = ' + str(elem.time_to_reach_next))
dist += elem.time_to_reach dist += elem.time_to_reach_next
else : else :
print('- ' + elem.name) print('- ' + elem.name)
@ -300,7 +300,7 @@ def respect_order(N: int, A_eq, b_eq):
# Computes the time to reach from each landmark to the next # Computes the time to reach from each landmark to the next
def add_time_to_reach(order: List[int], landmarks: List[Landmark])->List[Landmark] : def link_list(order: List[int], landmarks: List[Landmark])->List[Landmark] :
# Read the parameters from the file # Read the parameters from the file
with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f : with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f :
@ -308,24 +308,25 @@ def add_time_to_reach(order: List[int], landmarks: List[Landmark])->List[Landmar
detour_factor = parameters['detour factor'] detour_factor = parameters['detour factor']
speed = parameters['average walking speed'] speed = parameters['average walking speed']
L = []
j = 0 j = 0
L = [] total_dist = 0
prev = landmarks[0] while j < len(order)-1 :
while(len(L) != len(order)) :
elem = landmarks[order[j]] elem = landmarks[order[j]]
if elem != prev : next = landmarks[order[j+1]]
elem.time_to_reach = get_distance(elem.location, prev.location, detour_factor, speed)[1]
elem.must_do = True d = get_distance(elem.location, next.location, detour_factor, speed)[1]
elem.time_to_reach_next = d
L.append(elem) L.append(elem)
prev = elem
j += 1 j += 1
total_dist += d
return L L.append(next)
return L, total_dist
def add_time_to_reach_simple(ordered_visit: List[Landmark])-> List[Landmark] : def link_list_simple(ordered_visit: List[Landmark])-> List[Landmark] :
# Read the parameters from the file # Read the parameters from the file
with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f : with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f :
@ -334,17 +335,20 @@ def add_time_to_reach_simple(ordered_visit: List[Landmark])-> List[Landmark] :
speed = parameters['average walking speed'] speed = parameters['average walking speed']
L = [] L = []
prev = ordered_visit[0] j = 0
L.append(prev)
total_dist = 0 total_dist = 0
while j < len(ordered_visit)-1 :
elem = ordered_visit[j]
next = ordered_visit[j+1]
for elem in ordered_visit[1:] : elem.next_uuid = next.uuid
elem.time_to_reach = get_distance(elem.location, prev.location, detour_factor, speed)[1] d = get_distance(elem.location, next.location, detour_factor, speed)[1]
elem.must_do = True elem.time_to_reach_next = d
L.append(elem) L.append(elem)
prev = elem j += 1
total_dist += get_distance(elem.location, prev.location, detour_factor, speed)[1] total_dist += d
L.append(next)
return L, total_dist return L, total_dist
@ -394,7 +398,7 @@ def solve_optimization (landmarks :List[Landmark], max_steps: int, printing_deta
raise TimeoutError(f"Optimization took too long. No solution found after {timeout} iterations.") raise TimeoutError(f"Optimization took too long. No solution found after {timeout} iterations.")
# Add the times to reach and stop optimizing # Add the times to reach and stop optimizing
L = add_time_to_reach(order, landmarks) L, total_dist = link_list(order, landmarks)
if printing_details is True : if printing_details is True :
if i != 0 : if i != 0 :

View File

@ -9,7 +9,7 @@ from math import pi
from structs.landmarks import Landmark from structs.landmarks import Landmark
from landmarks_manager import take_most_important from landmarks_manager import take_most_important
from optimizer import solve_optimization, add_time_to_reach_simple, print_res, get_distance from optimizer import solve_optimization, link_list_simple, print_res, get_distance
def create_corridor(landmarks: List[Landmark], width: float) : def create_corridor(landmarks: List[Landmark], width: float) :
@ -214,7 +214,7 @@ def refine_optimization(landmarks: List[Landmark], base_tour: List[Landmark], ma
# get a new tour # get a new tour
new_tour = solve_optimization(full_set, max_time, False) new_tour = solve_optimization(full_set, max_time, False)
new_tour, new_dist = add_time_to_reach_simple(new_tour) new_tour, new_dist = link_list_simple(new_tour)
"""#if base_tour[0].location == base_tour[-1].location : """#if base_tour[0].location == base_tour[-1].location :
if False : if False :
@ -276,7 +276,7 @@ def refine_optimization(landmarks: List[Landmark], base_tour: List[Landmark], ma
better_tour, better_poly = find_shortest_path_through_all_landmarks(new_tour) better_tour, better_poly = find_shortest_path_through_all_landmarks(new_tour)
better_tour, better_dist = add_time_to_reach_simple(better_tour) better_tour, better_dist = link_list_simple(better_tour)
if new_dist < better_dist : if new_dist < better_dist :
final_tour = new_tour final_tour = new_tour

View File

@ -110,7 +110,7 @@ def test4(coordinates: tuple[float, float]) -> List[Landmark]:
return refined_tour return refined_tour
#test4(tuple((48.8344400, 2.3220540))) # Café Chez César test4(tuple((48.8344400, 2.3220540))) # Café Chez César
#test4(tuple((48.8375946, 2.2949904))) # Point random #test4(tuple((48.8375946, 2.2949904))) # Point random
test4(tuple((47.377859, 8.540585))) # Zurich HB #test4(tuple((47.377859, 8.540585))) # Zurich HB
#test3('Vienna, Austria') #test3('Vienna, Austria')