From bec182789196be01e0882d419292c74145def637 Mon Sep 17 00:00:00 2001
From: Helldragon67 <kilian.scheidecker@orange.fr>
Date: Wed, 26 Jun 2024 14:23:51 +0200
Subject: [PATCH] Corrected optimizer and landmark attributes in backend

---
 backend/src/optimizer.py | 54 +++++++++++++++++++++-------------------
 backend/src/refiner.py   |  6 ++---
 backend/src/tester.py    |  4 +--
 3 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/backend/src/optimizer.py b/backend/src/optimizer.py
index d45d51b..b346a8a 100644
--- a/backend/src/optimizer.py
+++ b/backend/src/optimizer.py
@@ -19,9 +19,9 @@ def print_res(L: List[Landmark], L_tot):
 
     dist = 0
     for elem in L : 
-        if elem.name != 'start' :
-            print('- ' + elem.name + ', time to reach = ' + str(elem.time_to_reach))
-            dist += elem.time_to_reach
+        if elem.time_to_reach_next is not None :
+            print('- ' + elem.name + ', time to reach next = ' + str(elem.time_to_reach_next))
+            dist += elem.time_to_reach_next
         else : 
             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
-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
     with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f :
@@ -308,44 +308,48 @@ def add_time_to_reach(order: List[int], landmarks: List[Landmark])->List[Landmar
         detour_factor = parameters['detour factor']
         speed = parameters['average walking speed']
     
+    L =  []
     j = 0
-    L =  []
-    prev = landmarks[0]
-
-    while(len(L) != len(order)) :
-        
+    total_dist = 0
+    while j < len(order)-1 :
         elem = landmarks[order[j]]
-        if elem != prev :
-            elem.time_to_reach = get_distance(elem.location, prev.location, detour_factor, speed)[1]
-            elem.must_do = True
+        next = landmarks[order[j+1]]
+
+        d = get_distance(elem.location, next.location, detour_factor, speed)[1]
+        elem.time_to_reach_next = d
         L.append(elem)
-        prev = elem   
         j += 1
+        total_dist += d
+
+    L.append(next)
     
-    return L
+    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
     with open (os.path.dirname(os.path.abspath(__file__)) + '/parameters/optimizer.params', "r") as f :
         parameters = json.loads(f.read())
         detour_factor = parameters['detour factor']
         speed = parameters['average walking speed']
-    
+
     L =  []
-    prev = ordered_visit[0]
-    L.append(prev)
+    j = 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.time_to_reach = get_distance(elem.location, prev.location, detour_factor, speed)[1]
-        elem.must_do = True
+        elem.next_uuid = next.uuid
+        d = get_distance(elem.location, next.location, detour_factor, speed)[1]
+        elem.time_to_reach_next = d
         L.append(elem)
-        prev = elem
-        total_dist += get_distance(elem.location, prev.location, detour_factor, speed)[1]
-        
+        j += 1
+        total_dist += d
 
+    L.append(next)
+    
     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.")
 
         # 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 i != 0 :
diff --git a/backend/src/refiner.py b/backend/src/refiner.py
index dec6101..282f2f9 100644
--- a/backend/src/refiner.py
+++ b/backend/src/refiner.py
@@ -9,7 +9,7 @@ from math import pi
 
 from structs.landmarks import Landmark
 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) :
@@ -214,7 +214,7 @@ def refine_optimization(landmarks: List[Landmark], base_tour: List[Landmark], ma
 
   # get a new tour
   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 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_dist = add_time_to_reach_simple(better_tour)
+  better_tour, better_dist = link_list_simple(better_tour)
 
   if new_dist < better_dist :
     final_tour = new_tour
diff --git a/backend/src/tester.py b/backend/src/tester.py
index 758e003..91a12b2 100644
--- a/backend/src/tester.py
+++ b/backend/src/tester.py
@@ -110,7 +110,7 @@ def test4(coordinates: tuple[float, float]) -> List[Landmark]:
     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((47.377859, 8.540585)))         # Zurich HB
+#test4(tuple((47.377859, 8.540585)))         # Zurich HB
 #test3('Vienna, Austria')
\ No newline at end of file