forked from iwky911/onedayinparis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp.py
91 lines (69 loc) · 2.86 KB
/
tsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Simple Travelling Salesperson Problem (TSP) between cities."""
"""
setup
python -m pip install --upgrade --user ortools
from https://developers.google.com/optimization/install
and https://developers.google.com/optimization/routing/tsp
"""
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def read_data():
import csv
import numpy
reader = csv.reader(open("full_matrix.csv", "r"), delimiter=",")
x = list(reader)
result = numpy.array(x).astype("int")
return result
def create_data_model():
"""Stores the data for the problem."""
data = {}
data['distance_matrix'] = read_data()
data['num_vehicles'] = 1
data['depot'] = 1
return data
def print_solution(manager, routing, solution):
"""Prints solution on console."""
print('Objective: {} minutes'.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 time: {} minutes\n'.format(route_distance)
print(plan_output)
def main():
"""Entry point of the program."""
# Instantiate the data problem.
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]
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)
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_MOST_CONSTRAINED_ARC)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if solution:
print_solution(manager, routing, solution)
if __name__ == '__main__':
main()
# print(read_data())