-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
189 lines (145 loc) · 5.71 KB
/
solver.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Simple travelling salesman problem on a circuit board."""
from __future__ import print_function
import sys
import math
import os
from subprocess import Popen, PIPE
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def create_data_model(cities):
"""Stores the data for the problem."""
data = {}
data['locations'] = cities
# Locations in block units
data['locations2'] = [
] # yapf: disable
data['num_vehicles'] = 1
data['depot'] = 0
return data
# [END data_model]
def compute_euclidean_distance_matrix(locations):
"""Creates callback to return distance between points."""
distances = {}
for from_counter, from_node in enumerate(locations):
distances[from_counter] = {}
for to_counter, to_node in enumerate(locations):
if from_counter == to_counter:
distances[from_counter][to_counter] = 0
else:
# Euclidean distance
distances[from_counter][to_counter] = (int(
math.hypot((from_node[0] - to_node[0]),
(from_node[1] - to_node[1]))))
return distances
def print_solution(manager, routing, solution):
"""Prints solution on console."""
index = routing.Start(0)
route = ''
route_distance = 0
while not routing.IsEnd(index):
route += '{} '.format(manager.IndexToNode(index))
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
route_distance += routing.GetArcCostForVehicle(previous_index, 0, 0)
plan_output = '{} 1\n'.format(route_distance);
plan_output += route;
return plan_output
def main(cities):
"""Entry point of the program."""
# Instantiate the data problem.
n_cities = len(cities)
data = create_data_model(cities)
if n_cities > 1000:
solver = 'greedy'
else:
solver = 'local_search'
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['locations']),
data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
distance_matrix = compute_euclidean_distance_matrix(data['locations'])
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 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)
if solver == 'simulated_annealing':
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.SIMULATED_ANNEALING)
elif solver == 'tabu_search':
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.TABU_SEARCH)
elif solver == 'greedy_descent':
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GREEDY_DESCENT)
else:
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.seconds = 120
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if solution:
output_data = print_solution(manager, routing, solution)
return output_data
def read_input_size(input_data):
lines = input_data.split('\n')
node_count = int(lines[0])
return node_count
def parse_cities(input_data):
# parse the input
lines = input_data.split('\n')
node_count = int(lines[0])
points = []
for i in range(1, node_count+1):
line = lines[i]
parts = line.split()
points.append((float(parts[0]), float(parts[1])))
return points
def solve_with_or_tools(input_data):
# Modify this code to run your optimization algorithm
cities = parse_cities(input_data)
output_data = main(cities)
return output_data
def solve_with_2opt(input_data):
# Writes the inputData to a temporay file
tmp_file_name = 'tmp.data'
tmp_file = open(tmp_file_name, 'w')
tmp_file.write(input_data)
tmp_file.close()
process = Popen(['cat ' + tmp_file_name + ' | ./solution'],
stdout=PIPE, shell = True, universal_newlines = True
)
(stdout, stderr) = process.communicate()
# removes the temporary file
os.remove(tmp_file_name)
return stdout.strip()
def solve_it(input_data):
# switch solver based on the input size
solution = 'None'
if read_input_size(input_data) > 2000:
solution = solve_with_2opt(input_data)
else:
solution = solve_with_or_tools(input_data)
return solution
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
with open(file_location, 'r') as input_data_file:
input_data = input_data_file.read()
print(solve_it(input_data))
else:
print('This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/tsp_51_1)')