-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeExplorer.cpp
More file actions
986 lines (854 loc) · 36.4 KB
/
NodeExplorer.cpp
File metadata and controls
986 lines (854 loc) · 36.4 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
//
// Created by nandgate on 10/27/2024.
//
#include "NodeExplorer.h"
#include <complex>
#include "DD.h"
OutObject NodeExplorer::process(const Node_t node, double optimalLB) {
/*
node should be eligible for processing.
*/
double lowerBound = node.lb;
double upperBound = node.ub;
STEP_1: // refine relaxed tree with global feasibility cuts.
{
DDNode root1 {0, node.globalLayer, node.states, node.solutionVector};
DD relaxedDD1{networkPtr,EXACT};
auto _ = relaxedDD1.build(root1);
// refine tree with feasibility cuts
#ifdef DEBUG
cout << "Applying Feasibility cuts heuristically on the relaxed tree." << endl;
#endif
for (auto start = feasibilityCuts.cuts.rbegin(); start != feasibilityCuts.cuts.rend(); ++start) {
const auto cut = *start;
// for (const auto& cut: feasibilityCuts.cuts) {
// cout << "-----------------------------------------------------------------------Zart" << endl;
// if any of the cuts make the tree infeasible? get another node to explore.
if (!relaxedDD1.applyFeasibilityCutHeuristic(cut)) {
#ifdef DEBUG
cout << "Feasibility cut made tree infeasible" << endl;
#endif
return {std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), {}, false}; // get next node
}
}
// apply optimality cuts to the relaxed tree.
DD relaxedDD2{networkPtr, EXACT};
DDNode root2 {0, node.globalLayer, node.states, node.solutionVector};
relaxedDD2.build(root2);
for(auto start = optimalityCuts.cuts.rbegin(); start != optimalityCuts.cuts.rend(); ++start) {
upperBound = relaxedDD1.applyOptimalityCutHeuristic(*start);
if (upperBound < optimalLB) {
// break
#ifdef DEBUG
cout << "upper bound < global lower bound." << endl;
#endif
return {std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), {}, false};
}
}
}
#ifdef DEBUG
cout << "STEP_1 completed" << endl;
#endif
STEP_2:
DDNode root2 {0, node.globalLayer, node.states, node.solutionVector};
// build restricted DD
DD restrictedDD{networkPtr,RESTRICTED};
auto cutset = restrictedDD.build(root2);
// goto STEP_2C;
// if(!cutset) goto STEP_2C;
// cout << "STEP_2 completed" << endl;
STEP_2A:
// apply feasibility cuts on restricted DD.
{
#ifdef DEBUG
cout << "Applying feasibility cuts on the restricted tree in reverse order" << endl;
#endif
// auto st = feasibilityCuts.cuts.rbegin();
// auto end = (feasibilityCuts.cuts.size() > 100) ? feasibilityCuts.cuts.rbegin() + 100 : feasibilityCuts.cuts.rend();
auto end = feasibilityCuts.cuts.rend();
for (auto start = feasibilityCuts.cuts.rbegin(); start != end; ++start){
const auto& cut = *start;
// for (const auto& cut: feasibilityCuts.cuts) {
if(!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
// tree is infeasible. a layer is removed. get cutset and set lb, ub to root's
// auto cs = cutset.value();
// for (auto& exactNode : cutset.value()) {
// exactNode.lb = node.lb;
// exactNode.ub = node.ub;
// }
#ifdef DEBUG
cout << "State < 0 after applying feasiblity cut on restricted DD" << endl;
#endif
// return {node.lb, node.ub, cutset.value(), true};
goto FINAL;
}
}
}
if (!cutset) goto STEP_2C; // if tree is exact, do not apply opt cuts on restricted tree.
// cout << "STEP_2A completed" << endl;
STEP_2B:
{
#ifdef DEBUG
cout << "Applying optimality cuts on restricted tree in reverse order." << endl;
#endif
//applying optimality cuts in reverse order.
// auto end = (optimalityCuts.cuts.size() > 100) ? optimalityCuts.cuts.rbegin()+100 : optimalityCuts.cuts.rend();
auto end = optimalityCuts.cuts.rend();
for (auto start = optimalityCuts.cuts.rbegin(); start != end; ++start) {
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(*start);
}
}
#ifdef DEBUG
cout << "Lower bound after applying all optimality cuts: " << lowerBound<< endl;
#endif
// // refinement
STEP_2C:
{
vector<int> previousSolution;
#ifdef DEBUG
cout << "Starting actual refinement." << endl;
#endif
while (true) {
// create relaxed tree and add refine with feasibility cuts
//if (iter++ > 5) break;
auto solution = restrictedDD.solution();
#ifdef DEBUG
cout << "Solution: "; for (auto s: solution) cout << s << " "; cout << endl;
#endif
if(previousSolution == solution) {
#ifdef DEBUG
cout << "Previous solution returned" << endl;
#endif
break;
}
previousSolution = solution;
GuroSolver solver {networkPtr,env};
auto y_bar = w2y(solution, networkPtr);
auto cut = solver.solveSubProblemInstance(y_bar,0);
#ifdef DEBUG
cout << "Cut: " << cut.cutType << " RHS: " << cut.RHS<< endl;
for (const auto&[k,v] : cut.cutCoeff) {
auto [i,q,j] = k;
// cout << i << ", " << q << ", " << j << " : " << v << endl;
}
#endif
// auto cut = solver.solveSubProblem(y_bar);
if (cut.cutType == FEASIBILITY) {
// check if cut exists
// if(feasibilityCuts.isCutExists(cut)) {
// break; // does it happen?
// }
feasibilityCuts.insertCut(cut);
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
// tree is removed, return the cutset and lower bound and upper bound.
lowerBound = node.lb;
goto FINAL;
// goto FINAL;
// if (cutset) goto FINAL;
// //return {lowerBound, node.ub, cutset.value(), true};
// return {lowerBound, node.lb, {}, true};
}
}
else {
// if (optimalityCuts.isCutExists(cut)) {
// // cout << "Optimality cut exists" << endl;
// break; // set lower bound
// }
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(cut);
// LATER if lower bound is < global lower bound, break it
optimalityCuts.insertCut(cut);
}
}
}
#ifdef DEBUG
cout << "Step 2 completed" << endl;
#endif
STEP_3:
{
DDNode root3 {0, node.globalLayer, node.states, node.solutionVector};
DD relaxedDD2{networkPtr, EXACT};
relaxedDD2.build(root3);
//double upperBound = node.ub;
#ifdef DEBUG
cout << "Applying optimality cuts on the relaxed tree heuristically." << endl;
#endif
for (auto start = optimalityCuts.cuts.rbegin(); start != optimalityCuts.cuts.rend(); ++start) {
upperBound = relaxedDD2.applyOptimalityCutHeuristic(*start);
}
}
#ifdef DEBUG
cout << "lower bound : " << lowerBound << " , upper bound: " << upperBound << endl;
#endif
// for (const auto& cut : optimalityCuts.cuts) {
// upperBound = relaxedDD2.applyOptimalityCutHeuristic(network, cut);
// // LATER if the upper bound is < global lower bound, then break it.
// }
// cout << "-----------------------------------Upper1 Bound so far: " << upperBound<< endl;
FINAL:
// optimalityCuts.clearContainer();
//upperBound = node.ub;
// cout << "One instance of node explorer finished" << endl;
if (cutset) {
// state is upper bound.
for (auto& cutNode : cutset.value()) {
cutNode.ub = upperBound;
cutNode.lb = lowerBound;
}
return {lowerBound, upperBound, cutset.value(), true};
}
return {lowerBound,upperBound, {}, true};
}
OutObject NodeExplorer::process2(const Node_t node, const double optimalLB) {
// relaxed Tree 1,
double lowerBound = node.lb;
double upperBound = node.ub;
STEP_1:
DDNode root1{0, node.globalLayer, node.states, node.solutionVector};
DD relaxedDD1{networkPtr, EXACT};
relaxedDD1.build(root1);
// apply cuts heuristically.
auto start = feasibilityCuts.cuts.rbegin();
auto end = feasibilityCuts.cuts.rend();
for (; start != end; ++start) {
if (!relaxedDD1.applyFeasibilityCutHeuristic(*start)) {
#ifdef DEBUG
cout << "tree became infeasible" << endl;
#endif
return {0, 0, {}, false};
}
}
#ifdef DEBUG
cout << "Step 1 completed" << endl;
#endif
DD restrictedDD{networkPtr, RESTRICTED};
DDNode root2{0, node.globalLayer, node.states, node.solutionVector};
auto cutset = restrictedDD.build(root2);
if (cutset) {
// build relaxed DD 2
DDNode root3{0, node.globalLayer, node.states, node.solutionVector};
DD relaxedDD2{networkPtr, EXACT};
relaxedDD2.build(root3);
// apply optimality cuts heuristically
auto start = optimalityCuts.cuts.rbegin();
auto end = optimalityCuts.cuts.rend();
for (; start != end; ++start) {
upperBound = relaxedDD2.applyOptimalityCutHeuristic(*start);
if( upperBound < optimalLB) {
#ifdef DEBUG
cout << "upperbound : " << upperBound << " , < optimal LB: " << optimalLB << endl;
#endif
return {std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), {}, false};
}
}
}
STEP_2:
{
// apply cuts on restricted tree.
auto start = feasibilityCuts.cuts.rbegin();
auto end = feasibilityCuts.cuts.rend();
for (; start != end; ++start) {
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(*start)) {
lowerBound = node.lb;
// upperBound = upperBound;
goto FIN;
}
}
auto start2 = optimalityCuts.cuts.rbegin();
auto end2 = optimalityCuts.cuts.rend();
for (; start2 != end2; ++start2) {
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(*start2);
if (lowerBound <= optimalLB) goto FIN;
}
}
// cout << "STEP 2 completed" << endl;
STEP_3: // actual refinement
{
vi previousSolution;
while (true) {
vi solution = restrictedDD.solution();
if (previousSolution == solution) {
#ifdef DEBUG
cout << "previous solution returned" << endl;
#endif
break;
}
previousSolution = solution;
auto y_bar = w2y(solution, networkPtr);
GuroSolver solver{networkPtr, env};
auto cut = solver.solveSubProblemInstance(y_bar, 0);
if (cut.cutType == FEASIBILITY) {
feasibilityCuts.insertCut(cut);
if (!relaxedDD1.applyFeasibilityCutHeuristic(cut)){return {0,0,{}, false};}
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
// tree became infeasible.
lowerBound = node.lb;
// cout << "Feasibility cut made tree infeasible." << endl;
// upperBound = no;
goto FIN;
}
}
else {
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(cut);
optimalityCuts.insertCut(cut);
if (lowerBound <= optimalLB) {
//
lowerBound = node.lb;
break;
}
}
}
}
FIN:
{
// if cutset is not empty, update the lower boudn
if (cutset) {
for (auto& e : cutset.value()) {
e.ub = upperBound;
e.lb = lowerBound;
}
return {lowerBound, upperBound, cutset.value(), true};
}
return {lowerBound, upperBound, {}, true};
}
// apply cuts heuristically
}
OutObject NodeExplorer::process3(const Node_t node, const double optimalLB) {
double lowerBound = node.lb;
double upperBound = node.ub;
DDNode root1{0, node.globalLayer, node.states, node.solutionVector};
DD restrictedDD {networkPtr, RESTRICTED};
auto cutset = restrictedDD.build(root1);
DD relaxedDD{networkPtr, EXACT};
if (cutset) {
DDNode root2{0, node.globalLayer, node.states, node.solutionVector};
relaxedDD.build(root2);
// apply cuts on the relaxed DD. if any cut make DD infeasible, process new node.
auto start = feasibilityCuts.cuts.rbegin();
auto end = feasibilityCuts.cuts.rend();
for (; start != end; ++start) {
if (!relaxedDD.applyFeasibilityCutHeuristic(*start)) return {0,0,{}, false};
}
auto start2 = optimalityCuts.cuts.rbegin();
auto end2 = optimalityCuts.cuts.rend();
for (; start2 != end2; ++start2) {
upperBound = relaxedDD.applyOptimalityCutHeuristic(*start2);
// this node cannot do better.
if (upperBound <= optimalLB) { return {0,0,{}, false};}
}
}
// apply cuts to restricted tree.
auto start = feasibilityCuts.cuts.rbegin();
auto end = feasibilityCuts.cuts.rend();
for (; start != end; ++start) {
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(*start)) {
if (cutset) {
for (auto& c_node: cutset.value()) {
c_node.ub = upperBound;
// c_node.ub = node.ub;
c_node.lb = node.lb;
}
return {node.lb, upperBound, cutset.value(), true};
}
return{node.lb, upperBound, {}, true};
}
}
auto start2 = optimalityCuts.cuts.rbegin();
auto end2 = optimalityCuts.cuts.rend();
for (; start2 != end2; ++start2) {
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(*start2);
}
/// actual refinement ///
vi previousSolution;
while (true) {
vi solution = restrictedDD.solution();
if (previousSolution == solution) {break;}
previousSolution = solution;
auto y_bar = w2y(solution , networkPtr);
GuroSolver solver{networkPtr, env};
auto cut = solver.solveSubProblemInstance(y_bar, 0);
if (cut.cutType == FEASIBILITY) {
feasibilityCuts.insertCut(cut);
if (cutset){if (!relaxedDD.applyFeasibilityCutHeuristic(cut)) return {0,0,{}, false};}
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
// copy lower bound from parent, upper bound achieved so far.
if (cutset) {
for (auto& c_node: cutset.value()) {
c_node.ub = upperBound;
c_node.lb = node.lb;
}
return {node.lb, upperBound, cutset.value(), true};
}
return {node.lb,upperBound,{}, true};
}
}
else {
optimalityCuts.insertCut(cut);
if (cutset) upperBound = relaxedDD.applyOptimalityCutHeuristic(cut);
if (upperBound <= optimalLB) {
return {0,0,{}, false};
}
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(cut);
}
}
if (cutset) {
for (auto& c_node: cutset.value()) {
c_node.ub = upperBound;
c_node.lb = lowerBound;
}
return {lowerBound, upperBound, cutset.value(), true};
}
return {lowerBound, upperBound, {}, true};
}
OutObject NodeExplorer::process4(Node_t node, double optimalLB,
const pair<vector<CutContainer *>, vector<CutContainer *>>& globalCuts) {
// apply node explroer
const auto& feasibilityCutContainer = globalCuts.first;
const auto& optimalityCutContainer = globalCuts.second;
// auto& feasibilityCutContainerStart = feasibilityCutContainer.begin();
double lowerBound = node.lb;
double upperBound = node.ub;
DDNode root1{0, node.globalLayer, node.states, node.solutionVector};
DD restrictedDD{networkPtr, RESTRICTED};
auto cutset = restrictedDD.build(root1);
if (cutset) { // tree is not exact.
DD relaxedDD {networkPtr, EXACT};
DDNode root2{0, node.globalLayer, node.states, node.solutionVector};
relaxedDD.build(root2);
// apply global cuts first
for (const auto& fStart : feasibilityCutContainer) {
for (const auto& cut: fStart->cuts) {
if (!relaxedDD.applyFeasibilityCutHeuristic(cut)) return {0,0,{}, false};
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
ranges::for_each(cutset.value(), [&](auto& c_node) {
c_node.ub = upperBound;
c_node.lb = node.lb;
});
return {node.lb, upperBound, cutset.value(), true};
}
}
}
// apply optimality cuts
for (const auto& oStart : optimalityCutContainer) {
for (const auto& cut: oStart->cuts) {
upperBound = min(relaxedDD.applyOptimalityCutHeuristic(cut), upperBound);
if (upperBound <= optimalLB) return {0,0,{}, false};
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(cut);
if (lowerBound <= optimalLB) {
ranges::for_each(cutset.value(), [&](auto& c_node) {
c_node.ub = upperBound;
c_node.lb = node.lb;
});
return {node.lb, upperBound, cutset.value(), true};
}
}
}
// actual refinement
double previousLowerBound;
vi previousSolution;
while (true) {
vi solution = restrictedDD.solution();
if (previousSolution == solution) {
if (previousLowerBound == lowerBound) {
ranges::for_each(cutset.value(), [&](auto& c_node) {
c_node.ub = upperBound;
c_node.lb = lowerBound; // changed here.
});
return {lowerBound, upperBound, cutset.value(), true};
}
else previousLowerBound = lowerBound;
}
else {
previousSolution = solution;
previousLowerBound = lowerBound;
}
auto y_bar = w2y(solution , networkPtr);
GuroSolver solver{networkPtr, env};
auto cut = solver.solveSubProblemInstance(y_bar, 0);
if (cut.cutType == FEASIBILITY) {
feasibilityCuts.insertCut(cut);
if (!relaxedDD.applyFeasibilityCutHeuristic(cut)) return {0,0,{}, false};
if (!restrictedDD.applyFeasibilityCutRestrictedLatest(cut)) {
ranges::for_each(cutset.value(), [&](auto& c_node) {
c_node.ub = upperBound;
c_node.lb = node.lb;
});
return {node.lb, upperBound, cutset.value(), true};
}
}
else {
// optimality cut.
optimalityCuts.insertCut(cut);
upperBound = min(relaxedDD.applyOptimalityCutHeuristic(cut), upperBound);
if (upperBound <= optimalLB) return {0,0,{}, false};
lowerBound = restrictedDD.applyOptimalityCutRestrictedLatest(cut);
if (lowerBound <= optimalLB) {
ranges::for_each(cutset.value(), [&](auto& c_node) {
c_node.ub = upperBound;
c_node.lb = node.lb;
});
return {node.lb, upperBound, cutset.value(), true};
}
}
}
}
else {
// tree is exact.
// apply global cuts.
}
}
void NodeExplorer::clearCuts() {
feasibilityCuts.clearContainer();
optimalityCuts.clearContainer();
}
[[always_inline]]
static void updateNodeBounds(vector<Inavap::Node>& nodes, double lowerBound, double upperBound ) {
for (auto& node : nodes) {
node.lb = lowerBound;
node.ub = upperBound;
}
}
void printBothCuts(const ::Cut& oldCut, const Inavap::Cut& newCut) {
cout << "Old RHS: " << oldCut.RHS << endl;
for (const auto&[key, val]: oldCut.cutCoeff) {
auto [i,q,j] = key;
cout << "(" << i << ", " << q << ", " << j << "): " << val << endl;
}
if (oldCut.cutType == FEASIBILITY) newCut.printCut(1);
else newCut.printCut(0);
}
Inavap::OutObject Inavap::NodeExplorer::processX3(Node node, double optimalLB,
const vector<CutContainer *>& globalFCuts, const vector<CutContainer *>& globalOCuts) {
// copied from Node Explorer 5.
double lowerBound = node.lb;
double upperBound = DOUBLE_MAX;
if (optimalLB >= -2205) {
// cout << "Node.ub: " << node.ub << " solution vector: " << node.solutionVector.size()
// << " states: " << node.states.size() << endl;
}
Inavap::RestrictedDDNew restrictedDD{networkPtr, 128};
auto cutset = restrictedDD.buildTree(node);
if (!cutset){ // TODO : set status field in RestrictedDD.
// complete tree is built.
// apply local cuts first.
for (const auto& cut : feasibilityCuts) {
if (!restrictedDD.applyFeasibilityCut(cut))
return INVALID_OBJECT;
}
// apply global feasibility cuts.
for (const auto& containerPtr: globalFCuts) {
for (const auto& cut : *containerPtr) {
if (!restrictedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
}
}
for (const auto& cut: optimalityCuts) {
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB)
return INVALID_OBJECT;
}
// apply global optimality cuts.
for (const auto& containerPtr : globalOCuts) {
for (const auto& cut : *containerPtr) {
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB)
return INVALID_OBJECT;
}
}
// actual refinement.
double previousLowerBound;
Path previousSolution;
while (true) {
Path solution = restrictedDD.getSolution();
if (previousSolution == solution) {
if (previousLowerBound == lowerBound) {
return {lowerBound, upperBound, {}, true};
}
previousLowerBound = lowerBound;
}
else {
previousSolution = solution;
previousLowerBound = lowerBound;
}
// if ((previousSolution == solution) && (previousLowerBound == lowerBound)) {
// cout <<"1" << endl;
// return {lowerBound, upperBound, {}, true};
// }
// previousSolution = solution;
// previousLowerBound = lowerBound;
// convert solution to vector<int>
vector<int> intSolution; for (auto s: solution) intSolution.push_back(s);
auto y_bar = w2y(intSolution, networkPtr);
GuroSolver solver{networkPtr, env};
// auto cut = cutToCut(solver.solveSubProblemInstance(y_bar, 0));
// find out the cut type and create Inavap::Cut.
auto temp = solver.solveSubProblem(y_bar);
if (temp.cutType == FEASIBILITY) {
auto cut = cutToCut(temp, networkPtr.get());
feasibilityCuts.insertCut(cut); // do not move cut.
// printBothCuts(temp, cut);
if (!restrictedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
}
else {
auto cut = cutToCut(temp, networkPtr.get());
optimalityCuts.insertCut(cut);
// printBothCuts(temp, cut);
lowerBound = restrictedDD.applyOptimalityCut(cut);
if (lowerBound <= optimalLB)
return INVALID_OBJECT;
}
}
}
else { // delete this else statement,
// tree is not exact, can build relaxed Tree.
RelaxedDD relaxedDD {networkPtr};
relaxedDD.buildTree(node);
// apply local feasibility cuts.
for (const auto& cut : feasibilityCuts) {
if (!relaxedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
if (!restrictedDD.applyFeasibilityCut(cut)) {
// get cutset. and update its value.
updateNodeBounds(cutset.value(), node.lb, upperBound);
// cout <<"2" << endl;
return {node.lb, upperBound, cutset.value(), true};
}
}
// apply global feasibility cuts)
for (const auto& containerPtr : globalFCuts) {
for (const auto& cut : *containerPtr) {
// if (!relaxedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
if (!restrictedDD.applyFeasibilityCut(cut)) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
// cout <<"3" << endl;
return {node.lb, upperBound, cutset.value(), true};
}
}
}
// apply optimality cuts.
for (const auto& cut : optimalityCuts) {
if (upperBound = min(relaxedDD.applyOptimalityCut(cut), upperBound); upperBound <= optimalLB)
return INVALID_OBJECT;
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
// apply global optimality cuts.
for (const auto& containerPtr : globalOCuts) {
for (const auto& cut : *containerPtr) {
// if (upperBound = min(relaxedDD.applyOptimalityCut(cut), upperBound); upperBound <= optimalLB)
// return INVALID_OBJECT;
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
}
// actual refinement.
double previousLowerBound = lowerBound;
Path previousSolution;
while (true) {
Path solution = restrictedDD.getSolution();
if (previousSolution == solution) {
if (previousLowerBound == lowerBound) {
updateNodeBounds(cutset.value(), lowerBound, upperBound);
return {lowerBound, upperBound, cutset.value(), true};
}
previousLowerBound = lowerBound;
}
else {
previousSolution = solution;
previousLowerBound = lowerBound;
}
// if (previousSolution == solution && previousLowerBound == lowerBound) {
// updateNodeBounds(cutset.value(), lowerBound, upperBound);
// return {lowerBound, upperBound, cutset.value(), true};
// }
// previousSolution = solution;
// previousLowerBound = lowerBound;
vector<int> intSolution; for (auto s: solution) intSolution.push_back(s);
GuroSolver solver{networkPtr, env};
auto y_bar = w2y(intSolution, networkPtr);
auto temp = solver.solveSubProblem(y_bar);
if (temp.cutType == FEASIBILITY) {
auto cut = cutToCut(temp, networkPtr.get());
feasibilityCuts.insertCut(cut);
if (!relaxedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
if (!restrictedDD.applyFeasibilityCut(cut)) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
else {
auto cut = cutToCut(temp, networkPtr.get());
optimalityCuts.insertCut(cut);
if (upperBound = min(relaxedDD.applyOptimalityCut(cut),upperBound); upperBound <= optimalLB)
return INVALID_OBJECT;
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
}
}
// return outobj;
}
Inavap::OutObject Inavap::NodeExplorer::process2(Node node, double optimalLB) {
double lowerBound = node.lb;
double upperBound = node.ub;
RestrictedDDNew restrictedDD{networkPtr, 128};
auto cutset = restrictedDD.buildTree(node);
if (restrictedDD.isTreeExact()) {
// apply feasibility cuts
for (const auto &cut : feasibilityCuts) {
if (!restrictedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
}
// apply optimality cuts
for (const auto &cut : optimalityCuts) {
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB)
return INVALID_OBJECT;
}
// actual refinement.
double previousLowerBound = std::numeric_limits<double>::lowest();
Path previousSolution;
while (true) {
Path solution = restrictedDD.getSolution();
if (previousSolution == solution) {
if (previousLowerBound == lowerBound)
return {lowerBound, upperBound, {}, true};
previousLowerBound = lowerBound;
}
else {
previousSolution = solution;
previousLowerBound = lowerBound;
}
vector<int> intSolution; for (auto s: solution) intSolution.push_back(s);
auto y_bar = w2y(intSolution, networkPtr);
GuroSolver solver{networkPtr, env};
// auto cut = cutToCut(solver.solveSubProblemInstance(y_bar, 0));
// find out the cut type and create Inavap::Cut.
auto temp = solver.solveSubProblemInstance(y_bar, 0);
if (temp.cutType == FEASIBILITY) {
auto cut = cutToCut(temp, networkPtr.get());
feasibilityCuts.insertCut(cut);
if (!restrictedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
}
else {
auto cut = cutToCut(temp, networkPtr.get());
optimalityCuts.insertCut(cut);
if (lowerBound = restrictedDD.applyOptimalityCut(cut); lowerBound <= optimalLB)
return INVALID_OBJECT;
}
}
}
else {
RelaxedDD relaxedDD{networkPtr};
relaxedDD.buildTree(node);
// apply feasibility cuts
for (const auto &cut : feasibilityCuts) {
if (!relaxedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
if (!restrictedDD.applyFeasibilityCut(cut)) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
//apply optimality cuts.
for (const auto &cut : optimalityCuts) {
upperBound = min(relaxedDD.applyOptimalityCut(cut), upperBound);
if (upperBound <= optimalLB) return INVALID_OBJECT;
lowerBound = restrictedDD.applyOptimalityCut(cut);
}
// actual refinement.
double previousLowerBound = std::numeric_limits<double>::lowest();
Path previousSolution;
while (true) {
Path solution = restrictedDD.getSolution();
if (previousSolution == solution) {
if (previousLowerBound == lowerBound) {
updateNodeBounds(cutset.value(), lowerBound, upperBound);
return {lowerBound, upperBound, cutset.value(), true};
}
previousLowerBound = lowerBound;
}
else {
previousSolution = solution;
previousLowerBound = lowerBound;
}
vector<int> intSolution; for (auto s: solution) intSolution.push_back(s);
auto y_bar = w2y(intSolution, networkPtr);
GuroSolver solver{networkPtr, env};
// auto cut = cutToCut(solver.solveSubProblemInstance(y_bar, 0));
// find out the cut type and create Inavap::Cut.
auto temp = solver.solveSubProblemInstance(y_bar, 0);
if (temp.cutType == FEASIBILITY) {
auto cut = cutToCut(temp, networkPtr.get());
feasibilityCuts.insertCut(cut);
if (!relaxedDD.applyFeasibilityCut(cut)) return INVALID_OBJECT;
if (!restrictedDD.applyFeasibilityCut(cut)) {
updateNodeBounds(cutset.value(), node.lb, upperBound);
return {node.lb, upperBound, cutset.value(), true};
}
}
else {
auto cut = cutToCut(temp, networkPtr.get());
optimalityCuts.insertCut(cut);
upperBound = min(relaxedDD.applyOptimalityCut(cut), upperBound);
if (upperBound <= optimalLB) return INVALID_OBJECT;
lowerBound = restrictedDD.applyOptimalityCut(cut);
}
}
}
}
Inavap::OutObject Inavap::NodeExplorer::process(Node node, double optimalLB,
Container &globalFeasCuts, Container &globalOptCuts) {
double upperBound = node.ub;
/* this relaxed DD is reused across multiple invocations of this function. tree is automatically
* reset to default, before compiling the new tree */
relaxedDD.buildTree(node);
/* The cut refinement occurs in the following manner: apply local feasibility cuts, global
* feasibility cuts, local optimality cuts and global optimality cuts. The container cuts
* are iterated in reverse order of the container. If any of the cuts prunes the entire tree,
* the node explorer gets a new node to explore.
*/
const auto *current_f_cut = globalFeasCuts.get();
const auto *current_opt_cut = globalOptCuts.get();
if (relaxedDD.isTreeExact()) {
for ( ; current_f_cut; current_f_cut = current_f_cut->next) {
if (!relaxedDD.applyFeasibilityCut(current_f_cut->cut))
return PRUNED_BY_FEASIBILITY_CUT;
}
for ( ; current_opt_cut; current_opt_cut = current_opt_cut->next) {
upperBound = relaxedDD.applyOptimalityCut(current_opt_cut->cut, optimalLB, upperBound);
if (upperBound <= optimalLB)
return PRUNED_BY_OPTIMALITY_CUT;
}
/* New cuts are generated only when the tree is exact. */
vector<Path> allSolutions;
while (true) {
auto path = relaxedDD.getSolution();
if (std::find(allSolutions.begin(), allSolutions.end(), path) != allSolutions.end()) {
return {upperBound, upperBound, {}, OutObj::STATUS_OP::SUCCESS};
}
allSolutions.push_back(path);
auto [cutType, cut] = solver.solveSubProblem(path);
cut_node_t *new_cut = new cut_node_t{cut};
if (cutType == FEASIBILITY) {
globalFeasCuts.add(new_cut);
if (!relaxedDD.applyFeasibilityCut(cut))
return PRUNED_BY_FEASIBILITY_CUT;
}
else {
globalOptCuts.add(new_cut);
upperBound = relaxedDD.applyOptimalityCut(cut, optimalLB, upperBound);
if (upperBound <= optimalLB)
return PRUNED_BY_OPTIMALITY_CUT;
}
}
}
/* Non-Exact Tree */
for ( ; current_f_cut; current_f_cut = current_f_cut->next) {
if (!relaxedDD.applyFeasibilityCut(current_f_cut->cut))
return PRUNED_BY_FEASIBILITY_CUT;
}
for ( ; current_opt_cut; current_opt_cut = current_opt_cut->next) {
upperBound = min(relaxedDD.applyOptimalityCut(current_opt_cut->cut,optimalLB, upperBound), upperBound);
if (upperBound <= optimalLB) return PRUNED_BY_OPTIMALITY_CUT;
}
return {DOUBLE_MIN, upperBound, relaxedDD.getCutset(upperBound), OutObj::STATUS_OP::SUCCESS};
}