-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpips_pars.cpp
More file actions
868 lines (733 loc) · 26 KB
/
Copy pathpips_pars.cpp
File metadata and controls
868 lines (733 loc) · 26 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
/*--------------------------------------------------------------------------*/
/*---------------------------- File pips_pars.cpp --------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Small tool for parsing the standard option maps that enable the support for
* plain PIPS-IPM++ parameters into the SMS++ PIPS-IPM++ solver class.
*
* The tool generates two files, <prefix>_defs.h and <prefix>_maps.h in the
* specified path. The path should be the include directory of the MILPSolver
* source tree.
*
* PIPS-IPM++ exposes no C API to enumerate options; this generator parses
* the option-registration assignments in PIPS-IPM/Core/Options, and bool
* options are mapped to SMS++ int parameters.
*
* \author Enrico Calandrini \n
* Dipartimento di Matematica \n
* Universita' di Pisa \n
*
* \copyright © by Enrico Calandrini
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <stdexcept>
#include <string>
#include <sys/stat.h>
#include <vector>
#include <getopt.h>
/*--------------------------------------------------------------------------*/
bool verbose = false; ///< If the tool should be verbose
std::string path{}; ///< Path for output files
std::string source_path{}; ///< Path to PIPS-IPM/Core/Options
std::string exe{}; ///< Name of the executable file
std::string docopt_desc{}; ///< Tool description
std::string solver_class = "PIPSMILPSolver";
std::string solver_header = "PIPSMILPSolver.h";
std::string prefix = "PIPS";
/*--------------------------------------------------------------------------*/
/// Gets the name of the executable from its full path
std::string get_filename( const std::string & fullpath )
{
std::size_t found = fullpath.find_last_of( "/\\" );
return( fullpath.substr( found + 1 ) );
}
/*--------------------------------------------------------------------------*/
/// Checks whether a regular file exists
bool file_exists( const std::string & filename )
{
struct stat buffer;
return( stat( filename.c_str() , &buffer ) == 0 &&
S_ISREG( buffer.st_mode ) );
}
/*--------------------------------------------------------------------------*/
/// Checks whether a directory exists
bool dir_exists( const std::string & dirname )
{
struct stat buffer;
return( stat( dirname.c_str() , &buffer ) == 0 && S_ISDIR( buffer.st_mode ) );
}
/*--------------------------------------------------------------------------*/
/// Removes one trailing slash, if present
std::string strip_trailing_slash( const std::string & p )
{
if( p.size() > 1 && ( p.back() == '/' || p.back() == '\\' ) )
return( p.substr( 0 , p.size() - 1 ) );
return( p );
}
/*--------------------------------------------------------------------------*/
/// Returns the basename of a path
std::string basename( const std::string & p )
{
std::string pp = strip_trailing_slash( p );
const std::size_t found = pp.find_last_of( "/\\" );
if( found == std::string::npos )
return( pp );
return( pp.substr( found + 1 ) );
}
/*--------------------------------------------------------------------------*/
/// Adds a non-empty path to a candidate list
void add_candidate( std::vector< std::string > & candidates ,
const std::string & candidate )
{
if( ! candidate.empty() )
candidates.push_back( candidate );
}
/*--------------------------------------------------------------------------*/
/// Adds an environment variable value to a candidate list, if it is defined
void add_env_candidate( std::vector< std::string > & candidates ,
const char * env )
{
const char * value = std::getenv( env );
if( value && *value )
add_candidate( candidates , value );
}
/*--------------------------------------------------------------------------*/
/// Finds the PIPS-IPM/Core/Options directory from a user-supplied path
std::string normalize_source_path( const std::string & p )
{
const std::string pp = strip_trailing_slash( p );
if( file_exists( pp + "/PIPSIPMppOptions.C" ) )
return( pp );
if( file_exists( pp + "/Core/Options/PIPSIPMppOptions.C" ) )
return( pp + "/Core/Options" );
if( file_exists( pp + "/PIPS-IPM/Core/Options/PIPSIPMppOptions.C" ) )
return( pp + "/PIPS-IPM/Core/Options" );
throw std::runtime_error( "Could not locate PIPS-IPM/Core/Options from "
"path: " + p );
}
/*--------------------------------------------------------------------------*/
/// Finds the PIPS-IPM/Core/Options directory automatically
std::string find_source_path()
{
std::vector< std::string > candidates;
add_env_candidate( candidates , "PIPSIPMPP_ROOT" );
add_env_candidate( candidates , "PIPS_ROOT" );
add_env_candidate( candidates , "PIPSIPM_ROOT" );
for( const auto & c : candidates ) {
try {
return( normalize_source_path( c ) );
}
catch( const std::exception & ) {}
}
throw std::runtime_error(
"Could not automatically locate PIPS-IPM/Core/Options. "
"Use -s or --source to specify it." );
}
/*--------------------------------------------------------------------------*/
/// Prints the tool description and usage
void docopt()
{
// http://docopt.org
std::cout << docopt_desc << std::endl;
std::cout << "Usage:\n"
<< " " << exe << " [-v] [-s <source>] [-c <class>] [-i <header>]"
<< " [-p <prefix>] <path>\n"
<< " " << exe << " -h | --help\n"
<< std::endl
<< "Options:\n"
<< " -v, --verbose Make the tool verbose.\n"
<< " -s, --source <source> Path to PIPS-IPM/Core/Options, to "
"PIPS-IPM,"
<< " or to the PIPS-IPM++ root.\n"
<< " If omitted, the tool searches common "
"local"
<< " repository locations.\n"
<< " -c, --class <class> Solver class name used in the "
"generated maps"
<< " [default: PIPSMILPSolver].\n"
<< " -i, --include <header> Solver header included by the "
"generated maps"
<< " [default: PIPSMILPSolver.h].\n"
<< " -p, --prefix <prefix> Prefix used for macros, arrays and "
"output files"
<< " [default: PIPS].\n"
<< " -h, --help Print this help.\n";
}
/*--------------------------------------------------------------------------*/
/// Processes the command line arguments
void process_args( int argc , char ** argv )
{
const char * const short_opts = "vhs:c:i:p:";
const option long_opts[] = {
{ "verbose" , no_argument , nullptr , 'v' } ,
{ "help" , no_argument , nullptr , 'h' } ,
{ "source" , required_argument , nullptr , 's' } ,
{ "class" , required_argument , nullptr , 'c' } ,
{ "include" , required_argument , nullptr , 'i' } ,
{ "prefix" , required_argument , nullptr , 'p' } ,
{ nullptr , no_argument , nullptr , 0 }
};
// Options
while( true ) {
const auto opt = getopt_long( argc , argv , short_opts , long_opts ,
nullptr );
if( -1 == opt )
break;
switch( opt ) {
case( 'v' ):
verbose = true;
break;
case( 'h' ):
docopt();
exit( 0 );
case( 's' ):
source_path = std::string( optarg );
break;
case( 'c' ):
solver_class = std::string( optarg );
break;
case( 'i' ):
solver_header = std::string( optarg );
break;
case( 'p' ):
prefix = std::string( optarg );
break;
case( '?' ):
default:
std::cout << "Try " << exe << "' --help' for more information.\n";
exit( 1 );
}
}
// Last argument
if( optind < argc )
path = std::string( argv[ optind ] );
}
/*--------------------------------------------------------------------------*/
/// 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)
}
/*--------------------------------------------------------------------------*/
/// Reads an entire file into a string
std::string read_file( const std::string & filename )
{
std::ifstream file( filename );
if( ! file )
throw std::runtime_error( "Could not open " + filename + ": " +
std::strerror( errno ) );
return( std::string( std::istreambuf_iterator< char >( file ) ,
std::istreambuf_iterator< char >() ) );
}
/*--------------------------------------------------------------------------*/
/// Trims leading and trailing whitespace
std::string trim( const std::string & s )
{
const auto first = s.find_first_not_of( " \t\r\n" );
if( first == std::string::npos )
return( "" );
const auto last = s.find_last_not_of( " \t\r\n" );
return( s.substr( first , last - first + 1 ) );
}
/*--------------------------------------------------------------------------*/
/// Removes C/C++ comments while preserving string literals
std::string remove_comments( const std::string & text )
{
std::string out;
out.reserve( text.size() );
bool in_string = false;
bool in_char = false;
bool in_line_comment = false;
bool in_block_comment = false;
bool escaped = false;
for( std::size_t i = 0 ; i < text.size() ; ++i ) {
const char c = text[ i ];
const char n = ( i + 1 < text.size() ? text[ i + 1 ] : '\0' );
if( in_line_comment ) {
if( c == '\n' ) {
in_line_comment = false;
out.push_back( c );
}
else
out.push_back( ' ' );
continue;
}
if( in_block_comment ) {
if( c == '*' && n == '/' ) {
in_block_comment = false;
out.push_back( ' ' );
out.push_back( ' ' );
++i;
}
else
out.push_back( c == '\n' ? '\n' : ' ' );
continue;
}
if( in_string ) {
out.push_back( c );
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '"' )
in_string = false;
continue;
}
if( in_char ) {
out.push_back( c );
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '\'' )
in_char = false;
continue;
}
if( c == '/' && n == '/' ) {
in_line_comment = true;
out.push_back( ' ' );
out.push_back( ' ' );
++i;
continue;
}
if( c == '/' && n == '*' ) {
in_block_comment = true;
out.push_back( ' ' );
out.push_back( ' ' );
++i;
continue;
}
if( c == '"' )
in_string = true;
else if( c == '\'' )
in_char = true;
out.push_back( c );
}
return( out );
}
/*--------------------------------------------------------------------------*/
/// Splits a function argument list at top-level commas
std::vector< std::string > split_top_level_arguments(
const std::string & args )
{
std::vector< std::string > result;
std::string current;
int paren_depth = 0;
int brace_depth = 0;
int bracket_depth = 0;
int angle_depth = 0;
bool in_string = false;
bool in_char = false;
bool escaped = false;
for( std::size_t i = 0 ; i < args.size() ; ++i ) {
const char c = args[ i ];
if( in_string ) {
current.push_back( c );
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '"' )
in_string = false;
continue;
}
if( in_char ) {
current.push_back( c );
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '\'' )
in_char = false;
continue;
}
if( c == '"' ) {
in_string = true;
current.push_back( c );
continue;
}
if( c == '\'' ) {
in_char = true;
current.push_back( c );
continue;
}
switch( c ) {
case( '(' ): ++paren_depth; break;
case( ')' ): --paren_depth; break;
case( '{' ): ++brace_depth; break;
case( '}' ): --brace_depth; break;
case( '[' ): ++bracket_depth; break;
case( ']' ): --bracket_depth; break;
case( '<' ): ++angle_depth; break;
case( '>' ): if( angle_depth > 0 ) --angle_depth; break;
default: break;
}
if( c == ',' && paren_depth == 0 && brace_depth == 0 &&
bracket_depth == 0 && angle_depth == 0 ) {
result.push_back( trim( current ) );
current.clear();
}
else
current.push_back( c );
}
result.push_back( trim( current ) );
return( result );
}
/*--------------------------------------------------------------------------*/
/// Finds the closing parenthesis matching open_pos
std::size_t find_matching_paren( const std::string & text ,
std::size_t open_pos )
{
int depth = 0;
bool in_string = false;
bool in_char = false;
bool escaped = false;
for( std::size_t i = open_pos ; i < text.size() ; ++i ) {
const char c = text[ i ];
if( in_string ) {
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '"' )
in_string = false;
continue;
}
if( in_char ) {
if( escaped )
escaped = false;
else if( c == '\\' )
escaped = true;
else if( c == '\'' )
in_char = false;
continue;
}
if( c == '"' ) {
in_string = true;
continue;
}
if( c == '\'' ) {
in_char = true;
continue;
}
if( c == '(' )
++depth;
else if( c == ')' ) {
--depth;
if( depth == 0 )
return( i );
}
}
return( std::string::npos );
}
/*--------------------------------------------------------------------------*/
/// Extracts a string literal argument such as "PARAM" or std::string("PARAM")
std::string extract_string_literal( const std::string & arg )
{
static const std::regex literal_regex( "\\\"([^\\\"]+)\\\"" );
std::smatch match;
if( std::regex_search( arg , match , literal_regex ) )
return( match[ 1 ].str() );
return( "" );
}
/*--------------------------------------------------------------------------*/
/// Returns true if the argument looks like a numeric floating-point literal
bool is_floating_literal( const std::string & arg )
{
const std::string value = trim( arg );
if( value.find( "std::numeric_limits<double>" ) != std::string::npos )
return( true );
static const std::regex floating_regex(
R"((^|[^A-Za-z0-9_])([+-]?(?:(?:[0-9]+\.[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+))([^A-Za-z0-9_]|$))" );
if( std::regex_search( value , floating_regex ) )
return( true );
// Some PIPS defaults are expressions built from a double variable.
return( value.find( "feastol" ) != std::string::npos );
}
/*--------------------------------------------------------------------------*/
/// Returns true if add_parameter's possible-values argument is string-valued
bool has_string_possible_values( const std::string & arg )
{
const std::string value = trim( arg );
if( value.empty() )
return( false );
// Inline initializer lists for string-valued enumerations look like
// {{"none", "..."}, {"iterref", "..."}}
if( value.find( "{{\"" ) != std::string::npos ||
value.find( "{ {\"" ) != std::string::npos )
return( true );
// PIPS stores the linear solver alternatives in a string-valued helper.
return( value == "possible_solvers" ||
value.find( "get_solver_possibles" ) != std::string::npos );
}
/*--------------------------------------------------------------------------*/
/// Parses PIPS-IPM++ add_parameter(...) registrations.
void parse_add_parameter_calls( const std::string & text ,
std::map< int , std::string > & int_parameters ,
std::map< int , std::string > & dbl_parameters ,
std::map< int , std::string > & str_parameters ,
std::set< std::string > & int_seen ,
std::set< std::string > & dbl_seen ,
std::set< std::string > & str_seen ,
int & int_counter ,
int & dbl_counter ,
int & str_counter )
{
const std::string clean = remove_comments( text );
const std::string needle = "add_parameter";
std::size_t pos = 0;
while( ( pos = clean.find( needle , pos ) ) != std::string::npos ) {
// Avoid matching the add_parameter function declarations/definitions.
const std::size_t prev =
clean.find_last_not_of( " \t\r\n" , pos == 0 ? 0 : pos - 1 );
if( prev != std::string::npos &&
( std::isalnum( static_cast< unsigned char >( clean[ prev ] ) ) ||
clean[ prev ] == '_' || clean[ prev ] == ':' ) ) {
pos += needle.size();
continue;
}
const std::size_t open = clean.find( '(' , pos + needle.size() );
if( open == std::string::npos )
break;
const std::size_t close = find_matching_paren( clean , open );
if( close == std::string::npos )
break;
const std::vector< std::string > args =
split_top_level_arguments( clean.substr( open + 1 , close - open - 1 ) );
if( args.size() >= 4 ) {
const std::string name = extract_string_literal( args[ 1 ] );
const std::string default_value = trim( args[ 3 ] );
if( ! name.empty() ) {
const bool is_string =
default_value.find( "std::string" ) != std::string::npos ||
( ! default_value.empty() && default_value[ 0 ] == '"' ) ||
( args.size() >= 5 && has_string_possible_values( args[ 4 ] ) );
const bool is_bool = default_value == "true" || default_value == "false";
const bool is_double = is_floating_literal( default_value );
if( is_string ) {
if( str_seen.insert( name ).second )
str_parameters.insert( { str_counter++ , name } );
}
else if( is_bool ) {
if( int_seen.insert( name ).second )
int_parameters.insert( { int_counter++ , name } );
}
else if( is_double ) {
if( dbl_seen.insert( name ).second )
dbl_parameters.insert( { dbl_counter++ , name } );
}
else {
if( int_seen.insert( name ).second )
int_parameters.insert( { int_counter++ , name } );
}
}
}
pos = close + 1;
}
}
/*--------------------------------------------------------------------------*/
/// Parses options of the requested PIPS-IPM++ map type from a source file
void parse_options( const std::string & text ,
const std::string & map_name ,
std::map< int , std::string > & parameters ,
std::set< std::string > & seen ,
int & counter )
{
/*
* The usual PIPS-IPM++ spelling is
* bool_options["PARAM"] = value;
* but this parser deliberately accepts a few equivalent spellings as well,
* so that small upstream formatting changes do not silently produce empty
* generated headers.
*/
const std::vector< std::regex > regexes = {
std::regex( "(?:this->)?" + map_name +
"\\s*\\[\\s*\\\"([^\\\"]+)\\\"\\s*\\]\\s*=",
std::regex::ECMAScript ),
std::regex( "(?:this->)?" + map_name +
"\\s*\\.\\s*emplace\\s*\\(\\s*\\\"([^\\\"]+)\\\"",
std::regex::ECMAScript ),
std::regex( "(?:this->)?" + map_name +
"\\s*\\.\\s*insert\\s*\\(.*?\\{\\s*\\\"([^\\\"]+)\\\"",
std::regex::ECMAScript )
};
for( const auto & option_regex : regexes ) {
auto begin = std::sregex_iterator( text.begin() , text.end() ,
option_regex );
auto end = std::sregex_iterator();
for( auto it = begin ; it != end ; ++it ) {
const std::string name = ( *it )[ 1 ].str();
if( seen.insert( name ).second )
parameters.insert( { counter++ , name } );
}
}
}
/*--------------------------------------------------------------------------*/
/// Writes a std::array<string, ...> mapping from SMS++ ids to native options
void write_forward_map( std::ofstream & maps_file ,
const std::string & type ,
const std::map< int , std::string > & parameters )
{
maps_file
<< "const std::array< std::string, " << prefix << "_NUM_"
<< type << "_PARS >"
<< " " << solver_class << "::SMSpp_to_" << prefix << "_"
<< ( type == "INT" ? "int" : type == "DBL" ? "dbl" : "str" )
<< "_pars{" << std::endl;
for( const auto & i : parameters )
maps_file << " \"" << i.second << "\"," << std::endl;
maps_file << "};" << std::endl << std::endl;
}
/*--------------------------------------------------------------------------*/
/// Writes a reverse std::array<pair<string, int>, ...> mapping
void write_reverse_map( std::ofstream & maps_file ,
const std::string & type ,
const std::string & first_name ,
const std::map< int , std::string > & parameters )
{
maps_file
<< "const std::array< std::pair< std::string, int >, " << prefix
<< "_NUM_" << type << "_PARS >" << std::endl
<< " " << solver_class << "::" << prefix << "_to_SMSpp_"
<< ( type == "INT" ? "int" : type == "DBL" ? "dbl" : "str" )
<< "_pars{" << std::endl
<< " {" << std::endl;
for( const auto & i : parameters )
maps_file << " { \"" << i.second << "\", " << first_name << " + "
<< i.first << " }," << std::endl;
maps_file
<< " }" << std::endl
<< "};" << std::endl
<< std::endl;
}
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
// override the default terminate handler to print the exception message
std::set_terminate( smspp_terminate );
// Manage options and help
path = "../include";
docopt_desc = "PIPS-IPM++ parameter map generator.\n";
exe = get_filename( argv[ 0 ] );
process_args( argc , argv );
if( source_path.empty() )
source_path = find_source_path();
else
source_path = normalize_source_path( source_path );
source_path = strip_trailing_slash( source_path );
if( verbose ) {
std::cout << "PIPS-IPM++ options path is " << source_path << std::endl;
std::cout << "Output path is " << path << std::endl;
std::cout << "Solver class is " << solver_class << std::endl;
std::cout << "Solver header is " << solver_header << std::endl;
std::cout << "Prefix is " << prefix << std::endl;
}
std::string defs_filename = prefix + "_defs.h";
std::string maps_filename = prefix + "_maps.h";
auto defs_path = path + "/" + defs_filename;
auto maps_path = path + "/" + maps_filename;
std::map< int , std::string > int_parameters;
std::map< int , std::string > dbl_parameters;
std::map< int , std::string > str_parameters;
std::set< std::string > int_seen;
std::set< std::string > dbl_seen;
std::set< std::string > str_seen;
int int_counter = 0;
int dbl_counter = 0;
int str_counter = 0;
const std::string filename = source_path + "/PIPSIPMppOptions.C";
if( verbose )
std::cout << "Parsing " << filename << std::endl;
const std::string text = read_file( filename );
// SMS++ represents boolean parameters as int parameters.
parse_options( text , "bool_options" , int_parameters , int_seen ,
int_counter );
parse_options( text , "int_options" , int_parameters , int_seen ,
int_counter );
parse_options( text , "double_options" , dbl_parameters , dbl_seen ,
dbl_counter );
parse_options( text , "string_options" , str_parameters , str_seen ,
str_counter );
parse_add_parameter_calls( text , int_parameters , dbl_parameters ,
str_parameters , int_seen , dbl_seen , str_seen ,
int_counter , dbl_counter , str_counter );
if( int_counter + dbl_counter + str_counter == 0 )
throw std::runtime_error(
"No PIPS-IPM++ options were found. Check that -s points to the source "
"tree containing Core/Options/PIPSIPMppOptions.C, and run with -v to see "
"which file is parsed." );
// Generate defs file
std::ofstream defs_file;
defs_file.open( defs_path );
if( ! defs_file )
throw std::runtime_error( "Could not open output file " + defs_path + ": " +
std::strerror( errno ) );
defs_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#ifndef __" << prefix << "_DEFS" << std::endl
<< "#define __" << prefix << "_DEFS" << std::endl
<< std::endl
<< "#define " << prefix << "_NUM_INT_PARS " << int_counter
<< std::endl
<< "#define " << prefix << "_NUM_DBL_PARS " << dbl_counter
<< std::endl
<< "#define " << prefix << "_NUM_STR_PARS " << str_counter
<< std::endl
<< std::endl
<< "#endif //__" << prefix << "_DEFS" << std::endl;
defs_file.close();
std::cout << "Defs file written on " << defs_path << std::endl;
// Generate maps file
std::ofstream maps_file;
maps_file.open( maps_path );
if( ! maps_file )
throw std::runtime_error( "Could not open output file " + maps_path + ": " +
std::strerror( errno ) );
maps_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#include <array>" << std::endl
<< "#include <string>" << std::endl
<< "#include <utility>" << std::endl
<< "#include \"" << solver_header << "\"" << std::endl
<< std::endl
<< "using namespace SMSpp_di_unipi_it;" << std::endl
<< std::endl;
write_forward_map( maps_file , "INT" , int_parameters );
write_forward_map( maps_file , "DBL" , dbl_parameters );
write_forward_map( maps_file , "STR" , str_parameters );
write_reverse_map( maps_file , "INT" , "intFirst" + prefix + "Par" ,
int_parameters );
write_reverse_map( maps_file , "DBL" , "dblFirst" + prefix + "Par" ,
dbl_parameters );
write_reverse_map( maps_file , "STR" , "strFirst" + prefix + "Par" ,
str_parameters );
maps_file.close();
std::cout << "Maps file written on " << maps_path << std::endl;
if( verbose ) {
std::cout << prefix << "_NUM_INT_PARS " << int_counter << std::endl;
std::cout << prefix << "_NUM_DBL_PARS " << dbl_counter << std::endl;
std::cout << prefix << "_NUM_STR_PARS " << str_counter << std::endl;
}
return( 0 );
}