/
RelaxIV.C
executable file
·4471 lines (3599 loc) · 109 KB
/
RelaxIV.C
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
/*--------------------------------------------------------------------------*/
/*------------------------- File RelaxIV.C ---------------------------------*/
/*--------------------------------------------------------------------------*/
/* Linear Min Cost Flow problems solver, based on the RELAXIV code by
* D. Bertsekas and P. Tseng, as described in
*
* Bertsekas, Dimitri P., and Paul Tseng.
* "RELAX-IV: A faster version of the RELAX code for solving minimum
* cost flow problems." (1994), Report LIDS-P-2276, MIT.
*
* Conforms to the standard (MCF) interface defined in MCFClass.h.
*
* \version 1.83
*
* \date 14 - 04 - 2019
*
* \author <b>(original FORTRAN code)</b> \n
* Dimitri P. Bertsekas \n
* Lab. for Information and Decision Systems \n
* Massachusetts Institute of Technology \n
*
* \author <b>(original FORTRAN code)</b> \n
* Paul Tseng \n
* Department of Mathematics \n
* University of Washington \m
*
* \author <b>(C++ porting and polishing)</b> \n
* Antonio Frangioni \n
* Operations Research Group \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author <b>(C++ porting and polishing)</b> \n
* Claudio Gentile \n
* Istituto di Analisi di Sistemi e Informatica \n
* Consiglio Nazionale delle Ricerche \n
*
* Copyright © 1996 - 2019 by Antonio Frangioni
*/
/*--------------------------------------------------------------------------*/
/*--------------------------- IMPLEMENTATION -------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "RelaxIV.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
#if( OPT_USE_NAMESPACES )
using namespace MCFClass_di_unipi_it;
#endif
/*--------------------------------------------------------------------------*/
/*----------------------------- CONSTANTS ----------------------------------*/
/*--------------------------------------------------------------------------*/
static MCFClass::cIndex npasslim = 2;
/* Number of single-node iterations that are attempted on all nodes before
the first multinode iteration is allowed. */
static MCFClass::cIndex tp = 10;
static MCFClass::cIndex ts_den = 15;
/* An adaptive strategy is used to decide whether to continue the scanning
process after a multinode price change. The thresold parameter tp tells
what is a "small number of nonzero deficit nodes". The thresold parameter
ts, defined as ts = n / ts_den, is used to decide whether or not to
continue scanning even after a price change. */
static int it_aug = 8;
/* If the number of iteration is greater of it_aug * the number of AugFlow()
calls (and/or if other conditions are verified), then another multinode
iteration is performed. */
static MCFClass::cIndex maxdns = 10;
/* In the standard initialization, the number of passes is controlled by the
density of the graph: if m / n > maxdns then 2 passes are done, otherwise
3 passes are done. */
#if( AUCTION )
static const int factor = 3;
static const int npassauct = 1;
static const int maxdf = 8;
/* Auction parameters:
- factor determines by how much eps is reduced at each minimization;
- npassauct determines how many auction scaling iterations are
performed, that is how many times eps is divided by factor;
- maxdf is used to set the initial value of eps in auction function:
eps = max( 1 , ( maxcost - mincost ) / maxdf ), where maxcost and
mincost are, respectively, the maximum and the mininum reduced cost
at the beginning of auction function. */
static cCNumber C_LARGE = Inf<CNumber>() / 4;
#endif
/*--------------------------------------------------------------------------*/
/*-------------------------- "PRIVATE" MACROS ------------------------------*/
/*--------------------------------------------------------------------------*/
#define P_ALLOC ( AUCTION || ( DYNMC_MCF_RIV > 1 ) )
#if( RELAXIV_STATISTICS )
#define pp( x ) x++
#else
#define pp( x )
#endif
/*--------------------------------------------------------------------------*/
/*--------------------- IMPLEMENTATION OF RIVState -------------------------*/
/*--------------------------------------------------------------------------*/
RelaxIV::RIVState::RIVState( MCFClass::cIndex m )
{
Flow = new RelaxIV::FNumber[ m ];
RedCost = new CNumber[ m ];
}
RelaxIV::RIVState::~RIVState()
{
delete[] Flow;
delete[] RedCost;
}
/*--------------------------------------------------------------------------*/
/*--------------------- IMPLEMENTATION OF RelaxIV --------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------- PUBLIC METHODS -------------------------------*/
/*--------------------------------------------------------------------------*/
/*---------------------------- CONSTRUCTOR ---------------------------------*/
/*--------------------------------------------------------------------------*/
RelaxIV::RelaxIV( cIndex nmx , cIndex mmx )
:
MCFClass( nmx , mmx )
{
// allocate memory - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( nmax && mmax )
MemAlloc();
else
nmax = mmax = 0;
// other initializations - - - - - - - - - - - - - - - - - - - - - - - - - -
InstCntr++;
} // end( RelaxIV )
/*--------------------------------------------------------------------------*/
/*-------------------------- OTHER INITIALIZATIONS -------------------------*/
/*--------------------------------------------------------------------------*/
void RelaxIV::LoadNet( cIndex nmx , cIndex mmx , cIndex pn , cIndex pm ,
cFRow pU , cCRow pC , cFRow pDfct , cIndex_Set pSn ,
cIndex_Set pEn )
{
// allocating and deallocating memory- - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( nmx != nmax ) || ( mmx != mmax ) ) {
if( nmax && mmax ) {
MemDeAlloc();
nmax = mmax = 0;
}
if( mmx && nmx ) {
nmax = nmx;
mmax = mmx;
MemAlloc();
}
}
if( ( ! nmax ) || ( ! mmax ) ) { // just sit down in the corner and wait
nmax = mmax = 0;
return;
}
// now setting up data - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
n = pn;
m = pm;
if( pDfct ) { // copy the deficits- - - - - - - - - - - - - - - - - - - - -
FRow tB = B + n;
for( pDfct += n ; tB > B ; )
*(tB--) = *(--pDfct);
}
else // deficits are all-0
for( FRow tB = B + n ; tB > B ; )
*(tB--) = 0;
bool InfCap = false;
if( pU ) { // copy the capacities - - - - - - - - - - - - - - - - - - - - -
FRow tCap = Cap + m;
for( pU += m ; tCap > Cap ; )
if( ( (*tCap--) = *(--pU) ) == Inf<FNumber>() ) {
InfCap = true;
break;
}
for( ; tCap > Cap ; )
(*tCap--) = *(--pU);
}
else { // capacities are all-INF
InfCap = true;
for( FRow tCap = Cap + m ; tCap > Cap ; )
(*tCap--) = Inf<FNumber>();
}
if( pC ) { // copy the costs- - - - - - - - - - - - - - - - - - - - - - - -
CRow tC = C + m;
CRow tRC = RC + m;
#if( SAME_GRPH_RIV && ( ! DYNMC_MCF_RIV ) )
FRow tCap = Cap + m;
for( pC += m ; tC > C ; tCap-- ) {
CNumber ttC = *(--pC);
if( ttC == Inf<CNumber>() ) {
*tCap = 0;
ttC = 0;
}
*(tC--) = *(tRC--) = ttC;
}
#else
for( pC += m ; tC > C ; ) {
cCNumber ttC = *(--pC);
if( ( *(tRC--) = ttC ) < Inf<CNumber>() )
*(tC--) = ttC;
else
*(tC--) = 0;
}
#endif
}
else { // costs are all-0 - - - - - - - - - - - - - - - - - - - - - - - - -
CRow tRC = RC + m;
for( CRow tC = C + m ; tC > C ; )
*(tC--) = *(tRC--) = 0;
}
if( InfCap ) { // make all capacities finite- - - - - - - - - - - - - - - -
FNumber maxcap = 0;
for( FRow tB = B + n ; tB > B ; tB-- )
if( *tB > 0 )
maxcap += *tB;
FRow tCap = Cap + m;
for( CRow tC = C + m ; tC > C ; tCap-- )
if( *(tC--) < 0 ) {
if( *tCap == Inf<FNumber>() )
throw(
MCFException( "RelaxIV::LoadNet(): C[ i , j ] < 0 and U[ i , j ] = INF"
) );
maxcap += *tCap;
}
for( tCap = Cap + m ; tCap > Cap ; tCap-- )
if( *tCap == Inf<FNumber>() )
*tCap = maxcap;
}
#if( SAME_GRPH_RIV )
if( ! Startn[ 1 ] ) // Startn[] and Endn[] have not been initialized yet -
#endif
{
Index_Set tEn = Endn + m;
Index_Set tSn = Startn + m;
for( pSn += m , pEn += m ; tSn > Startn ; ) {
*(tSn--) = *(--pSn) + USENAME0;
*(tEn--) = *(--pEn) + USENAME0;
}
}
#if( SAME_GRPH_RIV && ( ! DYNMC_MCF_RIV ) )
if( FIn[ 1 ] == Inf<Index>() ) // FS and BS have not been initialized yet-
#endif
{
// clean up the FS and BS information- - - - - - - - - - - - - - - - - - -
Index_Set tOu = FOu + n;
for( Index_Set tIn = FIn + n ; tIn > FIn ; )
*(tIn--) = *(tOu--) = 0;
// now construct the FS and BS structures- - - - - - - - - - - - - - - - -
for( Index j = 0 ; j++ < m ; )
#if( ( ! SAME_GRPH_RIV ) || DYNMC_MCF_RIV )
if( RC[ j ] == Inf<CNumber>() )
X[ j ] = 0;
else
#endif
{
Index i = Startn[ j ];
NxtOu[ j ] = FOu[ i ];
FOu[ i ] = j;
NxtIn[ j ] = FIn[ i = Endn[ j ] ];
FIn[ i ] = j;
}
}
#if( P_ALLOC )
PiOwnr = NULL;
#endif
#if( AUCTION )
crash = false;
#endif
#if( RELAXIV_STATISTICS )
iter = nmultinode = num_augm = num_ascnt = 0;
#if( AUCTION )
nsp = 0;
#endif
#endif
#if( DYNMC_MCF_RIV > 2 )
ffp = 0;
#endif
status = MCFClass::kUnSolved;
} // end( LoadNet )
/*--------------------------------------------------------------------------*/
void RelaxIV::PreProcess( void )
{
Index_Set tFOu = FOu + n;
Index_Set tFIn = FIn + n;
for( FRow tB = B + n ; tB > B ; tB-- ) {
FNumber dfctn = *tB;
FNumber tcap = 0;
Index arc = *tFOu;
while( arc ) {
tcap += Cap[ arc ];
arc = NxtOu[ arc ];
}
FNumber cap = tcap + dfctn;
if( LTZ( cap , EpsDfct ) ) { // problem is unfeasible
status = MCFClass::kUnfeasible;
error_node = tB - B;
error_info = 1;
return;
}
tcap = 0;
for( arc = *(tFIn--) ; arc ; ) {
if( cap < Cap[ arc ] )
Cap[ arc ] = cap;
tcap += Cap[ arc ];
arc = NxtIn[ arc ];
}
cap = tcap - dfctn;
if( LTZ( cap , EpsDfct ) ) { // problem is unfeasible
status = MCFClass::kUnfeasible;
error_node = tB - B;
error_info = 2;
return;
}
for( arc = *(tFOu--) ; arc ; ) {
if( cap < Cap[ arc ] )
Cap[ arc ] = cap;
arc = NxtOu[ arc ];
}
}
status = MCFClass::kUnSolved;
} // end( PreProcess )
/*--------------------------------------------------------------------------*/
/*-------------------- METHODS FOR SOLVING THE PROBLEM ---------------------*/
/*--------------------------------------------------------------------------*/
void RelaxIV::SolveMCF( void )
{
if( MCFt )
MCFt->Start();
#if( P_ALLOC )
PiOwnr = NULL;
#endif
FO = Inf<FONumber>();
iter = num_augm = 0;
#if( RELAXIV_STATISTICS )
nmultinode = num_ascnt = 0;
#endif
if( status ) // initializations are skipped if status == 0- - - - - - - - -
{ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
status = MCFClass::kOK;
// prices and flows are initialized by either calling the auction()
// routine or by performing only single-node iterations
#if( AUCTION )
if( crash )
auction();
else
#endif
init_standard();
if( status )
return;
init_tree();
}
// initialize other variables- - - - - - - - - - - - - - - - - - - - - - - -
Bool_Vec tmark = mark + n;
for( Bool_Vec tscan = scan + n ; tscan > scan ; )
*(tmark--) = *(tscan--) = false;
// an adaptive strategy is used to decide whether to continue the scanning
// process after a multinode price change: the thresold parameters that
// control this strategy are tp and ts, that is set in the next line
cIndex ts = n / ts_den;
// initialize the queue of nodes with nonzero deficit- - - - - - - - - - - -
Index node = 2;
for( Index_Set tnxtq = queue ; node <= n ; )
*(++tnxtq) = node++;
queue[ lastq = n ] = 1;
FNumber deficit;
Index ndfct = node = n;
Index nnonz = 0;
Index nlabel = 0;
Index npass = 0;
bool Switch = false;
for(;;) // main loop, repeated until there are unbalanced nodes - - - - - -
{ //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool posit = false;
for(;;) { // advancing the queue of nonzero deficit nodes- - - - - - - - -
prvnde = node;
node = queue[ node ];
deficit = Dfct[ node ];
if( node == lastq ) {
ndfct = nnonz;
nnonz = 0;
lastq = prvnde;
npass++;
}
// deleting a node from the queue- - - - - - - - - - - - - - - - - - - - -
if( ETZ( deficit , EpsDfct ) ) {
Index nxtnode = queue[ node ];
if( node == nxtnode ) {
posit = true; // condition for termination of SolveMCF
break;
}
else {
queue[ node ] = 0;
queue[ prvnde ] = node = nxtnode;
}
}
else // selected a node for the current relaxation iteration
break;
}
if( posit )
break; // terminate main loop
iter++;
nnonz++;
bool quit;
if( ( posit = GTZ( deficit , EpsDfct ) ) ) {
// attempt a single node iteration from node with positive deficit- - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool pchange = false;
FNumber indef = deficit;
FNumber delx = svblncdarcs( node , tfstou , tnxtou , tfstin , tnxtin );
// end of initial node scan - - - - - - - - - - - - - - - - - - - - - - -
bool cond1;
if( GEZ( delx - deficit , EpsDfct ) ) { // if no price change is
quit = ( deficit < indef ); // possible, skip do loop
cond1 = true;
}
else {
// search along the ascent direction for the best price by checking the
// slope of the dual cost at successive breakpoints; first, compute the
// the distance to the next breakpoint
CNumber delprc = nxtbrkpt( FIn + node , NxtIn , FOu + node , NxtOu );
for(;;) {
if( GTZ( deficit - delx , EpsDfct ) && ( delprc == Inf<CNumber>() ) ) {
error_node = node;
error_info = 5;
status = MCFClass::kUnfeasible;
return;
}
if( ! ETZ( delx , EpsDfct ) ) {
// skip flow adjustment if there is no flow to modify
// adjust the flow on the balanced arcs incident to node to maintain
// complementary slackness after the price change
Index j = nb_pos;
Index_Set t_save = save;
for( ; j-- ; ) {
Index arc = *(t_save++);
Index t2 = Endn[ arc ];
FNumber f = X[ arc ];
Dfct[ t2 ] += f;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
U[ arc ] += f;
X[ arc ] = 0;
}
for( j = m - nb_neg , t_save = save + m ; j-- ; ) {
Index arc = *(--t_save);
Index t2 = Startn[ arc ];
FNumber f = U[ arc ];
Dfct[ t2 ] += f;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] += f;
U[ arc ] = 0;
}
deficit -= delx;
} // end if( delx == 0 )
if( delprc == Inf<CNumber>() ) {
quit = true;
cond1 = false;
break;
}
// node corresponds to a dual ascent direction: decrease the price of
// node by delprc and compute the stepsize to the next breakpoint in
// the dual cost
pchange = true;
delx = dascnt( node , delprc , FOu , NxtOu , FIn , NxtIn );
if( GEZ( delx - deficit , EpsDfct ) ) { // if no price change is
quit = ( deficit < indef ); // possible, exit do loop
cond1 = true;
break;
}
} // end for( ever )
} // end else( delx != deficit )
// perform flow augmentation at node- - - - - - - - - - - - - - - - - - -
if( cond1 ) {
Index j = nb_pos;
Index_Set t_save = save;
for( ; j-- ; ) { // outgoing arcs from node
Index arc = *(t_save++);
Index t2 = Endn[ arc ];
FNumber f2 = Dfct[ t2 ];
if( GTZ( -f2 , EpsDfct ) ) { // decrease the total deficit by
quit = true; // decreasing flow of arc
FNumber f = X[ arc ];
FNumber dx = ( deficit < -f2 ? deficit : -f2 );
if( f < dx )
dx = f;
deficit -= dx;
Dfct[ t2 ] += dx;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] -= dx;
U[ arc ] += dx;
if( ETZ( deficit , EpsDfct ) )
break;
}
} // end for( j )
for( j = m - nb_neg , t_save = save + m ; j-- ; ) { // incoming arcs
Index arc = *(--t_save); // into node
Index t2 = Startn[ arc ];
FNumber f2 = Dfct[ t2 ];
if( GTZ( -f2 , EpsDfct ) ) { // decrease the total deficit by
quit = true; // increasing flow of arc
FNumber f = U[ arc ];
FNumber dx = ( deficit < -f2 ? deficit : -f2 );
if( f < dx )
dx = f;
deficit -= dx;
Dfct[ t2 ] += dx;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] += dx;
U[ arc ] -= dx;
if( ETZ( deficit , EpsDfct ) )
break;
}
} // end for( j )
} // end if( cond1 )
Dfct[ node ] = deficit;
// reconstruct the linked list of balanced arcs incident to this node:
// for each adjacent node, add any newly balanced arcs to the list, but do
// not bother removing formerly balanced ones (they will be removed the
// next time each adjacent node is scanned)
if( pchange )
relist( node );
} // end if( posit ) - single node iteration for deficit > 0 node- - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
else {
// attempt a single node iteration from node with negative deficit - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool pchange = false;
deficit = - deficit;
FNumber indef = deficit;
FNumber delx = svblncdarcs( node , tfstin , tnxtin , tfstou , tnxtou );
// end of initial node scan - - - - - - - - - - - - - - - - - - - - - - -
bool cond1;
if( GEZ( delx - deficit , EpsDfct ) ) { // if no price change is
quit = ( deficit < indef ); // possible, skip do loop
cond1 = true;
}
else {
// search along the ascent direction for the best price by checking the
// slope of the dual cost at successive breakpoints; first, compute the
// the distance to the next breakpoint
CNumber delprc = nxtbrkpt( FOu + node , NxtOu , FIn + node , NxtIn );
for(;;) {
if( GTZ( deficit - delx , EpsDfct ) && ( delprc == Inf<CNumber>() ) ) {
error_node = node;
error_info = 6;
status = MCFClass::kUnfeasible;
return;
}
if( ! ETZ( delx , EpsDfct ) ) {
// skip flow adjustment if there is no flow to modify
// adjust the flow on the balanced arcs incident to node to maintain
// complementary slackness after the price change
Index j = nb_pos;
Index_Set t_save = save;
for( ; j-- ; ) {
Index arc = *(t_save++);
Index t2 = Startn[ arc ];
FNumber f = X[ arc ];
Dfct[ t2 ] -= f;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
U[ arc ] += f;
X[ arc ] = 0;
}
for( j = m - nb_neg , t_save = save + m ; j-- ; ) {
Index arc = *(--t_save);
Index t2 = Endn[ arc ];
FNumber f = U[ arc ];
Dfct[ t2 ] -= f;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] += f;
U[ arc ] = 0;
}
deficit -= delx;
}
if( delprc == Inf<CNumber>() ) {
quit = true;
cond1 = false;
break;
}
// node corresponds to a dual ascent direction: increase the price of
// node by delprc and compute the stepsize to the next breakpoint in
// the dual cost
pchange = true;
delx = dascnt( node , delprc , FIn , NxtIn , FOu , NxtOu );
if( GEZ( delx - deficit , EpsDfct ) ) { // if no price change is
quit = ( deficit < indef ); // possible, exit do loop
cond1 = true;
break;
}
} // end for( ever )
} // end else( delx != deficit )
// perform flow augmentation at node- - - - - - - - - - - - - - - - - - -
if( cond1 ) {
Index j = nb_pos;
Index_Set t_save = save;
for( ; j-- ; ) { // incoming arcs into node
Index arc = *(t_save++);
Index t2 = Startn[ arc ];
FNumber f2 = Dfct[ t2 ];
if( GTZ( f2 , EpsDfct ) ) { // decrease the total deficit by
quit = true; // decreasing flow of arc
FNumber f = X[ arc ];
FNumber dx = ( deficit < f2 ? deficit : f2 );
if( f < dx )
dx = f;
deficit -= dx;
Dfct[ t2 ] -= dx;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] -= dx;
U[ arc ] += dx;
if( ETZ( deficit , EpsDfct ) )
break;
}
}
for( j = m - nb_neg , t_save = save + m ; j-- ; ) { // outgoing arcs
Index arc = *(--t_save); // from node
Index t2 = Endn[ arc ];
FNumber f2 = Dfct[ t2 ];
if( GTZ( f2 , EpsDfct ) ) { // decrease the total deficit by
quit = true; // increasing flow on arc
FNumber f = U[ arc ];
FNumber dx = ( deficit < f2 ? deficit : f2 );
if( f < dx )
dx = f;
deficit -= dx;
Dfct[ t2 ] -= dx;
if( ! queue[ t2 ] ) {
queue[ prvnde ] = t2;
queue[ t2 ] = node;
prvnde = t2;
}
X[ arc ] += dx;
U[ arc ] -= dx;
if( ETZ( deficit , EpsDfct ) )
break;
}
}
}
Dfct[ node ] = -deficit;
// reconstruct the linked list of balanced arcs incident to this node:
// for each adjacent node, add any newly balanced arcs to the list, but do
// not bother removing formerly balanced ones (they will be removed the
// next time each adjacent node is scanned)
if( pchange )
relist( node );
} // end else( ! posit ) - single node iteration for deficit < 0 node - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( ! quit ) && ( npass >= npasslim ) ) {
// multinode iteration from node - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pp( nmultinode );
Switch = ( ndfct < tp ); // if number of nonzero deficit nodes is small,
// continue labelling until a flow augmentation
// is done
bool nzerodfct = true; // if the deficit of node has been zeroed
// unmark nodes labeled earlier- - - - - - - - - - - - - - - - - - - - - -
for( Index_Set tl = label + nlabel ; tl > label ; ) {
Index i = *(--tl);
mark[ i ] = scan[ i ] = false;
}
// initialize labelling- - - - - - - - - - - - - - - - - - - - - - - - - -
nlabel = 1;
mark[ *label = node ] = true;
Prdcsr[ node ] = 0;
// scan starting node- - - - - - - - - - - - - - - - - - - - - - - - - - -
scan[ node ] = true;
Index nscan = 1;
FNumber dm = Dfct[ node ];
FNumber delx = 0;
Index j = nb_pos;
Index_Set t_save = save;
for( ; j-- ; ) {
Index arc = *(t_save++);
Index t2 = ( posit ? Endn[ arc ] : Startn[ arc ] );
if( ! mark[ t2 ] ) {
Prdcsr[ t2 ] = arc;
label[ nlabel++ ] = t2;
mark[ t2 ] = true;
delx += X[ arc ];
}
}
for( j = m - nb_neg , t_save = save + m ; j-- ; ) {
Index arc = *(--t_save);
Index t2 = ( posit ? Startn[ arc ] : Endn[ arc ] );
if( ! mark[ t2 ] ) {
Prdcsr[ t2 ] = -arc;
label[ nlabel++ ] = t2;
mark[ t2 ] = true;
delx += U[ arc ];
}
}
// start scanning a labeled but unscanned node - - - - - - - - - - - - - -
bool continua;
do {
Index i = label[ nscan++ ];
// check to see if Switch needs to be set to true so to continue
// scanning even after a price change
Switch = Switch || ( ( nscan > ts ) && ( ndfct < ts ) );
/* Scanning will continue until either an overestimate of the residual
capacity across the cut corresponding to the scanned set of nodes
(called delx) exceeds the absolute value of the total deficit of the
scanned nodes (called dm), or an augmenting path is found. Arcs that
are in the tree but are not balced are removed as part of the
scanning process. */
Index naugnod = 0;
scan[ i ] = true;
if( posit ) { // scanning node i in case of positive deficit - - - - - -
Index prvarc = 0;
Index arc = tfstou[ i ];
while( arc ) { // arc is an outgoing arc from node i
if( ETZ( RC[ arc ] , EpsCst ) ) {
Index t2 = Endn[ arc ];
if( ! mark[ t2 ] ) // t2 is not labeled
{
if( GTZ( X[ arc ], EpsFlw ) ) {
if( LTZ( Dfct[ t2 ], EpsDfct ) )
save[ naugnod++ ] = t2;
Prdcsr[ t2 ] = arc;
label[ nlabel++ ] = t2;
mark[ t2 ] = true;
delx += X[ arc ];
} else {
Prdcsr[ t2 ] = 0;
}
}
prvarc = arc;
arc = tnxtou[ arc ];
}
else {
Index tmparc = arc;
arc = tnxtou[ arc ];
tnxtou[ tmparc ] = tmparc;
if( prvarc )
tnxtou[ prvarc ] = arc;
else
tfstou[ i ] = arc;
}
}
prvarc = 0;
arc = tfstin[ i ];
while( arc ) { // arc is an incoming arc into node i
if( ETZ( RC[ arc ] , EpsCst ) ) {
Index t2 = Startn[ arc ];
if( ! mark[ t2 ] ) // t2 is not labeled
{
if( GTZ( U[ arc ], EpsFlw ) ) {
if( LTZ( Dfct[ t2 ], EpsDfct ) )
save[ naugnod++ ] = t2;
Prdcsr[ t2 ] = -arc;
label[ nlabel++ ] = t2;
mark[ t2 ] = true;
delx += U[ arc ];
} else {
Prdcsr[ t2 ] = 0;
}
}
prvarc = arc;
arc = tnxtin[ arc ];
}
else {
Index tmparc = arc;
arc = tnxtin[ arc ];
tnxtin[ tmparc ] = tmparc;
if( prvarc )
tnxtin[ prvarc ] = arc;
else
tfstin[ i ] = arc;
}
}
}
else { // scanning node i in case of negative deficit- - - - - - - - - -