-
Notifications
You must be signed in to change notification settings - Fork 1
/
quadratic_assignment.hpp
613 lines (542 loc) · 21.4 KB
/
quadratic_assignment.hpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
* Quadratic assignment problem
*
* Problem description:
* See https://github.com/fontanf/orproblems/blob/main/orproblems/quadratic_assignment.hpp
*
* TODO
*
*/
#pragma once
#include "localsearchsolver/common.hpp"
#include "orproblems/assignment/quadratic_assignment.hpp"
#include "optimizationtools/containers/indexed_set.hpp"
#include "optimizationtools//utils//utils.hpp"
namespace localsearchsolver
{
namespace quadratic_assignment
{
using namespace orproblems::quadratic_assignment;
class LocalScheme
{
public:
struct Parameters
{
double crossover_ux_weight = 1.0;
double crossover_swap_weight = 0.0;
double crossover_insert_weight = 0.0;
};
/*
* Constructors and destructor
*/
LocalScheme(
const Instance& instance,
Parameters parameters):
instance_(instance),
parameters_(parameters),
facilities_(instance.number_of_facilities()),
facility_pairs_(instance.number_of_facilities())
{
// Initialize facilities_.
std::iota(facilities_.begin(), facilities_.end(), 0);
// Initialize facility_pairs_.
for (FacilityId facility_id_1 = 0; facility_id_1 < instance.number_of_facilities(); ++facility_id_1)
for (FacilityId facility_id_2 = facility_id_1 + 1; facility_id_2 < instance.number_of_facilities(); ++facility_id_2)
facility_pairs_.push_back({facility_id_1, facility_id_2});
}
/*
* Global cost
*/
/** Global cost: <Cost>; */
using GlobalCost = std::tuple<Cost>;
inline Cost& cost(GlobalCost& global_cost) const { return std::get<0>(global_cost); }
inline Cost cost(const GlobalCost& global_cost) const { return std::get<0>(global_cost); }
inline GlobalCost global_cost_goal(double value) const
{
return {
value,
};
}
/*
* Solutions
*/
struct Solution
{
std::vector<LocationId> locations;
Cost cost = 0;
};
inline Solution empty_solution() const
{
Solution solution;
solution.locations.resize(instance_.number_of_facilities(), -1);
return solution;
}
inline Solution initial_solution(
Counter,
std::mt19937_64& generator) const
{
Solution solution = empty_solution();
std::vector<LocationId> locations(instance_.number_of_facilities(), -1);
std::iota(locations.begin(), locations.end(), 0);
std::shuffle(locations.begin(), locations.end(), generator);
for (FacilityId facility_id = 0; facility_id < instance_.number_of_facilities(); ++facility_id)
add(solution, facility_id, locations[facility_id]);
return solution;
}
inline GlobalCost global_cost(const Solution& solution) const
{
return {
solution.cost,
};
}
/*
* Local search
*/
struct Perturbation;
inline void local_search(
Solution& solution,
std::mt19937_64& generator,
const Perturbation& = Perturbation())
{
Counter it = 0;
(void)it;
for (;; ++it) {
//std::cout << "it " << it
// << " cost " << to_string(global_cost(solution))
// << std::endl;
std::shuffle(facility_pairs_.begin(), facility_pairs_.end(), generator);
FacilityId facility_id_1_best = -1;
FacilityId facility_id_2_best = -1;
GlobalCost c_best = global_cost(solution);
for (auto p: facility_pairs_) {
FacilityId facility_id_1 = p.first;
FacilityId facility_id_2 = p.second;
GlobalCost c = cost_swap(
solution,
facility_id_1,
facility_id_2);
if (c >= c_best)
continue;
if (facility_id_1 != -1 && !dominates(c, c_best))
continue;
facility_id_1_best = facility_id_1;
facility_id_2_best = facility_id_2;
c_best = c;
}
if (facility_id_1_best == -1)
break;
LocationId location_id_1 = solution.locations[facility_id_1_best];
LocationId location_id_2 = solution.locations[facility_id_2_best];
remove(solution, facility_id_1_best);
remove(solution, facility_id_2_best);
add(solution, facility_id_1_best, location_id_2);
add(solution, facility_id_2_best, location_id_1);
}
}
/*
* Genetic local search
*/
inline Solution crossover_ux(
const Solution& solution_parent_1,
const Solution& solution_parent_2,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
std::shuffle(facilities_.begin(), facilities_.end(), generator);
optimizationtools::IndexedSet free_locations(instance_.number_of_facilities());
for (LocationId location_id = 0;
location_id < instance_.number_of_facilities();
++location_id)
free_locations.add(location_id);
// If a facility is assigned to the same location in both parent
// solutions, keep it assigned to the same location in the child
// solution.
for (FacilityId facility_id: facilities_) {
LocationId location_id_1 = solution_parent_1.locations[facility_id];
LocationId location_id_2 = solution_parent_2.locations[facility_id];
if (location_id_1 == location_id_2) {
add(solution, facility_id, location_id_1);
free_locations.remove(location_id_1);
}
}
// If a location to which a facility is assigned in one of the parent
// solution is still free, assign the facility to it in the child
// solution.
for (FacilityId facility_id: facilities_) {
if (solution.locations[facility_id] != -1)
continue;
LocationId location_id_1 = solution_parent_1.locations[facility_id];
LocationId location_id_2 = solution_parent_2.locations[facility_id];
if (!free_locations.contains(location_id_1)
&& !free_locations.contains(location_id_2))
continue;
LocationId location_id = -1;
if (free_locations.contains(location_id_1)
&& free_locations.contains(location_id_2)) {
std::uniform_int_distribution<LocationId> d(0, 1);
LocationId pos = d(generator);
location_id = (pos == 0)? location_id_1: location_id_2;
} else if (free_locations.contains(location_id_1)) {
location_id = location_id_1;
} else {
location_id = location_id_2;
}
add(solution, facility_id, location_id);
free_locations.remove(location_id);
}
// Assign a random location to the remaining facilities.
for (FacilityId facility_id: facilities_) {
if (solution.locations[facility_id] != -1)
continue;
free_locations.shuffle_in(generator);
LocationId location_id = *free_locations.begin();
add(solution, facility_id, location_id);
free_locations.remove(location_id);
}
return solution;
}
/**
* References:
* - "A greedy genetic algorithm for the quadratic assignment problem"
* (Ahuja et al., 2000)
* https://doi.org/10.1016/S0305-0548(99)00067-2
*/
inline Solution crossover_swap(
const Solution& solution_parent_1,
const Solution& solution_parent_2,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
(void)solution_parent_1;
(void)solution_parent_2;
(void)generator;
return solution;
}
/**
* References:
* - "A greedy genetic algorithm for the quadratic assignment problem"
* (Ahuja et al., 2000)
* https://doi.org/10.1016/S0305-0548(99)00067-2
*/
inline Solution crossover_insert(
const Solution& solution_parent_1,
const Solution& solution_parent_2,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
(void)solution_parent_1;
(void)solution_parent_2;
(void)generator;
return solution;
}
inline Solution crossover(
const Solution& solution_parent_1,
const Solution& solution_parent_2,
std::mt19937_64& generator)
{
std::discrete_distribution<Counter> d_crossover({
parameters_.crossover_ux_weight,
parameters_.crossover_swap_weight,
parameters_.crossover_insert_weight,
});
Counter x = d_crossover(generator);
switch (x) {
case 1: {
return crossover_swap(solution_parent_1, solution_parent_2, generator);
} case 2: {
return crossover_insert(solution_parent_1, solution_parent_2, generator);
} default: {
return crossover_ux(solution_parent_1, solution_parent_2, generator);
}
}
}
inline FacilityId distance(
const Solution& solution_1,
const Solution& solution_2) const
{
FacilityId d = 0;
for (FacilityId facility_id = 0;
facility_id < instance_.number_of_facilities();
++facility_id) {
if (solution_1.locations[facility_id]
!= solution_2.locations[facility_id])
d++;
}
return d;
}
/*
* Iterated local search
*/
struct Perturbation
{
Perturbation(): global_cost(worst<GlobalCost>()) { }
std::vector<FacilityId> facilities;
GlobalCost global_cost;
};
inline std::vector<Perturbation> perturbations(
const Solution& solution,
std::mt19937_64& generator)
{
std::vector<Perturbation> perturbations;
for (FacilityId facility_id = 0;
facility_id < instance_.number_of_facilities();
++facility_id) {
Solution solution_tmp = solution;
std::vector<FacilityId> facility_ids = {facility_id};
FacilityId facility_id_1 = facility_id;
for (Counter c = 0; c < 16; ++c) {
FacilityId facility_id_2_best = -1;
GlobalCost c_best = worst<GlobalCost>();
std::shuffle(facilities_.begin(), facilities_.end(), generator);
for (FacilityId facility_id_2: facilities_) {
if (std::find(facility_ids.begin(), facility_ids.end(), facility_id_2)
!= facility_ids.end())
continue;
GlobalCost c = cost_swap(
solution_tmp,
facility_id_1,
facility_id_2);
if (c >= c_best)
continue;
facility_id_2_best = facility_id_2;
c_best = c;
}
if (facility_id_2_best == -1)
break;
LocationId location_id_1 = solution_tmp.locations[facility_id_1];
LocationId location_id_2 = solution_tmp.locations[facility_id_2_best];
remove(solution_tmp, facility_id_1);
remove(solution_tmp, facility_id_2_best);
add(solution_tmp, facility_id_1, location_id_2);
add(solution_tmp, facility_id_2_best, location_id_1);
facility_ids.push_back(facility_id_2_best);
facility_id_1 = facility_id_2_best;
}
Perturbation perturbation;
perturbation.facilities = facility_ids;
perturbation.global_cost = global_cost(solution_tmp);
perturbations.push_back(perturbation);
}
return perturbations;
}
inline void apply_perturbation(
Solution& solution,
const Perturbation& perturbation,
std::mt19937_64&) const
{
LocationId location_id_1 = solution.locations[perturbation.facilities[0]];
remove(solution, perturbation.facilities[0]);
for (FacilityId facility_pos = 0;
facility_pos < (Counter)perturbation.facilities.size() - 1;
++facility_pos) {
LocationId location_id = solution.locations[perturbation.facilities[facility_pos + 1]];
remove(solution, perturbation.facilities[facility_pos + 1]);
add(solution, perturbation.facilities[facility_pos], location_id);
}
add(solution, perturbation.facilities.back(), location_id_1);
assert(global_cost(solution) == perturbation.global_cost);
}
/*
* Best first local search
*/
using CompactSolution = std::vector<LocationId>;
struct CompactSolutionHasher
{
std::hash<LocationId> 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
{
size_t hash = 0;
for (LocationId location_id: *compact_solution)
optimizationtools::hash_combine(hash, hasher(location_id));
return hash;
}
};
inline CompactSolutionHasher compact_solution_hasher() const { return CompactSolutionHasher(); }
CompactSolution solution2compact(const Solution& solution)
{
return solution.locations;
}
Solution compact2solution(const CompactSolution& compact_solution)
{
Solution solution;
solution.locations = compact_solution;
for (FacilityId facility_id_1 = 0; facility_id_1 < instance_.number_of_facilities(); ++facility_id_1) {
LocationId location_id_1 = solution.locations[facility_id_1];
if (location_id_1 == -1)
continue;
solution.cost += instance_.flow(facility_id_1, facility_id_1)
* instance_.distance(location_id_1, location_id_1);
for (FacilityId facility_id_2 = facility_id_1 + 1;
facility_id_2 < instance_.number_of_facilities(); ++facility_id_2) {
LocationId location_id_2 = solution.locations[facility_id_2];
if (location_id_2 == -1)
continue;
solution.cost += instance_.flow(facility_id_1, facility_id_2)
* instance_.distance(location_id_1, location_id_2);
solution.cost += instance_.flow(facility_id_2, facility_id_1)
* instance_.distance(location_id_2, location_id_1);
}
}
return solution;
}
struct PerturbationHasher
{
std::hash<FacilityId> hasher;
inline bool hashable(const Perturbation&) const { return true; }
inline bool operator()(
const Perturbation& perturbation_1,
const Perturbation& perturbation_2) const
{
return perturbation_1.facilities[0] == perturbation_2.facilities[0];
}
inline std::size_t operator()(
const Perturbation& perturbation) const
{
size_t hash = hasher(perturbation.facilities[0]);
return hash;
}
};
inline PerturbationHasher perturbation_hasher() const { return PerturbationHasher(); }
/*
* Outputs
*/
void instance_format(
std::ostream& os,
int verbosity_level) const
{
os << "Quadratic assignment problem" << std::endl;
instance_.format(os, verbosity_level);
}
void solution_format(
std::ostream& os,
const Solution& solution,
int verbosity_level) const
{
(void)verbosity_level;
os << "locations:";
for (LocationId location_id: solution.locations)
os << " " << location_id;
os << std::endl;
os << "cost: " << solution.cost << std::endl;
}
void solution_write(
const Solution& solution,
const std::string& certificate_path) const
{
if (certificate_path.empty())
return;
std::ofstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + certificate_path + "\".");
}
for (LocationId location_id: solution.locations)
file << location_id << " ";
}
private:
/*
* Manipulate solutions
*/
inline void add(Solution& solution, FacilityId facility_id, LocationId location_id) const
{
assert(solution.locations[facility_id] == -1);
assert(std::find(solution.locations.begin(), solution.locations.end(), location_id) == solution.locations.end());
solution.locations[facility_id] = location_id;
solution.cost += instance_.flow(facility_id, facility_id)
* instance_.distance(location_id, location_id);
for (FacilityId facility_id_2 = 0; facility_id_2 < instance_.number_of_facilities(); facility_id_2++) {
LocationId location_id_2 = solution.locations[facility_id_2];
if (location_id_2 == -1 || location_id_2 == location_id)
continue;
solution.cost += instance_.flow(facility_id, facility_id_2)
* instance_.distance(location_id, location_id_2);
solution.cost += instance_.flow(facility_id_2, facility_id)
* instance_.distance(location_id_2, location_id);
}
}
inline void remove(Solution& solution, FacilityId facility_id) const
{
LocationId location_id = solution.locations[facility_id];
assert(location_id != -1);
solution.cost -= instance_.flow(facility_id, facility_id)
* instance_.distance(location_id, location_id);
for (FacilityId facility_id_2 = 0; facility_id_2 < instance_.number_of_facilities(); facility_id_2++) {
LocationId location_id_2 = solution.locations[facility_id_2];
if (location_id_2 == -1 || location_id_2 == location_id)
continue;
solution.cost -= instance_.flow(facility_id, facility_id_2)
* instance_.distance(location_id, location_id_2);
solution.cost -= instance_.flow(facility_id_2, facility_id)
* instance_.distance(location_id_2, location_id);
}
solution.locations[facility_id] = -1;
}
/*
* Evaluate moves
*/
inline GlobalCost cost_swap(
const Solution& solution,
FacilityId facility_id_1,
FacilityId facility_id_2) const
{
LocationId location_id_1 = solution.locations[facility_id_1];
LocationId location_id_2 = solution.locations[facility_id_2];
assert(location_id_1 != -1);
assert(location_id_2 != -1);
GlobalCost gc = global_cost(solution);
cost(gc)
+= instance_.flow(facility_id_1, facility_id_1)
* (instance_.distance(location_id_2, location_id_2)
- instance_.distance(location_id_1, location_id_1))
+ instance_.flow(facility_id_2, facility_id_2)
* (instance_.distance(location_id_1, location_id_1)
- instance_.distance(location_id_2, location_id_2))
+ instance_.flow(facility_id_1, facility_id_2)
* (instance_.distance(location_id_2, location_id_1)
- instance_.distance(location_id_1, location_id_2))
+ instance_.flow(facility_id_2, facility_id_1)
* (instance_.distance(location_id_1, location_id_2)
- instance_.distance(location_id_2, location_id_1));
for (FacilityId facility_id_3 = 0;
facility_id_3 < instance_.number_of_facilities();
++facility_id_3) {
if (facility_id_3 == facility_id_1)
continue;
if (facility_id_3 == facility_id_2)
continue;
LocationId location_id_3 = solution.locations[facility_id_3];
cost(gc)
+= instance_.flow(facility_id_1, facility_id_3)
* (instance_.distance(location_id_2, location_id_3)
- instance_.distance(location_id_1, location_id_3))
+ instance_.flow(facility_id_3, facility_id_1)
* (instance_.distance(location_id_3, location_id_2)
- instance_.distance(location_id_3, location_id_1))
+ instance_.flow(facility_id_2, facility_id_3)
* (instance_.distance(location_id_1, location_id_3)
- instance_.distance(location_id_2, location_id_3))
+ instance_.flow(facility_id_3, facility_id_2)
* (instance_.distance(location_id_3, location_id_1)
- instance_.distance(location_id_3, location_id_2));
}
return gc;
}
/*
* Private attributes
*/
/** Instance. */
const Instance& instance_;
/** Parameters. */
Parameters parameters_;
std::vector<FacilityId> facilities_;
std::vector<std::pair<FacilityId, FacilityId>> facility_pairs_;
};
}
}