-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
302 lines (239 loc) · 9.61 KB
/
Copy pathmain.cpp
File metadata and controls
302 lines (239 loc) · 9.61 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
/*--------------------------------------------------------------------------*/
/*----------------------------- File main.cpp ------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
*
* Tester for MMCFBlock. It loads an MMCF instance from file, taking the
* filename and the type from the command line, into both a MMCFBlock with
* an appropriate Solver (say, CPXMILPSolver) attached, and into a
* MMCFCplex object derived from MMCFClass. It solves the instance with
* both and compares the results.
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Enrico Gorgone \n
* Dipartimento di Matematica ed Informatica \n
* Universita' di Cagliari \n
*
* \copyright © by Antonio Frangioni
*/
/*--------------------------------------------------------------------------*/
/*-------------------------------- MACROS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#define LOG_LEVEL 0
// 0 = only pass/fail
// 1 = + solver log
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include "MMCFBlock.h"
#include "BlockSolverConfig.h"
#include "Graph.h"
#include "MMCFCple.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
using namespace MMCFClass_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*----------------------------- CONSTANTS ----------------------------------*/
/*--------------------------------------------------------------------------*/
#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
/*--------------------------------------------------------------------------*/
/*----------------------------- FUNCTIONS ----------------------------------*/
/*--------------------------------------------------------------------------*/
/*!!
template< class T >
static inline void str2val( const char* const str , T &sthg )
{
istringstream( str ) >> sthg;
}
!!*/
/*--------------------------------------------------------------------------*/
static void PrintResults( int rtrn , double lb , double ub ) {
cout << "MMCFB: ";
if( ( rtrn >= Solver::kOK ) && ( rtrn < Solver::kError ) )
cout << std::setprecision( 8 ) << lb << ", " << ub;
else
if( rtrn == Solver::kInfeasible )
cout << "Unfeas";
else
if( rtrn == Solver::kUnbounded )
cout << "Unbounded";
else
cout << "Error!";
}
/*--------------------------------------------------------------------------*/
/// 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 );
// reading command line parameters - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( argc < 2 ) {
cerr << "Usage: " << argv[ 0 ] << " file_name [typ]" << endl
<< " typ = s*, c, p, o, d, u, m (lower or uppercase)" << endl;
return( 1 );
}
char filetype = 's'; // type of the input file;
if( argc > 2 )
filetype = argv[ 2 ][ 0 ];
// read the Block- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
auto MMCFb = new MMCFBlock;
MMCFb->load( argv[ 1 ] , filetype );
MMCFb->PreProcess();
{
auto cfg = Configuration::deserialize( "BPar.txt" );
if( BlockConfig * bc = dynamic_cast< BlockConfig * >( cfg ) )
bc->apply( MMCFb );
else {
cerr << "Error: BPar.txt does not contain a BlockConfig" << endl;
exit( 1 );
}
delete( cfg );
}
MMCFb->generate_abstract_variables();
// attach the Solver to the Block- - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BlockSolverConfig * bsc;
{
auto c = Configuration::deserialize( "BSPar.txt" );
bsc = dynamic_cast< BlockSolverConfig * >( c );
if( ! bsc ) {
cerr << "Error: configuration file not a BlockSolverConfig" << endl;
delete( c );
exit( 1 );
}
bsc->apply( MMCFb );
bsc->clear();
}
if( MMCFb->get_registered_solvers().empty() ) {
cout << endl << "no Solver registered to the Block!" << endl;
exit( 1 );
}
Solver * slvr = ( MMCFb->get_registered_solvers() ).front();
// open log-file - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#if LOG_LEVEL > 0
ofstream LOGFile( "logMMCFB.txt" , ofstream::out );
if( ! LOGFile.is_open() )
cerr << "Warning: cannot open log file logMMCFB.txt" << endl;
else
slvr->set_log( &LOGFile );
#endif
std::clock_t c_start = std::clock();
int rtrn = slvr->compute( false );
double lb_value = slvr->get_lb();
double ub_value = slvr->get_ub();
double time = double( std::clock() - c_start ) / double( CLOCKS_PER_SEC );
// cleanup MMCFBlock and its Solver
bsc->apply( MMCFb );
delete( bsc );
delete( MMCFb );
// read and modify the problem - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Graph *Gh = new Graph( argv[ 1 ] , filetype );
Gh->PreProcess();
// allocate the solver - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MMCFCplex *mmcf = new MMCFCplex( Gh , nullptr );
// set tolerance - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mmcf->SetCplexParam( CPX_PARAM_EPOPT , 1e-8 );
mmcf->SetCplexParam( CPX_PARAM_EPGAP , 1e-8 );
// pass the number of threads - - - - - - - - - - - - - - - - - - - - - - -
mmcf->SetCplexParam( CPX_PARAM_THREADS , 1 );
// pass Log- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#if LOG_LEVEL > 0
ofstream LOGCPX( "logMMCFC.txt" , ofstream::out );
if( ! LOGCPX.is_open() )
cerr << "Warning: cannot open log file logMMCFC.txt" << endl;
else
mmcf->SetMMCFLog( &LOGCPX , 2 );
#endif
// free some memory- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
delete( Gh );
// set the timers on - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mmcf->SetMMCFTime();
// solve the problem - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MMCFClass::MMCFStatus Status = mmcf->SolveMMCF();
// get the results - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
double tu , ts;
mmcf->TimeMMCF( tu , ts ); // get the running time
MMCFClass::FONumber OV1 = mmcf->GetPVal(); // get the primal value
MMCFClass::FONumber OV2 = mmcf->GetDVal(); // get the dual value
// clean up- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
delete( mmcf );
// output the results- - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cout << time << " - " << tu + ts << ": ";
switch( Status ) {
case( MMCFClass::kStopped ):
case( MMCFClass::kOK ):
if( ( ( rtrn >= Solver::kOK ) && ( rtrn < Solver::kError ) ) &&
( abs( lb_value - OV1 ) <= 1e-8 *
max( double( 1 ) , max( abs( lb_value ) , abs( OV1 ) ) ) ) &&
( abs( ub_value - OV2 ) <= 1e-8 *
max( double( 1 ) , max( abs( ub_value ) , abs( OV2 ) ) ) ) ) {
cout << "OK(f) - " << GREEN( Test passed!! ) << endl;
return( 0 );
}
PrintResults( rtrn , lb_value , ub_value );
cout << "- MMCFC: " << OV1 << ", "<< OV2 << " - ";
break;
case( MMCFClass::kUnfeasible ):
if( rtrn == Solver::kInfeasible ) {
cout << "OK(e) - " << GREEN( Test passed!! ) << endl;
return( 0 );
}
PrintResults( rtrn , lb_value , ub_value );
cout << "- MMCFC: Unfeas - ";
break;
case( MMCFClass::kUnbounded ) :
if( rtrn == Solver::kUnbounded ) {
cout << "OK(u) - " << GREEN( Test passed!! ) << endl;
return( 0 );
}
PrintResults( rtrn , lb_value , ub_value );
cout << "- MMCFC: Unbounded - ";
break;
default :
PrintResults( rtrn , lb_value , ub_value );
cout << "- MMCFC: Error! - ";
}
cout << RED( Shit happened!! ) << endl;
return( 1 );
}
/*--------------------------------------------------------------------------*/
/*------------------------- End File main.cpp ------------------------------*/
/*--------------------------------------------------------------------------*/