Repository navigation
Expand file tree
/
Copy pathmosek_pars.cpp
More file actions
241 lines (203 loc) · 8.22 KB
/
Copy pathmosek_pars.cpp
File metadata and controls
241 lines (203 loc) · 8.22 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
/*--------------------------------------------------------------------------*/
/*--------------------------- File mosek_pars.cpp --------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Small tool for parsing the macros and the standard std::maps that enable
* the support for plain MOSEK parameters into MOSEKMILPSolver.
*
* The tool generates two files, MOSEK<MOSEK_VERSION>_defs.h and
* MOSEK<MOSEK_VERSION>_maps.h in the specified path, MOSEK_VERSION being
* the major, minor and revision numbers of MOSEK one after the other.
* The path should be the include directory of the MILPSolver source tree.
* The parameters are listed by MSK_getparamname() on a task, which needs
* no license.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <getopt.h>
#include <mosek.h>
/*--------------------------------------------------------------------------*/
bool verbose = false; ///< If the tool should be verbose
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";
docopt_desc = "MOSEK parameter map generator.\n";
exe = get_filename( argv[ 0 ] );
process_args( argc , argv );
std::string MOSEK_VERSION =
std::to_string( MSK_VERSION_MAJOR ) + std::to_string( MSK_VERSION_MINOR ) +
std::to_string( MSK_VERSION_REVISION );
auto defs_path = path + "/MOSEK" + MOSEK_VERSION + "_defs.h";
auto maps_path = path + "/MOSEK" + MOSEK_VERSION + "_maps.h";
if( verbose )
std::cout << "MOSEK_VERSION is " << MOSEK_VERSION << std::endl;
MSKenv_t env = nullptr;
MSKtask_t task = nullptr;
if( ( MSK_makeenv( & env , nullptr ) != MSK_RES_OK ) ||
( MSK_maketask( env , 0 , 0 , & task ) != MSK_RES_OK ) ) {
std::cerr << "Unable to create a MOSEK environment and task" << std::endl;
return( 1 );
}
// the parameters of each type, in the order of their enum
auto list = [ & ]( MSKparametertypee type , int begin , int end ) {
std::vector< std::string > names;
char name[ MSK_MAX_STR_LEN ];
for( int i = begin ; i < end ; ++i )
if( MSK_getparamname( task , type , i , name ) == MSK_RES_OK )
names.emplace_back( name );
return( names );
};
const auto int_parameters = list( MSK_PAR_INT_TYPE , MSK_IPAR_BEGIN ,
MSK_IPAR_END );
const auto dbl_parameters = list( MSK_PAR_DOU_TYPE , MSK_DPAR_BEGIN ,
MSK_DPAR_END );
const auto str_parameters = list( MSK_PAR_STR_TYPE , MSK_SPAR_BEGIN ,
MSK_SPAR_END );
MSK_deletetask( & task );
MSK_deleteenv( & env );
// Generate defs file
std::ofstream defs_file( defs_path );
defs_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#ifndef __MOSEK" << MOSEK_VERSION << "_DEFS" << std::endl
<< "#define __MOSEK" << MOSEK_VERSION << "_DEFS" << std::endl
<< std::endl
<< "#define MOSEK_NUM_INT_PARS " << int_parameters.size()
<< std::endl
<< "#define MOSEK_NUM_DBL_PARS " << dbl_parameters.size()
<< std::endl
<< "#define MOSEK_NUM_STR_PARS " << str_parameters.size()
<< std::endl << std::endl
<< "#endif //__MOSEK" << MOSEK_VERSION << "_DEFS" << std::endl;
defs_file.close();
std::cout << "Defs file written on " << defs_path << std::endl;
// Generate maps file
std::ofstream maps_file( maps_path );
maps_file << "/* FILE GENERATED AUTOMATICALLY, DO NOT EDIT */" << std::endl
<< std::endl
<< "#include <mosek.h>" << std::endl
<< "#include \"MOSEKMILPSolver.h\"" << std::endl
<< std::endl
<< "using namespace SMSpp_di_unipi_it;" << std::endl
<< std::endl;
// SMSpp_to_MOSEK_***_pars and MOSEK_to_SMSpp_***_pars maps
auto write = [ & ]( const std::vector< std::string > & names ,
const std::string & type , const std::string & num ,
const std::string & first ) {
maps_file << "const std::array< std::string, " << num << " >"
<< std::endl
<< " MOSEKMILPSolver::SMSpp_to_MOSEK_" << type << "_pars{"
<< std::endl;
for( const auto & n : names )
maps_file << " \"" << n << "\"," << std::endl;
maps_file << "};" << std::endl << std::endl;
maps_file << "const std::array< std::pair< std::string, int >, " << num
<< " >" << std::endl
<< " MOSEKMILPSolver::MOSEK_to_SMSpp_" << type << "_pars{"
<< std::endl << " {" << std::endl;
for( std::size_t i = 0 ; i < names.size() ; ++i )
maps_file << " { \"" << names[ i ] << "\", " << first << " + " << i
<< " }," << std::endl;
maps_file << " }" << std::endl << "};" << std::endl << std::endl;
};
write( int_parameters , "int" , "MOSEK_NUM_INT_PARS" , "intFirstMOSEKPar" );
write( dbl_parameters , "dbl" , "MOSEK_NUM_DBL_PARS" , "dblFirstMOSEKPar" );
write( str_parameters , "str" , "MOSEK_NUM_STR_PARS" , "strFirstMOSEKPar" );
maps_file.close();
std::cout << "Maps file written on " << maps_path << std::endl;
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*------------------------ End File mosek_pars.cpp -------------------------*/
/*--------------------------------------------------------------------------*/