Better routing for waypoints

Hi ORS,
I have a question to share, I am currently working to obtain the best route with waypoints, between 20 and 40 points, however in my tests the results are not optimized, I have a high mileage compared to a route made manually, I am carrying out tests in Python , my testing code:

# Crie um mapa usando o Folium e defina sua localização inicial e nível de zoom
m = folium.Map(location=list(reversed([-52.29068912724885,-27.67431428204886])), tiles="cartodbpositron", zoom_start=13)

cords=[[-53.0748607372178,-27.5964701638556],[-53.2843894020471,-27.631222386294],[-53.5095730836612,-27.5494171265095],[-53.4054673035279,-27.4804138165262],[-53.4669519596144,-27.3980360509079],[-53.4007767388514,-27.358882412381],[-53.2642960185149,-27.1922819750184],[-53.4021290554094,-27.1684450441447],[-53.431895485104,-27.280940664627],[-53.6195609569296,-27.2097627892597],[-53.6859940447681,-27.4960764230659],[-53.9296989580111,-27.4561598838426],[-53.9927678151194,-27.3599436088412],[-54.0853922050406,-27.396898884195],[-54.1041142397608,-27.6120981609471],[-54.1094812552698,-27.6715669967826],[-53.9669227230418,-27.7089288021284],[-53.9355261513435,-27.7877181698841],[-54.0573664131385,-27.8372999563703],[-53.9457497023822,-27.9173647540078],[-53.8028138686737,-27.6744926102172],[-53.3149539105514,-27.9006208754319],[-52.9254610594471,-27.9435951779041],[-53.0672769044206,-28.0584504612393],[-52.9137145496944,-28.1137262870376]]

random.shuffle(cords) 

start = [-52.28881158323103, -27.677276367835972]  
end = [-52.28881158323103, -27.677276367835972]  

route = client.directions(coordinates= [start] + cords + [end],
                          profile='driving-car',
                          optimize_waypoints=True,
                          format='geojson'               
                          )

route_json = json.dumps(route, indent=2)

waypoints = list(dict.fromkeys(reduce(operator.concat, list(map(lambda step: step['way_points'], route['features'][0]['properties']['segments'][0]['steps'])))))

folium.PolyLine(locations=[list(reversed(cords)) for cords in route ['features'][0]['geometry']['coordinates']], color="blue").add_to(m)

# Extrair as coordenadas da rota
route_coordinates = route['features'][0]['geometry']['coordinates']

# Criar uma lista vazia para armazenar as distâncias entre os waypoints
distances = []

# Iterar pelas coordenadas e calcular as distâncias
for i in range(1, len(cords)):
    # Coordenadas do waypoint atual e do waypoint anterior
    waypoint_current = cords[i]
    waypoint_previous = cords[i - 1]
    
    # Calcular a distância entre as coordenadas
    distance = geopy.distance.distance(waypoint_current, waypoint_previous).km
    
    # Adicionar a distância à lista de distâncias
    distances.append(distance)

# Calcular a soma das distâncias entre as coordenadas consecutivas
total_distance_km = sum(distances)

# Imprimir a distância total
print(f"{total_distance_km:.2f} km")


for cord in cords:
    folium.Marker(list(reversed(cord)), icon=folium.Icon(icon='pin')).add_to(m)

# Salvar o mapa em um arquivo HTML
m.save('mapa.html')

# Abrir o arquivo HTML no navegador padrão
import webbrowser
webbrowser.open('mapa.html')

Hey,

you are random.shuffle()-ing your coordinates, so comparing this with a manual route seems complicated to me, especially since your output doesn’t indicate the order of your random coordinates.

Could you reduce this to a minimum, deterministic, runnable example please?
That would help greatly with debugging whether there are issues in the openrouteservice python package.

Best regards