Какой же я дурак, надо было просто увеличить количество популяции и все заработало....

This commit is contained in:
maksim 2024-06-05 20:30:30 +04:00
parent 2d83c5c9ed
commit f6685b2d82
2 changed files with 12 additions and 2 deletions

View File

@ -91,7 +91,7 @@ def path_length_and_time(path, graph):
return length, start_time, end_time
def generate_population(size, start, end, graph, max_attempts=1000):
def generate_population(size, start, end, graph, max_attempts=100):
population = []
for i in range(size):
attempts = 0
@ -150,7 +150,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=100, generations=100):
def genetic_algorithm(start, end, graph, flights_data, type, population_size=1000, generations=100):
population = generate_population(population_size, start, end, graph)
best_paths = {} # Словарь для хранения уникальных лучших маршрутов и их длин
for generation in range(generations):

View File

@ -27,6 +27,16 @@ async def get_flight(request: TripRequest):
if not request.returnDate:
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
type="to")
resultTo.append({
"back": [
{
"id": None,
"departurePoint": None,
"destinationPoint": None
}
]
})
if not resultTo:
raise HTTPException(status_code=404, detail="No valid paths found")
return resultTo