-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
567 lines (449 loc) · 18.9 KB
/
Copy pathtest.cpp
File metadata and controls
567 lines (449 loc) · 18.9 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
/*--------------------------------------------------------------------------*/
/*-------------------------- File test.cpp ---------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing LagrangianDualSolver with MMCFBlock
*
* An MMCFBlock instance is loaded from a text file, two different Solver are
* registered to the MMCFBlock, the second of which is assumed to be a
* LagrangianDualSolver, the MMCFBlock is solved by the Solver and the
* results are compared.
*
* The tester has some parts for the future extension when the MMCFBlock is
* repeatedly randomly modified and re-solved several times, but this is not
* done yet.
*
* \author Francesca Demelas \n
* Laboratoire d'Informatique de Paris Nord \n
* Universite' Sorbonne Paris Nord \n
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Antonio Frangioni
*/
/*--------------------------------------------------------------------------*/
/*-------------------------------- MACROS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#define LOG_LEVEL 0
// 0 = only pass/fail
// 1 = result of each test
// 2 = + solver log
// 3 = + save LP file
#if( LOG_LEVEL >= 1 )
#define LOG1( x ) cout << x
#define CLOG1( y , x ) if( y ) cout << x
#if( LOG_LEVEL >= 2 )
#define LOG_ON_COUT 0
// if nonzero, the 2nd Solver (LagrangianDualSolver) log is sent on cout
// rather than on a file( bsc->get_SolverName( i ) == "BundleSolver" )
#endif
#else
#define LOG1( x )
#define CLOG1( y , x )
#endif
/*--------------------------------------------------------------------------*/
// if nonzero, the 1st Solver attached to the UCBlock is detached
// and re-attached to it at all iterations
#define DETACH_1ST 0
// if nonzero, the 2nd Solver attached to the UCBlock is detached and
// re-attached to it at all iterations
#define DETACH_2ND 0
/*--------------------------------------------------------------------------*/
// if nonzero, the two Block are not solved at every round of changes, but
// only every SKIP_BEAT + 1 rounds. this allows changes to accumulate, and
// therefore puts more pressure on the Modification handling of the Solver
// (in case this tries to do "smart" things rather than dumbly processing
// each one in turn)
//
// note that the number of rounds of changes is them multiplied by
// SKIP_BEAT + 1, so that the input parameter still dictates the number of
// Block solutions
#define SKIP_BEAT 0
/*--------------------------------------------------------------------------*/
#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 <sstream>
#include <iomanip>
#include <chrono>
#include <random>
#include "common_utils.h"
#include "CDASolver.h"
#include "LagrangianDualSolver.h"
#include "MMCFBlock.h"
//!!#include "MILPSolver.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- 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;
using FunctionValue = Function::FunctionValue;
/*--------------------------------------------------------------------------*/
/*------------------------------- CONSTANTS --------------------------------*/
/*--------------------------------------------------------------------------*/
const char *const logF = "log.txt";
const FunctionValue INF = SMSpp_di_unipi_it::Inf< FunctionValue >();
/*--------------------------------------------------------------------------*/
/*------------------------------- GLOBALS ----------------------------------*/
/*--------------------------------------------------------------------------*/
MMCFBlock * TestBlock; // the [MMCF]Block that is solved
int wprnt = 0;
std::mt19937 rg; // base random generator
std::uniform_real_distribution<> dis( 0.0 , 1.0 );
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
static double rndfctr( void )
{
// return a random number between 0.5 and 2, with 50% probability of being
// < 1
double fctr = dis( rg ) - 0.5;
return( fctr < 0 ? - fctr : fctr * 4 );
}
------------------------------------------------------------------------------
static Subset GenerateRand( Index m , Index k )
{
// generate a sorted random k-vector of unique integers in 0 ... m - 1
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( std::move( rnd ) );
}
----------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
static void PrintSol( CDASolver * slvr , bool first ,
double time , double objFunc )
{
if( ! wprnt )
return;
std::string name( filename );
name = name.substr( name.find_last_of( "/" ) + 1 , name.length() );
name = name.substr( 0 , name.find( "." ) );
if( first )
name.append( "_1" );
else
name.append( "_2" );
// start: reduced costs extraction
if( wprnt & 1 ) {
// for the second Solver only, read a Configuration for
// get_dual_solution( ) from file
Configuration * cfg = nullptr;
if( ! first )
cfg = Configuration::deserialize( "GetDualSolConfig.txt" );
slvr->get_dual_solution( cfg );
delete cfg;
ofstream dualFile( "./redCosts/" + name + "-Dual.txt" );
for( Index k = 0 ; k < TestBlock->get_NComm() ; ++k ) {
for( Index i = 0 ; i < TestBlock->get_NNodes() ; ++i )
dualFile << TestBlock->get_potential( k , i ) << " ";
dualFile << "\n";
}
dualFile.close();
}
// primal solution extraction
if( wprnt & 2 ) {
slvr->get_var_solution();
ofstream primalFile( "./primals/" + name + "-Prim.txt" );
for( Index k = 0 ; k < TestBlock->get_NComm() ; ++k ) {
for( Index i = 0 ; i < TestBlock->get_NArcs() ; ++i )
primalFile << TestBlock->get_flow( k , i ) << " ";
primalFile << "\n";
}
primalFile.close();
}
if( wprnt & 4 ) {
ofstream timeFile( "./times/" + name + "Sol-time.dat" );
timeFile << time;
timeFile << "\n";
timeFile << objFunc <<"\n";
timeFile.close();
}
}
/*--------------------------------------------------------------------------*/
static bool SolveBoth( void )
{
// own solve loop (kept for the per-Solver PrintSol() side output and the
// CDASolver check), then the shared common_utils cross_check + uniform line.
// Every Solver is an exact optimum read via get_var_value(); they must agree
// within 2e-7. The DETACH_* re-ordering is applied up front.
try {
#if DETACH_1ST
{ auto s = TestBlock->get_registered_solvers().front();
TestBlock->unregister_Solver( s );
TestBlock->register_Solver( s , true ); } // push it to the front
#endif
#if DETACH_2ND
{ auto s = TestBlock->get_registered_solvers().back();
TestBlock->unregister_Solver( s );
TestBlock->register_Solver( s ); } // push it to the back
#endif
const auto & reg = TestBlock->get_registered_solvers();
std::vector< Solver * > S( reg.begin() , reg.end() );
const std::size_t M = S.size();
std::vector< int > status( M );
std::vector< double > times( M );
std::vector< bool > hs( M );
std::vector< SolverReading > rd( M );
std::vector< std::string > tok( M );
for( std::size_t k = 0 ; k < M ; ++k ) {
auto Slvr = dynamic_cast< CDASolver * >( S[ k ] );
if( ! Slvr ) {
cout << "Error! Solver registred to TestBlock not a CDASolver";
exit( 1 );
}
#if( LOG_LEVEL >= 3 )
if( k == 0 )
Slvr->set_par( MILPSolver::strOutputFile , "LPBlock-CPXMILP.lp" );
#endif
auto start = std::chrono::system_clock::now();
status[ k ] = Slvr->compute( false );
auto end = std::chrono::system_clock::now();
times[ k ] = std::chrono::duration< double >( end - start ).count();
hs[ k ] = ( ( ( status[ k ] >= Solver::kOK )
&& ( status[ k ] < Solver::kError )
&& ( status[ k ] != Solver::kUnbounded )
&& ( status[ k ] != Solver::kInfeasible ) )
|| ( status[ k ] == Solver::kLowPrecision ) );
double fo = hs[ k ] ? Slvr->get_var_value() : -INF;
PrintSol( Slvr , k == 0 , times[ k ] , fo );
if( hs[ k ] ) {
rd[ k ] = SolverReading::exact( fo , eps_of( k , Slvr ) );
tok[ k ] = reading_token( rd[ k ] );
}
else if( status[ k ] == Solver::kInfeasible ) tok[ k ] = "Unfeas";
else if( status[ k ] == Solver::kUnbounded ) tok[ k ] = "Unbounded";
else tok[ k ] = "Error!";
}
std::string verdict;
double diff;
bool ok = cross_check( rd , hs , status ,
std::numeric_limits< double >::quiet_NaN() ,
2e-7 , verdict , diff );
print_instance_line( times , tok ,
std::numeric_limits< double >::quiet_NaN() ,
verdict , diff );
return( ok );
}
catch( exception &e ) {
cerr << e.what() << endl;
exit( 1 );
}
catch(...) {
cerr << "Error: unknown exception thrown" << endl;
exit( 1 );
}
}
/*--------------------------------------------------------------------------*/
// test-specific command-line knobs, set by process_specific_arg(); the
// standard parameters (instance positional, -B BlockConfig, -S
// BlockSolverConfig, -c/-p prefixes) are handled centrally by common_utils
char filetype = 's'; // type of the input file
// number of times the instance is (re-)solved; 0 means "not set on the
// command line", in which case the value is taken from the inner Solver's
// intNTrainRounds parameter (if it has one) and otherwise defaults to 1. A
// BundleSolverML configured with intMLTrainOnline trains once per re-solve.
int n_epochs = 0;
/*--------------------------------------------------------------------------*/
static bool process_specific_arg( int opt )
{
switch( opt ) {
case( 't' ): filetype = optarg[ 0 ]; return( true );
case( 'w' ): Str2Sthg( optarg , wprnt ); return( true );
case( 'e' ): Str2Sthg( optarg , n_epochs ); 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 - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// standard params (instance positional + -B + -S) are parsed by
// common_utils; the test only appends its own knobs
assert( SKIP_BEAT >= 0 );
docopt_desc = "SMS++ LagrangianDualSolver-on-MMCFBlock test.\n";
short_opts += "t:w:e:";
const std::vector< option > my_opts = {
{ "type" , required_argument , nullptr , 't' } ,
{ "wprnt" , required_argument , nullptr , 'w' } ,
{ "epochs" , required_argument , nullptr , 'e' } };
long_opts.insert( std::prev( long_opts.end() ) ,
my_opts.begin() , my_opts.end() );
help += " -t, --type <c> input file type "
"(s*,c,p,o,d,u,m) [s]\n"
" -w, --wprnt <bits> what to print to file, bit-wise "
"[0]:\n"
" 1 duals, 2 primal, "
"4 time & objective value\n"
" -e, --epochs <n> times the instance is re-solved "
"(training rounds);\n"
" overrides the inner Solver's "
"intNTrainRounds [1]\n";
process_args( argc , argv , process_specific_arg );
// both the BlockConfig (-B) and the BlockSolverConfig (-S) must be provided
// explicitly: the test never falls back to a hardcoded default Configuration
require_block_config();
require_solver_config();
// read the Block- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TestBlock = new MMCFBlock;
TestBlock->load( filename , filetype );
TestBlock->PreProcess();
// BC may be a plain BlockConfig or a meta-config
// SimpleConfiguration< std::map< std::string , Configuration * > >;
// b_config_Block() dispatches on the runtime type.
Configuration * cfg = Configuration::deserialize( bconf_file );
if( ! cfg ) {
cerr << "Error: cannot load BC from " << bconf_file << endl;
exit( 1 );
}
b_config_Block( TestBlock , cfg , bconf_file );
delete( cfg );
TestBlock->generate_abstract_variables();
// attach the Solver(s) to the Block - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// do this by reading an appropriate BlockSolverConfig from file and
// apply() it to the TestBlock; note that the BlockSolverConfig is
// clear()-ed and kept to do the cleanup at the end.
// 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( TestBlock , bsc , sconf_file );
if( TestBlock->get_registered_solvers().empty() ) {
cout << endl << "no Solver registered to the Block!" << endl;
exit( 1 );
}
// open log-file - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#if( LOG_LEVEL >= 2 )
#if( LOG_ON_COUT )
( ( TestBlock->get_registered_solvers() ).back() )->set_log( &cout );
#else
ofstream LOGFile( logF , ofstream::out );
if( ! LOGFile.is_open() )
cerr << "Warning: cannot open log file """ << logF << """" << endl;
else {
LOGFile.setf( ios::scientific, ios::floatfield );
LOGFile << setprecision( 10 );
( ( TestBlock->get_registered_solvers() ).back() )->set_log( &LOGFile );
}
#endif
#endif
// solver call(s) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// the instance is solved n_rounds times: this is a no-op repetition for a
// plain Solver, but it drives online training when the inner Solver is a
// BundleSolverML with intMLTrainOnline set. The count comes from -e if given,
// else from the inner Solver's intNTrainRounds parameter, else 1.
int n_rounds = 1;
if( n_epochs > 0 )
n_rounds = n_epochs;
else if( auto * lds = dynamic_cast< LagrangianDualSolver * >(
TestBlock->get_registered_solvers().back() ) )
if( Solver * inner = lds->get_inner_Solver() ) {
const auto idx = inner->int_par_str2idx( "intNTrainRounds" );
if( idx < inner->get_num_int_par() )
n_rounds = inner->get_int_par( idx );
}
bool AllPassed = false;
for( int r = 0 ; r < n_rounds ; ++r ) {
CLOG1( n_rounds > 1 , "[round " << r << "] " );
AllPassed = SolveBoth();
}
// main loop - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// now, for n_repeat times:
// - up to n_change ... are ...
// - up to n_change ... are ...
// - up to n_change ... are ...
// - up to n_change ... are ...
//
// then the TestBlock is re-solved with both Solver
/*!!
for( Index rep = 0 ; rep < n_repeat * ( SKIP_BEAT + 1 ) ; ) {
LOG1( rep << ": ");
// do stuff 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 1 ) && ( dis( rg ) <= p_change ) )
if( Index tochange = Index( dis( rg ) * n_change ) ) {
LOG1( "... " << tochange << " ... - " );
}
// do stuff 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( ( wchg & 2 ) && ( dis( rg ) <= p_change ) )
if( Index tochange = min( m - 1 , Index( dis( rg ) * n_change ) ) ) {
LOG1( "... " << tochange << " ..." );
if( dis( rg ) <= 0.5 ) { // in 50% of the cases do a ranged change
LOG1( "(r) - " );
}
else { // in the other 50% of the cases, do a sparse change
LOG1( "(s) - " );
Subset nms( GenerateRand( m , tochange ) );
}
}
// ...
// if verbose, print out stuff- - - - - - - - - - - - - - - - - - - - - - -
#if( LOG_LEVEL >= 3 )
( ( LPBlock->get_registered_solvers() ).front() )->set_par(
MILPSolver::strOutputFile , "LPBlock-" +
std::to_string( rep ) + ".lp" );
#endif
// finally, re-solve the problems- - - - - - - - - - - - - - - - - - - - -
// ... every SKIP_BEAT + 1 rounds
if( ! ( ++rep % ( SKIP_BEAT + 1 ) ) )
AllPassed &= SolveBoth();
#if( LOG_LEVEL >= 1 )
else
cout << endl;
#endif
} // end( main loop )- - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!!*/
if( AllPassed )
cout << GREEN( All tests passed!! ) << endl;
else
cout << RED( Shit happened!! ) << endl;
// destroy the Block - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// apply() the clear()-ed BlockSolverConfig (or meta-config) to cleanup Solver
s_config_Block( TestBlock , bsc );
// then delete the BlockSolverConfig
delete( bsc );
// finally the AbstractBlock can be deleted
delete( TestBlock );
// terminate - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return( AllPassed ? 0 : 1 );
} // end( main )
/*--------------------------------------------------------------------------*/
/*------------------------ End File test.cpp -------------------------------*/
/*--------------------------------------------------------------------------*/