-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknapsack_with_conflicts_main.cpp
More file actions
833 lines (738 loc) · 28.2 KB
/
knapsack_with_conflicts_main.cpp
File metadata and controls
833 lines (738 loc) · 28.2 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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/**
* Knapsack problem with conflicts
*
* Problem description:
* See https://github.com/fontanf/orproblems/blob/main/orproblems/knapsack_with_conflicts.hpp
*
* Local search:
*
* Three neighborhoods are implemented:
* - Toggle: remove an item from the knapsack or add an item into the knapsack
* and remove its neighbors from the knapsack
* Complexity: O(n)
* - Swap: swap an item from the knapsack with an item out of the knapsack
* which is not one of its neighbors (this is already considered by the
* Toggle neighborhood).
* Complexity: O(number of non-conflicts)
* - (2-1)-swap: remove an item from the knapsack and add two of its neighbors
* which are not neighbors.
* This neighborhood has originaly been proposed for the Maximum Independent
* Set Problem and for the Maximum-Weight Independent Set Problem.
* Complexity: O(number of conflicts)
* "Fast local search for the maximum independent set problem" (Andrade et al., 2012)
* https://doi.org/10.1007/s10732-012-9196-4
* "A hybrid iterated local search heuristic for the maximum weight independent set problem" (Nogueira et al., 2018)
* https://doi.org/10.1007/s11590-017-1128-7
*
*/
#include "read_args.hpp"
#include "localsearchsolver/common.hpp"
#include "orproblems/packing/knapsack_with_conflicts.hpp"
#include "optimizationtools//utils//utils.hpp"
using namespace localsearchsolver;
using namespace orproblems::knapsack_with_conflicts;
class LocalScheme
{
public:
struct Parameters
{
/** Enable Swap neighborhood. */
bool swap = true;
/** Enable (2-1)-swap neighborhood. */
bool swap_2_1 = true;
bool shuffle_neighborhood_order = true;
};
/*
* Constructors and destructor
*/
LocalScheme(
const Instance& instance,
Parameters parameters):
instance_(instance),
parameters_(parameters),
tabu_(instance_.number_of_items()),
items_(instance.number_of_items()),
neighbors_(instance_.number_of_items()),
free_items_(instance_.number_of_items()),
free_items_2_(instance_.number_of_items())
{
// Initialize items_.
std::iota(items_.begin(), items_.end(), 0);
}
/*
* Global cost
*/
/** Global cost: <Overweight, Profit, Weight>; */
using GlobalCost = std::tuple<Weight, Profit>;
inline Weight& overweight(GlobalCost& global_cost) const { return std::get<0>(global_cost); }
inline Profit& profit(GlobalCost& global_cost) const { return std::get<1>(global_cost); }
inline Weight overweight(const GlobalCost& global_cost) const { return std::get<0>(global_cost); }
inline Profit profit(const GlobalCost& global_cost) const { return std::get<1>(global_cost); }
inline GlobalCost global_cost_goal(double value) const
{
return {
0,
-value,
};
}
inline std::string to_string(const GlobalCost& global_cost) const
{
if (overweight(global_cost) > 0)
return "-inf";
return std::to_string(-profit(global_cost));
}
/*
* Solutions
*/
struct SolutionItem
{
/**
* in == true iff item j is in the solution.
*/
bool in = false;
/**
* neighbor_weight = w iff the sum of the weights of the neighbors of j
* which are in the solution is equal to w.
*/
Weight neighbor_weight = 0;
/**
* neighbor_profit = p iff the sum of the profits of the neighbors of j
* which are in the solution is equal to p.
*/
Profit neighbor_profit = 0;
};
struct Solution
{
std::vector<SolutionItem> items;
Profit profit = 0;
Weight weight = 0;
};
inline Solution empty_solution() const
{
Solution solution;
solution.items.resize(instance_.number_of_items());
return solution;
}
inline Solution initial_solution(
Counter,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
std::shuffle(items_.begin(), items_.end(), generator);
for (ItemId item_id: items_)
if (cost_add(solution, item_id) < global_cost(solution))
add(solution, item_id);
return solution;
}
inline GlobalCost global_cost(const Solution& solution) const
{
return {
overweight(solution),
-solution.profit,
};
}
/*
* Local search
*/
struct Perturbation;
inline void local_search(
Solution& solution,
std::mt19937_64& generator,
const Perturbation& tabu = Perturbation())
{
//std::cout << "> local search" << std::endl;
//print(std::cout, solution);
//std::cout << "tabu.j " << tabu.j << " " << tabu.add << std::endl;
// Get neighborhoods.
std::vector<Counter> neighborhoods = {0};
if (parameters_.swap)
neighborhoods.push_back(1);
if (parameters_.swap_2_1)
neighborhoods.push_back(2);
// We forbid changing the status of tabu.j. This means that, if tabu.j
// has been forced into the solution, we also need to forbid to add a
// neighbor of tabu.j.
std::fill(tabu_.begin(), tabu_.end(), 0);
if (tabu.item_id != -1) {
tabu_[tabu.item_id] = 1;
if (contains(solution, tabu.item_id))
for (ItemId item_id_neighbor: instance_.item(tabu.item_id).neighbors)
tabu_[item_id_neighbor] = 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: { // Toggle neighborhood.
std::shuffle(items_.begin(), items_.end(), generator);
ItemId item_id_best = -1;
GlobalCost c_best = global_cost(solution);
for (ItemId item_id: items_) {
if (tabu_[item_id] == 1)
continue;
GlobalCost c = (contains(solution, item_id))?
cost_remove(solution, item_id):
cost_add(solution, item_id);
if (c >= c_best)
continue;
if (item_id_best != -1 && !dominates(c, c_best))
continue;
item_id_best = item_id;
c_best = c;
}
if (item_id_best != -1) {
improved = true;
// Apply perturbation.
if (contains(solution, item_id_best)) {
remove(solution, item_id_best);
} else {
add(solution, item_id_best);
}
toggle_number_of_sucesses_++;
}
toggle_number_of_explorations_++;
break;
} case 1: { // Swap neighborhood.
std::shuffle(items_.begin(), items_.end(), generator);
items_in_.clear();
items_out_.clear();
for (ItemId item_id: items_) {
if (contains(solution, item_id)) {
items_in_.push_back(item_id);
} else {
items_out_.push_back(item_id);
}
}
ItemId item_id_in_best = -1;
ItemId item_id_out_best = -1;
GlobalCost c_best = global_cost(solution);
for (ItemId item_id_in: items_in_) {
if (tabu_[item_id_in] == 1)
continue;
neighbors_.clear();
for (ItemId item_id_neighbor: instance_.item(item_id_in).neighbors)
neighbors_.add(item_id_neighbor);
for (ItemId item_id_out: items_out_) {
if (tabu_[item_id_out] == 1)
continue;
if (neighbors_.contains(item_id_out))
continue;
GlobalCost c = cost_swap(
solution,
item_id_in,
item_id_out,
c_best);
if (c >= c_best)
continue;
if (item_id_in_best != -1 && !dominates(c, c_best))
continue;
item_id_in_best = item_id_in;
item_id_out_best = item_id_out;
c_best = c;
}
}
if (item_id_in_best != -1) {
improved = true;
// Apply perturbation.
remove(solution, item_id_in_best);
add(solution, item_id_out_best);
swap_number_of_sucesses_++;
}
swap_number_of_explorations_++;
break;
} case 2: { // (2-1)-swap neighborhood.
std::shuffle(items_.begin(), items_.end(), generator);
// Get items inside the knapsack.
items_in_.clear();
for (ItemId item_id: items_)
if (contains(solution, item_id))
items_in_.push_back(item_id);
ItemId item_id_in_best = -1;
ItemId item_id_out_1_best = -1;
ItemId item_id_out_2_best = -1;
GlobalCost c_best = global_cost(solution);
for (ItemId item_id_in: items_in_) {
if (tabu_[item_id_in] == 1)
continue;
// Update free_items_
free_items_.clear();
for (ItemId item_id_neighbor: instance_.item(item_id_in).neighbors)
if (tabu_[item_id_neighbor] == 0
&& solution.items[item_id_neighbor].neighbor_profit
== instance_.item(item_id_in).profit)
free_items_.add(item_id_neighbor);
if (free_items_.size() <= 2)
continue;
free_items_.shuffle_in(generator);
remove(solution, item_id_in);
for (ItemId item_id_out_1: free_items_) {
free_items_2_.clear();
for (ItemId item_id: free_items_)
free_items_2_.add(item_id);
free_items_2_.remove(item_id_out_1);
for (ItemId item_id_neighbor: instance_.item(item_id_out_1).neighbors)
if (free_items_2_.contains(item_id_neighbor))
free_items_2_.remove(item_id_neighbor);
if (free_items_2_.empty())
continue;
free_items_2_.shuffle_in(generator);
add(solution, item_id_out_1);
for (ItemId item_id_out_2: free_items_2_) {
GlobalCost c = cost_add(solution, item_id_out_2);
if (c >= c_best)
continue;
if (item_id_in_best != -1 && !dominates(c, c_best))
continue;
item_id_in_best = item_id_in;
item_id_out_1_best = item_id_out_1;
item_id_out_2_best = item_id_out_2;
c_best = c;
}
remove(solution, item_id_out_1);
}
add(solution, item_id_in);
}
if (item_id_in_best != -1) {
improved = true;
// Apply perturbation.
remove(solution, item_id_in_best);
add(solution, item_id_out_1_best);
add(solution, item_id_out_2_best);
swap_2_1_number_of_sucesses_++;
}
swap_2_1_number_of_explorations_++;
break;
}
}
if (improved)
break;
}
if (!improved)
break;
}
//print(std::cout, solution);
}
/*
* Genetic local search
*/
inline Solution crossover(
const Solution& solution_parent_1,
const Solution& solution_parent_2,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
std::vector<ItemId> items;
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (contains(solution_parent_1, item_id)
&& contains(solution_parent_2, item_id)) {
// Add items which are in both parents.
add(solution, item_id);
} else if (contains(solution_parent_1, item_id)
|| contains(solution_parent_2, item_id)) {
// Store items which are in one parent.
items.push_back(item_id);
}
}
// Add some of the items which are in one parent.
std::shuffle(items.begin(), items.end(), generator);
for (ItemId item_id: items)
if (cost_add(solution, item_id) < global_cost(solution))
add(solution, item_id);
return solution;
}
inline ItemId distance(
const Solution& solution_1,
const Solution& solution_2) const
{
ItemId d = 0;
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (contains(solution_1, item_id) != contains(solution_2, item_id))
d++;
}
return d;
}
/*
* Iterated local search
*/
struct Perturbation
{
Perturbation(): item_id(-1), global_cost(worst<GlobalCost>()) { }
ItemId item_id;
bool add;
GlobalCost global_cost;
};
inline std::vector<Perturbation> perturbations(
const Solution& solution,
std::mt19937_64&)
{
std::vector<Perturbation> perturbations;
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
Perturbation perturbation;
perturbation.item_id = item_id;
if (contains(solution, item_id)) {
perturbation.global_cost = global_cost(solution);
perturbation.add = false;
} else {
perturbation.global_cost = cost_add(solution, item_id);
overweight(perturbation.global_cost) = overweight(solution);
perturbation.add = true;
}
perturbations.push_back(perturbation);
}
return perturbations;
}
inline void apply_perturbation(
Solution& solution,
const Perturbation& perturbation,
std::mt19937_64&) const
{
if (perturbation.add) {
add(solution, perturbation.item_id);
} else {
remove(solution, perturbation.item_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> items(instance_.number_of_items(), false);
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (solution.items[item_id].in)
items[item_id] = true;
}
return items;
}
Solution compact2solution(const CompactSolution& compact_solution)
{
auto solution = empty_solution();
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (compact_solution[item_id])
add(solution, item_id);
}
return solution;
}
struct PerturbationHasher
{
std::hash<ItemId> hasher;
std::hash<bool> hasher_2;
inline bool hashable(const Perturbation&) const { return true; }
inline bool operator()(
const Perturbation& perturbation_1,
const Perturbation& perturbation_2) const
{
return perturbation_1.item_id == perturbation_2.item_id
&& perturbation_1.add == perturbation_2.add;
}
inline std::size_t operator()(
const Perturbation& perturbation) const
{
size_t hash = hasher(perturbation.item_id);
optimizationtools::hash_combine(hash, hasher_2(perturbation.add));
return hash;
}
};
inline PerturbationHasher perturbation_hasher() const { return PerturbationHasher(); }
/*
* Outputs
*/
void instance_format(
std::ostream& os,
int verbosity_level) const
{
os << "Knapsack problem with conflicts" << std::endl;
instance_.format(os, verbosity_level);
}
void solution_format(
std::ostream& os,
const Solution& solution,
int verbosity_level)
{
if (verbosity_level >= 1) {
ItemId number_of_items = 0;
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (contains(solution, item_id))
number_of_items++;
}
os
<< "Profit: " << solution.profit << std::endl
<< "Weight: " << solution.weight << " / " << instance_.capacity() << std::endl
<< "Number of items: " << number_of_items << " / " << instance_.number_of_items() << std::endl
;
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Item"
<< std::setw(12) << "Profit"
<< std::setw(12) << "Weight"
<< std::setw(12) << "Efficiency"
<< std::setw(12) << "# conflicts"
<< std::endl
<< std::setw(12) << "----"
<< std::setw(12) << "------"
<< std::setw(12) << "------"
<< std::setw(12) << "----------"
<< std::setw(12) << "-----------"
<< std::endl;
for (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (!contains(solution, item_id))
continue;
os
<< std::setw(12) << item_id
<< std::setw(12) << instance_.item(item_id).profit
<< std::setw(12) << instance_.item(item_id).weight
<< std::setw(12) << (double)instance_.item(item_id).profit / instance_.item(item_id).weight
<< std::setw(12) << instance_.item(item_id).neighbors.size()
<< 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 (ItemId item_id = 0;
item_id < instance_.number_of_items();
++item_id) {
if (contains(solution, item_id))
file << item_id << " ";
}
}
void parameters_format(
std::ostream& os) const
{
os
<< "Swap: " << parameters_.swap << std::endl
<< "(2,1)-swap: " << parameters_.swap_2_1 << std::endl;
}
void statistics_format(
std::ostream& os,
int verbosity_level) const
{
(void)verbosity_level;
os
<< "Toggle: "
<< toggle_number_of_explorations_
<< " / " << toggle_number_of_sucesses_
<< " / " << (double)toggle_number_of_sucesses_ / toggle_number_of_explorations_ * 100 << "%"
<< std::endl;
if (parameters_.swap) {
os
<< "Swap: "
<< swap_number_of_explorations_
<< " / " << swap_number_of_sucesses_
<< " / " << (double)swap_number_of_sucesses_ / swap_number_of_explorations_ * 100 << "%"
<< std::endl;
}
if (parameters_.swap_2_1) {
os
<< "(2-1)-swap: "
<< swap_2_1_number_of_explorations_
<< " / " << swap_2_1_number_of_sucesses_
<< " / " << (double)swap_2_1_number_of_sucesses_ / swap_2_1_number_of_explorations_ * 100 << "%"
<< std::endl;
}
}
private:
/*
* Manipulate solutions
*/
inline bool contains(
const Solution& solution,
ItemId item_id) const
{
return solution.items[item_id].in;
}
inline Weight overweight(const Solution& solution) const
{
return std::max((Weight)0, solution.weight - instance_.capacity());
}
inline void add(Solution& solution, ItemId item_id) const
{
solution.items[item_id].in = true;
Weight w = instance_.item(item_id).weight;
Profit p = instance_.item(item_id).profit;
solution.weight += w;
solution.profit += p;
for (ItemId item_id_neighbor: instance_.item(item_id).neighbors) {
if (contains(solution, item_id_neighbor))
remove(solution, item_id_neighbor);
solution.items[item_id_neighbor].neighbor_weight += w;
solution.items[item_id_neighbor].neighbor_profit += p;
}
}
inline void remove(
Solution& solution,
ItemId item_id) const
{
solution.items[item_id].in = false;
Weight w = instance_.item(item_id).weight;
Profit p = instance_.item(item_id).profit;
solution.weight -= w;
solution.profit -= p;
for (ItemId item_id_neighbor: instance_.item(item_id).neighbors) {
solution.items[item_id_neighbor].neighbor_weight -= w;
solution.items[item_id_neighbor].neighbor_profit -= p;
}
}
/*
* Evaluate moves
*/
inline GlobalCost cost_remove(
const Solution& solution,
ItemId item_id) const
{
Weight w = solution.weight - instance_.item(item_id).weight;
Profit p = solution.profit - instance_.item(item_id).profit;
return {std::max((Weight)0, w - instance_.capacity()), -p};
}
inline GlobalCost cost_add(
const Solution& solution,
ItemId item_id) const
{
Weight w = solution.weight
+ instance_.item(item_id).weight
- solution.items[item_id].neighbor_weight;
Profit p = solution.profit
+ instance_.item(item_id).profit
- solution.items[item_id].neighbor_profit;
return {std::max((Weight)0, w - instance_.capacity()), -p};
}
inline GlobalCost cost_swap(
const Solution& solution,
ItemId item_id_in,
ItemId item_id_out,
GlobalCost) const
{
Weight w = solution.weight
- instance_.item(item_id_in).weight
+ instance_.item(item_id_out).weight
- solution.items[item_id_out].neighbor_weight;
Profit p = solution.profit
- instance_.item(item_id_in).profit
+ instance_.item(item_id_out).profit
- solution.items[item_id_out].neighbor_profit;
return {std::max((Weight)0, w - instance_.capacity()), -p};
}
/*
* Private attributes
*/
/** Instance. */
const Instance& instance_;
/** Parameters. */
Parameters parameters_;
std::vector<int8_t> tabu_;
std::vector<ItemId> items_;
std::vector<ItemId> items_in_;
std::vector<ItemId> items_out_;
optimizationtools::IndexedSet neighbors_;
optimizationtools::IndexedSet free_items_;
optimizationtools::IndexedSet free_items_2_;
/*
* Statistics
*/
Counter toggle_number_of_explorations_ = 0;
Counter toggle_number_of_sucesses_ = 0;
Counter swap_number_of_explorations_ = 0;
Counter swap_number_of_sucesses_ = 0;
Counter swap_2_1_number_of_explorations_ = 0;
Counter swap_2_1_number_of_sucesses_ = 0;
};
int main(int argc, char *argv[])
{
// Create command line options.
boost::program_options::options_description desc = setup_args();
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << std::endl;;
throw "";
}
try {
boost::program_options::notify(vm);
} catch (const boost::program_options::required_option& e) {
std::cout << desc << std::endl;;
throw "";
}
// Create instance.
InstanceBuilder instance_builder;
instance_builder.read(
vm["input"].as<std::string>(),
vm["format"].as<std::string>());
const Instance instance = instance_builder.build();
// Create local scheme.
LocalScheme::Parameters parameters;
LocalScheme local_scheme(instance, parameters);
// Run algorithm.
std::string algorithm = "best-first-local-search";
if (vm.count("algorithm"))
algorithm = vm["algorithm"].as<std::string>();
auto output =
(algorithm == "multi-start-local-search")?
run_multi_start_local_search(local_scheme, vm):
(algorithm == "iterated-local-search")?
run_iterated_local_search(local_scheme, vm):
(algorithm == "best-first-local-search")?
run_best_first_local_search(local_scheme, vm):
run_genetic_local_search(local_scheme, vm);
// Run checker.
if (vm["print-checker"].as<int>() > 0
&& vm["certificate"].as<std::string>() != "") {
std::cout << std::endl
<< "Checker" << std::endl
<< "-------" << std::endl;
instance.check(
vm["certificate"].as<std::string>(),
std::cout,
vm["print-checker"].as<int>());
}
return 0;
}