further cleanup

This commit is contained in:
2024-07-08 11:55:00 +02:00
parent f9c86261cb
commit 8f23a4747d
4 changed files with 182 additions and 277 deletions

View File

@@ -1,51 +1,45 @@
from optimizer import solve_optimization
from refiner import refine_optimization
from landmarks_manager import generate_landmarks
# from refiner import refine_optimization
from landmarks_manager import LandmarkManager
from structs.landmarks import Landmark
from structs.landmarktype import LandmarkType
from structs.preferences import Preferences, Preference
from structs.preferences import Preferences
from fastapi import FastAPI, Query, Body
from typing import List
app = FastAPI()
manager = LandmarkManager()
# TODO: needs a global variable to store the landmarks accross function calls
# linked_tour = []
# Assuming frontend is calling like this :
#"http://127.0.0.1:8000/process?param1={param1}&param2={param2}"
@app.post("/optimizer_coords/{start_lat}/{start_lon}/{finish_lat}/{finish_lon}")
def main1(start_lat: float, start_lon: float, preferences: Preferences = Body(...), finish_lat: float = None, finish_lon: float = None) -> List[Landmark]:
@app.post("/route/new")
def main1(preferences: Preferences, start: tuple[float, float], end: tuple[float, float] = None) -> str:
'''
Main function to call the optimizer.
:param preferences: the preferences specified by the user as the post body
:param start: the coordinates of the starting point as a tuple of floats (as url query parameters)
:param end: the coordinates of the finishing point as a tuple of floats (as url query parameters)
:return: the uuid of the first landmark in the optimized route
'''
if preferences is None :
raise ValueError("Please provide preferences in the form of a 'Preference' BaseModel class.")
if bool(start_lat) ^ bool(start_lon) :
raise ValueError("Please provide both latitude and longitude for the starting point")
if bool(finish_lat) ^ bool(finish_lon) :
raise ValueError("Please provide both latitude and longitude for the finish point")
if start is None:
raise ValueError("Please provide the starting coordinates as a tuple of floats.")
if end is None:
end = start
start = Landmark(name='start', type=LandmarkType(landmark_type='start'), location=(start_lat, start_lon), osm_type='start', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
start_landmark = Landmark(name='start', type=LandmarkType(landmark_type='start'), location=(start[0], start[1]), osm_type='start', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
end_landmark = Landmark(name='end', type=LandmarkType(landmark_type='end'), location=(end[0], end[1]), osm_type='end', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
if bool(finish_lat) and bool(finish_lon) :
finish = Landmark(name='finish', type=LandmarkType(landmark_type='finish'), location=(finish_lat, finish_lon), osm_type='finish', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
else :
finish = Landmark(name='finish', type=LandmarkType(landmark_type='finish'), location=(start_lat, start_lon), osm_type='finish', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
start = Landmark(name='start', type=LandmarkType(landmark_type='start'), location=(48.8375946, 2.2949904), osm_type='start', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
finish = Landmark(name='finish', type=LandmarkType(landmark_type='finish'), location=(48.8375946, 2.2949904), osm_type='finish', osm_id=0, attractiveness=0, must_do=True, n_tags = 0)
# Generate the landmarks from the start location
landmarks, landmarks_short = generate_landmarks(preferences=preferences, coordinates=start.location)
landmarks, landmarks_short = LandmarkManager.get_landmark_lists(preferences=preferences, coordinates=start.location)
print([l.name for l in landmarks_short])
# insert start and finish to the landmarks list
landmarks_short.insert(0, start)
landmarks_short.append(finish)
landmarks_short.insert(0, start_landmark)
landmarks_short.append(end_landmark)
# TODO use these parameters in another way
# TODO infer these parameters from the preferences
max_walking_time = 4 # hours
detour = 30 # minutes
@@ -53,32 +47,11 @@ def main1(start_lat: float, start_lon: float, preferences: Preferences = Body(..
base_tour = solve_optimization(landmarks_short, max_walking_time*60, True)
# Second stage optimization
refined_tour = refine_optimization(landmarks, base_tour, max_walking_time*60+detour, True)
# refined_tour = refine_optimization(landmarks, base_tour, max_walking_time*60+detour, True)
# TODO: should look something like this
# # set time to reach and transform into fully functional linked list
# linked_tour += link(refined_tour)
# return {
# 'city_name': 'Paris',
# 'n_stops': len(linked_tour),
# 'first_landmark_uuid': linked_tour[0].uuid,
# }
return refined_tour
# input city, country in the form of 'Paris, France'
@app.post("/test2/{city_country}")
def test2(city_country: str, preferences: Preferences = Body(...)) -> List[Landmark]:
landmarks = generate_landmarks(city_country, preferences)
max_steps = 9000000
visiting_order = solve_optimization(landmarks, max_steps, True)
# linked_tour = ...
# return linked_tour[0].uuid
return base_tour[0].uuid
@@ -86,5 +59,3 @@ def test2(city_country: str, preferences: Preferences = Body(...)) -> List[Landm
def get_landmark(landmark_uuid: str) -> Landmark:
#cherche dans linked_tour et retourne le landmark correspondant
pass