Hector Rojo
Hector Rojo

Reputation: 25

OR-tools returning solution with 0 values

I am trying to solve a TSP problem with OR tools, but when changing the Distance matrix to mine the results are 0.

Here is the distance matrix I am using to run my code.

 data['distance_matrix'] = [[0.         0.40133922 0.58308059 0.44763244 0.59232887 0.13190757
  0.5562759  0.12633011 0.78330022 0.17998744]
 [0.40133922 0.         0.55530878 0.12847738 0.20445115 0.27022701
  0.30111459 0.52368492 0.44647366 0.27820636]
 [0.58308059 0.55530878 0.         0.68351728 0.56187331 0.55400228
  0.33387128 0.67251728 0.52304621 0.43480849]
 [0.44763244 0.12847738 0.68351728 0.         0.26454783 0.31920079
  0.41996521 0.5580511  0.52643066 0.36789118]
 [0.59232887 0.20445115 0.56187331 0.26454783 0.         0.46525396
  0.23296919 0.71765895 0.26265452 0.44149353]
 [0.13190757 0.27022701 0.55400228 0.31920079 0.46525396 0.
  0.45765072 0.25362887 0.67092774 0.11976824]
 [0.5562759  0.30111459 0.33387128 0.41996521 0.23296919 0.45765072
  0.         0.67900157 0.24463744 0.37723255]
 [0.12633011 0.52368492 0.67251728 0.5580511  0.71765895 0.25362887
  0.67900157 0.         0.90875577 0.30180013]
 [0.78330022 0.44647366 0.52304621 0.52643066 0.26265452 0.67092774
  0.24463744 0.90875577 0.         0.60904638]
 [0.17998744 0.27820636 0.43480849 0.36789118 0.44149353 0.11976824
  0.37723255 0.30180013 0.60904638 0.        ]]

This is the result I´m getting

Objective: 0 Kms Route for vehicle 0: 0 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 Route distance: 0Kms

from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp

def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = distance_matrix
    data['num_vehicles'] = 1
    data['depot'] = 0
    return data
def print_solution(manager, routing, solution):
    """Prints solution on console."""
    print('Objective: {} Kms'.format(solution.ObjectiveValue()))
    index = routing.Start(0)
    plan_output = 'Route for vehicle 0:\n'
    route_distance = 0
    while not routing.IsEnd(index):
        plan_output += ' {} ->'.format(manager.IndexToNode(index))
        previous_index = index
        index = solution.Value(routing.NextVar(index))
        route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
    plan_output += ' {}\n'.format(manager.IndexToNode(index))
    plan_output += 'Route distance: {}Kms\n'.format(route_distance)
    print(plan_output)

data = create_data_model()

# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
                                       data['num_vehicles'], data['depot'])

# Create Routing Model
routing = pywrapcp.RoutingModel(manager)

def distance_callback(from_index, to_index):
    """Returns the distance between the two nodes."""
    # Convert from routing variable Index to distance matrix NodeIndex.
    from_node = manager.IndexToNode(from_index)
    to_node = manager.IndexToNode(to_index)
    return data['distance_matrix'][from_node][to_node]

# Create and register a transit callback.
transit_callback_index = routing.RegisterTransitCallback(distance_callback)

# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
    routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)

if solution:
    print_solution(manager,routing,solution)
    print(solution.ObjectiveValue())
else:
    print('No Solution')

Upvotes: 0

Views: 1021

Answers (1)

Laurent Perron
Laurent Perron

Reputation: 11014

The solver is integral, and python silently rounds all distance value to 0.

You should scale everything by a constant factor (1000 for example) and round to integer.

See the note on the official guide: https://developers.google.com/optimization/routing/tsp

Upvotes: 2

Related Questions