-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
858 lines (716 loc) · 29.6 KB
/
Copy pathtest.cpp
File metadata and controls
858 lines (716 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*--------------------------------------------------------------------------*/
/*--------------------------- File test.cpp --------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing BinaryKnapsackBlock, comparing the results of all the
* Solvers attached to it: every exact Solver must agree on the optimal value,
* every relaxation Solver must bracket it (see CrossCheckSolvers() and batches/batch and batch-mixed
* for the cross-check of all the mathematically equivalent formulations).
*
* \author Federica Di Pasquale \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ MACROS ------------------------------------*/
/*--------------------------------------------------------------------------*/
#define STEP 3 // after modifications solve again at each multiple of STEP
#ifndef LOG_LEVEL
#define LOG_LEVEL 0
#endif
// 0 = only pass/fail
// 1 = list of modifications (and per-solve timings)
// 2 = also print verbose header about main configuration at start
#if( LOG_LEVEL > 0 )
#define LOG( x ) cout << x
#else
#define LOG( 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 "common_utils.h"
#include "BinaryKnapsackBlock.h"
#include <random>
#include <chrono>
#include <cstdlib>
#include <fstream>
/*--------------------------------------------------------------------------*/
/*------------------------------- USING ------------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace std;
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------- TYPES ------------------------------------*/
/*--------------------------------------------------------------------------*/
using Index = Block::Index;
using c_Index = Block::c_Index;
using Range = Block::Range;
using c_Range = Block::c_Range;
using Subset = Block::Subset;
using c_Subset = Block::c_Subset;
/*--------------------------------------------------------------------------*/
/*----------------------------- CONSTANTS ----------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------------------------------------------------*/
BinaryKnapsackBlock * BKB; // The Binary Knapsack Block
std::mt19937 rg; // random generator
std::uniform_real_distribution<> dis( 0.0 , 1.0 );
Index N = 100; // number of items
static constexpr Index rangeW = 100; // range values of weights
static constexpr double rangeP = 100; // range values of profits
// when have_ref is true (the Pisinger mode, see -C / run_pisinger()),
// CrossCheckSolvers additionally checks the optimum against the published
// reference value ref_opt
bool have_ref = false;
double ref_opt = 0;
/*--------------------------------------------------------------------------*/
/*----------------------------- FUNCTIONS ----------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// Generate a random Range of size m < N
Range generateRange( Index m )
{
Range rng;
rng.first = dis( rg ) * ( N - m );
rng.second = rng.first + m;
return( rng );
}
/*--------------------------------------------------------------------------*/
// Generate a random Subset of size m < N
Subset generateSubset( Index m )
{
Subset nms;
Subset idx( N );
iota( idx.begin() , idx.end() , 0 );
sample( idx.begin() , idx.end() , back_inserter( nms ) , m , rg );
return( nms );
}
/*--------------------------------------------------------------------------*/
bool CrossCheckSolvers( void )
{
// cross-check ALL the registered Solver via the shared common_utils engine:
// every exact one must agree on the optimal value z*, every relaxation
// Solver must bracket it, and (in the Pisinger mode, have_ref) z* must match
// the published optimum ref_opt. The engine also prints the uniform
// per-instance line with every Solver value; here we only add the
// BinaryKnapsackBlock-specific classifier (relaxation => [lb,ub] bracket) and
// the self-consistency check (each exact Solver's reported value equals the
// value recomputed from its returned solution)
const auto & reg = BKB->get_registered_solvers();
std::vector< Solver * > Solvers( reg.begin() , reg.end() );
const std::size_t M = Solvers.size();
if( M < 2 ) {
cerr << "Error: CrossCheckSolvers needs at least two registered Solver";
return( false );
}
// the relaxation brackets z* in [ lb , ub ]; any other Solver is an exact z*.
// The classifier is invoked by SolveAll() only for the Solver that found a
// solution, so we use it to record which ones to self-consistency-check below
// (skipping the infeasible ones, whose primal x cannot be read)
std::vector< char > feasible( M , 0 );
std::vector< char > bracket( M , 0 );
// the generic per-Solver-eps reading lives in common_utils; the eps below
// are positional with respect to the Solver order of BSPar.txt and
// BSPar-mixed.txt (which agree: 3rd = GreedyRelaxationBinaryKnapsackSolver,
// the relaxation, for which nothing beyond the base-contract bracket is
// claimed; every other Solver, the BranchAndXSolver variants included via
// the eps_getter() default, is exact up to the test tolerance). Here we
// only wrap it to record, per Solver, feasibility and whether it is a
// bracket (needed by the BinaryKnapsackBlock-specific self-consistency
// check below)
auto read = eps_getter( { 2e-06 , 2e-06 , Inf< double >() } );
SolverClassifier classify =
[ &feasible , &bracket , read ]( Solver * s , std::size_t k ) -> SolverReading {
feasible[ k ] = 1;
SolverReading r = read( s , k );
bracket[ k ] = ( r.kind == SolverReading::Kind::Bracket );
return( r );
};
const double ref = have_ref ? ref_opt
: std::numeric_limits< double >::quiet_NaN();
if( ! SolveAll( BKB , classify , ref , 2e-06 ) )
return( false );
// BinaryKnapsackBlock-specific self-consistency: every feasible exact Solver's
// reported value must equal the value recomputed from its returned solution
for( std::size_t k = 0 ; k < M ; ++k ) {
if( ! feasible[ k ] )
continue; // infeasible: no primal x to read
if( bracket[ k ] )
continue; // relaxations have no primal x
const double value = Solvers[ k ]->get_var_value();
Solvers[ k ]->get_var_solution();
double checksol = 0;
for( Index i = 0 ; i < N ; ++i )
checksol += BKB->get_x( i ) * BKB->get_Profit( i );
if( abs( checksol - value ) > 1e-06 * max( abs( value ) , 1.0 ) ) {
cerr << "Error: Solver " << k << " solution value " << checksol
<< " != its reported value " << value;
return( false );
}
}
return( true );
}
/*--------------------------------------------------------------------------*/
// a Pisinger benchmark instance read from a .csv (see read_pisinger())
struct PisingerInst {
std::string name;
Index N;
double C, z;
std::vector< double > W, P;
};
/*--------------------------------------------------------------------------*/
// read every instance of a Pisinger .csv, each a block "knapPI_... / n N /
// c C / z Z / time T / <idx,profit,weight,xstar> lines / -----"
static std::vector< PisingerInst > read_pisinger( const std::string & path )
{
std::vector< PisingerInst > out;
std::ifstream in( path );
std::string line;
while( std::getline( in , line ) ) {
if( line.compare( 0 , 6 , "knapPI" ) != 0 )
continue;
if( ! line.empty() && line.back() == '\r' )
line.pop_back();
PisingerInst pi;
pi.name = line;
unsigned nn = 0;
std::getline( in , line ); std::sscanf( line.c_str() , "n %u" , & nn );
std::getline( in , line ); std::sscanf( line.c_str() , "c %lf" , & pi.C );
std::getline( in , line ); std::sscanf( line.c_str() , "z %lf" , & pi.z );
std::getline( in , line ); // "time ..."
pi.N = nn; pi.W.resize( nn ); pi.P.resize( nn );
for( Index i = 0 ; i < pi.N ; ++i ) {
std::getline( in , line );
long idx; double p, w;
std::sscanf( line.c_str() , "%ld,%lf,%lf" , & idx , & p , & w );
pi.P[ i ] = p; pi.W[ i ] = w;
}
out.push_back( std::move( pi ) );
}
return( out );
}
/*--------------------------------------------------------------------------*/
// the Pisinger mode (-C <csv>): test every instance of the .csv with ALL the
// attached Solver (the CrossCheckSolvers() solver-vs-solver cross-check) AND against
// the published optimum z (solver-vs-reference), reusing one Block and one set
// of Solver across the whole class (the data of each instance is set with the
// chg_*() Modification, to which the Solver react)
static bool run_pisinger( const std::string & csv , const std::string & sconf )
{
auto insts = read_pisinger( csv );
if( insts.empty() ) {
cerr << "Error: no instance read from " << csv << endl;
return( false );
}
// build the Block from the first instance (Pisinger is pure 0-1: all integer)
BKB = new BinaryKnapsackBlock();
BKB->load( insts[ 0 ].N , insts[ 0 ].C ,
std::vector< double >( insts[ 0 ].W ) ,
std::vector< double >( insts[ 0 ].P ) );
BKB->generate_abstract_variables();
BKB->generate_abstract_constraints();
BKB->generate_objective();
Configuration * bsc = Configuration::deserialize( sconf );
if( ! bsc ) {
cerr << "Error: cannot load BSC from " << sconf << endl;
return( false );
}
s_config_Block( BKB , bsc , sconf );
if( BKB->get_registered_solvers().empty() ) {
cerr << "Error: BlockSolverConfig did not register any Solver" << endl;
return( false );
}
bool AllPassed = true;
have_ref = true;
for( std::size_t k = 0 ; k < insts.size() ; ++k ) {
N = insts[ k ].N;
BKB->chg_weights( insts[ k ].W.begin() ); // all the weights
BKB->chg_profits( insts[ k ].P.begin() ); // all the profits
BKB->chg_capacity( insts[ k ].C );
ref_opt = insts[ k ].z;
LOG( insts[ k ].name << ": " );
AllPassed &= CrossCheckSolvers();
}
have_ref = false;
if( AllPassed )
cout << GREEN( All tests passed!! ) << endl;
else
cout << RED( Errors happened!! ) << endl;
s_config_Block( BKB , bsc );
delete bsc;
delete BKB;
return( AllPassed );
}
/*--------------------------------------------------------------------------*/
// test-specific command-line knobs, set by process_specific_arg(); the
// standard parameter (-S BlockSolverConfig) is handled centrally by
// common_utils. This tester GENERATES its own BinaryKnapsackBlock from the
// seed, so it takes no instance positional (filename_optional = true).
// (N is declared as a global above.)
long int seed = 123123; // seed
Index wchg = 127; // what to change, coded bit-wise
Index n_repeat = 100; // number of repetitions
double delta = 0.01; // capacity parameter
double nW = 0.1; // percentage of negative weights
double nP = 0.1; // percentage of negative profits
double nI = 0.5; // percentage of integer variables
double nM = 0.2; // max percentage of items to modify
std::string pisinger_csv; // if set (-C), test these Pisinger instances vs z
/*--------------------------------------------------------------------------*/
static bool process_specific_arg( int opt )
{
switch( opt ) {
case( 'e' ): Str2Sthg( optarg , seed ); return( true );
case( 'k' ): Str2Sthg( optarg , wchg ); return( true );
case( 'N' ): Str2Sthg( optarg , N ); return( true );
case( 'n' ): Str2Sthg( optarg , n_repeat ); return( true );
case( 'd' ): Str2Sthg( optarg , delta ); return( true );
case( 'W' ): Str2Sthg( optarg , nW ); return( true );
case( 'P' ): Str2Sthg( optarg , nP ); return( true );
case( 'i' ): Str2Sthg( optarg , nI ); return( true );
case( 'M' ): Str2Sthg( optarg , nM ); return( true );
case( 'C' ): pisinger_csv = optarg; return( true );
default: return( false );
}
}
/*--------------------------------------------------------------------------*/
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 - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// the standard parameter (-S) is parsed by common_utils; the test only
// appends its own knobs and reads no instance file (it generates one)
// for small knapsacks nM * N may be too small (always 0 or 1 at most):
// minM is the minimum absolute number of items that can be modified
int minM = 10;
docopt_desc = "SMS++ BinaryKnapsackBlock test.\n";
filename_optional = true;
short_opts += "e:k:N:n:d:W:P:i:M:C:";
const std::vector< option > my_opts = {
{ "seed" , required_argument , nullptr , 'e' } ,
{ "wchg" , required_argument , nullptr , 'k' } ,
{ "nvar" , required_argument , nullptr , 'N' } ,
{ "rounds" , required_argument , nullptr , 'n' } ,
{ "delta" , required_argument , nullptr , 'd' } ,
{ "nW" , required_argument , nullptr , 'W' } ,
{ "nP" , required_argument , nullptr , 'P' } ,
{ "nI" , required_argument , nullptr , 'i' } ,
{ "nM" , required_argument , nullptr , 'M' } ,
{ "csv" , required_argument , nullptr , 'C' } };
long_opts.insert( std::prev( long_opts.end() ) ,
my_opts.begin() , my_opts.end() );
help += " -e, --seed <n> pseudo-random generator seed\n"
" -k, --wchg <bits> what to change, bit-wise [127]:\n"
" 1 sense, 2 capacity, 4 profits,\n"
" 8 weights, 16 fix, 32 unfix,\n"
" 64 integrality\n"
" -N, --nvar <n> number of variables [100]\n"
" -n, --rounds <n> number of repetitions [100]\n"
" -d, --delta <x> capacity parameter [0.01]\n"
" -W, --nW <x> fraction of negative weights "
"[0.1]\n"
" -P, --nP <x> fraction of negative profits "
"[0.1]\n"
" -i, --nI <x> fraction of integer variables "
"[0.5]\n"
" -M, --nM <x> max fraction of items to modify "
"[0.2]\n"
" -C, --csv <file> Pisinger .csv: test its instances "
"against their published optimum z\n";
process_args( argc , argv , process_specific_arg );
// the BlockSolverConfig (-S) must be provided explicitly: the test never
// falls back to a hardcoded default Configuration
require_solver_config();
// Pisinger mode: instead of generating a random instance and mutating it,
// run all the attached Solver on every instance of the given .csv, checking
// they agree with one another AND with the published optimum (see
// run_pisinger())
if( ! pisinger_csv.empty() )
return( run_pisinger( pisinger_csv , sconf_file ) ? 0 : 1 );
// sanity checks - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( delta < 0 ) || ( delta > 1 ) ) {
cerr << "error: delta must be in [ 0 , 1 ]" << endl;
exit( 1 );
}
if( ( nW < 0 ) || ( nW > 1 ) ) {
cerr << "error: nW must be in [ 0 , 1 ]" << endl;
exit( 1 );
}
if( ( nP < 0 ) || ( nP > 1 ) ) {
cerr << "error: nP must be in [ 0 , 1 ]" << endl;
exit( 1 );
}
if( ( nI < 0 ) || ( nI > 1 ) ) {
cerr << "error: nI must be in [ 0 , 1 ]" << endl;
exit( 1 );
}
if( ( nM < 0 ) || ( nM > 1 ) ) {
cerr << "error: nI must be in [ 0 , 1 ]" << endl;
exit( 1 );
}
const int minW = - int( nW * rangeW );
const int maxW = minW + rangeW;
const double minP = - nP * rangeP;
const double maxP = minP + rangeP;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// seed the pseudo-random number generator
rg.seed( seed );
// print verbose header- - - - - - - - - - - - - - - - - - - - - - - - - - -
#if( LOG_LEVEL > 1 )
cout << "seed = " << seed << " ~ N = " << N << " ~ n_repeat " << n_repeat
<< " ~ delta = " << delta << endl;
cout << "W in [ " << minW << " , " << maxW << " ] ~ P in [ " << minP
<< " , " << maxP << " ] ~ nI = " << nI << endl;
cout << endl << "Modifications: " << endl;
if( wchg & 1 )
cout << " - Objective Sense" << endl;
if( wchg & 2 )
cout << " - Capacity" << endl;
if( wchg & 4 )
cout << " - Profits" << endl;
if( wchg & 8 )
cout << " - Weights" << endl;
if( wchg & 16 )
cout << " - Fix" << endl;
if( wchg & 32 )
cout << " - Unfix" << endl;
if( wchg & 64 )
cout << " - Integrality";
cout << endl << endl;
#endif
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// create the BinaryKnapsackBlock- - - - - - - - - - - - - - - - - - - - - -
BKB = new BinaryKnapsackBlock();
// generate instance - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// generate weights from a uniform int distribution
uniform_int_distribution<> dist_W( minW , maxW );
// generate integrality from a uniform int distribution
uniform_int_distribution<> dist_I( 0 , 1 );
// generate profits from a uniform real distribution
uniform_real_distribution<> dist_P( minP , maxP );
vector< double > W( N ); // vector of weights
vector< double > P( N ); // vector of profits
vector< bool > I( N ); // vector of integrality
double C; // Capacity of the Knapsack
int totWp = 0; // total sum of the positive weights
int totWn = 0; // total sum of the negative weights
for( Index i = 0 ; i < N ; i++ ) {
W[ i ] = dist_W( rg );
P[ i ] = dist_P( rg );
I[ i ] = ( dist_I( rg ) < nI );
if( W[ i ] > 0 ) // update totWn and totWp
totWp += W[ i ];
else
totWn += W[ i ];
}
// generate the Capacity from a uniform real distribution
uniform_real_distribution<> dist_C( totWn ,
totWn + delta * ( totWp - totWn ) );
C = dist_C( rg );
// load the Binary Knapsack instance- - - - - - - - - - - - - - - - - - - -
if( nI < 1 )
BKB->load( N , C , std::move( W ) , std::move( P ) , std::move( I ) );
else
BKB->load( N , C , std::move( W ) , std::move( P ) );
// build the abstract representation (Objective + Constraint) up front, so
// that the test works also with solver configurations that do not trigger it
// themselves (e.g. the pure-DP benchmark config with no :MILPSolver); the
// generate_abstract_*() are idempotent, hence a no-op when already built
BKB->generate_abstract_variables();
BKB->generate_abstract_constraints();
BKB->generate_objective();
// attach two Solver to the BinaryKnapsackBlock- - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// do it by using a single a BlockSolverConfig, read from file
// BSC may be a plain BlockSolverConfig or a meta-config
// SimpleConfiguration< std::map< std::string , Configuration * > >;
// s_config_Block() dispatches on the runtime type and clears the config(s)
// for final cleanup.
Configuration * bsc = Configuration::deserialize( sconf_file );
if( ! bsc ) {
cerr << "Error: cannot load BSC from " << sconf_file << endl;
exit( 1 );
}
s_config_Block( BKB , bsc , sconf_file );
// check Solvers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( BKB->get_registered_solvers().empty() ) {
cerr << "Error: BlockSolverConfig did not register any Solver" << endl;
exit( 1 );
}
// get Objective and Constraint- - - - - - - - - - - - - - - - - - - - - - -
auto obj = BKB->get_objective< FRealObjective >();
auto cnst = BKB->get_static_constraint< FRowConstraint >( 0 );
// get the corresponding linear functions
auto lfobj = dynamic_cast< LinearFunction * >( obj->get_function() );
if( ! lfobj ) {
cerr << "Error: cannot get the Objective LinearFunction" << endl;
exit( 1 );
}
auto lfcnst = dynamic_cast< LinearFunction * >( cnst->get_function() );
if( ! lfcnst ) {
cerr << "Error: cannot get the Constraint LinearFunction" << endl;
exit( 1 );
}
// first call- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
LOG( "0: " );
bool AllPassed = CrossCheckSolvers();
// modifications loop- - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for( Index i = 1 ; i <= n_repeat * STEP ; ++i ) {
LOG( endl << i << ": " );
// change the sense of the objective - - - - - - - - - - - - - - - - - - -
if( ( wchg & 1 ) && ( dis( rg ) < 0.3 ) ) {
LOG( "sense" );
if( dis( rg ) < 0.5 )
BKB->set_objective_sense( 1 - BKB->get_objective_sense() ); // PR
else {
obj->set_sense( 1 - BKB->get_objective_sense() ); // AR
LOG( "(A)" );
}
LOG( " ~ " );
}
// change the Capacity of the Knapsack- - - - - - - - - - - - - - - - - - -
if( wchg & 2 && dis( rg ) < 0.3 ) {
LOG( "C" );
C = dist_C( rg );
if( dis( rg ) < 0.5 )
BKB->chg_capacity( C ); // PR
else {
cnst->set_rhs( C ); // AR
LOG( "(A)" );
}
LOG( " ~ " );
}
// change Profits (range or subset) - - - - - - - - - - - - - - - - - - - -
if( wchg & 4 && dis( rg ) < 0.3 ) {
Index m = dis( rg ) * max( int( nM * N ) , minM ); // n. of items to modify
m = min( m , N );
if( m ) {
LOG( "P" );
vector< double > nP( m ); // generate new profits
for( auto & p : nP )
p = dist_P( rg );
if( dis( rg ) < 0.5 ) { // ranged modification
Range rng = generateRange( m );
if( dis( rg ) < 0.5 ) {
BKB->chg_profits( nP.begin() , rng ); // PR
LOG( "(R)" );
}
else {
lfobj->modify_coefficients( std::move( nP ) , rng ); // AR
LOG( "(AR)" );
}
}
else { // or subset modification
Subset nms = generateSubset( m );
if( dis( rg ) < 0.5 ) {
BKB->chg_profits( nP.begin() , std::move( nms ) ); // PR
LOG( "(S)" );
}
else {
lfobj->modify_coefficients( std::move( nP ) , std::move( nms ) ); // AR
LOG( "(AS)" );
}
}
LOG( " ~ " );
}
}
// change Weights (range or subset) - - - - - - - - - - - - - - - - - - - -
if( wchg & 8 && dis( rg ) < 0.3 ) {
Index m = dis( rg ) * max( int( nM * N ) , minM ); // n. of items to modify
m = min( m , N );
if( m ) {
LOG( "W" );
vector< double > nW( m ); // generate new weights
for( auto & w : nW )
w = dist_W( rg );
if( dis( rg ) < 0.5 ) { // ranged modification
Range rng = generateRange( m );
if( dis( rg ) < 0.5 ) {
BKB->chg_weights( nW.begin() , rng ); // PR
LOG( "(R)" );
}
else {
lfcnst->modify_coefficients( std::move( nW ) , rng ); // AR
LOG( "(AR)" );
}
}
else { // or subset modification
Subset nms = generateSubset( m );
if( dis( rg ) < 0.5 ) {
BKB->chg_weights( nW.begin() , std::move( nms ) ); // PR
LOG( "(S)" );
}
else {
lfcnst->modify_coefficients( std::move( nW ) , std::move( nms ) ); // AR
LOG( "(AS)" );
}
}
LOG( " ~ " );
}
}
// Fix (range or subset)- - - - - - - - - - - - - - - - - - - - - - - - - -
if( wchg & 16 && dis( rg ) < 0.3 ) {
Index m = dis( rg ) * max( int( nM * N ) , minM ); // n. of items to modify
m = min( m , N );
if( m ) {
LOG( "F" );
vector< bool > nX( m );
for( Index i = 0 ; i < m ; i++ ) // generate new x values
nX[ i ] = ( dis( rg ) < 0.5 ) ? false : true;
auto nXit = nX.begin();
if( dis( rg ) < 0.5 ) { // ranged modification
Range rng = generateRange( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->fix_x( nXit , rng );
LOG( "(R)" );
}
else { // AR
for( Index j = rng.first ; j < rng.second ; j++ ) {
auto x = BKB->get_Var( j );
if( ! x->is_fixed() ) {
x->set_value( *nXit++ );
x->is_fixed( true );
}
}
LOG( "(AR)" );
}
}
else { // or subset modification
Subset nms = generateSubset( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->fix_x( nXit , std::move( nms ) );
LOG( "(S)" );
}
else { // AR
for( auto j : nms ) {
auto x = BKB->get_Var( j );
if( ! x->is_fixed() ) {
x->set_value( *nXit++ );
x->is_fixed( true );
}
}
LOG( "(AS)" );
}
}
LOG( " ~ " );
}
}
// Unfix (range or subset)- - - - - - - - - - - - - - - - - - - - - - - - -
if( wchg & 32 && dis( rg ) < 0.3 ) {
Index m = dis( rg ) * max( int( nM * N ) , minM ); // n. of items to modify
m = min( m , N );
if( m ) {
LOG( "U" );
if( dis( rg ) < 0.5 ) { // ranged modification
Range rng = generateRange( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->unfix_x( rng );
LOG( "(R)" );
}
else { // AR
for( Index j = rng.first ; j < rng.second ; j++ )
BKB->get_Var( j )->is_fixed( false );
LOG( "(AR)" );
}
}
else { // or subset modification
Subset nms = generateSubset( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->unfix_x( std::move( nms ) );
LOG( "(S)" );
}
else { // AR
for( auto j : nms )
BKB->get_Var( j )->is_fixed( false );
LOG( "(AS)" );
}
}
LOG( " ~ " );
}
}
// change Integrality (range or subset) - - - - - - - - - - - - - - - - - -
if( wchg & 64 && dis( rg ) < 0.3 ) {
Index m = dis( rg ) * max( int( nM * N ) , minM ); // n. of items to modify
m = min( m , N );
if( m ) {
LOG( "I" );
vector< bool > nI( m ); // generate new integrality vector
for( Index i = 0 ; i < m ; i++ )
nI[ i ] = ( dist_I( rg ) <= 0.5 );
if( dis( rg ) < 0.5 ) { // ranged modification
Range rng = generateRange( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->chg_integrality( nI.begin() , rng );
LOG( "(R)" );
}
else { // AR
auto nIit = nI.begin();
for( Index j = rng.first ; j < rng.second ; ++j )
BKB->get_Var( j )->set_type( *(nIit++) ? ColVariable::kBinary
: ColVariable::kPosUnitary );
LOG( "(AR)" );
}
}
else { // or subset modification
Subset nms = generateSubset( m );
if( dis( rg ) < 0.5 ) { // PR
BKB->chg_integrality( nI.begin() , std::move( nms ) );
LOG( "(S)" );
}
else { // AR
auto nIit = nI.begin();
for( auto j : nms )
BKB->get_Var( j )->set_type( *(nIit++) ? ColVariable::kBinary
: ColVariable::kPosUnitary );
LOG( "(AS)" );
}
}
LOG( " ~ " );
}
}
// finally, re-solve - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ! ( i % STEP ) )
AllPassed &= CrossCheckSolvers();
} // end( main loop ) - - - - - - - - - - - - - - - - - - - - - - - - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
LOG( endl );
if( AllPassed )
cout << GREEN( All tests passed!! ) << endl;
else
cout << RED( Errors happened!! ) << endl;
// final cleanup - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
s_config_Block( BKB , bsc ); // remove the Solver by re-apply()-ing the
// clear()-ed bsc (or meta-config)
delete( bsc ); // delete the BlockSolverConfig
delete( BKB ); // delete the BinaryKnapsackBlock
// all done- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return( AllPassed ? 0 : 1 );
} // end( main )
/*--------------------------------------------------------------------------*/
/*------------------------ End File test.cpp -------------------------------*/
/*--------------------------------------------------------------------------*/