-
Notifications
You must be signed in to change notification settings - Fork 322
/
tsp.cpp
335 lines (280 loc) · 10.4 KB
/
tsp.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
This file is part of VROOM.
Copyright (c) 2015-2016, Julien Coupey.
All rights reserved (see LICENSE).
*/
#include "tsp.h"
#include "../../structures/vroom/input/input.h"
tsp::tsp(const input& input,
index_t vehicle_rank):
vrp(input),
_vehicle_rank(vehicle_rank),
_matrix(_input._matrix), // TODO avoid this copy!
_symmetrized_matrix(_input._matrix.size()),
_is_symmetric(true),
_force_start(_input._vehicles[_vehicle_rank].has_start()),
_force_end(_input._vehicles[_vehicle_rank].has_end())
{
if(_force_start){
_start = _input._vehicles[_vehicle_rank].start.get().index;
assert(_start < _matrix.size());
}
if(_force_end){
_end = _input._vehicles[_vehicle_rank].end.get().index;
assert(_end < _matrix.size());
}
// Dealing with open tour cases. At most one of the following
// occurs.
if(_force_start and !_force_end){
// Forcing first location as start, end location decided during
// optimization.
for(index_t i = 0; i < _matrix.size(); ++i){
if(i != _start){
_matrix[i][_start] = 0;
}
}
}
if(!_force_start and _force_end){
// Forcing last location as end, start location decided during
// optimization.
for(index_t j = 0; j < _matrix.size(); ++j){
if(j != _end){
_matrix[_end][j] = 0;
}
}
}
if(_force_start and _force_end){
// Forcing first location as start, last location as end to
// produce an open tour.
assert(_start != _end);
_matrix[_end][_start] = 0;
for(index_t j = 0; j < _matrix.size(); ++j){
if((j != _start) and (j != _end)){
_matrix[_end][j] = INFINITE_DISTANCE;
}
}
}
// Compute symmetrized matrix and update _is_symmetric flag.
const distance_t& (*sym_f) (const distance_t&, const distance_t&)
= std::min<distance_t>;
if((_force_start and !_force_end)
or (!_force_start and _force_end)){
// Using symmetrization with max as when only start or only end is
// forced, the matrix has a line or a column filled with zeros.
sym_f = std::max<distance_t>;
}
for(index_t i = 0; i < _matrix.size(); ++i){
_symmetrized_matrix[i][i] = _matrix[i][i];
for(index_t j = i + 1; j < _matrix.size(); ++j){
_is_symmetric &= (_matrix[i][j] == _matrix[j][i]);
distance_t val = sym_f(_matrix[i][j], _matrix[j][i]);
_symmetrized_matrix[i][j] = val;
_symmetrized_matrix[j][i] = val;
}
}
}
distance_t tsp::cost(const std::list<index_t>& tour) const{
distance_t cost = 0;
index_t init_step = 0; // Initialization actually never used.
auto step = tour.cbegin();
if(tour.size() > 0){
init_step = *step;
}
index_t previous_step = init_step;
++step;
for(; step != tour.cend(); ++step){
cost += _matrix[previous_step][*step];
previous_step = *step;
}
if(tour.size() > 0){
cost += _matrix[previous_step][init_step];
}
return cost;
}
distance_t tsp::symmetrized_cost(const std::list<index_t>& tour) const{
distance_t cost = 0;
index_t init_step = 0; // Initialization actually never used.
auto step = tour.cbegin();
if(tour.size() > 0){
init_step = *step;
}
index_t previous_step = init_step;
++step;
for(; step != tour.cend(); ++step){
cost += _symmetrized_matrix[previous_step][*step];
previous_step = *step;
}
if(tour.size() > 0){
cost += _symmetrized_matrix[previous_step][init_step];
}
return cost;
}
solution tsp::solve(unsigned nb_threads) const{
// Applying heuristic.
auto start_heuristic = std::chrono::high_resolution_clock::now();
BOOST_LOG_TRIVIAL(info)
<< "[Heuristic] Start heuristic on symmetrized problem.";
std::list<index_t> christo_sol = christofides(_symmetrized_matrix);
distance_t christo_cost = this->symmetrized_cost(christo_sol);
auto end_heuristic = std::chrono::high_resolution_clock::now();
auto heuristic_computing_time =
std::chrono::duration_cast<std::chrono::milliseconds>
(end_heuristic - start_heuristic).count();
BOOST_LOG_TRIVIAL(info) << "[Heuristic] Done, took "
<< heuristic_computing_time << " ms.";
BOOST_LOG_TRIVIAL(info) << "[Heuristic] Symmetric solution cost is "
<< christo_cost << ".";
// Local search on symmetric problem.
// Applying deterministic, fast local search to improve the current
// solution in a small amount of time. All possible moves for the
// different neighbourhoods are performed, stopping when reaching a
// local minima.
auto start_sym_local_search = std::chrono::high_resolution_clock::now();
BOOST_LOG_TRIVIAL(info)
<< "[Local search] Start local search on symmetrized problem.";
BOOST_LOG_TRIVIAL(info)
<< "[Local search] Using " << nb_threads << " thread(s).";
local_search sym_ls (_symmetrized_matrix,
true, // Symmetrized problem.
christo_sol,
nb_threads);
distance_t sym_two_opt_gain = 0;
distance_t sym_relocate_gain = 0;
distance_t sym_or_opt_gain = 0;
do{
// All possible 2-opt moves.
sym_two_opt_gain = sym_ls.perform_all_two_opt_steps();
// All relocate moves.
sym_relocate_gain = sym_ls.perform_all_relocate_steps();
// All or-opt moves.
sym_or_opt_gain = sym_ls.perform_all_or_opt_steps();
}while((sym_two_opt_gain > 0)
or (sym_relocate_gain > 0)
or (sym_or_opt_gain > 0));
index_t first_loc_index;
if(_force_start){
// Use start value set in constructor from vehicle input.
first_loc_index = _start;
}
else{
assert(_force_end);
// Requiring the tour to be described from the "forced" end
// location.
first_loc_index = _end;
}
std::list<index_t> current_sol = sym_ls.get_tour(first_loc_index);
auto current_cost = this->symmetrized_cost(current_sol);
auto end_sym_local_search = std::chrono::high_resolution_clock::now();
auto sym_local_search_duration
= std::chrono::duration_cast<std::chrono::milliseconds>
(end_sym_local_search - start_sym_local_search).count();
BOOST_LOG_TRIVIAL(info) << "[Local search] Done, took "
<< sym_local_search_duration << " ms.";
BOOST_LOG_TRIVIAL(info) << "[Local search] Symmetric solution cost is now "
<< current_cost
<< " ("
<< std::fixed << std::setprecision(2)
<< 100 *(((double) current_cost) / christo_cost - 1)
<< "%).";
auto asym_local_search_duration = 0;
if(!_is_symmetric){
auto start_asym_local_search = std::chrono::high_resolution_clock::now();
// Back to the asymmetric problem, picking the best way.
std::list<index_t> reverse_current_sol (current_sol);
reverse_current_sol.reverse();
distance_t direct_cost = this->cost(current_sol);
distance_t reverse_cost = this->cost(reverse_current_sol);
// Cost reference after symmetric local search.
distance_t sym_ls_cost = std::min(direct_cost, reverse_cost);
// Local search on asymmetric problem.
local_search asym_ls (_matrix,
false, // Not the symmetrized problem.
(direct_cost <= reverse_cost) ?
current_sol: reverse_current_sol,
nb_threads);
BOOST_LOG_TRIVIAL(info)
<< "[Asym. local search] Back to asymmetric problem, initial solution cost is "
<< sym_ls_cost << ".";
BOOST_LOG_TRIVIAL(info)
<< "[Asym. local search] Start local search on asymmetric problem.";
BOOST_LOG_TRIVIAL(info)
<< "[Asym. local search] Using " << nb_threads << " thread(s).";
distance_t asym_two_opt_gain = 0;
distance_t asym_relocate_gain = 0;
distance_t asym_or_opt_gain = 0;
distance_t asym_avoid_loops_gain = 0;
do{
// All avoid-loops moves.
asym_avoid_loops_gain = asym_ls.perform_all_avoid_loop_steps();
// All possible 2-opt moves.
asym_two_opt_gain = asym_ls.perform_all_asym_two_opt_steps();
// All relocate moves.
asym_relocate_gain = asym_ls.perform_all_relocate_steps();
// All or-opt moves.
asym_or_opt_gain = asym_ls.perform_all_or_opt_steps();
}while((asym_two_opt_gain > 0)
or (asym_relocate_gain > 0)
or (asym_or_opt_gain > 0)
or (asym_avoid_loops_gain > 0));
current_sol = asym_ls.get_tour(first_loc_index);
current_cost = this->cost(current_sol);
auto end_asym_local_search = std::chrono::high_resolution_clock::now();
asym_local_search_duration
= std::chrono::duration_cast<std::chrono::milliseconds>
(end_asym_local_search - start_asym_local_search).count();
BOOST_LOG_TRIVIAL(info) << "[Asym. local search] Done, took "
<< asym_local_search_duration << " ms.";
BOOST_LOG_TRIVIAL(info)
<< "[Asym. local search] Asymmetric solution cost is now "
<< current_cost
<< " ("
<< std::fixed << std::setprecision(2)
<< 100 *(((double) current_cost) / sym_ls_cost - 1)
<< "%).";
}
// Deal with open tour cases requiring adaptation.
if(!_force_start and _force_end){
// The tour has been listed starting with the "forced" end. This
// index has to be popped and put back, the next element being the
// chosen start resulting from the optimization.
current_sol.push_back(current_sol.front());
current_sol.pop_front();
}
// current_sol;
// Steps for the one route.
std::vector<step> steps;
// Handle start.
auto job_start = current_sol.cbegin();
if(_force_start){
// Add start step.
steps.emplace_back(TYPE::START,
_input.get_location_at(current_sol.front()));
// Remember that jobs start further away in the list.
++job_start;
}
// Determine where to stop for last job.
auto job_end = current_sol.cend();
if(_force_end){
--job_end;
}
// Handle jobs.
for(auto job = job_start; job != job_end; ++job){
auto current_rank = _input.get_job_rank_from_index(*job);
steps.emplace_back(TYPE::JOB,
_input._jobs[current_rank],
_input._jobs[current_rank].id);
}
// Handle end.
if(_force_end){
// Add end step.
steps.emplace_back(TYPE::END,
_input.get_location_at(current_sol.back()));
}
// Route.
std::vector<route_t> routes;
routes.emplace_back(_input._vehicles[_vehicle_rank].id,
steps,
current_cost);
solution sol (0, std::move(routes), current_cost);
return sol;
}