-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
1250 lines (1023 loc) · 38.7 KB
/
Copy pathtest.cpp
File metadata and controls
1250 lines (1023 loc) · 38.7 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
/*--------------------------------------------------------------------------*/
/*-------------------------- File test.cpp ---------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing MCFBlock and MCFSolver.
*
* Reads an instance of a MCF from a file (in either DIMACS or netCDF format)
* in an MCFBlock, and from there in an object of a class MCFC derived from
* MCFClass, as decided by the macro WHICH_MCF. Then, a MCFSolver< MCFC > is
* attached to the MCFBlock. The MCF problem is then repeatedly solved with
* several changes in costs / capacities / deficits, arcs openings / closures
* and arcs additions / deletions. The same operations are performed on the
* two solvers, and the results are compared. This mostly tests MCFBlock and
* MCFSolver, since the actual MCFClass solved is the same, and so it can
* easily be wrong in the same way for both the objects.
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Antonio Frangioni
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ DEFINES -----------------------------------*/
/*--------------------------------------------------------------------------*/
#define LOG_LEVEL 0
// 0 = only pass/fail
// 1 = result of each test
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/* Defines which :MCFClass solver is included and the corresponding version
* of MCFSolver< :MCFClass > is tested:
*
* - 0 for the CS2 class
*
* - 1 for the MCFCplex class
*
* - 2 for the MCFSimplex class
*
* - 3 for the MCFZIB class
*
* - 4 for the RelaxIV class
*
* - 5 for the SPTree class; note that SPTree cannot solve
* most MCF instances, except those with SPT structure */
#ifndef WHICH_MCF
#define WHICH_MCF 1
#endif
#if( LOG_LEVEL >= 1 )
#define LOG1( x ) cout << x
#define CLOG1( y , x ) if( y ) cout << x
#else
#define LOG1( x )
#define CLOG1( y , x )
#endif
#define USECOLORS 1
#if( USECOLORS )
#define RED( x ) "\x1B[31m" #x "\033[0m"
#define GREEN( x ) "\x1B[32m" #x "\033[0m"
#else
#define RED( x ) #x
#define GREEN( x ) #x
#endif
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <fstream>
#include <iomanip>
#include <random>
#if WHICH_MCF == 0
#include "CS2.h"
#define MCFC CS2
#elif WHICH_MCF == 1
#include "MCFCplex.h"
#define MCFC MCFCplex
#elif WHICH_MCF == 2
#include "MCFSimplex.h"
#define MCFC MCFSimplex
#elif WHICH_MCF == 3
#include "MCFZIB.h"
#define MCFC MCFZIB
#elif WHICH_MCF == 4
#include "RelaxIV.h"
#define MCFC RelaxIV
#elif WHICH_MCF == 5
#include "SPTree.h"
#define MCFC SPTree
#endif
#include "MCFSolver.h"
#include "UpdateSolver.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- MACROS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#define arg_2_str( s ) #s
#define solver_name( snm ) "MCFSolver<" arg_2_str( snm ) ">"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace MCFClass_di_unipi_it;
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------- TYPES & CONSTEXPRS -----------------------------*/
/*--------------------------------------------------------------------------*/
// check that MCFClass types and MCFBlock types agree
using Index = Block::Index;
using c_Index = Block::c_Index;
static constexpr Index IInf = SMSpp_di_unipi_it::Inf< Index >();
static_assert( std::is_same< Index , MCFClass::Index >::value );
using FNumber = MCFBlock::FNumber;
static constexpr FNumber FInf = SMSpp_di_unipi_it::Inf< FNumber >();
static_assert( std::is_same< FNumber , MCFClass::FNumber >::value );
using CNumber = MCFBlock::CNumber;
static constexpr CNumber CInf = SMSpp_di_unipi_it::Inf< CNumber >();
static_assert( std::is_same< CNumber , MCFClass::CNumber >::value );
using Range = Block::Range;
using c_Range = Block::c_Range;
using Subset = Block::Subset;
using c_Subset = Block::c_Subset;
static constexpr double BA = 1e-12; // base accuracy of the MCFSolver
/*--------------------------------------------------------------------------*/
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------------------------------------------------*/
unsigned int mode = 0; // what is modified, what is solved
MCFBlock * oMCFB = nullptr; // original MCFBlock
MCFBlock * dMCFB = nullptr; // "derived" (i.e., R3B) MCFBlock
MCFBlock * sMCFB = nullptr; // MCFBlock that is solved
MCFBlock * mMCFB = nullptr; // MCFBlock that is modified
MCFClass * mcf; // the MCFClass object
bool isnc4 = false; // true if the file is a ntCDF one
std::mt19937 rg; // base random generator
std::uniform_real_distribution<> dis( 0.0 , 1.0 );
FNumber MaxC = 0; // max absolute value of costs
FNumber MaxU = 0; // max absolute value of capacities / deficits
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
template< class T >
static void Str2Sthg( const char* const str , T &sthg )
{
istringstream( str ) >> sthg;
}
/*--------------------------------------------------------------------------*/
// returns a random number between 0.5 and 2, with 50% probability of
// being < 1
static double rndfctr( void )
{
double fctr = dis( rg ) - 0.5;
return( fctr < 0 ? - fctr : fctr * 4 );
}
/*--------------------------------------------------------------------------*/
// generates a random k-vector of unique integers in 0 ... m - 1
static Subset GenerateRand( Index m , Index k )
{
Subset rnd( m );
std::iota( rnd.begin() , rnd.end() , 0 );
std::shuffle( rnd.begin() , rnd.end() , rg );
rnd.resize( k );
sort( rnd.begin() , rnd.end() );
return( rnd );
}
/*--------------------------------------------------------------------------*/
static void CreateProb( unsigned int Optns )
{
bool reoptmz = Optns & 1u;
Optns /= 2;
mcf = nullptr; // unknown solver, or the required solver is not
// available due to the macros settings
#if WHICH_MCF == 0 //- - - - - - - - - - - - - - - - - - - - - - - - - - -
mcf = new CS2();
LOG1( "CS2" );
assert( false ); // CS2 not fully supported yet
#elif WHICH_MCF == 1 //- - - - - - - - - - - - - - - - - - - - - - - - - -
MCFCplex *cpx = new MCFCplex();
if( Optns >= 0 )
cpx->SetPar( CPX_PARAM_NETPPRIIND , int( Optns ) );
mcf = cpx;
LOG1( "MCFCplex" );
#elif WHICH_MCF == 2 //- - - - - - - - - - - - - - - - - - - - - - - - - -
auto *mcfs = new MCFSimplex();
bool PrmlSmplx = Optns & 1u;
char Prcng = 0;
switch( Optns / 2 ) {
case( 0 ): Prcng = char( MCFSimplex::kDantzig ); break;
case( 1 ): Prcng = char( MCFSimplex::kFirstEligibleArc ); break;
default: Prcng = char( MCFSimplex::kCandidateListPivot );
}
if( ( ! PrmlSmplx ) && ( Prcng == MCFSimplex::kDantzig ) )
Prcng = char( MCFSimplex::kCandidateListPivot );
mcf = mcfs;
LOG1( "MCFSimplex" );
#elif WHICH_MCF == 3 //- - - - - - - - - - - - - - - - - - - - - - - - - -
MCFZIB *zib = new MCFZIB();
bool PrmlSmplx = Optns & 1;
char Prcng;
switch( Optns / 2 ) {
case( 0 ): Prcng = char( MCFZIB::kDantzig ); break;
case( 1 ): Prcng = char( MCFZIB::kFrstElA ); break;
default: Prcng = char( MCFZIB::kMltPrPr );
}
if( ( ! PrmlSmplx ) && ( Prcng == MCFZIB::kDantzig ) )
Prcng = char( MCFZIB::kMltPrPr );
zib->SetAlg( PrmlSmplx , Prcng );
mcf = zib;
LOG1( "MCFZIB" );
assert( false ); // MCFZIB not fully supported yet
#elif WHICH_MCF == 4 //- - - - - - - - - - - - - - - - - - - - - - - - - -
RelaxIV *rlx = new RelaxIV();
#if( AUCTION )
if( Optns )
rlx->SetPar( RelaxIV::kAuction , MCFClass::kYes );
#endif
mcf = rlx;
LOG1( "RelaxIV" );
#elif WHICH_MCF == 5 //- - - - - - - - - - - - - - - - - - - - - - - - - -
mcf = new SPTree();
LOG1( "SPTree" );
assert( false ); // SPTree not fully supported yet
#endif
if( ! reoptmz )
mcf->SetPar( MCFClass::kReopt , MCFClass::kNo );
if( ! mcf )
throw( std::logic_error( "No MCFClass defined" ) );
}
/*--------------------------------------------------------------------------*/
static void load( char * fn )
{
try {
// note that usually the "original" MCFBlock is loaded, unless mode == 2
// (==> original solved, R3 modified) *and* the R3 as been constructed
// already, in which case the R3 is loaded; this is perhaps stretching
// the concept of "R3Block" close to the breaking point, but in this case
// it works because the R3B is a copy *and* always the same instance is
// loaded
if( isnc4 ) {
netCDF::NcFile f( fn , netCDF::NcFile::read );
if( f.isNull() ) {
std::cerr << "cannot open nc4 file " << fn << std::endl;
exit( 1 );
}
netCDF::NcGroupAtt gtype = f.getAtt( "SMS++_file_type" );
if( gtype.isNull() ) {
std::cerr << fn << " is not an SNS++ nc4 file" << std::endl;
exit( 1 );
}
int type = 0;
gtype.getValues( & type );
if( type != eBlockFile ) {
std::cerr << fn << " is not an SNS++ nc4 Block file" << std::endl;
exit( 1 );
}
netCDF::NcGroup bg = f.getGroup( "Block_0" );
if( bg.isNull() ) {
std::cerr << "Block_0 empty or undefined in " << fn << std::endl;
exit( 1 );
}
if( ( ( mode & 3u ) == 2 ) && dMCFB ) {
dMCFB->deserialize( bg ); // load the (derived) MCFBlock
// load the MCFClass out of the MCFBlock using the in-memory interface
mcf->LoadNet( dMCFB->get_MaxNNodes() , dMCFB->get_MaxNArcs() ,
dMCFB->get_NNodes() , dMCFB->get_NArcs() ,
dMCFB->get_U().empty() ? nullptr : dMCFB->get_U().data() ,
dMCFB->get_C().empty() ? nullptr : dMCFB->get_C().data() ,
dMCFB->get_B().empty() ? nullptr : dMCFB->get_B().data() ,
dMCFB->get_SN().data() , dMCFB->get_EN().data() );
}
else {
oMCFB->deserialize( bg ); // load the (original) MCFBlock
// load the MCFClass out of the MCFBlock using the in-memory interface
mcf->LoadNet( oMCFB->get_MaxNNodes() , oMCFB->get_MaxNArcs() ,
oMCFB->get_NNodes() , oMCFB->get_NArcs() ,
oMCFB->get_U().empty() ? nullptr : oMCFB->get_U().data() ,
oMCFB->get_C().empty() ? nullptr : oMCFB->get_C().data() ,
oMCFB->get_B().empty() ? nullptr : oMCFB->get_B().data() ,
oMCFB->get_SN().data() , oMCFB->get_EN().data() );
}
}
else {
ifstream iFile( fn );
if( ! iFile ) {
cerr << "Can't open dmx file " << fn << endl;
exit( 1 );
}
mcf->LoadDMX( iFile ); // load the MCFClass
iFile.clear();
iFile.seekg( 0 ); // rewind the file
if( ( ( mode & 3u ) == 2 ) && dMCFB )
iFile >> *dMCFB; // load the (derived) MCFBlock
else
iFile >> *oMCFB; // load the (original) MCFBlock
}
if( mode & 4u ) {
// if so instructed, generate abstract representation for oMCFB
oMCFB->generate_abstract_constraints();
oMCFB->generate_objective();
}
if( ( mode & 8u ) && dMCFB ) {
// if so instructed, generate abstract representation for dMCFB (if any)
dMCFB->generate_abstract_constraints();
dMCFB->generate_objective();
}
}
catch( exception &e ) {
cerr << "MCFClass: " << e.what() << endl;
exit( 1 );
}
catch(...) {
cerr << "Error: unknown exception thrown" << endl;
exit( 1 );
}
} // end( load )
/*--------------------------------------------------------------------------*/
static bool SolveMCF( void )
{
try {
// solve the MCFClass- - - - - - - - - - - - - - - - - - - - - - - - - - - -
mcf->SolveMCF();
auto stat = mcf->MCFGetStatus();
// solve the MCFBlock- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Solver * slvr = ( sMCFB->get_registered_solvers() ).front();
int rtrn = slvr->compute( false );
if( ( stat == MCFClass::kOK ) &&
( rtrn >= Solver::kOK ) && ( rtrn < Solver::kError ) ) {
auto fo1 = mcf->MCFGetFO();
auto fo2 = slvr->get_ub();
if( abs( fo1 - fo2 )
<= 1e-9 * max( double( 1 ) , abs( max( fo1 , fo2 ) ) ) ) {
LOG1( "OK(f)" << endl );
return( true );
}
}
if( ( stat == MCFClass::kUnfeasible ) &&
( rtrn == Solver::kInfeasible ) ) {
LOG1( "OK(e)" << endl );
return( false );
}
if( ( stat == MCFClass::kUnbounded ) &&
( rtrn == Solver::kUnbounded ) ) {
LOG1( "OK(u)" << endl );
return( false );
}
#if( LOG_LEVEL >= 1 )
cout << "MCFClass = ";
switch( mcf->MCFGetStatus() ) {
case( MCFClass::kOK ): cout << mcf->MCFGetFO();
break;
case( MCFClass::kUnfeasible ): cout << " +INF";
break;
case( MCFClass::kUnbounded ): cout << " -INF";
break;
default: cout << " Error!";
}
cout << " ~ MCFBlock = ";
#endif
if( ( rtrn >= Solver::kOK ) && ( rtrn < Solver::kError ) ) {
LOG1( slvr->get_ub() << endl );
return( false );
}
#if( LOG_LEVEL >= 1 )
switch( rtrn ) {
case( Solver::kInfeasible ): cout << " +INF";
break;
case( Solver::kUnbounded ): cout << " -INF";
break;
default: cout << " Error!";
}
cout << endl;
#endif
return( false );
}
catch( exception &e ) {
cerr << e.what() << endl;
exit( 1 );
}
catch(...) {
cerr << "Error: unknown exception thrown" << endl;
exit( 1 );
}
}
/*--------------------------------------------------------------------------*/
/// Custom terminate function to print the exception message
void smspp_terminate( void ) {
std::cerr << "Uncaught exception in executing SMS++:\n";
try {
std::rethrow_exception( std::current_exception() );
}
catch( const std::exception & e ) {
std::cerr << "\tException type: " << typeid( e ).name() << "\n";
std::cerr << "\tException message: " << e.what() << "\n";
} catch( ... ) {
std::cerr << "\tUnknown exception" << std::endl;
}
std::abort(); // or exit(1)
}
/*--------------------------------------------------------------------------*/
int main( int argc , char **argv )
{
// override the default terminate handler to print the exception message
std::set_terminate( smspp_terminate );
// reading command line parameters - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
long int seed = 1;
unsigned int wchg = 127;
double p_change = 0.5;
Index n_change = 10;
Index n_repeat = 40;
int optns = 1;
switch( argc ) {
case( 9 ): Str2Sthg( argv[ 8 ] , optns );
case( 8 ): Str2Sthg( argv[ 7 ] , p_change );
case( 7 ): Str2Sthg( argv[ 6 ] , n_change );
case( 6 ): Str2Sthg( argv[ 5 ] , n_repeat );
case( 5 ): Str2Sthg( argv[ 4 ] , wchg );
case( 4 ): Str2Sthg( argv[ 3 ] , mode );
case( 3 ): Str2Sthg( argv[ 2 ] , seed );
case( 2 ): break;
default: cerr << "Usage: " << argv[ 0 ] <<
" <dmx file> [seed mode wchg #rounds #chng %chng optns]"
<< endl <<
" mode: 0 = only one, 1 = orig -> solve, 2 = solve -> orig"
<< endl <<
" +4 = abstract orig, +8 = abstract solve,"
<< endl <<
" +16 = also change abstract representation"
<< endl <<
" wchg: what to change, coded bit-wise "
<< endl <<
" 0 = cost, 1 = cap, 2 = dfct, 3 = o.arc, 4 = c.arc"
<< endl <<
" 5 = delete arc, 6 = add arc"
<< endl <<
" optns: bit 0 = re-optimize, other bits MCF-specific"
<< endl <<
" Relax : > 0 uses Auction"
<< endl <<
" Cplex : network pricing parameter"
<< endl <<
" ZIB : 1st bit == 1 ==> primal +"
<< endl <<
" 0 = Dantzig, 2 = First Eligible, 4 = MPP"
<< endl <<
" Simplex : 1st bit == 1 ==> primal +"
<< endl <<
" 0 = Dantzig, 2 = First Eligible, 4 = MPP"
<< endl;
return( 1 );
}
if( ( mode & 3u ) == 3 ) {
std::cerr << "Wrong mode (must be 0, 1 or 2)" << std::endl;
exit( 1 );
}
if( mode & 16u ) // if the abstract representations are changed
mode |= 12u; // ensure they exist in the first place
// construction and loading of the objects - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// construct MCFClass- - - - - - - - - - - - - - - - - - - - - - - - - - - -
CreateProb( optns );
// construct the "original" MCFBlock - - - - - - - - - - - - - - - - - - - -
// ... in a bit of a roundabout way by un-necessarily using the factory
oMCFB = dynamic_cast< MCFBlock * >( Block::new_Block( "MCFBlock" ) );
assert( oMCFB );
// load the instance - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// first of check if the file is a .dmx (default) or a .nc4 one
std::string name( argv[ 1 ] );
if( name.size() > 4 ) {
std::string sffx = name.substr( name.size() - 4 , 4 );
std::string nc4( ".nc4" );
isnc4 = std::equal( sffx.begin() , sffx.end() , nc4.begin() ,
[]( auto a , auto b ) {
return( std::tolower( a ) == std::tolower( b ) ); } );
}
load( argv[ 1 ] );
// if so instructed, construct the R3 MCFBlock = copy- - - - - - - - - - - -
if( mode & 3u ) {
dMCFB = dynamic_cast< MCFBlock * >( oMCFB->get_R3_Block() );
assert( dMCFB ); // excess of caution (we know it is)
if( ( mode & 8u ) && dMCFB ) {
// if so instructed, also generate abstract representation for dMCFB
dMCFB->generate_abstract_constraints();
dMCFB->generate_objective();
}
if( ( mode & 3u ) == 1 ) { // modify the original, solve the R3
mMCFB = oMCFB;
sMCFB = dMCFB;
}
else { // modify the R3, solve the original
mMCFB = dMCFB;
sMCFB = oMCFB;
}
// attach an UpdateSolver to the modified: if the modified is the original
// then map_forward, otherwise (i.e., it is the copy) map_back
mMCFB->register_Solver(
new UpdateSolver( sMCFB , nullptr , mMCFB != oMCFB ? 1 : 0 ) );
}
else // just use one MCFBlock
sMCFB = mMCFB = oMCFB;
// compute min/max cost & max deficit- - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Index n = mcf->MCFn();
Index m = mcf->MCFm();
LOG1( ", n = " << n << ", m = " << m << endl );
if( n_change > m )
n_change = m;
CNumber c_max = - CInf; // max cost
CNumber c_abs = 0; // max absolute value of cost
CNumber c_min = - c_max; // min cost
FNumber u_max = 0; // max (finite) capacity
FNumber u_avg = 0; // average capacity
FNumber u_min = FInf; // min capacity
FNumber b_abs = 0; // max absolute value of deficit
for( Index i = 0 ; i < m ; ++i ) {
auto ci = mcf->MCFCost( i );
if( std::abs( ci ) > c_abs )
c_abs = std::abs( ci );
if( ci < CInf ) {
if( ci < c_min )
c_min = ci;
if( ci > c_max )
c_max = ci;
}
auto ui = mcf->MCFUCap( i );
if( ui < FInf ) {
if( ui > u_max )
u_max = ui;
u_avg += ui;
if( ui < u_min )
u_min = ui;
}
}
u_avg /= m;
for( Index i = 0 ; i < n ; )
if( auto bi = mcf->MCFDfct( i++ ) )
if( std::abs( bi ) > b_abs )
b_abs = std::abs( bi );
bool nzdfct = b_abs > 0;
// set epsilons in MCFClass and MCFSolver- - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// set epsilons in MCFClass
mcf->SetPar( MCFClass::kEpsFlw ,
BA * std::max( u_max , std::max( b_abs , FNumber( 1 ) ) ) );
mcf->SetPar( MCFClass::kEpsCst , BA * std::max( c_abs , CNumber( 1 ) ) );
// attach a "true" MCFSolver to the one that is actually solved
// auto MCFS = Solver::new_Solver( solver_name( MCFC ) );
auto MCFS = new MCFSolver< MCFC >();
sMCFB->register_Solver( MCFS );
// set epsilons in MCFSolver
MCFS->set_par( Solver::dblAbsAcc ,
BA * std::max( u_max , std::max( b_abs , FNumber( 1 ) ) ) );
MCFS->set_par( CDASolver::dblAAccDSol ,
BA * std::max( c_abs , CNumber( 1 ) ) );
// first solver call - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#if( LOG_LEVEL >= 1 )
cout << "First call: ";
cout.setf( ios::scientific, ios::floatfield );
cout << setprecision( 6 );
#endif
auto OK = SolveMCF();
// main loop - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// now, for n_repeat times:
// - up tp n_change costs are changed, then the two problems are re-solved;
// - up to n_change capacities are changed, then the two problems are
// re-solved, then the original capacities are restored;
// - if the problem is not a circulation problem, 2 deficits are modified
// (adding and subtracting the same number), then the two problems are
// re-solved, then the original deficits are restored;
// - up to n_change arcs are closed, then the two problems are re-solved;
// the same arcs are re-opened, then the two problems are re-solved
rg.seed( seed ); // seed the pseudo-random number generator
bool diffarcs = false; // whether added arcs ended up with different names
for( Index iter = 0 ; iter < n_repeat ; ++iter ) {
LOG1( iter << ": " );
// before making changes, lock() mMCFB: this is of course useless since
// nothing else has it, but there you go. Use "mcf" as the "owner", since
// it clearly cannot be a reserved address
bool owned = mMCFB->is_owned_by( mcf );
if( ( ! owned ) && ( ! mMCFB->lock( mcf ) ) )
throw( std::logic_error( "can't lock mMCFB" ) );
// change costs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 1u ) && ( dis( rg ) <= p_change ) ) {
Index tochange = max( double( 1 ) , dis( rg ) * n_change );
LOG1( tochange << " cost" );
LinearFunction * lf = nullptr;
if( ( mode & 16u ) && ( dis( rg ) < 0.5 ) )
// change via abstract representation
lf = static_cast< LinearFunction * >( static_cast< FRealObjective * >(
mMCFB->get_objective() )->get_function() );
if( tochange == 1 ) {
CNumber newcst = c_min + CNumber( dis( rg ) * ( c_max - c_min ) );
Index arc = Index( dis( rg ) * ( m - 1 ) );
mcf->ChgCost( arc , newcst );
if( lf ) { // change via abstract representation
LOG1( "(a)" );
lf->modify_coefficient( arc , newcst );
}
else // change via call to chg_* method
mMCFB->chg_cost( newcst , arc );
LOG1( " - " );
}
else {
MCFBlock::Vec_CNumber newcsts( tochange );
for( Index i = 0 ; i < tochange ; ++i )
newcsts[ i ] = c_min + CNumber( dis( rg ) * ( c_max - c_min ) );
// in 50% of the cases do a ranged change, in the others a sparse change
if( dis( rg ) <= 0.5 ) {
Index strt = dis( rg ) * ( m - tochange );
Index stp = strt + tochange;
mcf->ChgCosts( newcsts.data() , nullptr , strt , stp );
if( lf ) { // change via abstract representation
LOG1( "s(r,a) - " );
lf->modify_coefficients( std::move( newcsts ) , Range( strt , stp ) );
}
else { // change via call to chg_* method
// in 50% of the cases a direct call, otherwise use the methods factory
//!! if( dis( rg ) <= 0.5 ) {
mMCFB->chg_costs( newcsts.begin() , Range( strt , stp ) );
LOG1( "s(r) - " );
//!! }
//!! else {
//!! std::string mthd_name = "MCFBlock::chg_costs";
//!! const auto * mthd = Block::get_method_fs( mthd_name ,
//!! Block::MS_dbl_rngd::args() );
//!! assert( mthd_name == Block::get_method_name_fs
//!! ( mthd , Block::MS_dbl_rngd::args() ) );
//!! std::invoke( *mthd , mMCFB , newcsts.begin() ,
//!! Range( strt , stp ) , eNoBlck , eNoBlck );
//!! LOG1( "s(r-mf) - " );
//!! }
}
}
else {
Subset nms( GenerateRand( m , tochange ) );
nms.push_back( OPTtypes_di_unipi_it::Inf< MCFClass::Index >() );
mcf->ChgCosts( newcsts.data() , nms.data() );
nms.resize( tochange );
if( lf ) { // change via abstract representation
LOG1( "s(s,a) - " );
lf->modify_coefficients( std::move( newcsts ) , std::move( nms ) ,
true );
}
else { // change via call to chg_* method
// in 50% of the cases a direct call, otherwise use the methods factory
//!! if( dis( rg ) <= 0.5 ) {
mMCFB->chg_costs( newcsts.begin() , std::move( nms ) , true );
LOG1( "s(s) - " );
//!! }
//!! else {
//!! std::string mthd_name = "MCFBlock::chg_costs";
//!! const auto * mthd = Block::get_method_fs( mthd_name,
//!! Block::MS_dbl_sbst::args() );
//!! assert( mthd_name == Block::get_method_name_fs
//!! ( mthd , Block::MS_dbl_sbst::args() ) );
//!! std::invoke( *mthd , mMCFB , newcsts.begin() , std::move( nms ) ,
//!! true , eNoBlck , eNoBlck );
//!! LOG1( "s(s-mf) - " );
//!! }
}
}
}
} // end( if( change costs ) )
// change capacities- - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 2u ) && ( dis( rg ) <= p_change ) ) {
Index tochange = max( double( 1 ) , dis( rg ) * n_change );
LOG1( tochange << " capacit" );
if( tochange == 1 ) {
auto arc = Index( dis( rg ) * ( m - 1 ) );
CNumber newcap = mcf->MCFUCap( arc ) * rndfctr();
mcf->ChgUCap( arc , newcap );
if( ( mode & 16u ) && ( dis( rg ) < 0.5 ) ) {
// change via abstract representation
LOG1( "y(a) - " );
mMCFB->i2p_ub( arc )->set_rhs( newcap );
}
else { // change via call to chg_* method
mMCFB->chg_ucap( newcap , arc );
LOG1( "y - " );
}
}
else {
MCFBlock::Vec_FNumber newcaps( tochange );
// in 50% of the cases do a ranged change, in the others a sparse change
if( dis( rg ) <= 0.5 ) {
Index strt = dis( rg ) * ( m - tochange );
Index stp = strt + tochange;
for( Index i = 0 ; i < tochange ; ++i )
newcaps[ i ] = mcf->MCFUCap( i + strt ) * rndfctr();
mcf->ChgUCaps( newcaps.data() , nullptr , strt , stp );
if( ( mode & 16u ) && ( dis( rg ) < 0.5 ) ) {
// change via abstract representation, sending to a new channel
LOG1( "ies(a,r) - " );
auto chnl = mMCFB->open_channel();
auto modpar = Observer::make_par( eModBlck , chnl );
for( Index i = 0 ; i < tochange ; ++i )
mMCFB->i2p_ub( i + strt )->set_rhs( newcaps[ i ] , modpar );
mMCFB->close_channel( chnl );
}
else { // change via call to chg_* method
// in 50% of the cases a direct call, otherwise use the methods factory
//!! if( dis( rg ) <= 0.5 ) {
mMCFB->chg_ucaps( newcaps.begin() , Range( strt , stp ) );
LOG1( "ies(r) - " );
//!! }
//!! else {
//!! std::string mthd_name = "MCFBlock::chg_ucaps";
//!! const auto * mthd = Block::get_method_fs( mthd_name,
//!! Block::MS_dbl_rngd::args() );
//!! assert( mthd_name == Block::get_method_name_fs
//!! ( mthd , Block::MS_dbl_rngd::args() ) );
//!! std::invoke( *mthd , mMCFB , newcaps.begin() ,
//!! Range( strt , stp ) , eNoBlck , eNoBlck );
//!! LOG1( "ies(r-mf) - " );
//!! }
}
}
else {
Subset nms( GenerateRand( m , tochange ) );
auto ncit = newcaps.begin();
for( auto i : nms )
*(ncit++) = mcf->MCFUCap( i ) * rndfctr();
nms.push_back( OPTtypes_di_unipi_it::Inf< MCFClass::Index >() );
mcf->ChgUCaps( newcaps.data() , nms.data() );
nms.resize( tochange );
if( ( mode & 16u ) && ( dis( rg ) < 0.5 ) ) {
// change via abstract representation, sending to a new channel
LOG1( "ies(a,s) - " );
auto chnl = mMCFB->open_channel();
auto modpar = Observer::make_par( eModBlck , chnl );
for( Index i = 0 ; i < tochange ; ++i )
mMCFB->i2p_ub( nms[ i ] )->set_rhs( newcaps[ i ] , modpar );
mMCFB->close_channel( chnl );
}
else { // change via call to chg_* method
// in 50% of the cases a direct call, otherwise use the methods factory
//!! if( dis( rg ) <= 0.5 ) {
mMCFB->chg_ucaps( newcaps.begin() , std::move( nms ) , true );
LOG1( "ies(s) - " );
//!! }
//!! else {
//!! std::string mthd_name = "MCFBlock::chg_ucaps";
//!! const auto * mthd = Block::get_method_fs( mthd_name,
//!! Block::MS_dbl_sbst::args() );
//!! assert( mthd_name == Block::get_method_name_fs
//!! ( mthd , Block::MS_dbl_sbst::args() ) );
//!! std::invoke( *mthd , mMCFB , newcaps.begin() , std::move( nms ) ,
//!! true , eNoBlck , eNoBlck );
//!! LOG1( "ies(s-mf) - " );
//!! }
}
}
}
} // end( if( change capacities ) )
// change deficits- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 4u ) && ( dis( rg ) <= p_change ) ) {
LOG1( "2 deficits" );
Index posn = 0;
Index negn = 0;
FNumber posd = NAN;
FNumber negd = NAN;
if( nzdfct ) { // if there are nonzero deficits
MCFBlock::Vec_FNumber dfcts( n );
mcf->MCFDfcts( dfcts.data() );
do
posn = Index( dis( rg ) * n ); // select node with positive
while( dfcts[ posn ] <= 0 ); // deficit (one must exist)
posd = dfcts[ posn ];
do
negn = Index( dis( rg ) * n ); // select node with negative
while( dfcts[ negn ] >= 0 ); // deficit (one must exist)
negd = dfcts[ negn ];
}
else {
posn = Index( dis( rg ) * n ); // just select at random
negn = Index( dis( rg ) * n );
posd = negd = 0;
}
FNumber Dlt = u_avg * 2 * dis( rg );
if( dis( rg ) <= 0.5 ) { // in 50% of cases up, in 50% of cases down
posd += Dlt;
negd -= Dlt;
}
else {
Dlt = min( Dlt , max( max( posd , - negd ) / 2 , double( 1 ) ) );
posd -= Dlt;
negd += Dlt;
}
mcf->ChgDfct( posn , posd );
mcf->ChgDfct( negn , negd );
// pack the two Modification into a new channel
auto chnl = mMCFB->open_channel();
auto modpar = Observer::make_par( eModBlck , chnl );
if( ( mode & 16u ) && ( dis( rg ) < 0.5 ) ) {
// change via abstract representation
LOG1( "(a)" );
mMCFB->i2p_e( posn )->set_both( posd , modpar );
mMCFB->i2p_e( negn )->set_both( negd , modpar );
}
else { // change via call to chg_* method
// note that eModBlck makes no sense for a physical Modification,
// but MCFBlock is supposed to take care of this
mMCFB->chg_dfct( posd , posn , modpar , modpar );
mMCFB->chg_dfct( negd , negn , modpar , modpar );
}
mMCFB->close_channel( chnl );
LOG1( " - " );
} // end( change deficits )
// closing arcs- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 8u ) && ( dis( rg ) <= p_change ) ) {
Index changed = 0;
Subset nms( n_change );
for( auto i = mMCFB->get_NStaticArcs() ; i < mMCFB->get_NArcs() ; ++i ) {
if( mcf->IsDeletedArc( i ) )
continue;
if( mcf->IsClosedArc( i ) )
continue;
if( dis( rg ) <= 0.5 )
continue;
nms[ changed++ ] = i;
mcf->CloseArc( i );
if( changed >= n_change )
break;
}