-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cc
More file actions
1263 lines (1147 loc) · 44.6 KB
/
solver.cc
File metadata and controls
1263 lines (1147 loc) · 44.6 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
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
/*
LENS Optimizer: fifth place winner of the IOPDDL track, ASPLOS&EuroSys'25
Contest.
Members: Haodi Jiang*, Yitian Yang*, Ruwen Fan, Shiwei Gao, Shaoxun Zeng,
Junrong Huang, Huajun Bai, Hao Guo and Youyou Lu
Below is the original copyright information from the framework:
Copyright 2024 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
https://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 "solver.h"
#include <absl/numeric/int128.h>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <thread>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_join.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "iopddl.h"
namespace iopddl {
using EdgeList = std::vector<std::vector<std::tuple<NodeIdx, EdgeIdx, bool>>>;
// =================================
// Auxilary Data Structures:
// =================================
// ==================================================
// Optimized Usage Calculation: Segment Tree Based
// ==================================================
class UsageTree {
struct Node {
TotalUsage max_usage;
TotalUsage lazy;
};
int64_t n;
std::vector<Node> tree;
void push_up(int64_t x) {
tree[x].max_usage =
std::max(tree[x * 2].max_usage, tree[x * 2 + 1].max_usage);
}
void push_down(int64_t x) {
if (tree[x].lazy) {
tree[x * 2].max_usage += tree[x].lazy;
tree[x * 2 + 1].max_usage += tree[x].lazy;
tree[x * 2].lazy += tree[x].lazy;
tree[x * 2 + 1].lazy += tree[x].lazy;
tree[x].lazy = 0;
}
}
void build(int64_t x, int64_t l, int64_t r,
const std::vector<TotalUsage> &v) {
if (l == r) {
tree[x].max_usage = v[l];
return;
}
int64_t mid = (l + r) / 2;
build(x * 2, l, mid, v);
build(x * 2 + 1, mid + 1, r, v);
push_up(x);
}
void update(int64_t x, int64_t l, int64_t r, int64_t ql, int64_t qr,
TotalUsage v) {
if (ql <= l && r <= qr) {
tree[x].max_usage += v;
tree[x].lazy += v;
return;
}
push_down(x);
int64_t mid = (l + r) / 2;
if (ql <= mid)
update(x * 2, l, mid, ql, qr, v);
if (qr > mid)
update(x * 2 + 1, mid + 1, r, ql, qr, v);
push_up(x);
}
TotalUsage query(int64_t x, int64_t l, int64_t r, int64_t ql, int64_t qr) {
if (ql <= l && r <= qr) {
return tree[x].max_usage;
}
push_down(x);
int64_t mid = (l + r) / 2;
TotalUsage ret = 0;
if (ql <= mid)
ret = std::max(ret, query(x * 2, l, mid, ql, qr));
if (qr > mid)
ret = std::max(ret, query(x * 2 + 1, mid + 1, r, ql, qr));
return ret;
}
public:
UsageTree(const std::vector<TotalUsage> &v) : n(v.size()), tree(4 * n) {
build(1, 0, n - 1, v);
}
void update(int64_t l, int64_t r, TotalUsage v) {
if (l < 0 || r >= n || l > r) {
return;
}
update(1, 0, n - 1, l, r, v);
}
TotalUsage query(int64_t l, int64_t r) {
if (l < 0 || r >= n || l > r) {
return 0;
}
return query(1, 0, n - 1, l, r);
}
TotalUsage query() { return query(0, n - 1); }
};
// ================================================================
// General Interface for the Problem's structure,
// which allows fast usage update, cost and validaty maintainance,
// and supports auto-revert
// ================================================================
class ExtendedProblem {
const Problem &problem;
const EdgeList &edges;
const std::vector<std::pair<TimeIdx, TimeIdx>> &new_interval;
std::vector<StrategyIdx> &solution;
UsageTree &usage_tree;
std::stack<std::pair<NodeIdx, StrategyIdx>> history;
TotalCost cost;
Cost get_edge_cost(EdgeIdx eid, StrategyIdx u, StrategyIdx v,
bool reverse = false) {
const auto &edge = problem.edges[eid];
StrategyIdx strategy_idx = 0;
if (reverse)
std::swap(u, v);
std::vector<StrategyIdx> p = {u, v};
auto idx = p.begin();
for (const NodeIdx node_idx : edge.nodes) {
strategy_idx *= problem.nodes[node_idx].strategies.size();
strategy_idx += *(idx++);
}
return edge.strategies[strategy_idx].cost;
}
public:
ExtendedProblem(const Problem &problem, const EdgeList &edges,
const std::vector<std::pair<TimeIdx, TimeIdx>> &new_interval,
Solution &solution, UsageTree &usage_tree, TotalCost cost)
: problem(problem), edges(edges), new_interval(new_interval),
solution(solution), usage_tree(usage_tree), cost(cost) {}
void check() { return; }
std::optional<TotalCost> get_cost() const {
auto max_usage =
problem.usage_limit ? *problem.usage_limit : absl::Int128Max();
return (usage_tree.query() > max_usage) ? std::nullopt
: std::make_optional(cost);
}
Solution get_solution() const { return solution; }
StrategyIdx get_strategy(NodeIdx x) const { return solution[x]; }
void update(NodeIdx x, StrategyIdx s, bool record = true) {
if (record)
history.push({x, solution[x]});
const auto &node = problem.nodes[x];
auto [begin, end] = new_interval[x];
auto old_strategy = solution[x];
solution[x] = s;
auto max_usage =
problem.usage_limit ? *problem.usage_limit : absl::Int128Max();
usage_tree.update(begin, end - 1,
node.strategies[s].usage -
node.strategies[old_strategy].usage);
for (auto [v, e, rev] : edges[x]) {
auto old_cost = get_edge_cost(e, old_strategy, solution[v], rev);
auto new_cost = get_edge_cost(e, s, solution[v], rev);
cost += new_cost - old_cost;
}
cost += node.strategies[s].cost - node.strategies[old_strategy].cost;
}
void undo() {
while (!history.empty()) {
auto [x, s] = history.top();
history.pop();
update(x, s, false);
}
}
void commit() { history = std::stack<std::pair<NodeIdx, StrategyIdx>>(); }
~ExtendedProblem() { undo(); }
};
// ================================================================
// Technique #1: Chained Restructuring
// A seperate module
// ================================================================
template <class UsageManager, class EdgeManager> struct ChainedRestruct {
std::random_device rd;
std::mt19937 gen{rd()};
std::uniform_real_distribution<> dis{0.0, 1.0};
const Problem &problem;
const EdgeManager &edges;
const std::vector<Interval> &interval;
std::tuple<NodeIdx, StrategyIdx, TotalCost> *modify_queue;
NodeIdx modify_queue_head = 0, modify_queue_tail = 0;
bool *modify_flag;
std::tuple<NodeIdx, StrategyIdx> *saved_modify_queue;
NodeIdx saved_modify_queue_length;
TotalCost saved_total_cost;
NodeIdx *random_seq;
bool *random_flag;
NodeIdx random_i = 0;
const TotalCost inf = 1000000000000000000;
std::vector<NodeIdx> first_strategy_global_indices;
std::vector<StrategyIdx> first_force_select_indices;
std::vector<std::pair<NodeIdx, StrategyIdx>> force_selects;
ChainedRestruct(const Problem &problem, const EdgeManager &edges,
const std::vector<Interval> &interval)
: problem(problem), edges(edges), interval(interval) {
modify_queue =
new std::tuple<NodeIdx, StrategyIdx, TotalCost>[problem.nodes.size()];
modify_flag = new bool[problem.nodes.size()]();
saved_modify_queue =
new std::tuple<NodeIdx, StrategyIdx>[problem.nodes.size()];
random_seq = new NodeIdx[problem.nodes.size()];
for (NodeIdx i = 0; i < problem.nodes.size(); ++i) {
random_seq[i] = i;
}
std::shuffle(random_seq, random_seq + problem.nodes.size(), gen);
random_flag = new bool[problem.nodes.size()]();
for (NodeIdx i = 0; i < problem.nodes.size(); ++i) {
first_strategy_global_indices.emplace_back(
first_force_select_indices.size());
auto &node = problem.nodes[i];
for (StrategyIdx k = 0; k < node.strategies.size(); ++k) {
first_force_select_indices.emplace_back(force_selects.size());
for (EdgeIdx j = 0; j < get_n_edges(i); ++j) {
auto &edge = problem.edges[get_edge_idx(i, j)];
auto pair_node_idx =
i == edge.nodes[0] ? edge.nodes[1] : edge.nodes[0];
auto &pair_node = problem.nodes[pair_node_idx];
StrategyIdx cnt = 0;
StrategyIdx best_pair_strategy_idx;
for (StrategyIdx l = 0; l < pair_node.strategies.size(); ++l) {
if (get_edge_cost(edge, i, pair_node_idx, k, l) != inf) {
cnt++;
best_pair_strategy_idx = l;
}
}
if (cnt == 1) {
force_selects.emplace_back(pair_node_idx, best_pair_strategy_idx);
}
}
}
}
first_strategy_global_indices.emplace_back(
first_force_select_indices.size());
first_force_select_indices.emplace_back(force_selects.size());
}
~ChainedRestruct() {
delete[] modify_queue;
delete[] modify_flag;
delete[] saved_modify_queue;
delete[] random_seq;
delete[] random_flag;
}
inline void add_usage(UsageManager &usage_manager, NodeIdx node_idx,
Usage usage) {
if constexpr (std::is_same<UsageManager, std::vector<TotalUsage>>::value ==
true) {
for (auto i = interval[node_idx].first; i < interval[node_idx].second;
++i) {
usage_manager[i] += usage;
}
} else if constexpr (std::is_same<UsageManager, UsageTree>::value == true) {
usage_manager.update(interval[node_idx].first,
interval[node_idx].second - 1, usage);
}
}
inline EdgeIdx get_n_edges(NodeIdx node_idx) {
if constexpr (std::is_same<EdgeManager, EdgeList>::value == true) {
return edges[node_idx].size();
}
return 0;
}
inline EdgeIdx get_edge_idx(NodeIdx node_idx, EdgeIdx edge_idx) {
if constexpr (std::is_same<EdgeManager, EdgeList>::value == true) {
return std::get<1>(edges[node_idx][edge_idx]);
}
return -1;
}
inline Cost get_edge_cost(Solution &solution, const Edge &edge) {
auto ret =
edge.strategies[solution[edge.nodes[0]] *
problem.nodes[edge.nodes[1]].strategies.size() +
solution[edge.nodes[1]]]
.cost;
return ret;
}
inline Cost get_edge_cost(const Edge &edge, NodeIdx node0_idx,
NodeIdx node1_idx, StrategyIdx strategy0_idx,
StrategyIdx strategy1_idx) {
return edge.nodes[0] == node0_idx
? edge.strategies[strategy0_idx * problem.nodes[node1_idx]
.strategies.size() +
strategy1_idx]
.cost
: edge.strategies[strategy1_idx * problem.nodes[node0_idx]
.strategies.size() +
strategy0_idx]
.cost;
}
template <bool mark_modify>
inline TotalCost get_node_total_cost(Solution &solution, NodeIdx node_idx) {
auto &node = problem.nodes[node_idx];
auto strategy_idx = solution[node_idx];
TotalCost total_cost = node.strategies[strategy_idx].cost;
for (EdgeIdx i = 0; i < get_n_edges(node_idx); ++i) {
auto &edge = problem.edges[get_edge_idx(node_idx, i)];
auto cost = get_edge_cost(solution, edge);
total_cost += cost;
if constexpr (mark_modify && false) {
auto pair_node_idx =
edge.nodes[0] != node_idx ? edge.nodes[0] : edge.nodes[1];
auto &pair_node = problem.nodes[pair_node_idx];
if (cost == inf && modify_flag[pair_node_idx] == false) {
auto saved_strategy_idx = solution[pair_node_idx];
for (StrategyIdx j = 0; j < pair_node.strategies.size(); ++j) {
solution[pair_node_idx] = j;
if (get_edge_cost(solution, edge) != inf) {
modify_queue[modify_queue_tail++] = {pair_node_idx, j, -1};
modify_flag[pair_node_idx] = true;
break;
}
}
solution[pair_node_idx] = saved_strategy_idx;
}
}
}
if constexpr (mark_modify && true) {
auto strategy_global_idx =
first_strategy_global_indices[node_idx] + solution[node_idx];
for (auto i = first_force_select_indices[strategy_global_idx];
i < first_force_select_indices[strategy_global_idx + 1]; ++i) {
auto &[pair_node_idx, pair_strategy_idx] = force_selects[i];
if (modify_flag[pair_node_idx] == false &&
solution[pair_node_idx] != pair_strategy_idx) {
modify_queue[modify_queue_tail++] = {pair_node_idx, pair_strategy_idx,
-1};
modify_flag[pair_node_idx] = true;
}
}
}
return total_cost;
}
bool try_modify(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager) {
while (modify_queue_head < modify_queue_tail) {
auto &[node_idx, saved_strategy_idx, saved_total_cost] =
modify_queue[modify_queue_head++];
auto &node = problem.nodes[node_idx];
add_usage(usage_manager, node_idx,
node.strategies[saved_strategy_idx].usage -
node.strategies[solution[node_idx]].usage);
saved_total_cost = total_cost;
total_cost -= get_node_total_cost<false>(solution, node_idx);
std::swap(solution[node_idx], saved_strategy_idx);
total_cost += get_node_total_cost<true>(solution, node_idx);
}
return check_modify(usage_manager);
}
void discard_modify(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager, NodeIdx step) {
for (NodeIdx i = step; i < modify_queue_tail; ++i) {
auto &[node_idx, saved_strategy_idx, saved_total_cost] = modify_queue[i];
auto &node = problem.nodes[node_idx];
add_usage(usage_manager, node_idx,
node.strategies[saved_strategy_idx].usage -
node.strategies[solution[node_idx]].usage);
solution[node_idx] = saved_strategy_idx;
modify_flag[node_idx] = false;
}
total_cost = std::get<2>(modify_queue[step]);
modify_queue_head = step;
modify_queue_tail = step;
}
void stage_modify(Solution &solution, TotalCost &total_cost) {
for (NodeIdx i = 0; i < modify_queue_tail; ++i) {
auto &[node_idx, saved_strategy_idx, saved_total_cost] = modify_queue[i];
saved_modify_queue[i] = {node_idx, solution[node_idx]};
}
saved_modify_queue_length = modify_queue_tail;
saved_total_cost = total_cost;
}
void commit_modify(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager) {
for (NodeIdx i = 0; i < saved_modify_queue_length; ++i) {
auto &[node_idx, saved_strategy_idx] = saved_modify_queue[i];
auto &node = problem.nodes[node_idx];
add_usage(usage_manager, node_idx,
node.strategies[saved_strategy_idx].usage -
node.strategies[solution[node_idx]].usage);
solution[node_idx] = saved_strategy_idx;
}
total_cost = saved_total_cost;
}
bool change_one_node(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager, double temperature) {
for (; random_i < problem.nodes.size() && random_flag[random_seq[random_i]];
++random_i)
;
if (random_i == problem.nodes.size()) {
std::shuffle(random_seq, random_seq + problem.nodes.size(), gen);
std::memset(random_flag, 0, problem.nodes.size());
random_i = 0;
}
auto node_idx = random_seq[random_i];
auto &node = problem.nodes[node_idx];
auto best_total_cost = absl::Int128Max();
for (StrategyIdx i = 0; i < node.strategies.size(); ++i) {
modify_queue[modify_queue_tail++] = {node_idx, i, -1};
modify_flag[node_idx] = true;
if (try_modify(solution, total_cost, usage_manager) == true &&
total_cost < best_total_cost) {
best_total_cost = total_cost;
stage_modify(solution, total_cost);
}
for (NodeIdx j = 0; j < modify_queue_tail; ++j) {
random_flag[std::get<0>(modify_queue[j])] = true;
}
discard_modify(solution, total_cost, usage_manager, 0);
}
if (best_total_cost < total_cost ||
exp(double(total_cost - best_total_cost) / temperature) > dis(gen)) {
commit_modify(solution, total_cost, usage_manager);
return true;
}
return false;
}
inline void modify(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager, NodeIdx node_idx,
StrategyIdx strategy_idx) {
auto &node = problem.nodes[node_idx];
total_cost += node.strategies[strategy_idx].cost -
node.strategies[solution[node_idx]].cost;
add_usage(usage_manager, node_idx,
node.strategies[strategy_idx].usage -
node.strategies[solution[node_idx]].usage);
for (EdgeIdx i = 0; i < get_n_edges(node_idx); ++i) {
auto &edge = problem.edges[get_edge_idx(node_idx, i)];
auto pair_node_idx =
edge.nodes[0] != node_idx ? edge.nodes[0] : edge.nodes[1];
total_cost += get_edge_cost(edge, node_idx, pair_node_idx, strategy_idx,
solution[pair_node_idx]) -
get_edge_cost(edge, node_idx, pair_node_idx,
solution[node_idx], solution[pair_node_idx]);
}
}
inline void modify(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager) {
while (modify_queue_head < modify_queue_tail) {
auto &[node_idx, saved_strategy_idx, saved_total_cost] =
modify_queue[modify_queue_head++];
saved_total_cost = total_cost;
modify(solution, total_cost, usage_manager, node_idx, saved_strategy_idx);
std::swap(saved_strategy_idx, solution[node_idx]);
auto strategy_global_idx =
first_strategy_global_indices[node_idx] + solution[node_idx];
for (auto i = first_force_select_indices[strategy_global_idx];
i < first_force_select_indices[strategy_global_idx + 1]; ++i) {
auto &[pair_node_idx, pair_strategy_idx] = force_selects[i];
if (modify_flag[pair_node_idx] == false &&
solution[pair_node_idx] != pair_strategy_idx) {
modify_queue[modify_queue_tail++] = {pair_node_idx, pair_strategy_idx,
-1};
modify_flag[pair_node_idx] = true;
}
}
}
}
inline bool check_modify(UsageManager &usage_manager) {
if constexpr (std::is_same<UsageManager, std::vector<TotalUsage>>::value ==
true) {
for (NodeIdx i = 0; i < modify_queue_tail; ++i) {
auto &[node_idx, saved_strategy_idx, saved_total_cost] =
modify_queue[i];
for (auto j = interval[node_idx].first; j < interval[node_idx].second;
++j) {
if (usage_manager[j] > *problem.usage_limit) {
return false;
}
}
}
return true;
} else if constexpr (std::is_same<UsageManager, UsageTree>::value == true) {
return usage_manager.query() <= *problem.usage_limit;
}
return false;
}
inline bool change_some_nodes(Solution &solution, TotalCost &total_cost,
UsageManager &usage_manager, NodeIdx max_nodes,
double temperature) {
std::vector<NodeIdx> node_indices;
std::vector<StrategyIdx> strategy_indices;
for (; random_i < problem.nodes.size() && random_flag[random_seq[random_i]];
++random_i)
;
if (random_i == problem.nodes.size()) {
std::shuffle(random_seq, random_seq + problem.nodes.size(), gen);
std::memset(random_flag, 0, problem.nodes.size());
random_i = 0;
}
node_indices.emplace_back(random_seq[random_i]);
strategy_indices.emplace_back(-1);
if (max_nodes == 2) {
for (EdgeIdx i = 0; i < get_n_edges(node_indices[0]); ++i) {
auto &edge = problem.edges[get_edge_idx(node_indices[0], i)];
auto pair_node_idx =
edge.nodes[0] != node_indices[0] ? edge.nodes[0] : edge.nodes[1];
auto &pair_node = problem.nodes[pair_node_idx];
auto flag = false;
for (StrategyIdx j = 0; j < pair_node.strategies.size(); ++j) {
if (get_edge_cost(edge, node_indices[0], pair_node_idx, 0, j) ==
inf) {
flag = true;
break;
}
}
if (flag == false) {
node_indices.emplace_back(pair_node_idx);
strategy_indices.emplace_back(-1);
strategy_indices[0] = 0;
break;
}
}
}
auto best_total_cost = absl::Int128Max();
auto original_total_cost = total_cost;
while (true) {
NodeIdx i = node_indices.size() - 1;
for (; i >= 0; --i) {
if (strategy_indices[i] + 1 ==
problem.nodes[node_indices[i]].strategies.size()) {
strategy_indices[i] = 0;
} else {
++strategy_indices[i];
break;
}
}
if (i < 0) {
break;
}
for (NodeIdx j = 0; j < node_indices.size(); ++j) {
if (modify_flag[node_indices[j]] == false) {
modify_queue[modify_queue_tail++] = {node_indices[j],
strategy_indices[j], -1};
modify_flag[node_indices[j]] = true;
modify(solution, total_cost, usage_manager);
}
}
if (check_modify(usage_manager) == true && total_cost < best_total_cost &&
total_cost != original_total_cost) {
best_total_cost = total_cost;
stage_modify(solution, total_cost);
}
for (NodeIdx j = 0; j < modify_queue_tail; ++j) { // TODO 优化?
random_flag[std::get<0>(modify_queue[j])] = true;
}
discard_modify(solution, total_cost, usage_manager, 0);
}
if (best_total_cost < total_cost ||
exp(double(total_cost - best_total_cost) / temperature * 100) >
dis(gen)) {
commit_modify(solution, total_cost, usage_manager);
return true;
}
return false;
}
};
absl::StatusOr<Solution> Solver::Solve(const Problem &problem,
absl::Duration timeout) {
const absl::Time start_time = absl::Now();
std::optional<TotalCost> best_cost;
std::optional<Solution> best_solution;
// =================== Initialization ===========================
// Find an inital solution
Solution cur_sol_global;
for (const auto &node : problem.nodes) {
Strategy best;
int idx = -1;
for (int i = 0; i < (int)node.strategies.size(); i++) {
const auto &strategy = node.strategies[i];
if ((idx == -1) ||
(strategy.usage < best.usage ||
(strategy.usage == best.usage && strategy.cost < best.cost))) {
idx = i;
best = strategy;
}
}
cur_sol_global.push_back(idx);
}
auto tmp_cost = Evaluate(problem, cur_sol_global);
TotalCost cur_cost_global;
if (!tmp_cost.ok()) {
std::cout << "No Initial Solution!" << std::endl;
return absl::NotFoundError("No solution found");
} else {
std::cout << "# Found initial solution ["
<< absl::StrJoin(cur_sol_global, ", ") << "] with cost "
<< *tmp_cost << std::endl;
std::cerr << *tmp_cost << std::endl;
best_cost = cur_cost_global = *tmp_cost;
best_solution = cur_sol_global;
}
// Generate the Usage Table
std::vector<Interval> new_interval;
NodeIdx n = problem.nodes.size();
std::vector<TotalUsage> cur_usage_global;
{
std::vector<TimeIdx> times;
for (const auto &node : problem.nodes) {
times.push_back(node.interval.first);
times.push_back(node.interval.second);
}
std::sort(times.begin(), times.end());
times.erase(std::unique(times.begin(), times.end()), times.end());
for (const auto &node : problem.nodes) {
auto [begin, end] = node.interval;
begin =
std::lower_bound(times.begin(), times.end(), begin) - times.begin();
end = std::lower_bound(times.begin(), times.end(), end) - times.begin();
new_interval.emplace_back(begin, end);
}
cur_usage_global.resize(times.size());
}
{
for (NodeIdx i = 0; i < n; i++) {
auto sel = cur_sol_global[i];
auto usage = problem.nodes[i].strategies[sel].usage;
auto [begin, end] = new_interval[i];
for (auto j = begin; j < end; j++) {
cur_usage_global[j] += usage;
}
}
}
// Helper function to get the edge cost
auto get_edge_cost =
[&problem = std::as_const(problem)](EdgeIdx eid, StrategyIdx u,
StrategyIdx v, bool reverse = false) {
const auto &edge = problem.edges[eid];
StrategyIdx strategy_idx = 0;
if (reverse)
std::swap(u, v);
std::vector<StrategyIdx> p = {u, v};
auto idx = p.begin();
for (const NodeIdx node_idx : edge.nodes) {
strategy_idx *= problem.nodes[node_idx].strategies.size();
strategy_idx += *(idx++);
}
return edge.strategies[strategy_idx].cost;
};
// Adj. List of the edges
std::vector<std::vector<std::tuple<NodeIdx, EdgeIdx, bool>>> edges(n);
for (EdgeIdx i = 0; i < problem.edges.size(); i++) {
const auto &edge = problem.edges[i];
if (edge.nodes.size() != 2) {
std::cerr << "Invalid Input (not an edge)" << std::endl;
return absl::InvalidArgumentError("# of node in an edge != 2");
}
auto u = edge.nodes[0];
auto v = edge.nodes[1];
if (u == v) {
std::cerr << "Invalid Input (u == v)" << std::endl;
return absl::InvalidArgumentError("u == v");
}
edges[u].emplace_back(v, i, 0);
edges[v].emplace_back(u, i, 1);
}
for (NodeIdx i = 0; i < n; i++) {
std::sort(edges[i].begin(), edges[i].end());
}
std::vector<std::pair<Cost, EdgeIdx>> edge_order_global;
EdgeIdx last_edge = 0;
{
for (EdgeIdx i = 0; i < problem.edges.size(); i++) {
auto u = problem.edges[i].nodes[0];
auto v = problem.edges[i].nodes[1];
auto c = get_edge_cost(i, cur_sol_global[u], cur_sol_global[v]);
edge_order_global.emplace_back(c, i);
}
std::sort(edge_order_global.begin(), edge_order_global.end(),
[](auto x, auto y) { return x.first > y.first; });
}
std::cerr << n << std::endl;
std::cerr << absl::Now() - start_time << std::endl;
Usage max_usage = problem.usage_limit ? *problem.usage_limit : INT64_MAX;
std::cerr << "!!!!" << std::endl;
std::cerr << absl::Now() - start_time << std::endl;
std::mutex result_mutex;
std::set<std::pair<NodeIdx, NodeIdx>> edge_set;
for (const auto &e : problem.edges) {
edge_set.emplace(e.nodes[0], e.nodes[1]);
edge_set.emplace(e.nodes[1], e.nodes[1]);
}
const double c_inc_ratio_bfs = 2;
const int c_max_ratio_bfs = 1e7;
const int c_min_iter_bfs = 2;
const int c_start_iter_bfs = 10;
auto init_sol = cur_sol_global;
auto init_cost = cur_cost_global;
auto init_usage = cur_usage_global;
// Get initial solution for reruns.
auto gen_init_sol = [&problem = std::as_const(problem)](unsigned int &seed) {
Solution sol;
for (const auto &node : problem.nodes) {
Strategy best;
std::vector<NodeIdx> idx;
for (int i = 0; i < (int)node.strategies.size(); i++) {
const auto &strategy = node.strategies[i];
if (idx.empty() || (strategy.usage < best.usage)) {
idx.clear();
idx.push_back(i);
best = strategy;
} else if (strategy.usage == best.usage) {
idx.push_back(i);
}
}
sol.push_back(idx[rand_r(&seed) % idx.size()]);
}
auto tmp_cost = Evaluate(problem, sol);
return std::make_pair(sol, *tmp_cost);
};
std::cerr << cur_usage_global.size() << std::endl;
std::cerr << edge_order_global.size() << std::endl;
// Entrance Function for each thread.
auto run = [=, &problem = std::as_const(problem), &result_mutex, &best_cost,
&best_solution](int threadid) {
int64_t iters = 0;
auto last = absl::Now();
auto edge_order = edge_order_global;
EdgeIdx last_edge = 0;
unsigned seed = 2025 + threadid;
auto cur_sol = cur_sol_global;
auto cur_cost = cur_cost_global;
auto best_sol_local = cur_sol;
auto best_cost_local = cur_cost;
UsageTree cur_usage_tree(cur_usage_global);
std::random_device rd;
std::mt19937 g(rd());
ChainedRestruct<UsageTree, EdgeList> chain_restruct(problem, edges,
new_interval);
const int c_use_ratio_bfs = 5000;
double f_init_temp = 50000;
double f_degrade_fact = 1e-5;
int f_use_ratio_bfs = c_use_ratio_bfs;
int f_stop_iter_bfs = c_start_iter_bfs;
double temperature = f_init_temp;
auto recompute_usage = [&]() {
cur_usage_tree = UsageTree(std::vector<TotalUsage>(n, 0));
for (NodeIdx i = 0; i < n; i++) {
auto sel = cur_sol[i];
auto usage = problem.nodes[i].strategies[sel].usage;
auto [begin, end] = new_interval[i];
cur_usage_tree.update(begin, end - 1, usage);
}
};
int64_t acc_iters = 0, acc_upds = 0;
int64_t last_upds = 0, last_iters = 0;
const int c_check_freq = 2;
const int c_stop_thres = 2;
auto last_check = absl::Now();
auto last_cost = best_cost_local;
std::cerr << threadid << "Ready" << std::endl;
// ================== Main iteration Starts ==========================
while (absl::Now() - start_time < timeout) {
iters++;
acc_iters++;
const int X = 2;
// ======================= Periodic checks & logging ===============
if (absl::Now() - last > absl::Seconds(X)) {
if (threadid == 0) {
std::cerr << "[" << absl::Now() - start_time << "]" << std::endl;
std::cerr << "Rate " << 1.0 * iters / X << "iters / s" << std::endl;
iters = 0;
last = absl::Now();
TotalUsage musage = cur_usage_tree.query();
std::cerr << "Usage: " << musage << " / " << max_usage << std::endl;
std::cerr << "Temperature: " << temperature << std::endl;
TotalCost x = 0;
edge_order.clear();
for (int i = 0; i < n; i++) {
x += problem.nodes[i].strategies[cur_sol[i]].cost;
}
std::cerr << "Node Cost: " << x << std::endl;
x = 0;
for (int i = 0; i < problem.edges.size(); i++) {
auto u = problem.edges[i].nodes[0];
auto v = problem.edges[i].nodes[1];
auto c = get_edge_cost(i, cur_sol[u], cur_sol[v]);
x += c;
edge_order.emplace_back(c, i);
}
std::sort(edge_order.begin(), edge_order.end(),
[](auto x, auto y) { return x.first > y.first; });
last_edge = 0;
std::cerr << "Edge Cost: " << x << std::endl;
} else {
edge_order.clear();
for (int i = 0; i < problem.edges.size(); i++) {
auto u = problem.edges[i].nodes[0];
auto v = problem.edges[i].nodes[1];
auto c = get_edge_cost(i, cur_sol[u], cur_sol[v]);
edge_order.emplace_back(c, i);
}
std::sort(edge_order.begin(), edge_order.end(),
[](auto x, auto y) { return x.first > y.first; });
last_edge = 0;
}
}
// =================== Checking Reruns & update globals ============
if (absl::Now() - last_check > absl::Seconds(c_check_freq) &&
(absl::Now() - start_time < timeout - absl::Seconds(c_stop_thres))) {
if ((acc_upds - last_upds) == 0 ||
((last_cost - best_cost_local) < best_cost_local / 1000 &&
(acc_upds - last_upds) < 10)) {
{
std::lock_guard<std::mutex> g(result_mutex);
if (best_cost_local < *best_cost) {
best_cost = best_cost_local;
best_solution = best_sol_local;
}
}
std::cerr << threadid << " Restart " << best_cost_local << "!"
<< std::endl;
std::tie(best_sol_local, best_cost_local) =
std::tie(cur_sol, cur_cost) = gen_init_sol(seed);
temperature = f_init_temp;
recompute_usage();
last_check = absl::Now();
} else {
last_check = absl::Now();
}
last_upds = acc_upds;
last_iters = acc_iters;
last_cost = best_cost_local;
}
const Solution &solution = cur_sol;
// =================== Technique #2: Greedy Restructuring =======
if (rand_r(&seed) % f_use_ratio_bfs == 0) {
std::vector<std::pair<TotalCost, NodeIdx>> total_costs;
for (int i = 0; i < n; i++) {
TotalCost c = 0;
for (auto [v, e, rev] : edges[i]) {
c += get_edge_cost(e, cur_sol[i], cur_sol[v], rev);
}
total_costs.emplace_back(c, i);
}
std::sort(total_costs.begin(), total_costs.end(),
[](auto x, auto y) { return x.first > y.first; });
bool found = false;
int i = 0;
int upds = 0;
int fails = 0;
for (auto [cost, node_u] : total_costs) {
i++;
if (i > 1 && cost < best_cost_local / 1000)
break;
auto alters = edges[node_u];
std::shuffle(alters.begin(), alters.end(), g);
bool stop = false;
for (StrategyIdx i = 0; i < problem.nodes[node_u].strategies.size();
i++) {
auto neo_ctx = ExtendedProblem(problem, edges, new_interval,
cur_sol, cur_usage_tree, cur_cost);
neo_ctx.update(node_u, i);
std::queue<NodeIdx> q;
std::set<NodeIdx> visited;
q.push(node_u);
visited.insert(node_u);
auto iters = 0;
auto last = cur_cost;
while (!q.empty()) {
auto cur = q.front();
q.pop();
auto s = neo_ctx.get_strategy(cur);
for (auto [v, e, rev] : edges[cur]) {
if (visited.count(v))
continue;
StrategyIdx x = neo_ctx.get_strategy(v);
for (StrategyIdx j = 0; j < problem.nodes[v].strategies.size();
j++) {
if (std::make_pair(get_edge_cost(e, s, j, rev),
problem.nodes[v].strategies[j].usage) <
std::make_pair(get_edge_cost(e, s, x, rev),
problem.nodes[v].strategies[x].usage)) {
x = j;
}
}
if (x != neo_ctx.get_strategy(v)) {
neo_ctx.update(v, x);
q.push(v);
visited.insert(v);
}
}
auto result = neo_ctx.get_cost();
if (result.has_value() && *result < best_cost_local) {