better errorhandling, slimmed down optimizer
This commit is contained in:
		@@ -29,9 +29,11 @@ def new_trip(preferences: Preferences, start: tuple[float, float], end: tuple[fl
 | 
			
		||||
    :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.")
 | 
			
		||||
        raise HTTPException(status_code=406, detail="Preferences not provided")
 | 
			
		||||
    if preferences.shopping.score == 0 and preferences.sightseeing.score == 0 and preferences.nature.score == 0:
 | 
			
		||||
        raise HTTPException(status_code=406, detail="All preferences are 0.")
 | 
			
		||||
    if start is None:
 | 
			
		||||
        raise ValueError("Please provide the starting coordinates as a tuple of floats.")
 | 
			
		||||
        raise HTTPException(status_code=406, detail="Start coordinates not provided")
 | 
			
		||||
    if end is None:
 | 
			
		||||
        end = start
 | 
			
		||||
        logger.info("No end coordinates provided. Using start=end.")
 | 
			
		||||
@@ -50,7 +52,12 @@ def new_trip(preferences: Preferences, start: tuple[float, float], end: tuple[fl
 | 
			
		||||
    landmarks_short.append(end_landmark)
 | 
			
		||||
    
 | 
			
		||||
    # First stage optimization
 | 
			
		||||
    base_tour = optimizer.solve_optimization(preferences.max_time_minute, landmarks_short)
 | 
			
		||||
    try:
 | 
			
		||||
        base_tour = optimizer.solve_optimization(preferences.max_time_minute, landmarks_short)
 | 
			
		||||
    except ArithmeticError:
 | 
			
		||||
        raise HTTPException(status_code=500, detail="No solution found")
 | 
			
		||||
    except TimeoutError:
 | 
			
		||||
        raise HTTPException(status_code=500, detail="Optimzation took too long")
 | 
			
		||||
    
 | 
			
		||||
    # Second stage optimization
 | 
			
		||||
    refined_tour = refiner.refine_optimization(landmarks, base_tour, preferences.max_time_minute, preferences.detour_tolerance_minute)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
city_bbox_side: 5000 #m
 | 
			
		||||
radius_close_to: 50
 | 
			
		||||
church_coeff: 0.8
 | 
			
		||||
park_coeff: 1.2
 | 
			
		||||
park_coeff: 1.0
 | 
			
		||||
tag_coeff: 10
 | 
			
		||||
N_important: 40
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,6 @@ class LandmarkManager:
 | 
			
		||||
 | 
			
		||||
    logger = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
    city_bbox_side: int     # bbox side in meters
 | 
			
		||||
    radius_close_to: int    # radius in meters
 | 
			
		||||
    church_coeff: float     # coeff to adjsut score of churches
 | 
			
		||||
    park_coeff: float       # coeff to adjust score of parks
 | 
			
		||||
@@ -36,12 +35,17 @@ class LandmarkManager:
 | 
			
		||||
 | 
			
		||||
        with constants.LANDMARK_PARAMETERS_PATH.open('r') as f:
 | 
			
		||||
            parameters = yaml.safe_load(f)
 | 
			
		||||
            self.city_bbox_side = parameters['city_bbox_side']
 | 
			
		||||
            self.max_bbox_side = parameters['city_bbox_side']
 | 
			
		||||
            self.radius_close_to = parameters['radius_close_to']
 | 
			
		||||
            self.church_coeff = parameters['church_coeff']
 | 
			
		||||
            self.park_coeff = parameters['park_coeff']
 | 
			
		||||
            self.tag_coeff = parameters['tag_coeff']
 | 
			
		||||
            self.N_important = parameters['N_important']
 | 
			
		||||
            
 | 
			
		||||
        with constants.OPTIMIZER_PARAMETERS_PATH.open('r') as f:
 | 
			
		||||
            parameters = yaml.safe_load(f)
 | 
			
		||||
            self.walking_speed = parameters['average_walking_speed']
 | 
			
		||||
            self.detour_factor = parameters['detour_factor']
 | 
			
		||||
 | 
			
		||||
        self.overpass = Overpass()
 | 
			
		||||
        CachingStrategy.use(JSON, cacheDir=constants.OSM_CACHE_DIR)
 | 
			
		||||
@@ -65,23 +69,26 @@ class LandmarkManager:
 | 
			
		||||
                - A list of the most important landmarks based on the user's preferences.
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
        max_walk_dist = (preferences.max_time_minute/2)/60*self.walking_speed*1000/self.detour_factor
 | 
			
		||||
        reachable_bbox_side = min(max_walk_dist, self.max_bbox_side)
 | 
			
		||||
 | 
			
		||||
        L = []
 | 
			
		||||
        bbox = self.create_bbox(center_coordinates)
 | 
			
		||||
        bbox = self.create_bbox(center_coordinates, reachable_bbox_side)
 | 
			
		||||
        # list for sightseeing
 | 
			
		||||
        if preferences.sightseeing.score != 0:
 | 
			
		||||
            score_function = lambda loc, n_tags: int((self.count_elements_close_to(loc) + ((n_tags**1.2)*self.tag_coeff) )*self.church_coeff)  
 | 
			
		||||
            score_function = lambda loc, n_tags: int((((n_tags**1.2)*self.tag_coeff) )*self.church_coeff)   # self.count_elements_close_to(loc) +
 | 
			
		||||
            L1 = self.fetch_landmarks(bbox, self.amenity_selectors['sightseeing'], preferences.sightseeing.type, score_function)
 | 
			
		||||
            L += L1
 | 
			
		||||
 | 
			
		||||
        # list for nature
 | 
			
		||||
        if preferences.nature.score != 0:
 | 
			
		||||
            score_function = lambda loc, n_tags: int((self.count_elements_close_to(loc) + ((n_tags**1.2)*self.tag_coeff) )*self.park_coeff)  
 | 
			
		||||
            score_function = lambda loc, n_tags: int((((n_tags**1.2)*self.tag_coeff) )*self.park_coeff)   # self.count_elements_close_to(loc) +
 | 
			
		||||
            L2 = self.fetch_landmarks(bbox, self.amenity_selectors['nature'], preferences.nature.type, score_function)
 | 
			
		||||
            L += L2
 | 
			
		||||
 | 
			
		||||
        # list for shopping
 | 
			
		||||
        if preferences.shopping.score != 0:
 | 
			
		||||
            score_function = lambda loc, n_tags: int(self.count_elements_close_to(loc) + ((n_tags**1.2)*self.tag_coeff))
 | 
			
		||||
            score_function = lambda loc, n_tags: int(((n_tags**1.2)*self.tag_coeff)) # self.count_elements_close_to(loc) +
 | 
			
		||||
            L3 = self.fetch_landmarks(bbox, self.amenity_selectors['shopping'], preferences.shopping.type, score_function)
 | 
			
		||||
            L += L3
 | 
			
		||||
 | 
			
		||||
@@ -183,12 +190,13 @@ class LandmarkManager:
 | 
			
		||||
            return 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def create_bbox(self, coordinates: tuple[float, float]) -> tuple[float, float, float, float]:
 | 
			
		||||
    def create_bbox(self, coordinates: tuple[float, float], reachable_bbox_side: int) -> tuple[float, float, float, float]:
 | 
			
		||||
        """
 | 
			
		||||
        Create a bounding box around the given coordinates.
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            coordinates (tuple[float, float]): The latitude and longitude of the center of the bounding box.
 | 
			
		||||
            reachable_bbox_side (int): The side length of the bounding box in meters.
 | 
			
		||||
 | 
			
		||||
        Returns:
 | 
			
		||||
            tuple[float, float, float, float]: The minimum latitude, minimum longitude, maximum latitude, and maximum longitude
 | 
			
		||||
@@ -199,7 +207,7 @@ class LandmarkManager:
 | 
			
		||||
        lon = coordinates[1]
 | 
			
		||||
 | 
			
		||||
        # Half the side length in km (since it's a square bbox)
 | 
			
		||||
        half_side_length_km = self.city_bbox_side / 2 / 1000
 | 
			
		||||
        half_side_length_km = reachable_bbox_side / 2 / 1000
 | 
			
		||||
 | 
			
		||||
        # Convert distance to degrees
 | 
			
		||||
        lat_diff = half_side_length_km / 111  # 1 degree latitude is approximately 111 km
 | 
			
		||||
@@ -288,19 +296,24 @@ class LandmarkManager:
 | 
			
		||||
                        break
 | 
			
		||||
 | 
			
		||||
                    if "wikipedia" in tag:
 | 
			
		||||
                        n_tags += 3             # wikipedia entries count more
 | 
			
		||||
                        n_tags += 1             # wikipedia entries count more
 | 
			
		||||
 | 
			
		||||
                    if tag == "wikidata":
 | 
			
		||||
                        Q = elem.tag('wikidata')
 | 
			
		||||
                        site = Site("wikidata", "wikidata")
 | 
			
		||||
                        item = ItemPage(site, Q)
 | 
			
		||||
                        item.get()
 | 
			
		||||
                        n_languages = len(item.labels)
 | 
			
		||||
                        n_tags += n_languages/10
 | 
			
		||||
                    # if tag == "wikidata":
 | 
			
		||||
                    #     Q = elem.tag('wikidata')
 | 
			
		||||
                    #     site = Site("wikidata", "wikidata")
 | 
			
		||||
                    #     item = ItemPage(site, Q)
 | 
			
		||||
                    #     item.get()
 | 
			
		||||
                    #     n_languages = len(item.labels)
 | 
			
		||||
                    #     n_tags += n_languages/10
 | 
			
		||||
                    if "viewpoint" in tag:
 | 
			
		||||
                        n_tags += 10
 | 
			
		||||
 | 
			
		||||
                    if elem_type != "nature":
 | 
			
		||||
                        if "leisure" in tag and elem.tag('leisure') == "park":
 | 
			
		||||
                            elem_type = "nature"
 | 
			
		||||
                    
 | 
			
		||||
                    if elem_type == "nature":
 | 
			
		||||
                        n_tags += 1 
 | 
			
		||||
 | 
			
		||||
                    if landmarktype != "shopping":
 | 
			
		||||
                        if "shop" in tag:
 | 
			
		||||
@@ -310,7 +323,6 @@ class LandmarkManager:
 | 
			
		||||
                        if tag == "building" and elem.tag('building') in ['retail', 'supermarket', 'parking']:
 | 
			
		||||
                            skip = True
 | 
			
		||||
                            break
 | 
			
		||||
 | 
			
		||||
                if skip:
 | 
			
		||||
                    continue
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user