Or-tools is not giving the best rout for a matrix

I have this function to find the best route:

from ortools.constraint_solver import pywrapcp, routing_enums_pb2
import numpy as np

def sin_restriccion(matriz_input, indices_input):
    # Convertir el DataFrame a una matriz de distancias
    distance_matrix = matriz_input
    locations = indices_input

    # Crear el modelo de datos
    def create_data_model():
        data = {}
        data['distance_matrix'] = distance_matrix
        data['num_vehicles'] = 1
        data['depot'] = 0  # Puedes ajustar el depot según sea necesario
        return data

    # Crear el modelo de datos
    data = create_data_model()

    # Crear el gestor de rutas
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
                                           data['num_vehicles'], data['depot'])

    # Crear el modelo de rutas
    routing = pywrapcp.RoutingModel(manager)

    # Crear la función de distancia
    def distance_callback(from_index, to_index):
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

    # Definir el costo de la distancia
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    # Eliminar la restricción de retorno al depot
    routing.SetFixedCostOfAllVehicles(0)
    end_index = manager.NodeToIndex(data['depot'])
    routing.AddDisjunction([end_index], 1000000)  # Penalización alta para no regresar al depot

    # Definir parámetros de búsqueda
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
    search_parameters.local_search_metaheuristic = (
        routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
    search_parameters.time_limit.seconds = 10  # Ajustar el límite de tiempo según sea necesario

    # Resolver el problema
    solution = routing.SolveWithParameters(search_parameters)

    # Preparar el resultado como un diccionario
    if solution:
        index = routing.Start(0)
        route_dict = {}
        step = 1
        total_distance = 0
        while not routing.IsEnd(index):
            node_index = manager.IndexToNode(index)
            route_dict[step] = locations[node_index]
            next_index = solution.Value(routing.NextVar(index))
            total_distance += distance_callback(index, next_index)
            index = next_index
            step += 1
        # Imprimir distancia total recorrida
        print(f'Distancia total recorrida: {total_distance}')
        return route_dict
    else:
        print('No hay solución')
        return 'No hay solución'

# Ejemplo de uso con la matriz proporcionada
matriz = np.array([
    [0.0, 22.3, 1965.4, 2108.5],
    [500.0, 0.0, 1611.7, 2130.8],  # B a A es 500 en lugar de 22.3
    [2033.1, 2080.6, 0.0, 2267.8],
    [2037.2, 2084.7, 2189.1, 0.0]
])

indices = ['A', 'B', 'C', 'D']

# Llamada a la función
ruta_optima = sin_restriccion(matriz, indices)
print(ruta_optima)

But the result is:
{1: ‘A’, 2: ‘D’, 3: ‘C’, 4: ‘B’}

It is not too hard to see that A -> B -> C -> D is better that the result. Is there any configuration I am missing? Or its just how ortool works, not always finding the shortest solution?

I am not an expert with ortool so there may be some things I am ignoring.

New contributor

diego medina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật