forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_neighborhoods.cc
1188 lines (1103 loc) · 41.9 KB
/
routing_neighborhoods.cc
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/constraint_solver/routing_neighborhoods.h"
#include <algorithm>
namespace operations_research {
MakeRelocateNeighborsOperator::MakeRelocateNeighborsOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
RoutingTransitCallback2 arc_evaluator)
: PathWithPreviousNodesOperator(vars, secondary_vars, 2,
std::move(start_empty_path_class)),
arc_evaluator_(std::move(arc_evaluator)) {}
bool MakeRelocateNeighborsOperator::MakeNeighbor() {
const int64 before_chain = BaseNode(0);
if (IsPathEnd(before_chain)) {
return false;
}
int64 chain_end = Next(before_chain);
if (IsPathEnd(chain_end)) {
return false;
}
const int64 destination = BaseNode(1);
const int64 max_arc_value = arc_evaluator_(destination, chain_end);
int64 next = Next(chain_end);
while (!IsPathEnd(next) && arc_evaluator_(chain_end, next) <= max_arc_value) {
chain_end = next;
next = Next(chain_end);
}
return MoveChainAndRepair(before_chain, chain_end, destination);
}
bool MakeRelocateNeighborsOperator::MoveChainAndRepair(int64 before_chain,
int64 chain_end,
int64 destination) {
if (MoveChain(before_chain, chain_end, destination)) {
if (!IsPathStart(destination)) {
int64 current = Prev(destination);
int64 last = chain_end;
if (current == last) { // chain was just before destination
current = before_chain;
}
while (last >= 0 && !IsPathStart(current) && current != last) {
last = Reposition(current, last);
current = Prev(current);
}
}
return true;
}
return false;
}
int64 MakeRelocateNeighborsOperator::Reposition(int64 before_to_move,
int64 up_to) {
const int64 kNoChange = -1;
const int64 to_move = Next(before_to_move);
int64 next = Next(to_move);
if (Var(to_move)->Contains(next)) {
return kNoChange;
}
int64 prev = next;
next = Next(next);
while (prev != up_to) {
if (Var(prev)->Contains(to_move) && Var(to_move)->Contains(next)) {
MoveChain(before_to_move, to_move, prev);
return up_to;
}
prev = next;
next = Next(next);
}
if (Var(prev)->Contains(to_move)) {
MoveChain(before_to_move, to_move, prev);
return to_move;
}
return kNoChange;
}
MakePairActiveOperator::MakePairActiveOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& pairs)
: PathOperator(vars, secondary_vars, 2, false,
std::move(start_empty_path_class)),
inactive_pair_(0),
pairs_(pairs) {}
bool MakePairActiveOperator::MakeNextNeighbor(Assignment* delta,
Assignment* deltadelta) {
// TODO(user): Support pairs with disjunctions.
while (inactive_pair_ < pairs_.size()) {
if (!IsInactive(pairs_[inactive_pair_].first[0]) ||
!IsInactive(pairs_[inactive_pair_].second[0]) ||
!PathOperator::MakeNextNeighbor(delta, deltadelta)) {
ResetPosition();
++inactive_pair_;
} else {
return true;
}
}
return false;
}
bool MakePairActiveOperator::MakeNeighbor() {
DCHECK_EQ(StartNode(0), StartNode(1));
// Inserting the second node of the pair before the first one which ensures
// that the only solutions where both nodes are next to each other have the
// first node before the second (the move is not symmetric and doing it this
// way ensures that a potential precedence constraint between the nodes of the
// pair is not violated).
return MakeActive(pairs_[inactive_pair_].second[0], BaseNode(1)) &&
MakeActive(pairs_[inactive_pair_].first[0], BaseNode(0));
}
int64 MakePairActiveOperator::GetBaseNodeRestartPosition(int base_index) {
// Base node 1 must be after base node 0 if they are both on the same path.
if (base_index == 0 || StartNode(base_index) != StartNode(base_index - 1)) {
return StartNode(base_index);
} else {
return BaseNode(base_index - 1);
}
}
void MakePairActiveOperator::OnNodeInitialization() {
for (int i = 0; i < pairs_.size(); ++i) {
if (IsInactive(pairs_[i].first[0]) && IsInactive(pairs_[i].second[0])) {
inactive_pair_ = i;
return;
}
}
inactive_pair_ = pairs_.size();
}
MakePairInactiveOperator::MakePairInactiveOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 1,
std::move(start_empty_path_class)) {
int64 max_pair_index = -1;
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
}
}
bool MakePairInactiveOperator::MakeNeighbor() {
const int64 base = BaseNode(0);
if (IsPathEnd(base)) {
return false;
}
const int64 next = Next(base);
if (next < pairs_.size() && pairs_[next] != -1) {
return MakeChainInactive(Prev(pairs_[next]), pairs_[next]) &&
MakeChainInactive(base, next);
}
return false;
}
PairRelocateOperator::PairRelocateOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 3,
std::move(start_empty_path_class)) {
int64 index_max = 0;
for (const IntVar* const var : vars) {
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
is_first_[index_pair.first[0]] = true;
}
}
bool PairRelocateOperator::MakeNeighbor() {
DCHECK_EQ(StartNode(1), StartNode(2));
const int64 first_pair_node = BaseNode(kPairFirstNode);
if (IsPathStart(first_pair_node)) {
return false;
}
int64 first_prev = Prev(first_pair_node);
const int second_pair_node =
first_pair_node < pairs_.size() ? pairs_[first_pair_node] : -1;
if (second_pair_node < 0) {
return false;
}
if (!is_first_[first_pair_node]) {
return false;
}
if (IsPathStart(second_pair_node)) {
return false;
}
const int64 second_prev = Prev(second_pair_node);
if (BaseNode(kPairFirstNodeDestination) == second_pair_node) {
// The second_pair_node -> first_pair_node link is forbidden.
return false;
}
if (second_prev == first_pair_node &&
BaseNode(kPairFirstNodeDestination) == first_prev &&
BaseNode(kPairSecondNodeDestination) == first_prev) {
// If the current sequence is first_prev -> first_pair_node ->
// second_pair_node, and both 1st and 2nd are moved both to prev, the result
// of the move will be first_prev -> first_pair_node -> second_pair_node,
// which is no move.
return false;
}
// Relocation is a success if at least one of the nodes moved and all the
// moves are successful.
bool moved = false;
// Do not allow to move second_pair_node to its current prev.
if (second_prev != BaseNode(kPairSecondNodeDestination)) {
if (!MoveChain(second_prev, second_pair_node,
BaseNode(kPairSecondNodeDestination))) {
return false;
}
if (BaseNode(kPairSecondNodeDestination) == first_prev) {
first_prev = second_pair_node;
}
moved = true;
}
// Do not allow to move first_pair_node to its current prev.
if (first_prev != BaseNode(kPairFirstNodeDestination)) {
if (!MoveChain(first_prev, first_pair_node,
BaseNode(kPairFirstNodeDestination))) {
return false;
}
moved = true;
}
return moved;
}
int64 PairRelocateOperator::GetBaseNodeRestartPosition(int base_index) {
// Destination node of the second node of a pair must be after the
// destination node of the first node of a pair.
if (base_index == kPairSecondNodeDestination) {
return BaseNode(kPairFirstNodeDestination);
} else {
return StartNode(base_index);
}
}
LightPairRelocateOperator::LightPairRelocateOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 2,
std::move(start_empty_path_class)) {
int64 max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
}
}
bool LightPairRelocateOperator::MakeNeighbor() {
const int64 prev1 = BaseNode(0);
if (IsPathEnd(prev1)) return false;
const int64 node1 = Next(prev1);
if (IsPathEnd(node1) || node1 >= pairs_.size()) return false;
const int64 sibling1 = pairs_[node1];
if (sibling1 == -1) return false;
const int64 node2 = BaseNode(1);
if (node2 == sibling1 || IsPathEnd(node2) || node2 >= pairs_.size()) {
return false;
}
const int64 sibling2 = pairs_[node2];
if (sibling2 == -1) return false;
int64 prev_sibling1 = Prev(sibling1);
if (prev_sibling1 == node1) {
prev_sibling1 = prev1;
} else if (prev_sibling1 == node2) {
prev_sibling1 = node1;
}
return MoveChain(prev1, node1, node2) &&
(sibling2 == prev_sibling1 ||
MoveChain(prev_sibling1, sibling1, sibling2));
}
PairExchangeOperator::PairExchangeOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 2,
std::move(start_empty_path_class)) {
int64 index_max = 0;
for (const IntVar* const var : vars) {
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
is_first_[index_pair.first[0]] = true;
}
}
bool PairExchangeOperator::MakeNeighbor() {
const int64 node1 = BaseNode(0);
int64 prev1, sibling1, sibling_prev1 = -1;
if (!GetPreviousAndSibling(node1, &prev1, &sibling1, &sibling_prev1)) {
return false;
}
const int64 node2 = BaseNode(1);
int64 prev2, sibling2, sibling_prev2 = -1;
if (!GetPreviousAndSibling(node2, &prev2, &sibling2, &sibling_prev2)) {
return false;
}
bool status = true;
// Exchanging node1 and node2.
if (node1 == prev2) {
status = MoveChain(prev2, node2, prev1);
if (sibling_prev1 == node2) sibling_prev1 = node1;
if (sibling_prev2 == node2) sibling_prev2 = node1;
} else if (node2 == prev1) {
status = MoveChain(prev1, node1, prev2);
if (sibling_prev1 == node1) sibling_prev1 = node2;
if (sibling_prev2 == node1) sibling_prev2 = node2;
} else {
status = MoveChain(prev1, node1, node2) && MoveChain(prev2, node2, prev1);
if (sibling_prev1 == node1) {
sibling_prev1 = node2;
} else if (sibling_prev1 == node2) {
sibling_prev1 = node1;
}
if (sibling_prev2 == node1) {
sibling_prev2 = node2;
} else if (sibling_prev2 == node2) {
sibling_prev2 = node1;
}
}
if (!status) return false;
// Exchanging sibling1 and sibling2.
if (sibling1 == sibling_prev2) {
status = MoveChain(sibling_prev2, sibling2, sibling_prev1);
} else if (sibling2 == sibling_prev1) {
status = MoveChain(sibling_prev1, sibling1, sibling_prev2);
} else {
status = MoveChain(sibling_prev1, sibling1, sibling2) &&
MoveChain(sibling_prev2, sibling2, sibling_prev1);
}
return status;
}
bool PairExchangeOperator::GetPreviousAndSibling(
int64 node, int64* previous, int64* sibling,
int64* sibling_previous) const {
if (IsPathStart(node)) return false;
*previous = Prev(node);
*sibling = node < pairs_.size() ? pairs_[node] : -1;
*sibling_previous = *sibling >= 0 ? Prev(*sibling) : -1;
return *sibling_previous >= 0 && is_first_[node];
}
PairExchangeRelocateOperator::PairExchangeRelocateOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 6,
std::move(start_empty_path_class)) {
int64 index_max = 0;
for (const IntVar* const var : vars) {
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
is_first_[index_pair.first[0]] = true;
}
}
bool PairExchangeRelocateOperator::MakeNeighbor() {
DCHECK_EQ(StartNode(kSecondPairFirstNodeDestination),
StartNode(kSecondPairSecondNodeDestination));
DCHECK_EQ(StartNode(kSecondPairFirstNode),
StartNode(kFirstPairFirstNodeDestination));
DCHECK_EQ(StartNode(kSecondPairFirstNode),
StartNode(kFirstPairSecondNodeDestination));
if (StartNode(kFirstPairFirstNode) == StartNode(kSecondPairFirstNode)) {
SetNextBaseToIncrement(kSecondPairFirstNode);
return false;
}
// Through this method, <base>[X][Y] represent the <base> variable for the
// node Y of pair X. <base> is in node, prev, dest.
int64 nodes[2][2];
int64 prev[2][2];
int64 dest[2][2];
nodes[0][0] = BaseNode(kFirstPairFirstNode);
nodes[1][0] = BaseNode(kSecondPairFirstNode);
if (nodes[1][0] <= nodes[0][0]) {
// Exchange is symetric.
SetNextBaseToIncrement(kSecondPairFirstNode);
return false;
}
if (!GetPreviousAndSibling(nodes[0][0], &prev[0][0], &nodes[0][1],
&prev[0][1])) {
SetNextBaseToIncrement(kFirstPairFirstNode);
return false;
}
if (!GetPreviousAndSibling(nodes[1][0], &prev[1][0], &nodes[1][1],
&prev[1][1])) {
SetNextBaseToIncrement(kSecondPairFirstNode);
return false;
}
if (!LoadAndCheckDest(0, 0, kFirstPairFirstNodeDestination, nodes, dest)) {
SetNextBaseToIncrement(kFirstPairFirstNodeDestination);
return false;
}
if (!LoadAndCheckDest(0, 1, kFirstPairSecondNodeDestination, nodes, dest)) {
SetNextBaseToIncrement(kFirstPairSecondNodeDestination);
return false;
}
if (StartNode(kSecondPairFirstNodeDestination) !=
StartNode(kFirstPairFirstNode) ||
!LoadAndCheckDest(1, 0, kSecondPairFirstNodeDestination, nodes, dest)) {
SetNextBaseToIncrement(kSecondPairFirstNodeDestination);
return false;
}
if (!LoadAndCheckDest(1, 1, kSecondPairSecondNodeDestination, nodes, dest)) {
SetNextBaseToIncrement(kSecondPairSecondNodeDestination);
return false;
}
if (!MoveNode(0, 1, nodes, dest, prev)) {
SetNextBaseToIncrement(kFirstPairSecondNodeDestination);
return false;
}
if (!MoveNode(0, 0, nodes, dest, prev)) {
SetNextBaseToIncrement(kFirstPairSecondNodeDestination);
return false;
}
if (!MoveNode(1, 1, nodes, dest, prev)) {
return false;
}
if (!MoveNode(1, 0, nodes, dest, prev)) {
return false;
}
return true;
}
bool PairExchangeRelocateOperator::MoveNode(int pair, int node,
int64 nodes[2][2], int64 dest[2][2],
int64 prev[2][2]) {
if (!MoveChain(prev[pair][node], nodes[pair][node], dest[pair][node])) {
return false;
}
// Update the other pair if needed.
if (prev[1 - pair][0] == dest[pair][node]) {
prev[1 - pair][0] = nodes[pair][node];
}
if (prev[1 - pair][1] == dest[pair][node]) {
prev[1 - pair][1] = nodes[pair][node];
}
return true;
}
bool PairExchangeRelocateOperator::LoadAndCheckDest(int pair, int node,
int64 base_node,
int64 nodes[2][2],
int64 dest[2][2]) const {
dest[pair][node] = BaseNode(base_node);
// A destination cannot be a node that will be moved.
return !(nodes[0][0] == dest[pair][node] || nodes[0][1] == dest[pair][node] ||
nodes[1][0] == dest[pair][node] || nodes[1][1] == dest[pair][node]);
}
bool PairExchangeRelocateOperator::OnSamePathAsPreviousBase(int64 base_index) {
// Ensuring the destination of the first pair is on the route of the second.
// pair.
// Ensuring that destination of both nodes of a pair are on the same route.
return base_index == kFirstPairFirstNodeDestination ||
base_index == kFirstPairSecondNodeDestination ||
base_index == kSecondPairSecondNodeDestination;
}
int64 PairExchangeRelocateOperator::GetBaseNodeRestartPosition(int base_index) {
if (base_index == kFirstPairSecondNodeDestination ||
base_index == kSecondPairSecondNodeDestination) {
return BaseNode(base_index - 1);
} else {
return StartNode(base_index);
}
}
bool PairExchangeRelocateOperator::GetPreviousAndSibling(
int64 node, int64* previous, int64* sibling,
int64* sibling_previous) const {
if (IsPathStart(node)) return false;
*previous = Prev(node);
*sibling = node < pairs_.size() ? pairs_[node] : -1;
*sibling_previous = *sibling >= 0 ? Prev(*sibling) : -1;
return *sibling_previous >= 0 && is_first_[node];
}
SwapIndexPairOperator::SwapIndexPairOperator(
const std::vector<IntVar*>& vars, const std::vector<IntVar*>& path_vars,
const RoutingIndexPairs& index_pairs)
: IntVarLocalSearchOperator(vars),
index_pairs_(index_pairs),
pair_index_(0),
first_index_(0),
second_index_(0),
number_of_nexts_(vars.size()),
ignore_path_vars_(path_vars.empty()) {
if (!ignore_path_vars_) {
AddVars(path_vars);
}
}
bool SwapIndexPairOperator::MakeNextNeighbor(Assignment* delta,
Assignment* deltadelta) {
const int64 kNoPath = -1;
CHECK(delta != nullptr);
while (true) {
RevertChanges(true);
if (pair_index_ < index_pairs_.size()) {
const int64 path =
ignore_path_vars_ ? 0LL : Value(first_active_ + number_of_nexts_);
const int64 prev_first = prevs_[first_active_];
const int64 next_first = Value(first_active_);
// Making current active "pickup" unperformed.
SetNext(first_active_, first_active_, kNoPath);
// Inserting "pickup" alternative at the same position.
const int64 insert_first = index_pairs_[pair_index_].first[first_index_];
SetNext(prev_first, insert_first, path);
SetNext(insert_first, next_first, path);
int64 prev_second = prevs_[second_active_];
if (prev_second == first_active_) {
prev_second = insert_first;
}
DCHECK_EQ(path, ignore_path_vars_
? 0LL
: Value(second_active_ + number_of_nexts_));
const int64 next_second = Value(second_active_);
// Making current active "delivery" unperformed.
SetNext(second_active_, second_active_, kNoPath);
// Inserting "delivery" alternative at the same position.
const int64 insert_second =
index_pairs_[pair_index_].second[second_index_];
SetNext(prev_second, insert_second, path);
SetNext(insert_second, next_second, path);
// Move to next "pickup/delivery" alternative.
++second_index_;
if (second_index_ >= index_pairs_[pair_index_].second.size()) {
second_index_ = 0;
++first_index_;
if (first_index_ >= index_pairs_[pair_index_].first.size()) {
first_index_ = 0;
++pair_index_;
UpdateActiveNodes();
}
}
} else {
return false;
}
if (ApplyChanges(delta, deltadelta)) {
VLOG(2) << "Delta (" << DebugString() << ") = " << delta->DebugString();
return true;
}
}
return false;
}
void SwapIndexPairOperator::OnStart() {
prevs_.resize(number_of_nexts_, -1);
for (int index = 0; index < number_of_nexts_; ++index) {
const int64 next = Value(index);
if (next >= prevs_.size()) prevs_.resize(next + 1, -1);
prevs_[next] = index;
}
pair_index_ = 0;
first_index_ = 0;
second_index_ = 0;
first_active_ = -1;
second_active_ = -1;
while (true) {
if (!UpdateActiveNodes()) break;
if (first_active_ != -1 && second_active_ != -1) {
break;
}
++pair_index_;
}
}
bool SwapIndexPairOperator::UpdateActiveNodes() {
if (pair_index_ < index_pairs_.size()) {
for (const int64 first : index_pairs_[pair_index_].first) {
if (Value(first) != first) {
first_active_ = first;
break;
}
}
for (const int64 second : index_pairs_[pair_index_].second) {
if (Value(second) != second) {
second_active_ = second;
break;
}
}
return true;
}
return false;
}
IndexPairSwapActiveOperator::IndexPairSwapActiveOperator(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 1,
std::move(start_empty_path_class)),
inactive_node_(0) {
int64 max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
}
pairs_.resize(max_pair_index + 1, -1);
for (const auto& index_pair : index_pairs) {
pairs_[index_pair.first[0]] = index_pair.second[0];
pairs_[index_pair.second[0]] = index_pair.first[0];
}
}
bool IndexPairSwapActiveOperator::MakeNextNeighbor(Assignment* delta,
Assignment* deltadelta) {
while (inactive_node_ < Size()) {
if (!IsInactive(inactive_node_) ||
!PathOperator::MakeNextNeighbor(delta, deltadelta)) {
ResetPosition();
++inactive_node_;
} else {
return true;
}
}
return false;
}
bool IndexPairSwapActiveOperator::MakeNeighbor() {
const int64 base = BaseNode(0);
if (IsPathEnd(base)) {
return false;
}
const int64 next = Next(base);
if (next < pairs_.size() && pairs_[next] != -1) {
return MakeChainInactive(Prev(pairs_[next]), pairs_[next]) &&
MakeChainInactive(base, next) && MakeActive(inactive_node_, base);
}
return false;
}
void IndexPairSwapActiveOperator::OnNodeInitialization() {
PathWithPreviousNodesOperator::OnNodeInitialization();
for (int i = 0; i < Size(); ++i) {
if (IsInactive(i) && i < pairs_.size() && pairs_[i] == -1) {
inactive_node_ = i;
return;
}
}
inactive_node_ = Size();
}
RelocateExpensiveChain::RelocateExpensiveChain(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class, int num_arcs_to_consider,
std::function<int64(int64, int64, int64)> arc_cost_for_path_start)
: PathOperator(vars, secondary_vars, 1, false,
std::move(start_empty_path_class)),
num_arcs_to_consider_(num_arcs_to_consider),
current_path_(0),
current_expensive_arc_indices_({-1, -1}),
arc_cost_for_path_start_(std::move(arc_cost_for_path_start)),
end_path_(0),
has_non_empty_paths_to_explore_(false) {}
bool RelocateExpensiveChain::MakeNeighbor() {
const int first_arc_index = current_expensive_arc_indices_.first;
const int second_arc_index = current_expensive_arc_indices_.second;
DCHECK_LE(0, first_arc_index);
DCHECK_LT(first_arc_index, second_arc_index);
DCHECK_LT(second_arc_index, most_expensive_arc_starts_and_ranks_.size());
const std::pair<int, int>& first_start_and_rank =
most_expensive_arc_starts_and_ranks_[first_arc_index];
const std::pair<int, int>& second_start_and_rank =
most_expensive_arc_starts_and_ranks_[second_arc_index];
if (first_start_and_rank.second < second_start_and_rank.second) {
return MoveChain(first_start_and_rank.first, second_start_and_rank.first,
BaseNode(0));
}
return MoveChain(second_start_and_rank.first, first_start_and_rank.first,
BaseNode(0));
}
bool RelocateExpensiveChain::MakeOneNeighbor() {
while (has_non_empty_paths_to_explore_) {
if (!PathOperator::MakeOneNeighbor()) {
ResetPosition();
// Move on to the next expensive arcs on the same path.
if (IncrementCurrentArcIndices()) {
continue;
}
// Move on to the next non_empty path.
IncrementCurrentPath();
has_non_empty_paths_to_explore_ =
current_path_ != end_path_ &&
FindMostExpensiveChainsOnRemainingPaths();
} else {
return true;
}
}
return false;
}
void RelocateExpensiveChain::OnNodeInitialization() {
if (current_path_ >= path_starts().size()) {
// current_path_ was made empty by last move (and it was the last non-empty
// path), restart from 0.
current_path_ = 0;
}
end_path_ = current_path_;
has_non_empty_paths_to_explore_ = FindMostExpensiveChainsOnRemainingPaths();
}
void RelocateExpensiveChain::IncrementCurrentPath() {
const int num_paths = path_starts().size();
if (++current_path_ == num_paths) {
current_path_ = 0;
}
}
bool RelocateExpensiveChain::IncrementCurrentArcIndices() {
int& second_index = current_expensive_arc_indices_.second;
if (++second_index < most_expensive_arc_starts_and_ranks_.size()) {
return true;
}
int& first_index = current_expensive_arc_indices_.first;
if (first_index + 2 < most_expensive_arc_starts_and_ranks_.size()) {
first_index++;
second_index = first_index + 1;
return true;
}
return false;
}
bool RelocateExpensiveChain::FindMostExpensiveChainsOnRemainingPaths() {
do {
if (FindMostExpensiveChainsOnCurrentPath()) {
return true;
}
IncrementCurrentPath();
} while (current_path_ != end_path_);
return false;
}
bool RelocateExpensiveChain::FindMostExpensiveChainsOnCurrentPath() {
const int64 current_path_start = path_starts()[current_path_];
if (IsPathEnd(OldNext(current_path_start))) {
// Empty path.
current_expensive_arc_indices_.first =
current_expensive_arc_indices_.second = -1;
return false;
}
// TODO(user): Investigate the impact of using a limited size priority
// queue instead of vectors on performance.
std::vector<int64> most_expensive_arc_costs(num_arcs_to_consider_, -1);
most_expensive_arc_starts_and_ranks_.assign(num_arcs_to_consider_, {-1, -1});
int64 before_node = current_path_start;
int rank = 0;
while (!IsPathEnd(before_node)) {
const int64 after_node = OldNext(before_node);
const int64 arc_cost =
arc_cost_for_path_start_(before_node, after_node, current_path_start);
if (most_expensive_arc_costs.back() < arc_cost) {
// Insert this arc in most_expensive_* vectors.
most_expensive_arc_costs.back() = arc_cost;
most_expensive_arc_starts_and_ranks_.back().first = before_node;
most_expensive_arc_starts_and_ranks_.back().second = rank;
// Move the newly added element in the vectors to keep
// most_expensive_arc_costs sorted decreasingly.
int index_before_added_arc = num_arcs_to_consider_ - 2;
while (index_before_added_arc >= 0 &&
most_expensive_arc_costs[index_before_added_arc] < arc_cost) {
std::swap(most_expensive_arc_costs[index_before_added_arc + 1],
most_expensive_arc_costs[index_before_added_arc]);
std::swap(
most_expensive_arc_starts_and_ranks_[index_before_added_arc + 1],
most_expensive_arc_starts_and_ranks_[index_before_added_arc]);
index_before_added_arc--;
}
}
before_node = after_node;
rank++;
}
// If there are less than num_arcs_to_consider_ arcs on the path, resize the
// vector of arc starts.
if (rank < num_arcs_to_consider_) {
most_expensive_arc_starts_and_ranks_.resize(rank);
}
current_expensive_arc_indices_.first = 0;
current_expensive_arc_indices_.second = 1;
return true;
}
RelocateSubtrip::RelocateSubtrip(
const std::vector<IntVar*>& vars,
const std::vector<IntVar*>& secondary_vars,
std::function<int(int64)> start_empty_path_class,
const RoutingIndexPairs& pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars,
/*number_of_base_nodes*/ 2,
std::move(start_empty_path_class)) {
is_pickup_node_.resize(number_of_nexts_, false);
is_delivery_node_.resize(number_of_nexts_, false);
pair_of_node_.resize(number_of_nexts_, -1);
for (int pair_index = 0; pair_index < pairs.size(); ++pair_index) {
for (const int node : pairs[pair_index].first) {
is_pickup_node_[node] = true;
pair_of_node_[node] = pair_index;
}
for (const int node : pairs[pair_index].second) {
is_delivery_node_[node] = true;
pair_of_node_[node] = pair_index;
}
}
opened_pairs_bitset_.resize(pairs.size(), false);
}
bool RelocateSubtrip::RelocateSubTripFromPickup(const int64 chain_first_node,
const int64 insertion_node) {
if (IsPathEnd(insertion_node)) return false;
if (Prev(chain_first_node) == insertion_node)
return false; // Skip null move.
int num_opened_pairs = 0;
// Split chain into subtrip and rejected nodes.
rejected_nodes_ = {Prev(chain_first_node)};
subtrip_nodes_ = {insertion_node};
int current = chain_first_node;
do {
if (current == insertion_node) {
// opened_pairs_bitset_ must be all false when we leave this function.
opened_pairs_bitset_.assign(opened_pairs_bitset_.size(), false);
return false;
}
const int pair = pair_of_node_[current];
if (is_delivery_node_[current] && !opened_pairs_bitset_[pair]) {
rejected_nodes_.push_back(current);
} else {
subtrip_nodes_.push_back(current);
if (is_pickup_node_[current]) {
++num_opened_pairs;
opened_pairs_bitset_[pair] = true;
} else if (is_delivery_node_[current]) {
--num_opened_pairs;
opened_pairs_bitset_[pair] = false;
}
}
current = Next(current);
} while (num_opened_pairs != 0 && !IsPathEnd(current));
DCHECK_EQ(num_opened_pairs, 0);
rejected_nodes_.push_back(current);
subtrip_nodes_.push_back(Next(insertion_node));
// Set new paths.
const int64 rejected_path = Path(chain_first_node);
for (int i = 1; i < rejected_nodes_.size(); ++i) {
SetNext(rejected_nodes_[i - 1], rejected_nodes_[i], rejected_path);
}
const int64 insertion_path = Path(insertion_node);
for (int i = 1; i < subtrip_nodes_.size(); ++i) {
SetNext(subtrip_nodes_[i - 1], subtrip_nodes_[i], insertion_path);
}
return true;
}
bool RelocateSubtrip::RelocateSubTripFromDelivery(const int64 chain_last_node,
const int64 insertion_node) {
if (IsPathEnd(insertion_node)) return false;
// opened_pairs_bitset_ should be all false.
DCHECK(std::none_of(opened_pairs_bitset_.begin(), opened_pairs_bitset_.end(),
[](bool value) { return value; }));
int num_opened_pairs = 0;
// Split chain into subtrip and rejected nodes. Store nodes in reverse order.
rejected_nodes_ = {Next(chain_last_node)};
subtrip_nodes_ = {Next(insertion_node)};
int current = chain_last_node;
do {
if (current == insertion_node) {
opened_pairs_bitset_.assign(opened_pairs_bitset_.size(), false);
return false;
}
const int pair = pair_of_node_[current];
if (is_pickup_node_[current] && !opened_pairs_bitset_[pair]) {
rejected_nodes_.push_back(current);
} else {
subtrip_nodes_.push_back(current);
if (is_delivery_node_[current]) {
++num_opened_pairs;
opened_pairs_bitset_[pair] = true;
} else if (is_pickup_node_[current]) {
--num_opened_pairs;
opened_pairs_bitset_[pair] = false;
}
}
current = Prev(current);
} while (num_opened_pairs != 0 && !IsPathStart(current));
DCHECK_EQ(num_opened_pairs, 0);
if (current == insertion_node) return false; // Skip null move.
rejected_nodes_.push_back(current);
subtrip_nodes_.push_back(insertion_node);
// TODO(user): either remove those std::reverse() and adapt the loops
// below, or refactor the loops into a function that also DCHECKs the path.
std::reverse(rejected_nodes_.begin(), rejected_nodes_.end());
std::reverse(subtrip_nodes_.begin(), subtrip_nodes_.end());
// Set new paths.
const int64 rejected_path = Path(chain_last_node);
for (int i = 1; i < rejected_nodes_.size(); ++i) {
SetNext(rejected_nodes_[i - 1], rejected_nodes_[i], rejected_path);
}
const int64 insertion_path = Path(insertion_node);
for (int i = 1; i < subtrip_nodes_.size(); ++i) {
SetNext(subtrip_nodes_[i - 1], subtrip_nodes_[i], insertion_path);
}
return true;