-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrb_pars.cpp
More file actions
322 lines (270 loc) · 9.48 KB
/
Copy pathgrb_pars.cpp
File metadata and controls
322 lines (270 loc) · 9.48 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
/*--------------------------------------------------------------------------*/
/*---------------------------- File grb_pars.cpp ---------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Small tool for parsing the macros and the standard std::maps that enable
* the support for plain Gurobi parameters into GRBMILPSolver.
*
* The tool generates two files, GRB<GRB_VERSION>_defs.h and
* GRB<GRB_VERSION>_maps.h in the specified path.
* The path should be the include directory of the MILPSolver source tree.
*
* \author Niccolo' Iardella \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Enrico Calandrini \n
* Dipartimento di Matematica \n
* Universita' di Pisa \n
*
* Copyright © Niccolo' Iardella
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <map>
#include <getopt.h>
// #include <filesystem>
#include <gurobi_c.h>
/*--------------------------------------------------------------------------*/
bool verbose = false; ///< If the tool should be verbose
// std::filesystem::path path{};
std::string path{}; ///< Path for output files
std::string exe{}; ///< Name of the executable file
std::string docopt_desc{}; ///< Tool description
/*--------------------------------------------------------------------------*/
/// 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 ) );
}
/*--------------------------------------------------------------------------*/
/// Prints the tool description and usage
void docopt()
{
// http://docopt.org
std::cout << docopt_desc << std::endl;
std::cout << "Usage:\n"
<< " " << exe << " [-v] <path>\n"
<< " " << exe << " -h | --help\n"
<< std::endl
<< "Options:\n"
<< " -v, --verbose Make the tool verbose.\n"
<< " -h, --help Print this help.\n";
}
/*--------------------------------------------------------------------------*/
/// Processes the command line arguments
void process_args( int argc , char ** argv )
{
const char * const short_opts = "vh";
const option long_opts[] = {
{ "verbose" , no_argument , nullptr , 'v' } ,
{ "help" , no_argument , nullptr , 'h' } ,
{ 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( '?' ):
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)
}
/*--------------------------------------------------------------------------*/
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";
// path = std::filesystem::current_path();
docopt_desc = "Gurobi parameter map generator.\n";
exe = get_filename( argv[ 0 ] );
process_args( argc , argv );
// if( ! std::filesystem::exists( path ) ) {
// std::filesystem::create_directory( path );
// }
std::string GRB_VERSION =
std::to_string( GRB_VERSION_MAJOR ) + std::to_string( GRB_VERSION_MINOR ) +
std::to_string( GRB_VERSION_TECHNICAL );
std::string defs_filename = "GRB" + GRB_VERSION + "_defs.h";
std::string maps_filename = "GRB" + GRB_VERSION + "_maps.h";
// auto defs_path = path / defs_filename;
// auto maps_path = path / maps_filename;
auto defs_path = path + "/" + defs_filename;
auto maps_path = path + "/" + maps_filename;
std::ofstream defs_file;
std::ofstream maps_file;
GRBenv * envptr;
int status;
char * name;
std::map< int , std::string > int_parameters;
std::map< int , std::string > dbl_parameters;
std::map< int , std::string > str_parameters;
#define GRB_PARAMTYPE_INT 1
#define GRB_PARAMTYPE_DBL 2
#define GRB_PARAMTYPE_STR 3
int int_counter = 0;
int dbl_counter = 0;
int str_counter = 0;
status = GRBemptyenv( &envptr );
if( verbose ) {
std::cout << "GRB_VERSION is " << GRB_VERSION << std::endl;
}
for( int i = 0 ; GRBgetparamname( envptr , i , &name ) == 0 ; ++i ) {
if( strlen( name ) == 0 ) {
break;
}
int type;
type = GRBgetparamtype( envptr , name );
switch( type ) {
case( GRB_PARAMTYPE_INT ):
int_parameters.insert( { int_counter++ , std::string( name ) } );
break;
case( GRB_PARAMTYPE_DBL ):
dbl_parameters.insert( { dbl_counter++ , std::string( name ) } );
break;
case( GRB_PARAMTYPE_STR ):
str_parameters.insert( { str_counter++ , std::string( name ) } );
break;
default:
std::cerr << "Unknown type from GRBgetparamtype()" << std::endl;
return( 1 );
}
}
// Generate defs file
defs_file.open( defs_path );
defs_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#ifndef __GRB" << GRB_VERSION << "_DEFS"
<< std::endl
<< "#define __GRB" << GRB_VERSION << "_DEFS"
<< std::endl << std::endl
<< "#define GRB_NUM_INT_PARS " << int_counter << std::endl
<< "#define GRB_NUM_DBL_PARS " << dbl_counter << std::endl
<< "#define GRB_NUM_STR_PARS " << str_counter << std::endl
<< std::endl
<< "#endif //__GRB" << GRB_VERSION << "_DEFS"
<< std::endl;
defs_file.close();
std::cout << "Defs file written on " << defs_path << std::endl;
// Generate maps file
maps_file.open( maps_path );
maps_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#include <gurobi_c.h>" << std::endl
<< "#include \"GRBMILPSolver.h\"" << std::endl
<< std::endl
<< "using namespace SMSpp_di_unipi_it;" << std::endl
<< std::endl;
// SMSpp_to_GUROBI_***_pars maps
maps_file
<< "const std::array< std::string, GRB_NUM_INT_PARS >"
<< " GRBMILPSolver::SMSpp_to_GUROBI_int_pars{"
<< std::endl;
for( const auto & i : int_parameters ) {
maps_file << " \"" << i.second << "\"," << std::endl;
}
maps_file << "};" << std::endl;
maps_file << std::endl;
maps_file
<< "const std::array< std::string, GRB_NUM_DBL_PARS >"
<< " GRBMILPSolver::SMSpp_to_GUROBI_dbl_pars{"
<< std::endl;
for( const auto & i : dbl_parameters ) {
maps_file << " \"" << i.second << "\"," << std::endl;
}
maps_file << "};" << std::endl;
maps_file << std::endl;
maps_file
<< "const std::array< std::string, GRB_NUM_STR_PARS >"
<< " GRBMILPSolver::SMSpp_to_GUROBI_str_pars{"
<< std::endl;
for( const auto & i : str_parameters ) {
maps_file << " \"" << i.second << "\"," << std::endl;
}
maps_file << "};" << std::endl;
maps_file << std::endl;
// Reverse GUROBI_to_SMSpp_***_pars maps
maps_file
<< "const std::array< std::pair< std::string, int >, GRB_NUM_INT_PARS >"
<< std::endl
<< " GRBMILPSolver::GUROBI_to_SMSpp_int_pars{" << std::endl
<< " {" << std::endl;
for( const auto & i : int_parameters ) {
maps_file << " { \"" << i.second << "\", intFirstGUROBIPar + " << i.first
<< " },"
<< std::endl;
}
maps_file
<< " }" << std::endl
<< "};" << std::endl
<< std::endl;
maps_file
<< "const std::array< std::pair< std::string, int >, GRB_NUM_DBL_PARS >"
<< std::endl
<< " GRBMILPSolver::GUROBI_to_SMSpp_dbl_pars{" << std::endl
<< " {" << std::endl;
for( const auto & i : dbl_parameters ) {
maps_file << " { \"" << i.second << "\", dblFirstGUROBIPar + " << i.first
<< " },"
<< std::endl;
}
maps_file
<< " }" << std::endl
<< "};" << std::endl
<< std::endl;
maps_file
<< "const std::array< std::pair< std::string, int >, GRB_NUM_STR_PARS >"
<< std::endl
<< " GRBMILPSolver::GUROBI_to_SMSpp_str_pars{" << std::endl
<< " {" << std::endl;
for( const auto & i : str_parameters ) {
maps_file << " { \"" << i.second << "\", strFirstGUROBIPar + " << i.first
<< " },"
<< std::endl;
}
maps_file
<< " }" << std::endl
<< "};" << std::endl;
maps_file.close();
std::cout << "Maps file written on " << maps_path << std::endl;
return( 0 );
}