Как я устал править ГА. Добавил дату, но как будто не до конца, сейчас фикшу
This commit is contained in:
parent
25c34daadd
commit
0cb70cc8e4
@ -18,6 +18,10 @@ def load_graph_from_request(request: TripRequest) -> Tuple[Dict[str, Dict[str, L
|
||||
flightsBack = request.flightsBack
|
||||
start_point = request.fromPoint
|
||||
end_point = request.toPoint
|
||||
departure_date = datetime.fromisoformat(request.departureDate).date() # Преобразование строки в объект даты
|
||||
departure_date_return = request.returnDate
|
||||
if departure_date_return is not None and departure_date_return != "":
|
||||
departure_date_return = datetime.fromisoformat(request.returnDate).date() # Преобразование строки в объект даты
|
||||
|
||||
# Добавить каждый полет в граф
|
||||
for flight in flightsTo:
|
||||
@ -69,7 +73,7 @@ def load_graph_from_request(request: TripRequest) -> Tuple[Dict[str, Dict[str, L
|
||||
# Добавление ребра в граф
|
||||
graphBack[departure_point][destination_point] = [flight.distance, departure_time, destination_time]
|
||||
|
||||
return graphTo,graphBack, start_point, end_point, flightsDataTo, flightsDataBack
|
||||
return graphTo,graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return
|
||||
|
||||
|
||||
def path_length_and_time(path, graph):
|
||||
@ -122,7 +126,6 @@ def select_parents(population, graph):
|
||||
return valid_parents[:len(valid_parents) // 2]
|
||||
|
||||
|
||||
|
||||
def crossover(parent1, parent2):
|
||||
crossover_point = len(parent1) // 2
|
||||
child = parent1[:crossover_point]
|
||||
@ -150,7 +153,7 @@ def update_best_paths(best_paths, path, graph, max_best_paths=3):
|
||||
del best_paths[worst_path]
|
||||
|
||||
|
||||
def genetic_algorithm(start, end, graph, flights_data, type, population_size=1000, generations=100):
|
||||
def genetic_algorithm(start, end, graph, flights_data, type, departure_date, population_size=2000, generations=100):
|
||||
population = generate_population(population_size, start, end, graph)
|
||||
best_paths = {} # Словарь для хранения уникальных лучших маршрутов и их длин
|
||||
for generation in range(generations):
|
||||
@ -191,12 +194,12 @@ def genetic_algorithm(start, end, graph, flights_data, type, population_size=100
|
||||
# Проверка на корректность длины пути и времени
|
||||
if length == float('inf') or start_time is None or end_time is None:
|
||||
continue
|
||||
if datetime(start_time.year, start_time.month, start_time.day).date() != departure_date:
|
||||
continue
|
||||
result.append({
|
||||
type: path_data
|
||||
# "start_time": start_time.isoformat() if start_time else None,
|
||||
# "end_time": end_time.isoformat() if end_time else None,
|
||||
# "best_length": length
|
||||
type: path_data,
|
||||
"start_time": start_time.isoformat() if start_time else None,
|
||||
"end_time": end_time.isoformat() if end_time else None,
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
return result
|
@ -17,16 +17,16 @@ router = APIRouter(
|
||||
|
||||
@router.post("/get_flight/")
|
||||
async def get_flight(request: TripRequest):
|
||||
graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack = load_graph_from_request(request)
|
||||
graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(request)
|
||||
if request.returnDate:
|
||||
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to")
|
||||
resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back")
|
||||
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to", departure_date = departure_date)
|
||||
resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back", departure_date = departure_date_return)
|
||||
if not resultTo or not resulFrom:
|
||||
raise HTTPException(status_code=404, detail="No valid paths found")
|
||||
return resultTo, resulFrom
|
||||
if not request.returnDate:
|
||||
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
|
||||
type="to")
|
||||
type="to", departure_date = departure_date)
|
||||
resultTo.append({
|
||||
"back": []
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user