-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal_search.cpp
More file actions
522 lines (452 loc) · 17.5 KB
/
Copy pathlocal_search.cpp
File metadata and controls
522 lines (452 loc) · 17.5 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "stablesolver/stable/algorithms/local_search.hpp"
#include "stablesolver/stable/algorithm_formatter.hpp"
#include "localsearchsolver/best_first_local_search.hpp"
using namespace stablesolver::stable;
namespace
{
class LocalScheme
{
public:
/*
* Constructors and destructor
*/
struct Parameters
{
/** Enable (2-1)-swap neighborhood. */
bool swap_2_1 = true;
bool shuffle_neighborhood_order = true;
};
LocalScheme(
const Instance& instance,
Parameters parameters):
instance_(instance),
parameters_(parameters),
vertices_(instance.number_of_vertices()),
neighbors_(instance_.number_of_vertices()),
free_vertices_(instance_.number_of_vertices()),
free_vertices_2_(instance_.number_of_vertices())
{
// Initialize vertices_.
std::iota(vertices_.begin(), vertices_.end(), 0);
}
/*
* Global cost
*/
/** Global cost: <Weight>; */
using GlobalCost = std::tuple<Weight>;
inline Weight& weight(GlobalCost& global_cost) { return std::get<0>(global_cost); }
inline Weight weight(const GlobalCost& global_cost) { return std::get<0>(global_cost); }
/*
* Solutions
*/
struct SolutionVertex
{
/**
* in == true iff the vertex is in the solution.
*/
bool in = false;
/**
* neighbor_weight = p iff the sum of the weights of the neighbors of j
* which are in the solution is equal to p.
*/
Weight neighbor_weight = 0;
};
struct Solution
{
std::vector<SolutionVertex> vertices;
Weight weight = 0;
};
inline Solution empty_solution() const
{
Solution solution;
solution.vertices.resize(instance_.number_of_vertices());
return solution;
}
inline Solution initial_solution(
Counter,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
std::shuffle(vertices_.begin(), vertices_.end(), generator);
for (VertexId vertex_id: vertices_)
if (solution.vertices[vertex_id].neighbor_weight == 0)
add(solution, vertex_id);
return solution;
}
inline GlobalCost global_cost(const Solution& solution) const
{
return {
-solution.weight,
};
}
/*
* Local search.
*/
struct Perturbation;
inline void local_search(
Solution& solution,
std::mt19937_64& generator,
const Perturbation& tabu = Perturbation())
{
// Get neighborhoods.
std::vector<Counter> neighborhoods = {0};
if (parameters_.swap_2_1)
neighborhoods.push_back(1);
Counter it = 0;
(void)it;
for (;; ++it) {
//std::cout << "it " << it
// << " c " << to_string(global_cost(solution))
// << std::endl;
//print(std::cout, solution);
if (parameters_.shuffle_neighborhood_order)
std::shuffle(neighborhoods.begin(), neighborhoods.end(), generator);
bool improved = false;
// Loop through neighborhoods.
for (Counter neighborhood: neighborhoods) {
switch (neighborhood) {
case 0: { // Add neighborhood.
std::shuffle(vertices_.begin(), vertices_.end(), generator);
VertexId vertex_id_best = -1;
GlobalCost c_best = global_cost(solution);
for (VertexId vertex_id: vertices_) {
if (vertex_id == tabu.vertex_id)
continue;
if (contains(solution, vertex_id))
continue;
GlobalCost c = cost_add(solution, vertex_id, c_best);
if (c >= c_best)
continue;
if (vertex_id_best != -1
&& !localsearchsolver::dominates(c, c_best)) {
continue;
}
vertex_id_best = vertex_id;
c_best = c;
}
if (vertex_id_best != -1) {
improved = true;
// Apply perturbation.
add(solution, vertex_id_best);
if (global_cost(solution) != c_best) {
throw std::logic_error(
"Add. Costs do not match:\n"
"* Expected new cost: " + localsearchsolver::to_string(c_best) + "\n"
+ "* Actual new cost: " + localsearchsolver::to_string(global_cost(solution)) + "\n");
}
}
break;
} case 1: { // (2-1)-swap neighborhood.
std::shuffle(vertices_.begin(), vertices_.end(), generator);
// Get the vertices of the solution.
vertices_in_.clear();
for (VertexId vertex_id: vertices_) {
if (vertex_id == tabu.vertex_id)
continue;
if (contains(solution, vertex_id))
vertices_in_.push_back(vertex_id);
}
VertexId vertex_id_in_best = -1;
VertexId vertex_id_out_1_best = -1;
VertexId vertex_id_out_2_best = -1;
GlobalCost c_best = global_cost(solution);
for (VertexId vertex_id_in: vertices_in_) {
// Update free_vertices_
free_vertices_.clear();
for (const VertexEdge& edge: instance_.vertex(vertex_id_in).edges)
if (solution.vertices[edge.vertex_id].neighbor_weight
== instance_.vertex(vertex_id_in).weight)
free_vertices_.add(edge.vertex_id);
if (free_vertices_.size() <= 2)
continue;
free_vertices_.shuffle_in(generator);
remove(solution, vertex_id_in);
for (VertexId vertex_id_out_1: free_vertices_) {
assert(vertex_id_out_1 != vertex_id_in);
free_vertices_2_.clear();
for (VertexId v: free_vertices_)
free_vertices_2_.add(v);
free_vertices_2_.remove(vertex_id_out_1);
for (const VertexEdge& edge: instance_.vertex(vertex_id_out_1).edges)
if (free_vertices_2_.contains(edge.vertex_id))
free_vertices_2_.remove(edge.vertex_id);
if (free_vertices_2_.empty())
continue;
free_vertices_2_.shuffle_in(generator);
assert(!contains(solution, vertex_id_out_1));
add(solution, vertex_id_out_1);
for (VertexId vertex_id_out_2: free_vertices_2_) {
assert(vertex_id_out_2 != vertex_id_in);
assert(vertex_id_out_2 != vertex_id_out_1);
GlobalCost c = cost_add(solution, vertex_id_out_2, c_best);
if (c >= c_best)
continue;
if (vertex_id_in_best != -1
&& !localsearchsolver::dominates(c, c_best)) {
continue;
}
vertex_id_in_best = vertex_id_in;
vertex_id_out_1_best = vertex_id_out_1;
vertex_id_out_2_best = vertex_id_out_2;
c_best = c;
}
remove(solution, vertex_id_out_1);
}
assert(!contains(solution, vertex_id_in));
add(solution, vertex_id_in);
}
if (vertex_id_in_best != -1) {
improved = true;
// Apply perturbation.
remove(solution, vertex_id_in_best);
assert(!contains(solution, vertex_id_out_1_best));
add(solution, vertex_id_out_1_best);
assert(!contains(solution, vertex_id_out_2_best));
add(solution, vertex_id_out_2_best);
if (global_cost(solution) != c_best) {
throw std::logic_error(
"(2,1-swap). Costs do not match:\n"
"* Expected new cost: " + localsearchsolver::to_string(c_best) + "\n"
+ "* Actual new cost: " + localsearchsolver::to_string(global_cost(solution)) + "\n");
}
}
break;
}
}
if (improved)
break;
}
if (!improved)
break;
}
//print(std::cout, solution);
}
/*
* Iterated local search.
*/
struct Perturbation
{
Perturbation(): vertex_id(-1) { }
VertexId vertex_id;
GlobalCost global_cost;
};
inline std::vector<Perturbation> perturbations(
const Solution& solution,
std::mt19937_64&)
{
std::vector<Perturbation> perturbations;
for (VertexId vertex_id: vertices_) {
GlobalCost c = (contains(solution, vertex_id))?
cost_remove(solution, vertex_id, localsearchsolver::worst<GlobalCost>()):
cost_add(solution, vertex_id, localsearchsolver::worst<GlobalCost>());
Perturbation perturbation;
perturbation.vertex_id = vertex_id;
perturbation.global_cost = c;
perturbations.push_back(perturbation);
}
return perturbations;
}
inline void apply_perturbation(
Solution& solution,
const Perturbation& perturbation,
std::mt19937_64&) const
{
if (contains(solution, perturbation.vertex_id)) {
remove(solution, perturbation.vertex_id);
} else {
add(solution, perturbation.vertex_id);
}
}
/*
* Best first local search.
*/
using CompactSolution = std::vector<bool>;
struct CompactSolutionHasher
{
std::hash<CompactSolution> hasher;
inline bool operator()(
const std::shared_ptr<CompactSolution>& compact_solution_1,
const std::shared_ptr<CompactSolution>& compact_solution_2) const
{
return *compact_solution_1 == *compact_solution_2;
}
inline std::size_t operator()(
const std::shared_ptr<CompactSolution>& compact_solution) const
{
return hasher(*compact_solution);
}
};
inline CompactSolutionHasher compact_solution_hasher() const { return CompactSolutionHasher(); }
CompactSolution solution2compact(const Solution& solution)
{
std::vector<bool> vertices(instance_.number_of_vertices(), false);
for (VertexId vertex_id = 0;
vertex_id < instance_.number_of_vertices();
++vertex_id) {
if (solution.vertices[vertex_id].in)
vertices[vertex_id] = true;
}
return vertices;
}
Solution compact2solution(const CompactSolution& compact_solution)
{
auto solution = empty_solution();
for (VertexId vertex_id = 0;
vertex_id < instance_.number_of_vertices();
++vertex_id) {
if (compact_solution[vertex_id])
add(solution, vertex_id);
}
return solution;
}
struct PerturbationHasher
{
std::hash<VertexId> hasher;
inline bool hashable(const Perturbation&) const { return true; }
inline bool operator()(
const Perturbation& perturbation_1,
const Perturbation& perturbation_2) const
{
return perturbation_1.vertex_id == perturbation_2.vertex_id;
}
inline std::size_t operator()(
const Perturbation& perturbation) const
{
size_t hash = hasher(perturbation.vertex_id);
return hash;
}
};
inline PerturbationHasher perturbation_hasher() const { return PerturbationHasher(); }
/*
* Outputs
*/
std::ostream& print(
std::ostream& os,
const Solution& solution)
{
os << "vertices:";
for (VertexId vertex_id = 0;
vertex_id < instance_.number_of_vertices();
++vertex_id) {
if (contains(solution, vertex_id))
os << " " << vertex_id;
}
os << std::endl;
os << "weight: " << solution.weight << std::endl;
return os;
}
inline void write(const Solution&, std::string) const { return; }
private:
/*
* Manipulate solutions.
*/
inline bool contains(
const Solution& solution,
VertexId vertex_id) const
{
return solution.vertices[vertex_id].in;
}
inline void add(
Solution& solution,
VertexId vertex_id) const
{
assert(vertex_id >= 0);
assert(!contains(solution, vertex_id));
Weight weight = instance_.vertex(vertex_id).weight;
// Reperturbation conflicting vertices.
for (const VertexEdge& edge: instance_.vertex(vertex_id).edges) {
if (contains(solution, edge.vertex_id))
remove(solution, edge.vertex_id);
solution.vertices[edge.vertex_id].neighbor_weight += weight;
}
solution.vertices[vertex_id].in = true;
solution.weight += weight;
}
inline void remove(
Solution& solution,
VertexId vertex_id) const
{
assert(vertex_id >= 0);
assert(contains(solution, vertex_id));
solution.vertices[vertex_id].in = false;
Weight weight = instance_.vertex(vertex_id).weight;
solution.weight -= weight;
for (const VertexEdge& edge: instance_.vertex(vertex_id).edges)
solution.vertices[edge.vertex_id].neighbor_weight -= weight;
}
/*
* Evaluate perturbations.
*/
inline GlobalCost cost_remove(
const Solution& solution,
VertexId vertex_id,
GlobalCost) const
{
return {
- (solution.weight - instance_.vertex(vertex_id).weight),
};
}
inline GlobalCost cost_add(
const Solution& solution,
VertexId vertex_id,
GlobalCost) const
{
return {
-(solution.weight
+ instance_.vertex(vertex_id).weight
- solution.vertices[vertex_id].neighbor_weight),
};
}
/*
* Private attributes.
*/
const Instance& instance_;
Parameters parameters_;
std::vector<VertexId> vertices_;
std::vector<VertexId> vertices_in_;
std::vector<VertexId> vertices_out_;
optimizationtools::IndexedSet neighbors_;
optimizationtools::IndexedSet free_vertices_;
optimizationtools::IndexedSet free_vertices_2_;
};
}
const Output stablesolver::stable::local_search(
const Instance& instance,
const LocalSearchParameters& parameters)
{
Output output(instance);
AlgorithmFormatter algorithm_formatter(parameters, output);
algorithm_formatter.start("Local search");
// Reduction.
if (parameters.reduction_parameters.reduce)
return solve_reduced_instance(local_search, instance, parameters, algorithm_formatter, output);
algorithm_formatter.print_header();
// Create LocalScheme.
LocalScheme::Parameters parameters_local_scheme;
LocalScheme local_scheme(instance, parameters_local_scheme);
// Run A*.
localsearchsolver::BestFirstLocalSearchParameters<LocalScheme> llsbfls_parameters;
llsbfls_parameters.verbosity_level = 0;
llsbfls_parameters.timer = parameters.timer;
llsbfls_parameters.maximum_number_of_nodes = parameters.maximum_number_of_nodes;
llsbfls_parameters.number_of_threads_1 = 1;
llsbfls_parameters.number_of_threads_2 = parameters.number_of_threads;
llsbfls_parameters.initial_solution_ids = std::vector<Counter>(
llsbfls_parameters.number_of_threads_2, 0);
llsbfls_parameters.new_solution_callback
= [&instance, &algorithm_formatter](
const localsearchsolver::Output<LocalScheme>& ls_output)
{
Solution solution(instance);
for (VertexId vertex_id = 0;
vertex_id < instance.number_of_vertices();
++vertex_id) {
if (ls_output.solution_pool.best().vertices[vertex_id].in)
solution.add(vertex_id);
}
algorithm_formatter.update_solution(solution, "");
};
best_first_local_search(local_scheme, llsbfls_parameters);
algorithm_formatter.end();
return output;
}