Обновить route.py
This commit is contained in:
25
route.py
25
route.py
@@ -2,7 +2,7 @@ import math
|
||||
import itertools
|
||||
import requests
|
||||
from typing import List, Tuple, Dict, Optional, Set
|
||||
|
||||
import random
|
||||
class Point:
|
||||
def __init__(self, coord: List[float], tag: str, visit_time: int):
|
||||
self.coord = coord
|
||||
@@ -78,7 +78,7 @@ def group_points_by_significance(points: List[Point], tag_importance: Dict[str,
|
||||
"""Group points by their importance level."""
|
||||
grouped = {}
|
||||
for point in points:
|
||||
importance = tag_importance.get(point.tag, float('inf'))
|
||||
importance = tag_importance.get(point.tag, float('inf'))[0]
|
||||
if importance not in grouped:
|
||||
grouped[importance] = []
|
||||
grouped[importance].append(point)
|
||||
@@ -134,7 +134,7 @@ def generate_routes_exact_tags(grouped_points: Dict[int, List[Point]],
|
||||
# Group points by importance
|
||||
points_by_importance = {}
|
||||
for point in point_combination:
|
||||
imp = tag_importance[point.tag]
|
||||
imp = tag_importance[point.tag][0]
|
||||
if imp not in points_by_importance:
|
||||
points_by_importance[imp] = []
|
||||
points_by_importance[imp].append(point)
|
||||
@@ -187,7 +187,7 @@ def generate_routes_with_repeats(grouped_points: Dict[int, List[Point]],
|
||||
# We have exactly the right number of points
|
||||
points_by_importance = {}
|
||||
for point in mandatory_points:
|
||||
imp = tag_importance[point.tag]
|
||||
imp = tag_importance[point.tag][0]
|
||||
if imp not in points_by_importance:
|
||||
points_by_importance[imp] = []
|
||||
points_by_importance[imp].append(point)
|
||||
@@ -206,8 +206,9 @@ def generate_routes_with_repeats(grouped_points: Dict[int, List[Point]],
|
||||
|
||||
# Get all available points excluding mandatory points
|
||||
all_available_points = []
|
||||
for points_list in grouped_points.values():
|
||||
all_available_points.extend(points_list)
|
||||
for key in grouped_points.keys():
|
||||
if tag_importance[key][1]:
|
||||
all_available_points.extend(grouped_points[key])
|
||||
|
||||
# Remove mandatory points from available points
|
||||
available_points = [p for p in all_available_points if p not in mandatory_points]
|
||||
@@ -252,7 +253,7 @@ def form_point_list(data):
|
||||
point_list.append(point)
|
||||
return point_list
|
||||
|
||||
def build_route(data, mapping,start_coord,total_time,n_nodes):
|
||||
def build_route(data, mapping,start_coord,total_time,n_nodes,strategy='best'):
|
||||
# Example input data - теперь с не более чем 5 уникальными тегами
|
||||
|
||||
start_coord_test = [56.331576, 44.003277]
|
||||
@@ -416,6 +417,12 @@ def build_route(data, mapping,start_coord,total_time,n_nodes):
|
||||
return
|
||||
|
||||
# Step 8: Find optimal route (minimum time)
|
||||
|
||||
if strategy=='random':
|
||||
optimal_route, min_time = random.choice(valid_routes)
|
||||
elif strategy=='longest':
|
||||
optimal_route, min_time = max(valid_routes, key=lambda x: x[1])
|
||||
else:
|
||||
optimal_route, min_time = min(valid_routes, key=lambda x: x[1])
|
||||
|
||||
print(f"\nOptimal route (time: {min_time:.2f} min):")
|
||||
@@ -439,7 +446,7 @@ def build_route(data, mapping,start_coord,total_time,n_nodes):
|
||||
|
||||
# Display all tags covered by the route
|
||||
route_tags = set(point.tag for point in optimal_route)
|
||||
print(f"\nTags covered in this route: {', '.join(route_tags)}")
|
||||
#print(f"\nTags covered in this route: {', '.join(route_tags)}")
|
||||
if all_tags.issubset(route_tags):
|
||||
print("All tags are covered in this route!")
|
||||
|
||||
@@ -449,6 +456,6 @@ def build_route(data, mapping,start_coord,total_time,n_nodes):
|
||||
print("All coordinates in the route are unique!")
|
||||
else:
|
||||
print("ERROR: Duplicate coordinates found in the route!")
|
||||
|
||||
return route_coords
|
||||
#if __name__ == "__main__":
|
||||
# build_route()
|
||||
Reference in New Issue
Block a user